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

Commit 244c271

Browse files
committed
Modificatoins to Paint tool to better support new transparency value.
1 parent d0a911b commit 244c271

File tree

11 files changed

+113
-96
lines changed

11 files changed

+113
-96
lines changed

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

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ function PaintTool:CreateCanvasPanel()
8080
-- Create new background canvas
8181
self.backgroundLayerCanvas = NewCanvas( self.imageLayerCanvas.Width, self.imageLayerCanvas.Height )
8282

83+
local pixels = Sprite(MetaSprite("emptymaskcolor").Sprites[1].Id)
84+
85+
-- TODO this is a hack, shouldn't have to subtract 1 from the pixel data
86+
for i = 1, #pixels do
87+
pixels[i] = pixels[i] - 1
88+
end
89+
90+
self.backgroundLayerCanvas:SetStroke(0, 0)
91+
self.backgroundLayerCanvas:SetPattern(pixels, 8, 8)
92+
8393
self.gridCanvas = NewCanvas(self.viewportRect.Width, self.viewportRect.Height)
8494

8595
-- Invalidate the background so it renders
@@ -174,23 +184,6 @@ function PaintTool:CreateCanvasPanel()
174184

175185
end
176186

177-
function PaintTool:ChangeCanvasLayer(name)
178-
179-
-- name = name or "image"
180-
181-
-- print("Change layer", name)
182-
183-
-- if(name == "image") then
184-
185-
-- self.
186-
187-
-- elseif(name == "tmp") then
188-
189-
-- end
190-
191-
192-
end
193-
194187
function PaintTool:InvalidateCanvas(mergeTmpLayer)
195188
self.displayInvalid = true
196189
self:InvalidateGrid()
@@ -530,7 +523,6 @@ function PaintTool:DrawCanvasPanel()
530523
self.selectedPixelData = nil
531524

532525
end
533-
534526

535527
-- Clear the selection state
536528
self.selectionState = "none"
@@ -610,38 +602,32 @@ function PaintTool:DrawCanvasPanel()
610602
end
611603

612604
-- Redraw the background of the canvas
613-
-- self.backgroundLayerCanvas:Fill(15)
614-
self.backgroundLayerCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, 0, self.maskColor, 0, self.scaledViewport)
615-
605+
if(self.backgroundMode ~= 1) then
606+
self.backgroundLayerCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, 0, 0, 0, self.scaledViewport, false)
607+
end
616608

617-
-- print("self.mouseState", self.mouseState)
618-
619609
if(self.mergerTmpLayer == true and self.mouseState == "released") then
620610

621-
-- print("Start Undo")
622611
-- -- TODO we can optimize this by passing in a rect for the area to merge
623612
srcCanvas:MergeCanvas(self.tmpLayerCanvas, 0, true)
624613

625-
-- print("End undo")
626-
-- self.tmpLayerCanvas:Clear()
627614
self.mergerTmpLayer = false
628615

629-
-- -- Invalidate the canvas and the selection
630-
-- self:InvalidateUndo()
631-
632616
end
633617

618+
local tmpMaskId = 1
619+
634620
-- Draw the pixel data in the upper left hand corner of the tool's window
635-
self.imageLayerCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, self.maskColor, -1, self.colorOffset, self.scaledViewport)
621+
self.imageLayerCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, tmpMaskId, 0, self.colorOffset, self.scaledViewport, self.backgroundMode ~= 1)
636622

637623
-- Only draw the flag layer when we need to
638624
if(self.pickerMode == FlagMode) then
639-
self.flagLayerCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, -1, self.emptyColorID, 0, self.scaledViewport)
625+
self.flagLayerCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, 0, self.emptyColorID, 0, self.scaledViewport)
640626
end
641627

642628
-- Only draw the temp layer when we need to
643629
if(self.drawTmpLayer == true) then
644-
self.tmpLayerCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, -1, self.emptyColorID, self.colorOffset, self.scaledViewport)
630+
self.tmpLayerCanvas:DrawPixels(self.viewportRect.X, self.viewportRect.Y, DrawMode.TilemapCache, self.scale, 0, self.emptyColorID, self.colorOffset, self.scaledViewport)
645631
end
646632

647633
-- if(self.showGrid == true) then
@@ -1310,21 +1296,25 @@ end
13101296
function PaintTool:InvalidateBackground()
13111297

13121298
-- Mode 1 will render the transparent canvas texture for the background
1313-
if(self.backgroundMode == 1) then
1299+
-- if(self.backgroundMode == 1) then
1300+
1301+
-- print("Sprite", dump(Sprite(MetaSprite("emptymaskcolor").Sprites[1].Id)))
13141302

1315-
-- self.backgroundLayerCanvas:SetStroke(0, 0)
1316-
-- self.backgroundLayerCanvas:SetPattern(Sprite(MetaSprite("emptymaskcolor").Sprites[1].Id), 8, 8)
1317-
-- self.backgroundLayerCanvas:DrawRectangle(0, 0, self.backgroundLayerCanvas.Width, self.backgroundLayerCanvas.Height, true)
1303+
1304+
13181305

13191306
-- Mode 2 will render the background color
1320-
elseif(self.backgroundMode == 2) then
1307+
-- else
1308+
if(self.backgroundMode == 2) then
13211309

1322-
self.backgroundLayerCanvas:Clear(self.backgroundColorId + self.colorOffset)
1310+
self.backgroundLayerCanvas:fill(self.backgroundColorId + self.colorOffset)
13231311

13241312
else
13251313

13261314
-- Use the mask color as the default background
1327-
self.backgroundLayerCanvas:Clear(self.maskColor + self.colorOffset)
1315+
-- self.backgroundLayerCanvas:fill(self.colorOffset)
1316+
1317+
self.backgroundLayerCanvas:DrawRectangle(0, 0, self.backgroundLayerCanvas.Width, self.backgroundLayerCanvas.Height, true)
13281318

13291319
end
13301320

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,36 @@ function PaintTool:OnImportColors(path)
129129

130130
if(colors == nil) then
131131
colors = self.defaultColors
132+
else
133+
-- table.insert(colors, 1, "#FF00FF")
132134
end
133135

134-
self.image.RemapColors(colors)
136+
self:ImportColors(colors)
135137

136-
self.imageLayerCanvas.SetPixels(0, 0, self.image.Width, self.image.Height, self.image.GetPixels())
138+
-- Add the mask
139+
140+
-- print("Color", dump(colors))
141+
142+
self.image:RemapColors(colors)
143+
144+
self.imageLayerCanvas:SetPixels(0, 0, self.image.Width, self.image.Height, self.image.GetPixels())
137145

138-
self:ImportColors(colors)
146+
139147

140148
end
141149

142150
function PaintTool:ImportColors(colors)
143151

152+
-- Add the mask
153+
154+
-- table.insert(colors, 1, "#FF00FF")
155+
144156
if(colors == nil) then
145157
colors = self.defaultColors
146158
end
147159

148160
local tmpColorIndex, tmpRefIndex = 0, 0
149161

150-
print("colors", dump(colors))
151-
152162
-- Loop through all of the system colors
153163
for i = 1, self.totalColors do
154164

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function PaintTool:Init()
6363
},
6464
colorsPath = "",
6565
-- Set the color offset after the tool's default colors + 1 for the mask
66-
colorOffset = 17,
66+
colorOffset = 16,
6767
-- Save the default bg color so we don't have to keep calling it the loop below
6868
defaultMaskColor = MaskColor(),
6969
-- Save the total colors so we don't have to keep recalculating this later on
@@ -78,7 +78,7 @@ function PaintTool:Init()
7878
end
7979

8080
-- Set the mask color to the color offset - 1
81-
_paintTool.maskColor = _paintTool.colorOffset - 1
81+
_paintTool.maskColor = _paintTool.colorOffset -- TODO modified this to be the same as the color offset
8282

8383
-- Set the mask to the tool's default mask color
8484
Color(_paintTool.maskColor, _paintTool.defaultMaskColor)
@@ -128,7 +128,7 @@ function PaintTool:LoadImage(defaultColors)
128128
-- Get a reference to the image's colors
129129
local imageColors = self.image.colors
130130

131-
if(#imageColors <= 1 and imageColors[1] == "#FF00FF") then
131+
if(#imageColors <= 1) then
132132

133133
imageColors = self.defaultColors
134134

@@ -140,6 +140,7 @@ function PaintTool:LoadImage(defaultColors)
140140

141141
end
142142

143+
print("Default colors", dump(imageColors))
143144
self:ImportColors(imageColors)
144145

145146
local flagPath = targetPath.ChangeExtension(".flags.png")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ function PaintTool:RebuildBrushPreview()
458458
self.brushCanvas:Clear()
459459

460460
-- Fill the brush with the brush color based on the stroke size
461-
self.brushCanvas:Clear(self.brushColor, 0, 0, self.defaultStrokeWidth, self.defaultStrokeWidth)
461+
self.brushCanvas:Fill(self.brushColor + 1, 0, 0, self.defaultStrokeWidth, self.defaultStrokeWidth)
462462

463463
self.brushColorOffset = self.colorOffset
464464

SDK/Editor/Exporters/SpriteExporter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ public virtual void ConfigurePixelData()
101101

102102
// spriteChip.texture.CopyPixels(ref pixelData, 0, 0, width, height);
103103

104-
exporter = new PixelDataExporter(fullFileName, pixelData, width, height, colors, imageExporter,
105-
engine.GameChip.MaskColor());
104+
exporter = new PixelDataExporter(fullFileName, pixelData, width, height, colors, imageExporter);
106105
}
107106
}
108107
}

SDK/Editor/Services/GameDataExportService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ public void ExportImage(string path, int[] pixelData, PixelVision engine, int wi
140140
var colors = ColorUtils.ConvertColors(engine.ColorChip.HexColors)/*, engine.ColorChip.MaskColor, true)*/.Select(c=> new ColorData(c.R, c.G, c.B)).ToArray();
141141

142142

143-
AddExporter(new PixelDataExporter(path, pixelData, width, height, colors, imageExporter,
144-
engine.GameChip.MaskColor()));
143+
AddExporter(new PixelDataExporter(path, pixelData, width, height, colors, imageExporter));
145144
}
146145
}
147146
}

SDK/Lua/Services/LuaService.SystemIO.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void RegisterSystemIO(Script luaScript)
4141

4242
// Save file helpers
4343
luaScript.Globals["SaveText"] = new Action<WorkspacePath, string>(SaveText);
44-
luaScript.Globals["SaveImage"] = new Action<WorkspacePath, ImageData, string>(SaveImage);
44+
luaScript.Globals["SaveImage"] = new Action<WorkspacePath, ImageData>(SaveImage);
4545

4646
// Read file helpers
4747
luaScript.Globals["ReadJson"] = new Func<WorkspacePath, Dictionary<string, object>>(ReadJson);
@@ -241,19 +241,30 @@ public ImageData ReadImage(WorkspacePath src, string maskHex = Constants.MaskCol
241241
// Manually call each step
242242
imageParser.ParseImageData();
243243

244+
List<string> finalColors;
245+
244246
// If no colors are passed in, used the image's palette
245247
if (colorRefs == null)
246248
{
247-
colorRefs = reader.ColorPalette.Select(c => c.ToString()).ToArray();
249+
finalColors = reader.ColorPalette.Select(c => c.ToString()).ToList();
250+
}
251+
else
252+
{
253+
finalColors = colorRefs.ToList();
248254
}
249255

256+
if(finalColors.IndexOf(maskHex) > -1)
257+
finalColors.RemoveAt(finalColors.IndexOf(maskHex));
258+
259+
finalColors.Insert(0, maskHex);
260+
250261
// Resize the color chip
251-
tmpColorChip.Total = colorRefs.Length;
262+
tmpColorChip.Total = finalColors.Count;
252263

253264
// Add the colors
254-
for (int i = 0; i < colorRefs.Length; i++)
265+
for (int i = 0; i < finalColors.Count; i++)
255266
{
256-
tmpColorChip.UpdateColorAt(i, colorRefs[i]);
267+
tmpColorChip.UpdateColorAt(i, finalColors[i]);
257268
}
258269

259270
// Parse the image with the new colors

SDK/Player/Utils/Utilities.PixelData.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ public static void SetPixels<T>(T[] srcPixels, int x, int y, int blockWidth, int
9393

9494
T tmpPixel;
9595

96+
// TODO shouldn't this use copy instead of looping thorugh all the pixels?
97+
9698
for (i = 0; i < total; i++)
9799
{
98100

@@ -126,6 +128,7 @@ public static void Fill<T>(PixelData<T> pixelData, T colorRef) where T : ICompar
126128
for (int i = 0; i < pixelData.Total; i++) pixelData[i] = colorRef;
127129
}
128130

131+
// TODO need to optimize this if we are not flipping the pixel data and ignoreTransparent is false
129132
public static void MergePixels<T>(
130133
// The source pixel data
131134
PixelData<T> src,

SDK/Runner/Data/DisplayTarget.MonoVision.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ public Stream ShaderPath
122122

123123
public void RebuildColorPalette(string[] hexColors, int bgColorId = 1, bool debugMode = false)
124124
{
125+
125126
_cachedColors = ColorUtils.ConvertColors(
126127
hexColors,
127128
/*maskColor,
128129
debugMode,*/
129-
bgColorId
130+
bgColorId,
131+
debugMode
130132
);
131133

132134
// _cachedColors[0] = _cachedColors[bgColorId];

SDK/Runner/Parsers/SpriteImageParser.cs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -146,37 +146,37 @@ public virtual void CreateImage()
146146
}
147147

148148
// Sort colors
149-
// uniqueColorIDs.Sort();
150-
151-
// var indexes = new List<int>();
152-
153-
// // find open slots
154-
// for (int i = 0; i < colorMap.Length; i++)
155-
// {
156-
// if (colorMap[i] == null)
157-
// {
158-
// indexes.Add(i);
159-
// }
160-
// }
161-
162-
// var totalOrphanColors = orphanColors.Count;
163-
164-
// for (int i = 0; i < indexes.Count; i++)
165-
// {
166-
// if (i < totalOrphanColors)
167-
// {
168-
// colorMap[indexes[i]] = orphanColors[i];
169-
// }
170-
// }
171-
172-
// // clean up the color map
173-
// for (int i = 0; i < colorMap.Length; i++)
174-
// {
175-
// if (colorMap[i] == null)
176-
// {
177-
// colorMap[i] = colorRefs[i];
178-
// }
179-
// }
149+
uniqueColorIDs.Sort();
150+
151+
var indexes = new List<int>();
152+
153+
// find open slots
154+
for (int i = 0; i < colorMap.Length; i++)
155+
{
156+
if (colorMap[i] == null)
157+
{
158+
indexes.Add(i);
159+
}
160+
}
161+
162+
var totalOrphanColors = orphanColors.Count;
163+
164+
for (int i = 0; i < indexes.Count; i++)
165+
{
166+
if (i < totalOrphanColors)
167+
{
168+
colorMap[indexes[i]] = orphanColors[i];
169+
}
170+
}
171+
172+
// clean up the color map
173+
for (int i = 0; i < colorMap.Length; i++)
174+
{
175+
if (colorMap[i] == null)
176+
{
177+
colorMap[i] = colorRefs[i];
178+
}
179+
}
180180

181181
// Convert all of the pixels into color ids
182182
var pixelIDs = Parser.ColorPixels.Select(c => Array.IndexOf(colorMap, ColorUtils.RgbToHex(c)))

0 commit comments

Comments
 (0)
X Tutup