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

Commit 0bda0c8

Browse files
committed
Fixed PixelData resize to correctly clear the pixel array and cleaning up color selection/preview in the Paint Tool.
1 parent 754151d commit 0bda0c8

File tree

14 files changed

+82
-70
lines changed

14 files changed

+82
-70
lines changed

Disks/PixelVisionOS/System/Libs/pixel-vision-ui-utils-v2.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ end
2121

2222
string.starts = function(String, Start)
2323

24+
if(String == nil or Start == nil) then
25+
return false
26+
end
27+
2428
return string.sub(String, 1, string.len(Start)) == Start
2529
end
2630

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function PaintTool:CreateCanvasPanel()
177177
self.mergerTmpLayer = false
178178

179179
self:InvalidateCanvas()
180-
180+
self:InvalidateBackground()
181181
self:InvalidateUndo()
182182

183183
end
@@ -611,7 +611,7 @@ function PaintTool:DrawCanvasPanel()
611611
if(self.mergerTmpLayer == true and self.mouseState == "released") then
612612

613613
-- -- TODO we can optimize this by passing in a rect for the area to merge
614-
srcCanvas:MergeCanvas(self.tmpLayerCanvas, 0, true)
614+
srcCanvas:MergeCanvas(self.tmpLayerCanvas, 0, 0)
615615

616616
self.mergerTmpLayer = false
617617

@@ -622,12 +622,12 @@ function PaintTool:DrawCanvasPanel()
622622

623623
-- Only draw the flag layer when we need to
624624
if(self.pickerMode == FlagMode) then
625-
self.flagLayerCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, self.colorOffset, self.maskId, self.scaledViewport)
625+
self.flagLayerCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, self.colorOffset, 0, self.scaledViewport)
626626
end
627627

628628
-- Only draw the temp layer when we need to
629629
if(self.drawTmpLayer == true) then
630-
self.tmpLayerCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, self.colorOffset, self.maskId, self.scaledViewport)
630+
self.tmpLayerCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, self.colorOffset, 0, self.scaledViewport)
631631
end
632632

633633
-- if(self.showGrid == true) then
@@ -1300,25 +1300,37 @@ function PaintTool:InvalidateBackground()
13001300

13011301
-- print("Sprite", dump(Sprite(MetaSprite("emptymaskcolor").Sprites[1].Id)))
13021302

1303-
self.maskId = -1
1303+
self.lastMaskId = self.maskId > -1 and self.maskId or 0
13041304

1305-
-- Mode 2 uses the mask and transparent background
1306-
elseif(self.backgroundMode == 2) then
1305+
self.maskId = -1
13071306

1308-
self.maskId = 0 -- TODO this needs to be provided by the selected mask color
1307+
pixelVisionOS:EnableMenuItemByName("BG Color", false)
1308+
pixelVisionOS:EnableMenuItemByName("Mask Color", false)
13091309

1310-
self.backgroundLayerCanvas:DrawRectangle(0, 0, self.backgroundLayerCanvas.Width, self.backgroundLayerCanvas.Height, true)
1311-
1312-
-- Mode 3 shows the background color
1310+
13131311
else
13141312

1315-
self.maskId = 0 -- TODO this needs to be provided by the selected mask color
1313+
self.maskId = self.lastMaskId ~= nil and self.lastMaskId or self.maskId -- TODO this needs to be provided by the selected mask color
1314+
1315+
self.lastMaskId = nil
1316+
1317+
-- Mode 2 uses the mask and transparent background
1318+
if(self.backgroundMode == 2) then
1319+
1320+
self.backgroundLayerCanvas:DrawRectangle(0, 0, self.backgroundLayerCanvas.Width, self.backgroundLayerCanvas.Height, true)
1321+
1322+
-- Mode 3 shows the background color
1323+
else
1324+
1325+
self.backgroundLayerCanvas:fill(self.backgroundColorId + self.colorOffset)
13161326

1317-
self.backgroundLayerCanvas:fill(self.backgroundColorId + self.colorOffset)
1327+
end
1328+
1329+
pixelVisionOS:EnableMenuItemByName("BG Color", true)
1330+
pixelVisionOS:EnableMenuItemByName("Mask Color", true)
13181331

13191332
end
13201333

1321-
13221334
self:InvalidateCanvas()
13231335

13241336
end

Disks/PixelVisionOS/System/Tools/PaintTool/Src/color-editor-modal.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ function EditColorModal:Open( ... )
308308

309309
self.colorCache = colorCache
310310

311-
self.canvas:Clear(0, 4, 1, self.canvas.width, 8)
311+
self.canvas:Fill(6, 0, 4, 1, self.canvas.width)
312312

313313
local tmpX = (self.canvas.width - (#self.title * 4)) * .5
314314

Disks/PixelVisionOS/System/Tools/PaintTool/Src/drop-down-menu.lua

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function PaintTool:CreateDropDownMenu()
3939
{name = "Edit Color", action = function() self:OnEditColor() end, enabled = false, key = Keys.E, toolTip = "Learn about PV8."},
4040
{name = "Fill Color", action = function() self:SetFillColor() end, enabled = false, toolTip = "Learn about PV8."},
4141
{name = "BG Color", action = function() self:OnSetBackgroundColor() end, enabled = false, toolTip = "Learn about PV8."},
42-
{name = "Mask Color", action = function() self:OnEditColor(self.maskColor) end, enabled = false, toolTip = "Learn about PV8."},
42+
{name = "Mask Color", action = function() self:SetMaskColor() end, enabled = false, toolTip = "Learn about PV8."},
4343

4444
{divider = true},
4545
{name = "Canvas Size", action = function() self:OnResize() end, enabled = true, key = Keys.I, toolTip = "Learn about PV8."},
@@ -887,10 +887,24 @@ end
887887

888888
function PaintTool:OnSetBackgroundColor()
889889

890+
-- TODO need to make sure the mask or the bg is not the same
891+
890892
self.backgroundColorId = self.currentState.selectedId
891893

892894
self:InvalidateBackground()
893895
-- print(self.currentState.selectedId)
894896
-- TODO need to wire this up
895897
-- print("Change BG color")
898+
899+
end
900+
901+
function PaintTool:SetMaskColor()
902+
903+
-- TODO need to make sure the mask or the bg is not the same
904+
905+
906+
self.maskId = self.currentState.selectedId
907+
908+
self:InvalidateCanvas()
909+
896910
end

Disks/PixelVisionOS/System/Tools/PaintTool/Src/index-colors.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ function PaintTool:IndexColors()
77
return
88
end
99

10-
-- print("IndexColors pass")
11-
1210
-- Configure the canvas
1311
self:ClearCurrentCanvas()
1412

@@ -21,24 +19,28 @@ function PaintTool:IndexColors()
2119
-- Create a table to store the unique colors
2220
local uniqueColors = {}
2321

22+
local uniqueIds = {}
23+
2424
-- Loop through all of the colors and pull out the unique colors
2525
for i = self.colorOffset, 255 do
2626

2727
-- Get the color stating at the colorOffset
2828
local tmpColor = Color(i)
2929

3030
-- Make sure it is not transparent
31-
if(tmpColor ~= MaskColor()) then
31+
if(table.indexOf(uniqueColors, tmpColor) == -1) then
32+
33+
table.insert(uniqueColors, tmpColor)
3234

3335
-- Add to the unique color table
34-
table.insert(uniqueColors, i)
36+
table.insert(uniqueIds, i)
3537

3638
end
3739

3840
end
3941

4042
-- Set the total number of colors in the picker
41-
self.currentState.pickerTotal = #uniqueColors
43+
self.currentState.pickerTotal = #uniqueIds
4244

4345
-- Loop through each of the colors
4446
for i = 1, self.currentState.pickerTotal do
@@ -48,7 +50,7 @@ function PaintTool:IndexColors()
4850

4951
-- Loop through all of the pixels in the tmpPattern to change the value to match the color
5052
for j = 1, 64 do
51-
tmpPattern[j] = uniqueColors[i]
53+
tmpPattern[j] = uniqueIds[i]
5254
end
5355

5456
-- Set the pattern on the canvas
@@ -59,8 +61,8 @@ function PaintTool:IndexColors()
5961

6062
end
6163

62-
-- TODO hardcoded for testing - should be read from game file?
63-
self.backgroundColorId = 0
64+
self.maskId = 0
65+
self.backgroundColorId = 1
6466

6567
-- Change the picker label
6668
self:SetPickerLabel("Colors")

Disks/PixelVisionOS/System/Tools/PaintTool/Src/picker-panel.lua

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ function PaintTool:CreatePickerPanel()
1818

1919
self.pickerGridPos = NewPoint()
2020

21-
22-
-- self.currentState = {
23-
-- selectedPos = {id = -1},
24-
-- selectedId = -1,
25-
-- overPos = {id = -1},
26-
-- }
2721

2822
-- Used to sample pixel data from the active canvas
2923
self.pickerSampleRect = NewRect( 0, 0, 8, 8 )
@@ -64,25 +58,11 @@ function PaintTool:CreatePickerPanel()
6458
end
6559

6660

67-
-- self.emptyPatternPixelData = Sprite(MetaSprite( "emptycolor" ).Sprites[1].Id)
68-
69-
-- for i = 1, #self.emptyPatternPixelData, 1 do
70-
-- self.emptyPatternPixelData[i] = -8
71-
-- end
72-
73-
-- self.colorPixelData = {}
74-
75-
-- for i = 1, 8*8 do
76-
-- table.insert(self.colorPixelData, -1)
77-
-- end
78-
7961
self.colorCanvas = NewCanvas(128, 128) -- TODO need to account for missing colors and color offset
80-
81-
8262

8363

8464
self.pickerOverCanvas = NewCanvas( 12, 12 )
85-
-- self.pickerOverCanvas:Clear()
65+
self.pickerOverCanvas:Clear()
8666
self.pickerOverCanvas:SetStroke(0, 1);
8767
self.pickerOverCanvas:DrawRectangle( 0, 0, self.pickerOverCanvas.width, self.pickerOverCanvas.height)
8868
self.pickerOverCanvas:DrawRectangle( 2, 2, self.pickerOverCanvas.width - 4, self.pickerOverCanvas.height -4)
@@ -283,33 +263,29 @@ function PaintTool:DrawPickerPanel()
283263
self.pickerSampleRect.X = self.currentState.overPos.X
284264
self.pickerSampleRect.Y = self.currentState.overPos.pos.Y * 8
285265

286-
-- self.currentCanvas:DrawPixels(self.currentState.overPos.X + self.pickerPanelRect.X , self.currentState.overPos.Y+ self.pickerPanelRect.Y, DrawMode.SpriteAbove, 1, 0, self.maskColor, 0, self.pickerSampleRect)
266+
self.currentCanvas:DrawPixels(self.currentState.overPos.X + self.pickerPanelRect.X, self.currentState.overPos.Y + self.pickerPanelRect.Y, DrawMode.SpriteAbove, 1, 0, -1, self.pickerSampleRect)
287267

288-
self.pickerOverCanvas:DrawPixels( self.currentState.overPos.X + self.pickerPanelRect.X - 2 , self.currentState.overPos.Y+ self.pickerPanelRect.Y - 2, DrawMode.SpriteAbove )
268+
self.pickerOverCanvas:DrawPixels( self.currentState.overPos.X + self.pickerPanelRect.X - 2 , self.currentState.overPos.Y + self.pickerPanelRect.Y - 2, DrawMode.SpriteAbove )
289269

290270
if(self.pickerMessage ~= nil) then
291271

292272
pixelVisionOS:DisplayMessage(self.pickerMessage)
293273

294274
end
275+
295276
end
296277

297278
if(self.pickerMode == ColorMode) then
298279

299-
-- TODO Calculate icon position
300-
-- DrawMetaSprite( "iconmask", self.pickerPanelRect.X, self.pickerPanelRect.Y, false, false, DrawMode.SpriteAbove )
301-
302-
-- TODO need to make sure we only show the color when we are on the right page
303-
304-
305-
-- TODO need to loop through all the icons
306-
280+
-- local page = pos/16
281+
local pos = CalculatePosition( self.maskId, 16 )
307282

308-
local pos = CalculatePosition( self.backgroundColorId, 16 )
283+
if(self.maskId > -1) then
284+
DrawMetaSprite( "iconoutline", (pos.X * 8) + self.pickerPanelRect.X, (pos.Y * 8) + self.pickerPanelRect.Y, false, false, DrawMode.SpriteAbove )
285+
end
309286

310-
-- local page = pos/16
287+
pos = CalculatePosition( self.backgroundColorId, 16 )
311288

312-
-- TODO make sure we are on the right page first and use the page for the Y position
313289
DrawMetaSprite( "iconbgcolor", (pos.X * 8) + self.pickerPanelRect.X, (pos.Y * 8) + self.pickerPanelRect.Y, false, false, DrawMode.SpriteAbove )
314290

315291
if(self.fillColor > -1 and self.fill == true) then
@@ -451,16 +427,19 @@ function PaintTool:RebuildBrushPreview()
451427
-- Check to see which picker mode we are in
452428
if(self.pickerMode == ColorMode) then
453429

430+
-- We play with the color offset here to account for transparency in the brush. So the offset is -1
431+
454432
-- Set the brush color based on the current color selection or to the mask color if we are using the eraser
455-
self.brushColor = self.tool == "eraser" and self.maskColor or self.currentState.selectedId
433+
self.brushColor = self.tool == "eraser" and self.maskColor or self.currentState.selectedId - 1
456434

457435
-- Clear the entire brush
458436
self.brushCanvas:Clear()
459437

460438
-- Fill the brush with the brush color based on the stroke size
461-
self.brushCanvas:Fill(self.brushColor + 1, 0, 0, self.defaultStrokeWidth, self.defaultStrokeWidth)
439+
self.brushCanvas:Fill(self.brushColor + 2, 0, 0, self.defaultStrokeWidth, self.defaultStrokeWidth)
462440

463-
self.brushColorOffset = self.colorOffset
441+
442+
self.brushColorOffset = self.colorOffset - 1
464443

465444
editorUI:Enable(self.toolButtons[self.shapeTools], true)
466445
editorUI:Enable(self.toolButtons[self.fillTools], true)

SDK/Player/Data/Canvas/Canvas.Layers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private void ChangeTargetCanvasAction(CanvasDrawRequest drawRequest)
5050
currentTexture = tmpLayer;
5151

5252
if (drawRequest.Bounds.Width != currentTexture.Width || drawRequest.Bounds.Height != currentTexture.Height)
53-
Utilities.Resize(currentTexture, drawRequest.Bounds.Width, drawRequest.Bounds.Height, Constants.EmptyPixel);
53+
Utilities.Resize(currentTexture, drawRequest.Bounds.Width, drawRequest.Bounds.Height);
5454
else
5555
Utilities.Fill(currentTexture, Constants.EmptyPixel);
5656
}

SDK/Player/Data/Canvas/Canvas.Pattern.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void SetPattern(int[] newPixels, int newWidth, int newHeight)
4242

4343
if (newRequest.PixelData.Width != newWidth || newRequest.PixelData.Height != newHeight)
4444
{
45-
Utilities.Resize(newRequest.PixelData, newWidth, newHeight, Constants.EmptyPixel);
45+
Utilities.Resize(newRequest.PixelData, newWidth, newHeight);
4646
}
4747

4848
newRequest.PixelData.SetPixels(newPixels, newWidth, newHeight);

SDK/Player/Data/Canvas/Canvas.Stroke.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void SetStroke(int color, int size = 1)
3737

3838
if (newRequest.PixelData.Width != size || newRequest.PixelData.Height != size)
3939
{
40-
Utilities.Resize(newRequest.PixelData, size, size, Constants.EmptyPixel);
40+
Utilities.Resize(newRequest.PixelData, size, size);
4141
}
4242

4343
var newPixels = new int[size * size];

SDK/Player/Data/Canvas/Canvas.Text.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void DrawText(string text, int x, int y, string font = "default", int col
2525
// We need at least 1 pixel to save the sprite ID
2626
if (newRequest.PixelData.Width != spriteSize.X || newRequest.PixelData.Height != spriteSize.Y)
2727
{
28-
Utilities.Resize(newRequest.PixelData, spriteSize.X, spriteSize.X, Constants.EmptyPixel);
28+
Utilities.Resize(newRequest.PixelData, spriteSize.X, spriteSize.X);
2929
}
3030

3131
// Copy over the pixel

0 commit comments

Comments
 (0)
X Tutup