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

Commit 59a7ba3

Browse files
committed
Cleaning up selection logic.
1 parent c6f91b2 commit 59a7ba3

File tree

10 files changed

+700
-262
lines changed

10 files changed

+700
-262
lines changed

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

Lines changed: 375 additions & 206 deletions
Large diffs are not rendered by default.

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

Lines changed: 118 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,84 @@ function PaintTool:CreateDropDownMenu()
1818
{name = "About", action = function() pixelVisionOS:ShowAboutModal(self.toolName) end, toolTip = "Learn about PV8."},
1919

2020
{divider = true},
21+
{name = "Undo", action = function() self:OnUndo() end, key = Keys.Z, toolTip = "Learn about PV8."},
22+
{name = "Redo", action = function() self:OnRedo() end, key = Keys.Y, toolTip = "Learn about PV8."},
2123

2224
{divider = true},
25+
{name = "Cut", action = function() end, key = Keys.X, toolTip = "Learn about PV8."},
26+
{name = "Copy", action = function() end, key = Keys.C, toolTip = "Learn about PV8."},
27+
{name = "Paste", action = function() end, key = Keys.V, toolTip = "Learn about PV8."},
28+
{name = "Flip H", action = function() end, key = Keys.H, toolTip = "Learn about PV8."},
29+
{name = "Flip V", action = function() end, key = Keys.J, toolTip = "Learn about PV8."},
30+
31+
{divider = true},
32+
{name = "Line Thicker", action = function() end, toolTip = "Learn about PV8."},
33+
{name = "Line Thinner", action = function() end, toolTip = "Learn about PV8."},
34+
{name = "Fill", action = function() end, toolTip = "Learn about PV8."},
35+
36+
{divider = true},
37+
{name = "Edit Color", action = function() end, toolTip = "Learn about PV8."},
38+
{name = "Outline Color", action = function() end, toolTip = "Learn about PV8."},
39+
{name = "Fill Color", action = function() self:FillCanvasSelection(self.brushColor) end, toolTip = "Learn about PV8."},
40+
{name = "BG Color", action = function() end, toolTip = "Learn about PV8."},
41+
{name = "Mask Color", action = function() end, toolTip = "Learn about PV8."},
42+
43+
{divider = true},
44+
{name = "Canvas Size", action = function() end, key = Keys.I, toolTip = "Learn about PV8."},
45+
-- {name = "Color Mode", action = function() end, toolTip = "Learn about PV8."},
46+
-- {name = "Sprite Mode", action = function() end, toolTip = "Learn about PV8."},
47+
-- {name = "Flag Mode", action = function() end, toolTip = "Learn about PV8."},
48+
{name = "Toggle BG", action = function() end, key = Keys.I, toolTip = "Learn about PV8."},
49+
-- {divider = true},
50+
51+
-- {name = "Zoom In", action = function() end, toolTip = "Learn about PV8."},
52+
-- {name = "Zoom Out", action = function() end, toolTip = "Learn about PV8."},
53+
54+
{divider = true},
55+
{name = "Export Colors", action = function() end, toolTip = "Learn about PV8."},
56+
{name = "Export Sprites", action = function() end, toolTip = "Learn about PV8."},
57+
{name = "Export Flags", action = function() end, toolTip = "Learn about PV8."},
58+
59+
{divider = true},
60+
{name = "Run Game", action = function() end, key = Keys.R, toolTip = "Learn about PV8."},
61+
{name = "Save", action = function() end, key = Keys.S, toolTip = "Learn about PV8."},
2362
{name = "Quit", key = Keys.Q, action = QuitCurrentTool, toolTip = "Quit the current game."}, -- Quit the current game
2463
}
2564

26-
local editorMapping = pixelVisionOS:FindEditors()
65+
self.RedoShortcut = 4
66+
self.UndoShortcut = 3
67+
68+
-- local editorMapping = pixelVisionOS:FindEditors()
2769

28-
local addLastDivider = false
70+
-- local addLastDivider = false
2971

30-
-- Only add these if the version of PV8 supports drawing tools
31-
if(editorMapping["colors"] ~= nil) then
72+
-- -- Only add these if the version of PV8 supports drawing tools
73+
-- if(editorMapping["colors"] ~= nil) then
3274

33-
table.insert(menuOptions, #menuOptions, {name = "Toggle Palette", enabled = true, action = function() debugMode = not debugMode end, toolTip = "Shows a preview of the color palette."})
75+
-- table.insert(menuOptions, #menuOptions, {name = "Toggle Palette", enabled = true, action = function() debugMode = not debugMode end, toolTip = "Shows a preview of the color palette."})
3476

35-
table.insert(menuOptions, #menuOptions, {divider = true})
77+
-- table.insert(menuOptions, #menuOptions, {divider = true})
3678

37-
table.insert(menuOptions, #menuOptions, {name = "Save Colors", enabled = true, action = function() OnSavePNG(true, false, false) end, toolTip = "Create a 'color-map.png' file."})
79+
-- table.insert(menuOptions, #menuOptions, {name = "Save Colors", enabled = true, action = function() OnSavePNG(true, false, false) end, toolTip = "Create a 'color-map.png' file."})
3880

39-
addLastDivider = true
40-
end
81+
-- addLastDivider = true
82+
-- end
4183

42-
if(editorMapping["sprites"] ~= nil) then
84+
-- if(editorMapping["sprites"] ~= nil) then
4385

44-
table.insert(menuOptions, #menuOptions, {name = "Save Sprites", enabled = true, action = function() OnSavePNG(false, true, false) end, toolTip = "Create a 'sprite.png' file."})
45-
addLastDivider = true
46-
end
86+
-- table.insert(menuOptions, #menuOptions, {name = "Save Sprites", enabled = true, action = function() OnSavePNG(false, true, false) end, toolTip = "Create a 'sprite.png' file."})
87+
-- addLastDivider = true
88+
-- end
4789

48-
if(editorMapping["tilemap"] ~= nil) then
90+
-- if(editorMapping["tilemap"] ~= nil) then
4991

50-
table.insert(menuOptions, #menuOptions, {name = "Save Tilemap", enabled = true, action = function() OnSavePNG(false, false, true) end, toolTip = "Create a 'tilemap.json' file."})
51-
addLastDivider = true
52-
end
92+
-- table.insert(menuOptions, #menuOptions, {name = "Save Tilemap", enabled = true, action = function() OnSavePNG(false, false, true) end, toolTip = "Create a 'tilemap.json' file."})
93+
-- addLastDivider = true
94+
-- end
5395

54-
if(addLastDivider == true) then
55-
table.insert(menuOptions, #menuOptions, {divider = true})
56-
end
96+
-- if(addLastDivider == true) then
97+
-- table.insert(menuOptions, #menuOptions, {divider = true})
98+
-- end
5799

58100
pixelVisionOS:CreateTitleBarMenu(menuOptions, "See menu options for this tool.")
59101

@@ -118,19 +160,64 @@ function PaintTool:OnQuit()
118160

119161
end
120162

163+
function PaintTool:GetState()
121164

122-
function PaintTool:ToggleLineNumbers()
165+
-- Select the correct canvas based on image or flag mode
166+
local srcCanvas = self.pickerMode == FlagMode and self.flagLayerCanvas or self.imageLayerCanvas
123167

124-
if(self.codeMode == false) then
125-
return
126-
end
127-
128-
-- TODO need to save this value to the bios
129-
130-
self.showLines = not self.showLines
131-
132-
WriteBiosData("ShowLinesInTextEditor", self.showLines == true and "True" or "False")
168+
-- Get the size of the canvas
169+
local width = srcCanvas.Width
170+
local height = srcCanvas.Height
171+
172+
-- Get the pixels from the canvas
173+
local pixels = srcCanvas:GetPixels()
174+
175+
176+
-- TODO save brush state
177+
178+
179+
-- TODO save picker state
180+
local pickerMode = self.pickerMode
181+
182+
-- Save the selection rect
183+
local selection = self.selectRect
133184

134-
self:InvalidateLineNumbers()
185+
local state = {
186+
187+
Action = function()
188+
189+
-- Restore the selection state
190+
if(selection ~= nil) then
191+
self.selection = NewRect( selection.X, selection.Y, selection.Width, selection.Height )
192+
end
193+
194+
-- Check the size of the image layer vs the last saved state
195+
if(srcCanvas.Width ~= width or srcCanvas.Height ~= height) then
196+
self:ResizeCanvas(width, height)
197+
end
198+
199+
-- Restore the pixel data
200+
srcCanvas:SetPixels(pixels)
201+
202+
-- Restore picker mode
203+
self:ChangeMode(pickerMode)
204+
205+
-- Force the canvas to redraw
206+
self:InvalidateCanvas()
207+
208+
end
209+
210+
}
211+
212+
return state
135213

136214
end
215+
216+
function PaintTool:ResizeCanvas(width, height)
217+
218+
-- TODO need to resize all of the canvases
219+
220+
self.imageLayerCanvas:Resize(width, height)
221+
self.tmpLayerCanvas:Resize(width, height)
222+
223+
end

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ function PaintTool:IndexColors()
55
return
66
end
77

8+
self.colorBrightness = {}
9+
810
-- Configure the canvas
911
self:ClearCurrentCanvas()
1012

@@ -28,6 +30,11 @@ function PaintTool:IndexColors()
2830

2931
-- Add to the unique color table
3032
table.insert(uniqueColors, i)
33+
34+
35+
local h, s, v = self:RGBToHSV(self:HexToRGB(tmpColor))
36+
-- local r,g,b = )
37+
print("Color", tmpColor, v)
3138

3239
end
3340

@@ -68,4 +75,46 @@ function PaintTool:ResetColorValidation()
6875

6976
self.colorsInvalid = false
7077

78+
end
79+
80+
-- TODO need to create a look up table for lum
81+
82+
function PaintTool:HexToRGB(hex)
83+
hex = hex:gsub("#", "")
84+
return tonumber("0x"..hex:sub(1, 2), 16), tonumber("0x"..hex:sub(3, 4), 16), tonumber("0x"..hex:sub(5, 6), 16)
85+
end
86+
87+
--[[
88+
* Converts an RGB color value to HSV. Conversion formula
89+
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
90+
* Assumes r, g, and b are contained in the set [0, 255] and
91+
* returns h, s, and v in the set [0, 1].
92+
*
93+
* @param Number r The red color value
94+
* @param Number g The green color value
95+
* @param Number b The blue color value
96+
* @return Array The HSV representation
97+
]]
98+
function PaintTool:RGBToHSV(r, g, b)
99+
r, g, b = r / 255, g / 255, b / 255
100+
local max, min = math.max(r, g, b), math.min(r, g, b)
101+
local h, s, v
102+
v = max
103+
104+
local d = max - min
105+
if max == 0 then s = 0 else s = d / max end
106+
107+
if max == min then
108+
h = 0 -- achromatic
109+
else
110+
if max == r then
111+
h = (g - b) / d
112+
if g < b then h = h + 6 end
113+
elseif max == g then h = (b - r) / d + 2
114+
elseif max == b then h = (r - g) / d + 4
115+
end
116+
h = h / 6
117+
end
118+
119+
return h, s, v
71120
end

Disks/PixelVisionOS/System/Tools/PaintTool/code-index-sprites.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ function PaintTool:IndexSprites()
1717

1818
self.spriteCounter = 0
1919

20-
self.spriteColumns = math.ceil(self.imageCanvas.Width / 8)
21-
self.spriteRows = math.ceil(self.imageCanvas.Height / 8)
20+
self.spriteColumns = math.ceil(self.imageLayerCanvas.Width / 8)
21+
self.spriteRows = math.ceil(self.imageLayerCanvas.Height / 8)
2222

2323
self.currentState.pickerTotal = self.spriteColumns * self.spriteRows
2424

@@ -36,7 +36,7 @@ function PaintTool:UpdateSpriteIndex()
3636

3737
local tilePos = CalculatePosition( self.spriteCounter, self.spriteColumns )
3838

39-
local pixelData = self.imageCanvas:GetPixels(tilePos.X * 8, tilePos.Y * 8, 8 , 8)
39+
local pixelData = self.imageLayerCanvas:GetPixels(tilePos.X * 8, tilePos.Y * 8, 8 , 8)
4040

4141
local spriteUID = table.concat(pixelData,",")
4242

Disks/PixelVisionOS/System/Tools/PaintTool/code-paint-tool.lua

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ LoadScript( "code-toolbar" )
2020
LoadScript( "code-index-sprites" )
2121
LoadScript( "code-index-colors" )
2222
LoadScript( "code-index-flags" )
23+
LoadScript( "code-selection" )
2324

2425
function CreateTool()
2526
return PaintTool:Init()
@@ -41,6 +42,8 @@ function PaintTool:Init()
4142
-- Create a global reference of the new workspace tool
4243
setmetatable(_paintTool, PaintTool)
4344

45+
pixelVisionOS:ResetUndoHistory(_paintTool)
46+
4447
-- Get the target file
4548

4649
if(_paintTool.targetFile ~= nil) then
@@ -78,14 +81,14 @@ function PaintTool:LoadSuccess()
7881
-- Get the last color index for the offset
7982

8083
-- TODO need to limit the number of colors the tool has to reduce the color offset
81-
self.colorOffset = 16
82-
self.maskColor = self.colorOffset
84+
self.colorOffset = 17
85+
self.maskColor = self.colorOffset - 1
8386

8487
Color(self.maskColor, "FF00FF")
8588

8689
-- Add the image colors to the color chip
8790
for i = 1, #imageColors do
88-
Color(self.colorOffset - 1 + i , imageColors[i])
91+
Color(self.colorOffset + i - 1 , imageColors[i])
8992
end
9093

9194
-- TODO display message if there are more colors than the tool can show
@@ -96,13 +99,13 @@ function PaintTool:LoadSuccess()
9699

97100
-- Configure the menu
98101
self:CreateDropDownMenu()
102+
103+
self:CreateToolbar()
99104

100105
self:CreateCanvasPanel()
101106

102107
self:CreatePickerPanel()
103108

104-
self:CreateToolbar()
105-
106109
-- Reset Tool Validation
107110
self:ResetDataValidation()
108111

0 commit comments

Comments
 (0)
X Tutup