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

Commit

Permalink
Fixing render issues introduce by new Canvas draw logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed Oct 6, 2020
1 parent ccf6ede commit 87f45a9
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 310 deletions.
185 changes: 25 additions & 160 deletions Disks/PixelVisionOS/System/Libs/pixel-vision-os-canvas-v3.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,8 @@ function PixelVisionOS:CreateCanvas(rect, size, scale, colorOffset, toolTip, emp

data.gridCanvas = NewCanvas(data.rect.w, data.rect.h)
data.gridCanvas.wrap = false
-- TODO find a real empty color


-- local spriteData = _G["pixelselection1x"]

-- data.overDrawArgs = {spriteData.spriteIDs, 0, 0, spriteData.width, false, false, DrawMode.Sprite, 36, true, false}


data.brushDrawArgs = {0, 0, 8, 8, data.brushColorID, DrawMode.Sprite}
-- DrawRect( 0, 0, 8, 8, 54, DrawMode.Sprite )

data.onClick = function(tmpData)

Expand Down Expand Up @@ -93,12 +86,6 @@ function PixelVisionOS:CreateCanvas(rect, size, scale, colorOffset, toolTip, emp

self:ResizeCanvas(data, size, scale)


-- Clear the background so the mask color shows through
-- DrawRect(rect.x, rect.y, rect.w, rect.h, emptyColorID, DrawMode.TilemapCache)

-- data.currentTool = 1 -- default tool

editorUI:ResetValidation(data)

return data
Expand All @@ -107,8 +94,6 @@ end

function PixelVisionOS:ChangeCanvasTool(data, toolName, cursorID)

-- print("Change canvas tool", toolName)

data.tool = toolName

-- Clear the selection when changing tools/
Expand Down Expand Up @@ -137,11 +122,6 @@ function PixelVisionOS:ChangeCanvasTool(data, toolName, cursorID)
-- Save the new cursor for tools that need to restore
data.defaultCursorID = data.currentCursorID

-- data.showPixelSelection = data.tool ~= "eyedropper" and data.tool ~= "fill"


-- TODO change the drawing tool

end

function PixelVisionOS:SetCanvasPixels(data, pixelData)
Expand All @@ -155,8 +135,6 @@ function PixelVisionOS:UpdateCanvas(data, hitRect)
return
end



-- If the button has data but it's not enabled exit out of the update
if(data.enabled == false) then

Expand Down Expand Up @@ -209,7 +187,6 @@ function PixelVisionOS:UpdateCanvas(data, hitRect)

end


end

local tmpPos = NewPoint(
Expand All @@ -221,8 +198,6 @@ function PixelVisionOS:UpdateCanvas(data, hitRect)
tmpPos.x = tmpPos.x / data.scale
tmpPos.y = tmpPos.y / data.scale



if(data.selectRect ~= nil) then

if(data.selectRect:Contains(tmpPos) == true) then
Expand Down Expand Up @@ -258,42 +233,22 @@ function PixelVisionOS:UpdateCanvas(data, hitRect)

local tmpColor = data.paintCanvas:ReadPixelAt(tmpPos.x, tmpPos.y) - data.colorOffset

-- TODO this is duplicated below

-- Update over color if using eye picker
-- if(pixelVisionOS.paletteMode == true) then

-- TODO this is hard coded to look for a palette color picker

-- Shift the value to offset for the palette
-- tmpColor = tmpColor + PaletteOffset(paletteColorPickerData.pages.currentSelection - 1)

-- end

ReplaceColor(data.brushColorID, tmpColor + data.colorOffset)

end


-- Only update the over rect if the mouse is over the canvas
if(inRect == true and data.tool ~= "select") then

-- Update over rect position
-- data.overDrawArgs[2] = (position.x * data.gridSize) + data.rect.x - data.borderOffset
-- data.overDrawArgs[3] = (position.y * data.gridSize) + data.rect.y - data.borderOffset
data.brushDrawArgs[1] = (position.x * data.gridSize) + data.rect.x - data.borderOffset
data.brushDrawArgs[2] = (position.y * data.gridSize) + data.rect.y - data.borderOffset
-- Draw over rect
-- editorUI:NewDraw("DrawSprites", data.overDrawArgs)

if(data.tool ~= "eyedropper") then
editorUI:NewDraw("DrawRect", data.brushDrawArgs)
end

end

-- end

-- Check to see if the button is pressed and has an onAction callback
if(editorUI.collisionManager.mouseReleased == true) then

Expand All @@ -311,8 +266,6 @@ function PixelVisionOS:UpdateCanvas(data, hitRect)
-- Save start position
data.startPos = NewPoint(tmpPos.x, tmpPos.y)

-- if(data.triggerOnFirstPress ~= nil) then
-- Call the onPress method for the button
data.triggerOnFirstPress(data)
-- end

Expand All @@ -321,8 +274,6 @@ function PixelVisionOS:UpdateCanvas(data, hitRect)
-- Change the flag so we don't trigger first press again
data.firstPress = false



end

self:DrawOnCanvas(data, tmpPos)
Expand Down Expand Up @@ -476,23 +427,13 @@ function PixelVisionOS:DrawOnCanvas(data, mousePos, toolID)

if(data.selectedPixelData == nil) then

data.selectedPixelData = self:CutPixels(data)--data.paintCanvas:GetPixels(data.selectRect.Left, data.selectRect.Top, data.selectRect.Right, data.selectRect.Bottom)
-- print("selectedPixelData", #data.selectedPixelData.pixelData, dump(data.selectedPixelData))

data.selectedPixelData = self:CutPixels(data)
end
-- print("data.moveOffset", data.moveOffset)

-- TODO copy pixel data from selection into memory
-- TODO delete pixel data from selection

else

self:CancelCanvasSelection(data)
-- data.selectionState = "none"

-- data.selectRect = nil
-- data.selectionSize = nil


end

end
Expand All @@ -513,25 +454,12 @@ function PixelVisionOS:DrawOnCanvas(data, mousePos, toolID)

else

-- print("data.selectionState", data.selectionState)



editorUI.cursorID = 2

data.selectRect.X = mousePos.X + data.moveOffset.X -- data.selectionSize.X
data.selectRect.Y = mousePos.Y + data.moveOffset.Y -- data.selectionSize.Y

-- if(data.selectionState == "move" and data.selectedPixelData == nil) then

-- data.selectedPixelData = self:CutPixels(data)--data.paintCanvas:GetPixels(data.selectRect.Left, data.selectRect.Top, data.selectRect.Right, data.selectRect.Bottom)

-- print("Selected pixels", #data.selectedPixelData.pixelData, dump(data.selectedPixelData))




-- end


data.selectionState = "move"

Expand All @@ -540,8 +468,6 @@ function PixelVisionOS:DrawOnCanvas(data, mousePos, toolID)

end

-- print("Selection", data.selectionState)

elseif(data.tool == "circle") then

data.tmpPaintCanvas:Clear()
Expand Down Expand Up @@ -643,13 +569,7 @@ function PixelVisionOS:CancelCanvasSelection(data, mergeSelection, action)
data.selectedPixelData = nil
data.selectionState = "none"
data.selectRect = nil
-- data.defaultCursorID = 7
-- self.cursorID = 7
-- self.mouseCursor:SetCursor(data.defaultCursorID)


-- print("Cursor", data.defaultCursorID)


if(action ~= false) then
--Fire a release event
self:CanvasRelease(data, true)
Expand All @@ -666,19 +586,7 @@ function PixelVisionOS:ResetCanvasStroke(data)

-- Change the stroke to a single pixel
data.tmpPaintCanvas:SetStroke(realBrushColor, 1)
data.tmpPaintCanvas:SetPattern(realBrushColor, 1)

-- Check to see if we are in palete mode
-- if(se/lf.paletteColorPickerData ~= nil) then

-- -- TODO this is hard coded to look for a palette color picker

-- -- TODO this is not finding the palette picker?
-- print("Test", self.paletteColorPickerData)
-- -- Shift the value to offset for the palette
-- tmpColor = tmpColor + PaletteOffset(self.paletteColorPickerData.pages.currentSelection - 1)

-- end
data.tmpPaintCanvas:SetPattern({realBrushColor}, 1, 1)

ReplaceColor(data.brushColorID, tmpColor + data.colorOffset)

Expand Down Expand Up @@ -718,70 +626,57 @@ function PixelVisionOS:RedrawCanvas(data)
end

if(data.selectedPixelData ~= nil) then

data.tmpLayerCanvas:Clear()
data.tmpLayerCanvas:SetPixels(data.selectRect.Left, data.selectRect.Top, data.selectedPixelData.size.Width, data.selectedPixelData.size.Height, data.selectedPixelData.pixelData)
data.tmpLayerCanvas:DrawPixels(data.rect.x, data.rect.y, DrawMode.TilemapCache, data.scale, bgColor, data.emptyColorID)
data.paintCanvas.Invalidate()
-- data.tmpLayerCanvas.ResetValidation()

end

-- TODO need to find a way to only draw this when the canvas has been invalidated
if(data.showGrid == true) then
data.gridCanvas:DrawPixels(data.rect.x, data.rect.y, DrawMode.Sprite, 1, -1, -1)
end

-- if(data.selectionCanvas.invalid == true) then

if(data.selectRect ~= nil) then

data.selectionCanvas:Clear()

-- Loop through pixels and invert if needed

-- Draw the canvas to the display
if(math.abs(data.selectRect.Width) > 0 and math.abs(data.selectRect.Height) > 0) then

-- Adjust right and bottom by 1 so selection is inside of selected pixels
data.selectionCanvas:DrawSquare(data.selectRect.Left * data.scale, data.selectRect.Top * data.scale, data.selectRect.Right * data.scale - 1, data.selectRect.Bottom * data.scale -1, false)

bgColor = data.showBGColor and gameEditor:BackgroundColor() + 256 or -1

data.selectionCanvas:DrawPixels(data.rect.x, data.rect.y, DrawMode.Sprite, 1, -1, -1)
end
-- Draw the canvas to the display
if(math.abs(data.selectRect.Width) > 0 and math.abs(data.selectRect.Height) > 0) then

-- Adjust right and bottom by 1 so selection is inside of selected pixels
data.selectionCanvas:DrawSquare(data.selectRect.Left * data.scale, data.selectRect.Top * data.scale, data.selectRect.Right * data.scale - 1, data.selectRect.Bottom * data.scale -1, false)

bgColor = data.showBGColor and gameEditor:BackgroundColor() + 256 or -1

data.selectionCanvas:DrawPixels(data.rect.x, data.rect.y, DrawMode.Sprite, 1, -1, -1)
end

end

-- end

end

-- Use this to perform a click action on a button. It's used internally when a mouse click is detected.
function PixelVisionOS:CanvasRelease(data, callAction)

-- print("Canvas Release")


-- Clear the start position
data.startPos = nil

data.mouseState = data.mouseState == "released" and "up" or "released"


-- print("Released")
-- Return if the selection rect is nil
-- if(data.selectRect ~= nil) then
-- return
-- end

if(data.tmpPaintCanvas.invalid == true) then

data.tmpPaintCanvas:Draw()

-- Merge the pixel data from the tmp canvas into the main canvas before it renders
data.paintCanvas:MergeCanvas(data.tmpPaintCanvas, 0, true)

-- Clear the canvas
data.tmpPaintCanvas:Clear()

-- Normally clearing the canvas invalidates it but se want to reset it until its drawn in again
-- Normally clearing the canvas invalidates it but we want to reset it until its drawn in again
data.tmpPaintCanvas:ResetValidation()

-- print("Merged tmp canvas")
Expand All @@ -792,25 +687,6 @@ function PixelVisionOS:CanvasRelease(data, callAction)
data.selectRect = nil
end

-- if(data.selectedPixelData ~= nil) then
-- data.tmpLayerCanvas:Clear()
-- print("Draw" , data.selectRect)
-- -- data.tmpLayerCanvas:SetPixels(data.selectRect.Left, data.selectRect.Top, 1, 1, data.selectedPixelData)
-- data.tmpLayerCanvas.Invalidate()
-- end

-- if(data.tmpLayerCanvas.invalid == true) then

-- print("Copy layer to paint canvas")

-- data.paintCanvas:MergeCanvas(data.tmpLayerCanvas, 0, true)

-- data.tmpLayerCanvas.Clear()

-- data.tmpLayerCanvas:ResetValidation()

-- end

if(data.selectionCanvas.invalid == true) then

data.selectionCanvas:ResetValidation()
Expand All @@ -823,10 +699,6 @@ function PixelVisionOS:CanvasRelease(data, callAction)

oldPixelData = data.paintCanvas:GetPixels()

-- data.paintCanvas:MergeCanvas(data.selectionCanvas, 0, true)

-- prin/t("Pixels", dump(data.selectedPixelData.pixelData))

--TODO need to test for a special key down and toggle ignoring transparency
data.ignoreMaskColor = true

Expand All @@ -852,8 +724,6 @@ end

function PixelVisionOS:CanvasPress(data, callAction)

-- print("onPress", "Update canvas")

data.tmpPaintCanvas:Invalidate()

if(data.onPress ~= nil and callAction ~= false) then
Expand Down Expand Up @@ -901,7 +771,7 @@ function PixelVisionOS:ResizeCanvas(data, size, scale, pixelData)
end

data.paintCanvas:SetPixels(0, 0, size.x, size.y, pixelData);

end

-- Redraw the grid
Expand Down Expand Up @@ -975,17 +845,12 @@ function PixelVisionOS:ClearCanvas(data)

end


-- local brushSizes = {16, 8, 8, 8, 4}

function PixelVisionOS:ChangeCanvasPixelSize(data, size)

-- data.pixelSelectionSize = size

data.brushDrawArgs[3] = size-- brushSizes[size]
data.brushDrawArgs[4] = size --brushSizes[size]
data.brushDrawArgs[3] = size
data.brushDrawArgs[4] = size

-- Calculate the gridSize TODO this is off because we don't support scale 3 so clamping
data.gridSize = size--Clamp((3 - data.pixelSelectionSize) * 8, 4, 16)
data.gridSize = size

end

0 comments on commit 87f45a9

Please sign in to comment.