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-canvas-panel.lua
executable file
·279 lines (179 loc) · 7.1 KB
/
code-canvas-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
--[[
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 canvasID = "CanvasUI"
function DrawTool:CreateCanvas()
self.showBGColor = false
self.lastCanvasScale = 0
self.lastCanvasSize = 0
self.cps = gameEditor:ColorsPerSprite()
self.canvasData = pixelVisionOS:CreateCanvas(
{
x = 32,
y = 32,
w = 128,
h = 128
},
{
x = 128,
y = 128
},
1,
pixelVisionOS.colorOffset,
"Draw on the canvas",
pixelVisionOS.emptyColorID
)
self.canvasData.onFirstPress = function()
--print("First Press!", editorUI.inFocusUI.name, self.canvasData.inDrawMode, self.canvasData.mouseState)
self:BeginUndo()
self.canvasData.inDrawMode = true
self:ForcePickerFocus(self.canvasData)
end
-- editorUI.collisionManager:EnableDragging(self.canvasData, .5, "SpritePicker")
-- self.canvasData.onDropTarget = function(src, dest) self:OnCanvasDrop(src, dest) end
self.canvasData.onAction = function()
if(editorUI.inFocusUI ~= nil and editorUI.inFocusUI.name == self.canvasData.name) then
print("Release")
self:OnSaveCanvasChanges()
self:EndUndo()
end
end
local clearBackgroundPattern = {}
local totalTiles = 128/8 * 128/8
local tileID = MetaSprite(FindMetaSpriteId("emptycolor")).Sprites[1].Id
for i = 1, totalTiles do
table.insert(clearBackgroundPattern, tileID)
end
self.clearBGPatterId = 250
-- TODO need to make sure this is an empty sprite
NewMetaSprite(250, "clearBackgroundPattern", clearBackgroundPattern, 128/8, 0)
end
local paletteKeys = {Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8}
-- TODO need to rename this and change other UpdateCanvas function
function DrawTool:UpdateCanvasUI()
if(self.canvasPanelBackgroundInvalid == true) then
self:ClearCanvasPanelBackground()
end
pixelVisionOS:UpdateCanvas(self.canvasData)
local newCopyValue = self.canvasData.selectRect ~= nil
if(self.lastCopyValue ~= newCopyValue) then
pixelVisionOS:EnableMenuItem(CopyShortcut, newCopyValue)
pixelVisionOS:EnableMenuItem(ClearShortcut, newCopyValue)
pixelVisionOS:EnableMenuItem(FillShortcut, newCopyValue)
self.lastCopyValue = newCopyValue
end
if(self.selectionMode == DrawingMode) then
for i = 1, 8 do
if(Key( paletteKeys[i], InputState.Released )) then
local index = i -1
if(Key( Keys.LeftShift, InputState.Down ) or Key(Keys.RightShift)) then
index = index + 8
end
-- TODO need to select color in palette
-- self:OnSelectPaletteColor(index)
break
end
end
end
end
function DrawTool:ShowCanvasPanel()
if(self.canvasPanelActive == true) then
return
end
self.canvasPanelActive = true
pixelVisionOS:RegisterUI({name = canvasID}, "UpdateCanvasUI", self)
pixelVisionOS:InvalidateCanvas(self.canvasData)
end
function DrawTool:HideCanvasPanel()
if(self.canvasPanelActive == false) then
return
end
self.canvasPanelActive = false
pixelVisionOS:RemoveUI(canvasID)
end
function DrawTool:ToggleBackgroundColor(value)
self.showBGColor = value
self.canvasData.showBGColor = value
if(self.usePalettes == true) then
-- pixelVisionOS:SelectColorPage(self.paletteColorPickerData, self.paletteColorPickerData.picker.selected)
else
self.canvasData.emptyColorID = pixelVisionOS.emptyColorID
end
pixelVisionOS:InvalidateCanvas(self.canvasData)
end
function DrawTool:InvalidateCanvasPanelBackground()
-- TODO should only need to do this when there is a switch between modes
if(self.selectionSize.x ~= self.selectionSize) then
-- print("Canvas is invalid")
self.canvasPanelBackgroundInvalid = true
end
end
function DrawTool:ClearCanvasPanelBackground()
-- print("Canvas BG Cleared")
-- Need to draw immediately since the canvas doesn't run through the UI draw queue
-- DrawSprites()
DrawMetaSprite(FindMetaSpriteId(self.clearBGPatterId), 4, 4, false, false, DrawMode.Tile)
self.canvasPanelBackgroundInvalid = false
end
function DrawTool:UpdateCanvas(value, flipH, flipV)
if(value == nil) then return end
print("value", value)
self:InvalidateCanvasPanelBackground()
flipH = flipH or false
flipV = flipV or false
-- Save the original pixel data from the selection
local tmpPixelData = gameEditor:ReadGameSpriteData(value, self.selectionSize.x, self.selectionSize.y, flipH, flipV)--
-- print("Test", self.spriteMode)
self.lastCanvasScale = self.selectionSize.scale--Clamp(8 * spriteSelection.scale, 4, 16)
self.lastCanvasSize = NewPoint((8 * self.selectionSize.x), (8 * self.selectionSize.y))
pixelVisionOS:ResizeCanvas(self.canvasData, self.lastCanvasSize, self.lastCanvasScale, tmpPixelData)
self.originalPixelData = {}
-- Need to loop through the pixel data and change the offset
local total = #tmpPixelData
for i = 1, total do
-- TODO index the canvas colors here
local newColor = tmpPixelData[i] - self.colorOffset
self.originalPixelData[i] = newColor
end
self.lastSelection = value
end
function DrawTool:IsSpriteEmpty(pixelData)
local total = #pixelData
for i = 1, total do
if(pixelData[i] ~= -1) then
return false
end
end
return true
end
function DrawTool:OnSaveCanvasChanges()
self.canvasData.inDrawMode = false
-- Get the raw pixel data
local pixelData = pixelVisionOS:GetCanvasPixelData(self.canvasData)
-- Get the canvas size
local canvasSize = pixelVisionOS:GetCanvasSize(self.canvasData)
-- Get the total number of pixel
local total = #pixelData
-- Loop through all the pixel data
for i = 1, total do
-- Shift the color value based on the canvas color offset
local newColor = pixelData[i] - self.canvasData.colorOffset
-- Set the new pixel index value
pixelData[i] = newColor < 0 and - 1 or newColor
end
-- Update the spritePickerData
if(self.spritePickerData.currentSelection > - 1) then
pixelVisionOS:UpdateItemPickerPixelDataAt(self.spritePickerData, self.spritePickerData.currentSelection, pixelData, canvasSize.width, canvasSize.height)
end
-- Update the current sprite in the picker
gameEditor:WriteSpriteData(self.spritePickerData.currentSelection, pixelData, self.selectionSize.x, self.selectionSize.y)
-- Invalidate the sprite tool since we change some pixel data
self:InvalidateData()
-- Reset the canvas invalidation since we copied it
editorUI:ResetValidation(self.canvasData)
end