X Tutup
Skip to content
This repository was archived by the owner on Jan 4, 2023. It is now read-only.

Commit 0c91180

Browse files
committed
Fixing tool selection in Paint Tool.
1 parent 01e3df3 commit 0c91180

File tree

6 files changed

+212
-141
lines changed

6 files changed

+212
-141
lines changed

Disks/PixelVisionOS/System/Libs/pixel-vision-ui-button-v3.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ function EditorUI:CreateButton(rect, spriteName, toolTip, forceDraw)
2525
data.doubleClickDelay = .45
2626
data.doubleClickActive = false
2727

28+
data.drawMode = DrawMode.TilemapCache
29+
2830
data.buttonCursor = 2
2931

3032
-- By default, we don't want buttons to redraw the background
@@ -171,7 +173,9 @@ function EditorUI:UpdateButton(data, hitRect)
171173

172174
if(spriteData ~= nil) then-- and data.spriteDrawArgs ~= nil) then
173175

174-
DrawMetaSprite(spriteData, data.rect.x, data.rect.y)
176+
DrawMetaSprite(spriteData, data.rect.x, data.rect.y, false, false, DrawMode.UI)
177+
178+
175179

176180
end
177181

@@ -252,12 +256,16 @@ function EditorUI:RedrawButton(data, stateOverride)
252256
data.rect.y,
253257
false,
254258
false,
255-
DrawMode.TilemapCache, 0
259+
data.drawMode,
260+
0
256261
)
257262

258263
end
259264

260-
self:ResetValidation(data)
265+
-- If we are drawing the button into the tilemap we can reset the validation so it doesn't draw on the next frame
266+
if(data.drawMode == DrawMode.TilemapCache or data.drawMode == DrawMode.Tile) then
267+
self:ResetValidation(data)
268+
end
261269

262270
end
263271

Disks/PixelVisionOS/System/Tools/PaintTool/code-canvas-panel.lua

Lines changed: 106 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function PaintTool:CreateCanvasPanel()
5454
self.imageCanvas.SetPixels(0, 0, self.image.Width, self.image.Height, pixelData)
5555

5656

57-
self.tmpPaintCanvas = NewCanvas(self.viewportRect.width, self.viewportRect.height)
57+
self.tmpPaintCanvas = NewCanvas(self.imageCanvas.Width, self.imageCanvas.Width)
5858
self.tmpPaintCanvas.Clear(-1);
5959

6060
self.vSliderData = editorUI:CreateSlider({x = 235-3, y = 44+3 + 8, w = 10, h = 193-24 - 7 - 8}, "vsliderhandle", "Scroll text vertically.")
@@ -123,6 +123,11 @@ end
123123

124124
function PaintTool:UpdateCanvasPanel(timeDelta)
125125

126+
-- Ignore the update if the option menu is open
127+
-- if(self.optionMenuOpen == true) then
128+
-- return
129+
-- end
130+
126131
-- If the button has data but it's not enabled exit out of the update
127132
if(self.canvasPanel.enabled == false) then
128133

@@ -178,12 +183,14 @@ function PaintTool:UpdateCanvasPanel(timeDelta)
178183

179184
-- TODO need to adjust for scroll and scale
180185
local tmpPos = NewPoint(
181-
math.floor((editorUI.collisionManager.mousePos.x - self.viewportRect.x ) / self.scale) + self.scaledViewport.X,
182-
math.floor((editorUI.collisionManager.mousePos.y - self.viewportRect.y)/ self.scale) + self.scaledViewport.Y
186+
math.floor((editorUI.collisionManager.mousePos.x - self.viewportRect.X)/ self.scale) + self.scaledViewport.X ,
187+
math.floor((editorUI.collisionManager.mousePos.y - self.viewportRect.Y)/ self.scale) + self.scaledViewport.Y
183188
)
184189

185190
-- print("Mouse Pos", dump(editorUI.collisionManager.mousePos), "adjusted", tmpPos, self.scaledViewport.X)
186191

192+
-- print("Mouse", dump(editorUI.collisionManager.mousePos), (math.floor((editorUI.collisionManager.mousePos.x - self.viewportRect.X)/ self.scale) + self.scaledViewport.X))
193+
187194
-- Check to see if the button is pressed and has an onAction callback
188195
if(editorUI.collisionManager.mouseReleased == true) then
189196

@@ -289,37 +296,13 @@ function PaintTool:DrawCanvasPanel()
289296
-- Draw the pixel data in the upper left hand cornver of the tool's window
290297
self.imageCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, -1, self.maskColor, self.colorOffset, self.scaledViewport)
291298

292-
self.tmpPaintCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, -1, self.emptyColorID, self.colorOffset, NewRect(0, 0, self.scaledViewport.Width, self.scaledViewport.Height))
299+
self.tmpPaintCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, -1, self.emptyColorID, self.colorOffset, self.scaledViewport)
293300

294301

295302
self.displayInvalid = false
296303

297304
end
298305

299-
-- if(self.canvasPanel.inFocus == true) then
300-
301-
-- -- print(editorUI.mouseCursor.pos)
302-
303-
-- local tmpX = self.mCol * self.gridSize
304-
-- local tmpY = self.mRow * self.gridSize
305-
306-
-- if(tmpX < (self.tmpPaintCanvas.width * self.scale) and tmpY < (self.tmpPaintCanvas.height * self.scale)) then
307-
308-
-- -- TODO If dragging, snap this to the mouse position
309-
310-
-- tmpX = tmpX + self.viewportRect.X - self.scaledViewport.X
311-
-- tmpY = tmpY + self.viewportRect.Y- self.scaledViewport.Y
312-
313-
-- self.overCanvas:DrawPixels( tmpX - 3 , tmpY - 3, DrawMode.UI )
314-
315-
-- self.tmpPaintCanvas:DrawPixels(tmpX, tmpY, DrawMode.SpriteAbove, self.scale, -1, self.maskColor, 0, NewRect( self.mCol * 8 + self.scaledViewport.X, self.mRow * 8 + self.scaledViewport.Y, 8, 8 ))
316-
317-
-- else
318-
-- editorUI:ClearFocus(self.canvasPanel)
319-
-- end
320-
-- end
321-
322-
323306
end
324307

325308

@@ -363,6 +346,8 @@ function PaintTool:OnNextZoom(reverse)
363346
self.scaledViewport.Width = Clamp(viewWidth, 1, math.max(imageWidth, self.imageCanvas.width)) --math.min(self.viewportRect.Width, math.min(self.tmpPaintCanvas.width * self.scale, math.ceil(self.viewportRect.Width / self.scale)))
364347
self.scaledViewport.Height = Clamp(viewHeight, 1, math.max(imageHeight, self.imageCanvas.height))--, self.viewportRect.Height) --math.min(self.viewportRect.Height, math.min(self.tmpPaintCanvas.height * self.scale, math.ceil(self.viewportRect.Height / self.scale)))
365348

349+
350+
print("self.scaledViewport", dump(self.scaledViewport))
366351
-- Calculate the boundary for scrolling
367352
self.boundaryRect.Width = self.imageCanvas.width - self.scaledViewport.Width
368353
self.boundaryRect.Height = self.imageCanvas.height - self.scaledViewport.Height
@@ -530,6 +515,9 @@ function PaintTool:CanvasRelease(callAction)
530515

531516
-- -- Clear the canvas
532517
self.tmpPaintCanvas:Clear()
518+
519+
520+
self:InvalidateSprites()
533521

534522
-- if(data.selectRect ~= nil and (data.selectRect.Width == 0 or data.selectRect.Height == 0)) then
535523
-- data.selectRect = nil
@@ -663,67 +651,69 @@ function PaintTool:DrawOnCanvas(mousePos)
663651

664652
self:InvalidateCanvas()
665653

666-
-- elseif(data.tool == "select") then
654+
elseif(self.tool == "select") then
667655

668-
-- if(data.mouseState == "pressed") then
656+
if(self.mouseState == "pressed") then
669657

670-
-- if(data.selectRect == nil) then
658+
if(self.selectRect == nil) then
671659

672-
-- data.selectionState = "new"
660+
self.selectionState = "new"
673661

674-
-- data.selectRect = NewRect(data.startPos.x, data.startPos.y, 0, 0)
662+
self.selectRect = NewRect(self.startPos.x, self.startPos.y, 0, 0)
675663

676-
-- else
664+
else
677665

678-
-- if(data.selectRect:Contains(mousePos) == true) then
666+
if(self.selectRect:Contains(mousePos) == true) then
679667

680-
-- data.selectionState = "newmove"
668+
self.selectionState = "newmove"
681669

682-
-- data.moveOffset = NewPoint(data.selectRect.X - mousePos.X, data.selectRect.Y - mousePos.Y)
670+
self.moveOffset = NewPoint(self.selectRect.X - mousePos.X, self.selectRect.Y - mousePos.Y)
683671

684-
-- if(data.selectedPixelData == nil) then
672+
if(self.selectedPixelData == nil) then
685673

686-
-- data.selectedPixelData = self:CutPixels(data)
687-
-- end
674+
self.selectedPixelData = self:CutPixels(self)
675+
end
688676

689-
-- else
677+
else
690678

691-
-- self:CancelCanvasSelection(data)
679+
self:CancelCanvasSelection(self)
692680

693-
-- end
681+
end
694682

695-
-- end
683+
end
696684

697685
elseif(self.mouseState == "dragging") then
698686

699-
-- if(data.selectRect ~= nil) then
700-
-- if(data.selectionState == "new" or data.selectionState == "resize") then
687+
if(self.selectRect ~= nil) then
688+
if(self.selectionState == "new" or self.selectionState == "resize") then
701689

702-
-- data.selectionState = "resize"
690+
self.selectionState = "resize"
703691

704-
-- -- print("resize", data.selectRect, mousePos, , )
692+
-- print("resize", data.selectRect, mousePos, , )
705693

706-
-- data.selectRect.X = math.min(data.startPos.X, mousePos.X)
707-
-- data.selectRect.Y = math.min(data.startPos.Y, mousePos.Y)
708-
-- data.selectRect.Width = Clamp(math.abs(mousePos.X - data.startPos.X), 0, data.tmpPaintCanvas.width)
709-
-- data.selectRect.Height = Clamp(math.abs(mousePos.Y - data.startPos.Y), 0, data.tmpPaintCanvas.height)
694+
self.selectRect.X = math.min(self.startPos.X, mousePos.X)
695+
self.selectRect.Y = math.min(self.startPos.Y, mousePos.Y)
696+
self.selectRect.Width = Clamp(math.abs(mousePos.X - self.startPos.X), 0, self.tmpPaintCanvas.width)
697+
self.selectRect.Height = Clamp(math.abs(mousePos.Y - self.startPos.Y), 0, self.tmpPaintCanvas.height)
710698

711-
-- else
699+
else
712700

713701

714-
-- editorUI.cursorID = 2
702+
editorUI.cursorID = 2
715703

716-
-- data.selectRect.X = mousePos.X + data.moveOffset.X -- data.selectionSize.X
717-
-- data.selectRect.Y = mousePos.Y + data.moveOffset.Y -- data.selectionSize.Y
704+
self.selectRect.X = mousePos.X + self.moveOffset.X -- data.selectionSize.X
705+
self.selectRect.Y = mousePos.Y + self.moveOffset.Y -- data.selectionSize.Y
718706

719707

720-
-- data.selectionState = "move"
708+
self.selectionState = "move"
721709

722-
-- end
723-
-- end
710+
end
711+
end
712+
724713

725-
-- end
714+
end
726715

716+
print("Selection", dump(self.selectRect))
727717

728718

729719
elseif(self.tool == "eyedropper") then
@@ -743,3 +733,59 @@ function PaintTool:DrawOnCanvas(mousePos)
743733

744734
end
745735

736+
function PixelVisionOS:CutPixels()
737+
738+
if(self.selectRect == nil) then
739+
return
740+
end
741+
742+
local selection =
743+
{
744+
size = NewRect(self.selectRect.X, self.selectRect.Y, self.selectRect.Width, self.selectRect.Height)
745+
}
746+
747+
selection.pixelData = self.paintCanvas:GetPixels(selection.size.X, selection.size.Y, selection.size.Width, selection.size.Height)
748+
749+
-- Convert the mask colors to the tool's mask color
750+
for i = 1, #selection.pixelData do
751+
if(selection.pixelData[i] == 255) then
752+
selection.pixelData[i] = -1
753+
end
754+
end
755+
756+
selection.pixelData[i] = 5
757+
758+
local bgColor = self.showBGColor and gameEditor:BackgroundColor() + 256 or self.emptyColorID
759+
760+
761+
-- Change the stroke to a single pixel of white
762+
self.tmpPaintCanvas:SetStroke(bgColor, self.defaultStrokeWidth)
763+
764+
-- Change the stroke to a single pixel of white
765+
self.tmpPaintCanvas:SetPattern({ bgColor }, self.defaultStrokeWidth, self.defaultStrokeWidth)
766+
767+
-- Adjust right and bottom to account for 1 px border
768+
self.tmpPaintCanvas:DrawRectangle(selection.size.Left, selection.size.Top, selection.size.Right - 1, selection.size.Bottom -1, true)
769+
770+
return selection
771+
772+
end
773+
774+
function PixelVisionOS:FillCanvasSelection(self, colorID)
775+
776+
if(self.selectRect == nil) then
777+
return
778+
end
779+
780+
if(self.selectedPixelData == nil) then
781+
self.selectedPixelData = self:CutPixels(self)
782+
end
783+
784+
for i = 1, #self.selectedPixelData.pixelData do
785+
self.selectedPixelData.pixelData[i] = colorID or (self.brushColor + self.colorOffset)
786+
end
787+
788+
--Fire a release event
789+
self:CanvasRelease(self, true)
790+
791+
end

Disks/PixelVisionOS/System/Tools/PaintTool/code-picker-modal.lua

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
11
ToolPickerModal = {}
22
ToolPickerModal.__index = ToolPickerModal
33

4-
function ToolPickerModal:Init(pos, buttons)
4+
function ToolPickerModal:Init(pos, toolButtons, optionButtons)
55

66
local _toolPickerModal = {}
77
setmetatable(_toolPickerModal, ToolPickerModal)
88

9-
_toolPickerModal:Configure(pos, buttons)
9+
_toolPickerModal:Configure(pos, toolButtons, optionButtons)
1010

1111
return _toolPickerModal
1212

1313
end
1414

15-
function ToolPickerModal:Configure(pos, buttons)
15+
function ToolPickerModal:Configure(pos, toolButtons, optionButtons)
1616

1717

1818
self.selection = -1
1919

20-
-- if(buttons == nil) then
21-
-- buttons =
22-
-- {
23-
-- {
24-
-- name = "pointer",
25-
-- tooltip = "Press 'enter' to close",
26-
-- },
27-
-- {
28-
-- name = "line",
29-
-- tooltip = "Press 'enter' to close",
30-
-- }
31-
-- }
32-
-- end
20+
self.toolButtons = toolButtons
3321

3422
-- Reset the modal so it redraws correctly when opened
3523
self.firstRun = true
@@ -38,15 +26,15 @@ function ToolPickerModal:Configure(pos, buttons)
3826

3927
local tmpButton = nil
4028

41-
local total = #buttons
29+
local total = #optionButtons
4230

4331
local bX = pos.X
4432
local bY = pos.Y
4533

4634
for i = 1, total do
4735

4836
-- Get a reference to the button properties
49-
tmpButton = buttons[i]
37+
tmpButton = optionButtons[i]
5038

5139
bY = bY + 16
5240

@@ -83,14 +71,14 @@ end
8371

8472
function ToolPickerModal:Update(timeDelta)
8573

86-
local tmpBtn = nil
87-
8874
for i = 1, #self.buttonData do
8975

90-
tmpBtn = self.buttonData[i]
91-
92-
editorUI:UpdateButton(tmpBtn)
76+
editorUI:UpdateButton(self.buttonData[i])
77+
78+
end
9379

80+
for i = 1, #self.toolButtons do
81+
editorUI:UpdateButton(self.toolButtons[i])
9482
end
9583

9684
if(MouseButton( 0, InputState.Released )) then

0 commit comments

Comments
 (0)
X Tutup