/
palette.lua
449 lines (366 loc) · 11.7 KB
/
palette.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
--[[
palette is implemented as a uikit node.
Therefore it requires uikit module to work.
Usage:
uikit = require("uikit")
palette = require("palette")
local p = palette:create(uikit)
p:setParent(ui.root)
p.LocalPosition = {0, 50, 0}
-- Called when palette layout's been updated.
p.didRefresh = function(self)
-- ...
end
-- Called when selection just changed.
p.didChangeSelection = function(self, color)
-- print("didChangeSelection: ", color.R, color.G, color.B)
end
-- Called when the palettes requires edit for color at index.
-- This signal can be used to show a color picker.
p.requiresEdit = function(self, index, color)
-- print("requiresEdit:", index, color.R, color.G, color.B)
end
-- Sets all palette colors to be shared with those of the given Shape or Palette.
p:setColors(Map.Palette)
-- Adds colors from the given Shape or Palette, avoiding duplicates.
local s = Shape(Items.official.head)
p:mergeColors(s.Palette)
-- Adds one single color
p:addColor(Color(255,0,0))
--]]
local palette = {}
theme = require("uitheme").current
palette.create = function(_, uikit, btnColor)
local node = uikit:createNode()
-- config
node.nbColumns = 6
node.maxSquareSize = 40
node.maxHeight = 200 -- max total height for component
node.padding = theme.padding
node._selectedIndex = 0
node._squareSize = 0
node._maxScreenWidthFactor = 0.5
-- callbacks
node.didRefresh = nil -- function(self)
node.didAdd = nil -- function(self, color)
node.didChangeSelection = nil -- function(self, color)
node.requiresEdit = nil -- function(self, index, color)
local bg = uikit:createFrame(Color(50, 50, 50, 200))
bg.Width = 200
bg.Height = 200
bg:setParent(node)
node.background = bg
node._width = function(self)
return self.background.Width
end
node._height = function(self)
return self.background.Height
end
local editBtn = uikit:createButton("✏️")
if btnColor then
editBtn:setColor(btnColor)
end
editBtn:setParent(node)
node.editBtn = editBtn
editBtn.onRelease = function()
if node.requiresEdit ~= nil then
node:requiresEdit(node._selectedIndex, node:getCurrentColor())
end
end
local addBtn = uikit:createButton("➕")
if btnColor then
addBtn:setColor(btnColor)
end
addBtn:setParent(node)
node.addBtn = addBtn
addBtn.onRelease = function()
local default = Color.White
node:addColor(default)
if node.didAdd then
node:didAdd(default)
end
end
local deleteBtn = uikit:createButton("➖")
if btnColor then
deleteBtn:setColor(btnColor)
end
deleteBtn:setParent(node)
node.deleteBtn = deleteBtn
deleteBtn.onRelease = function()
node:_removeIndex(node._selectedIndex)
node:_refresh()
end
-- SELECTION FRAME
local selectionFrameShape = MutableShape(false)
selectionFrameShape.CollisionGroups = {}
local size = 10
for y = 0, size - 1 do
for x = 0, size - 1 do
if y == 0 or x == 0 or y == size - 1 or x == size - 1 then
selectionFrameShape:AddBlock(Color.White, x, y, 0)
end
if y == 1 or x == 1 or y == size - 2 or x == size - 2 then
selectionFrameShape:AddBlock(Color.Black, x, y, 0)
end
end
end
local selectionFrame = uikit:createShape(selectionFrameShape, { doNotFlip = true })
selectionFrame:setParent(node)
selectionFrame.Width = 40
selectionFrame.Height = 40
node.selectionFrame = selectionFrame
-- COLORS
node._resetColorsShape = function(self)
if self.colors ~= nil then
self.colors:remove()
end
local colorsShape = MutableShape()
colorsShape.CollisionGroups = {}
local colors = uikit:createShape(colorsShape, { doNotFlip = true, perBlockCollisions = true })
colors:setParent(self)
self.colors = colors
self.colorsShape = colorsShape
colors.onPress = function(self, _, block)
local index = self.parent:_indexFromBlock(block)
self.parent:_selectIndex(index)
end
end
node:_resetColorsShape()
node._indexFromBlock = function(self, block)
local column = block.Coords.X + 1
local row = -block.Coords.Y - 1
local index = row * self.nbColumns + column
return index
end
node._blockFromIndex = function(self, index)
local x = (index - 1) % self.nbColumns
local y = -1 - math.floor((index - 1) / self.nbColumns)
return self.colorsShape:GetBlock(x, y, 0)
end
node._selectIndex = function(self, index)
self._selectedIndex = math.tointeger(index)
self:_refreshSelectionFrame()
if self.didChangeSelection ~= nil then -- function(self, color)
self:didChangeSelection(self:getCurrentColor())
end
end
node._removeIndex = function(self, index)
local b = self:_blockFromIndex(index)
if b == nil then
return
end
-- The -1 accounts for the one block representing that color inside the palette widget itself
if self.colorsShape.Palette[self._selectedIndex].BlocksCount - 1 > 0 then
print("Cannot remove a color that is currently used in the item")
return
end
local count = self.colorsShape.BlocksCount
local b2
for i = index, count do
b2 = self:_blockFromIndex(i)
b:Replace(b2)
b = b2
end
b:Remove()
self.colorsShape.Palette:RemoveColor(index)
if index > count - 1 then
if count - 1 == 0 then
self:_selectIndex(0)
else
self:_selectIndex(1)
end
else
self:_selectIndex(index)
end
end
node._addBlock = function(self, colorIdx)
local nBlocks = self.colorsShape.BlocksCount
local x = nBlocks % self.nbColumns
local y = -(math.floor(nBlocks / self.nbColumns)) - 1
self.colorsShape:AddBlock(colorIdx, x, y, 0)
return nBlocks + 1
end
node._refreshSelectionFrame = function(self)
if self._selectedIndex == 0 then
self.selectionFrame:hide()
return
else
self.selectionFrame:show()
end
local b = self:_blockFromIndex(self._selectedIndex)
if b == nil then
self.selectionFrame:hide()
return
end
self.selectionFrame.Width = self._squareSize
self.selectionFrame.Height = self._squareSize
self.selectionFrame.LocalPosition.X = self.colors.pos.X + b.Coords.X * self._squareSize
self.selectionFrame.LocalPosition.Y = self.colors.pos.Y + self.colors.Height + b.Coords.Y * self._squareSize
end
node._refresh = function(self)
local controls = { self.deleteBtn, self.editBtn, self.addBtn }
local controlsWidth = self.padding
for _, element in ipairs(controls) do
controlsWidth = controlsWidth + element.Width + self.padding
end
local controlsWidthWithoutPadding = controlsWidth - self.padding * 2
local heightWithoutColors = self.padding * 3 + self.editBtn.Height
local availableHeight = self.maxHeight - heightWithoutColors
local columns = self.colorsShape.Width
local rows = self.colorsShape.Height
-- commented this part to use the Height of buttons instead
self._squareSize = self.addBtn.Height
local colorsHeight = self._squareSize * rows
if colorsHeight > availableHeight then
colorsHeight = availableHeight
self._squareSize = availableHeight / rows
end
local colorsWidth = self._squareSize * columns
if colorsWidth > Screen.Width * self._maxScreenWidthFactor then
colorsWidth = Screen.Width * self._maxScreenWidthFactor
self._squareSize = colorsWidth / columns
colorsHeight = self._squareSize * rows
end
-- force beyond availableHeight if colorsWidth
-- is less than controlsWidthWithoutPadding
if colorsWidth < controlsWidthWithoutPadding then
colorsWidth = controlsWidthWithoutPadding
self._squareSize = colorsWidth / columns
colorsHeight = self._squareSize * rows
end
if self._squareSize > self.maxSquareSize then
self._squareSize = self.maxSquareSize
colorsWidth = self._squareSize * columns
colorsHeight = self._squareSize * rows
end
self.colors:refresh()
self.colors.Width = colorsWidth
self.colors.Height = colorsHeight
self.colors.pos = { self.padding, self.deleteBtn.LocalPosition.Y + self.deleteBtn.Height + self.padding, 0 }
colorsWidth = colorsWidth + self.padding * 2
local width = controlsWidth > colorsWidth and controlsWidth or colorsWidth
self.background.Height = heightWithoutColors + self.colors.Height
self.background.Width = width
self.addBtn.LocalPosition = { width - self.padding - self.addBtn.Width, self.padding, 0 }
self.editBtn.LocalPosition =
{ self.addBtn.LocalPosition.X - self.editBtn.Width - self.padding, self.padding, 0 }
self.deleteBtn.LocalPosition =
{ self.editBtn.LocalPosition.X - self.deleteBtn.Width - self.padding, self.padding, 0 }
self:_refreshSelectionFrame()
if self.didRefresh ~= nil then
self:didRefresh()
end
end
node.parentDidResize = function(self)
self:_refresh()
end
node.getCurrentColor = function(self)
return self.colorsShape.Palette[self._selectedIndex].Color
end
node.getCurrentIndex = function(self)
return self._selectedIndex
end
-- returns true if added
node.selectIndexOrAddColorIfMissing = function(self, paletteIndex, color)
if paletteIndex > 0 and paletteIndex <= #self.colorsShape.Palette then
self:_selectIndex(paletteIndex)
return false
end
self:addColor(color)
return true
end
node.addColor = function(self, color, skipRefreshAndSelect)
local colorIdx = self.colorsShape.Palette:AddColor(color)
local blockIdx = self:_addBlock(colorIdx)
if skipRefreshAndSelect ~= true then
self:_refresh()
self:_selectIndex(blockIdx)
end
end
node.setColors = function(self, container)
local palette
if type(container) == "Palette" then
palette = container
elseif type(container) == "Shape" or type(container) == "MutableShape" then
palette = container.Palette
else
error("palette.setColors expects a Palette or a Shape")
end
node:_resetColorsShape()
-- share palette, any change to it will affect original palette automatically
self.colorsShape.Palette = palette
-- add blocks representing each color
for i = 1, #palette do
self:_addBlock(i)
end
if #palette == 0 then
self:_selectIndex(0)
else
self:_selectIndex(1)
end
node:_refresh()
end
node.setSelectedColor = function(self, color)
self.colorsShape.Palette[self._selectedIndex].Color = color
end
node.mergeColors = function(self, container)
local palette
if type(container) == "Palette" then
palette = container
elseif type(container) == "Shape" or type(container) == "MutableShape" then
palette = container.Palette
else
error("palette.mergeColors expects a Palette or a Shape")
end
self.colorsShape.Palette:Merge(palette)
if #palette == 0 then
self:_selectIndex(0)
else
self:_selectIndex(1)
end
node:_refresh()
end
-- DEFAULT COLORS
--[[ node:addColor(Color(86, 51, 23))
node:addColor(Color(129, 88, 54))
node:addColor(Color(234, 159, 98))
node:addColor(Color(255, 220, 191))
node:addColor(Color(0, 0, 0))
node:addColor(Color(84, 84, 84))
node:addColor(Color(168, 168, 168))
node:addColor(Color(255, 255, 255))
node:addColor(Color(0, 47, 142))
node:addColor(Color(0, 81, 173))
node:addColor(Color(0, 120, 255))
node:addColor(Color(158, 189, 255))
node:addColor(Color(0, 81, 123))
node:addColor(Color(17, 139, 174))
node:addColor(Color(76, 215, 255))
node:addColor(Color(164, 250, 255))
node:addColor(Color(2, 83, 0))
node:addColor(Color(20, 160, 17))
node:addColor(Color(6, 238, 0))
node:addColor(Color(132, 255, 32))
node:addColor(Color(255, 191, 0))
node:addColor(Color(255, 224, 58))
node:addColor(Color(255, 221, 120))
node:addColor(Color(255, 253, 211))
node:addColor(Color(127, 65, 50))
node:addColor(Color(188, 75, 0))
node:addColor(Color(253, 110, 14))
node:addColor(Color(253, 174, 78))
node:addColor(Color(184, 13, 0))
node:addColor(Color(255, 18, 0))
node:addColor(Color(255, 117, 156))
node:addColor(Color(255, 175, 198))
node:addColor(Color(178, 0, 113))
node:addColor(Color(255, 0, 120))
node:addColor(Color(255, 157, 219))
node:addColor(Color(248, 203, 231))
node:addColor(Color(61, 0, 85))
node:addColor(Color(136, 0, 252))
node:addColor(Color(182, 122, 233))
node:addColor(Color(237, 215, 255)) ]]
return node
end
return palette