This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
code-palette-panel.lua
executable file
·322 lines (207 loc) · 10.3 KB
/
code-palette-panel.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
--[[
Pixel Vision 8 - Draw Tool
Copyright (C) 2017, Pixel Vision 8 (http://pixelvision8.com)
Created by Jesse Freeman (@jessefreeman)
Please do not copy and distribute verbatim copies
of this license document, but modifications without
distributing is allowed.
]]--
local palettePanelID = "PalettePanelUI"
function DrawTool:CreatePalettePanel()
-- local totalColors = pixelVisionOS.totalSystemColors
-- local totalPerPage = 16--pixelVisionOS.systemColorsPerPage
-- local maxPages = 16
-- self.colorOffset = pixelVisionOS.colorOffset
-- self.paletteLabelArgs = {gamecolortext.spriteIDs, 4, 21, gamecolortext.width, false, false, DrawMode.Tile}
-- Create the palette color picker
self.paletteColorPickerData = pixelVisionOS:CreateColorPicker(
{x = 32, y = 184, w = 128, h = 32},
{x = 16, y = 16},
pixelVisionOS.totalPaletteColors,
16, -- Total per page
8, -- Max pages
pixelVisionOS.colorOffset + 128,
"itempicker",
"palette color",
false,
true,
false
)
pixelVisionOS:ColorPickerVisiblePerPage(self.paletteColorPickerData, pixelVisionOS.colorsPerSprite)
self.paletteColorPickerData.picker.borderOffset = 8
-- TODO this shouldn't have to be called?
pixelVisionOS:RebuildColorPickerCache(self.paletteColorPickerData)
self.paletteColorPickerData.UpdateToolTip = function(tmpData)
-- print("Update Tool Tip", tmpData.name, tmpData.dragging)
local action = ""
local ending = ""
local pos = 0
local page = tmpData.pages.currentSelection - 1
-- print("page", self.paletteColorPickerData.picker.overPos.index)
local toolTipMessage = "%s color %02d from palette %d %s"
if(tmpData.dragging) then
local shiftColor = page * 16
local palIndex = tmpData.overPos.index - shiftColor
-- print("tmpData.overPos.index", )
if(palIndex > -1 and palIndex < tmpData.visiblePerPage) then
action = tmpData.copyDrag == true and "Copy" or "Swap"
-- index = palIndex
ending = string.format("%s color %02d in palette %d", (tmpData.copyDrag == true and "to" or "with"), tostring(palIndex), page)
pos = tmpData.picker.selected
elseif(editorUI.collisionManager:MouseInRect(self.spritePickerData.rect)) then
-- This is a special use case where we don't want to use the default tool tip message
tmpData.picker.toolTip = string.format("Preview color offset %03d", (page * 16) + tmpData.picker.selected + 128)
-- -- Exit this since we already set the tooltip value
return
else
action = "Dragging"
index = tmpData.pressSelection.index
pos = tmpData.picker.selected
-- tmpData.picker.toolTip = "Dragging "..tmpData.toolTipLabel.." ID " .. string.lpad(tostring(tmpData.pressSelection.index), tmpData.totalItemStringPadding, "0")
end
elseif(tmpData.overPos.index ~= nil and tmpData.overPos.index ~= -1) then
action = "Select"
index = tmpData.overPos.index
pos = editorUI:CalculatePickerPosition(tmpData.picker).index
else
toolTipMessage = ""
end
-- This is hard coded to add 128 to the index since that is where palette IDs begin in the re-mapped color chip
tmpData.picker.toolTip = string.format(toolTipMessage, action, pos, page, ending)
-- print ("New Tool Tip", tmpData.picker.name, tmpData.picker.toolTip)
end
-- print("paletteColorPickerData")
self.paletteColorPickerData.onAction = function(value, doubleClick)
if(doubleClick == true and self.canEdit == true) then
-- editorUI:ToggleButton(self.modeButton, not self.modeButton.value)
self:ChangeEditMode(ColorMode)
local colorID = table.indexOf(pixelVisionOS.systemColors, Color(pixelVisionOS.colorOffset + 128 + value)) - 1
print("Focus Pal")
self:ForcePickerFocus(self.systemColorPickerData)
self:OnSelectSystemColor(colorID)
pixelVisionOS:SelectColorPickerIndex(self.systemColorPickerData, colorID)
-- TODO find and select color in picker
return
end
print("paletteColorPickerData.onAction", value)
self:OnSelectPaletteColor(value)
self:ForcePickerFocus(self.paletteColorPickerData)
end
self.paletteColorPickerData.onDropTarget = function(src, dest) self:OnPalettePickerDrop(src, dest) end
-- if(self.usePalettes == true) then
-- Force the palette picker to only display the total colors per sprite
self.paletteColorPickerData.visiblePerPage = pixelVisionOS.paletteColorsPerPage
pixelVisionOS:OnColorPickerPage(self.paletteColorPickerData, 1)
-- else
-- end
-- Wire up the picker to change the color offset of the sprite picker
self.paletteColorPickerData.onPageAction = function(value)
-- Calculate page offset value
local pageOffset = ((value - 1) * 16)
-- Calculate the new color offset
local newColorOffset = pixelVisionOS.colorOffset + pixelVisionOS.totalPaletteColors + pageOffset
-- Change the spite picker color offset
pixelVisionOS:ChangeItemPickerColorOffset(self.spritePickerData, newColorOffset)
-- Update the canvas color offset
self.canvasData.colorOffset = newColorOffset
pixelVisionOS:InvalidateItemPickerDisplay(self.spritePickerData)
self:UpdateCanvas(self.lastSelection)
-- Need to reselect the current color in the new palette if we are in draw mode
-- if(self.canvasData.tool ~= "eraser" and self.canvasData.tool ~= "select") then
-- local colorID = (self.lastPaletteColorID % 16) + pageOffset
-- -- print("colorID", colorID, self.paletteColorPickerData.picker.selected, pageOffset, self.lastPaletteColorID % 16)
-- if(self.paletteColorPickerData.currentSelection ~= colorID) then
-- pixelVisionOS:SelectItemPickerIndex(self.paletteColorPickerData, colorID, false, false)
-- self:OnSelectPaletteColor(colorID)
-- end
-- end
end
self.paletteColorPickerData.onDrawColor = function(data, id, x, y)
-- if(id < data.total and (id % data.totalPerPage) < data.visiblePerPage) then
local colorID = id + data.altColorOffset
-- if(Color(colorID) == pixelVisionOS.maskColor) then
-- -- data.canvas.DrawSprites(emptymaskcolor.spriteIDs, x, y, emptymaskcolor.width, false, false)
-- else
data.canvas.Clear(colorID+10, x, y, data.itemSize.x, data.itemSize.y)
-- end
-- else
-- print("Find", FindMetaSpriteId("emptycolor"))
-- data.canvas.DrawMetaSprite(FindMetaSpriteId("emptycolor"), x, y)
-- end
end
self.paletteColorPickerData.onChange = function(index, color)
Color(index, color)
end
self:DrawPalettePanelLabel()
pixelVisionOS:RegisterUI({name = palettePanelID}, "UpdatePalettePanel", self)
end
function DrawTool:OnSelectPaletteColor(value)
print("OnSelectPaletteColor", value)
-- print("OnSelectPaletteColor", value)
self.lastPaletteColorID = value
-- -- Force value to be in palette mode
value = self.paletteColorPickerData.picker.selected
-- -- print("Saving last pal", self.lastPaletteColorID)
-- -- Set the canvas brush color
pixelVisionOS:CanvasBrushColor(self.canvasData, value)
end
function DrawTool:DrawPalettePanelLabel()
-- if(self.usePalettes == true) then
-- self.paletteLabelArgs[1] = gamepalettetext.spriteIDs
-- self.paletteLabelArgs[4] = gamepalettetext.width
-- else
-- self.paletteLabelArgs[1] = gamecolortext.spriteIDs
-- self.paletteLabelArgs[4] = gamecolortext.width
-- end
DrawMetaSprite(
FindMetaSpriteId("gamepalettetext"),
4,
21,
false,
false,
DrawMode.Tile
)
-- editorUI:NewDraw("DrawSprites", self.paletteLabelArgs)
end
function DrawTool:UpdatePalettePanel()
pixelVisionOS:UpdateColorPicker(self.paletteColorPickerData)
if(self.canvasData.tool == "eyedropper" and self.canvasData.inFocus and MouseButton(0)) then
local colorID = self.canvasData.overColor
-- Only update the color selection when it's new
if(colorID ~= self.lastColorID) then
self.lastColorID = colorID
if(colorID < 0) then
pixelVisionOS:ClearItemPickerSelection(self.paletteColorPickerData)
-- Force the lastColorID to be back in range so there is a color to draw with
self.lastColorID = -1
else
pixelVisionOS:CanvasBrushColor(self.canvasData, self.lastColorID)
local selectionID = lastColorID
-- Check to see if in palette mode
-- if(usePalettes == true) then
local pageOffset = ((self.paletteColorPickerData.pages.currentSelection - 1) * 16)
selectionID = Clamp(self.lastColorID, 0, 15) + pageOffset
-- pixelVisionOS:SelectColorPickerColor(paletteColorPickerData, Clamp(lastColorID, 0, 15) + pageOffset)
-- end
-- else
pixelVisionOS:SelectColorPickerColor(self.paletteColorPickerData, selectionID)
-- end
-- Select the
end
end
end
end
function DrawTool:OnPalettePickerDrop(src, dest)
if(src.name == self.systemColorPickerData.name) then
-- Get the index and add 1 to offset it correctly
local id = pixelVisionOS:CalculateItemPickerPosition(dest).index
-- Get the correct hex value
local srcHex = Color(src.pressSelection.index + src.altColorOffset)
self:OnAddDroppedColor(id, dest, srcHex)
else
-- Test that the destination position is visible
if(editorUI:CalculatePickerPosition(dest.picker).index < dest.visiblePerPage) then
self:OnSystemColorDropTarget(src, dest)
end
end
end