Skip to content

Commit 68e661c

Browse files
committed
add non-contiguous fill (with left ctrl down while fill), add PixelColor compare operators
1 parent 3325d8e commit 68e661c

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

PixelArtTool/EnumsAndStructs.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,18 @@ public PixelColor Inverted(byte alphaOverride=255)
6161
pc.Alpha = alphaOverride;
6262
return pc;
6363
}
64-
}
64+
65+
public static bool operator ==(PixelColor c1, PixelColor c2)
66+
{
67+
return c1.ColorBGRA == c2.ColorBGRA;
68+
}
69+
70+
public static bool operator !=(PixelColor c1, PixelColor c2)
71+
{
72+
return c1.ColorBGRA != c2.ColorBGRA;
73+
}
74+
75+
} // PixelColor
6576

6677
// helper for converting bool<>enum for xaml linked values
6778
// https://stackoverflow.com/a/2908885/5452781

PixelArtTool/MainWindow.xaml.cs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ public partial class MainWindow : Window, INotifyPropertyChanged
5555
int prevX;
5656
int prevY;
5757

58-
// drawing lines
5958
bool leftShiftDown = false;
59+
bool leftCtrlDown = false;
60+
61+
62+
// drawing lines
6063
private readonly int ddaMODIFIER_X = 0x7fff;
6164
private readonly int ddaMODIFIER_Y = 0x7fff;
6265

@@ -453,7 +456,7 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
453456
}
454457
break;
455458
case ToolMode.Fill:
456-
// NOTE: doesnt work with single pixel area.. because nothing to fill
459+
// NOTE: double click doesnt work with single pixel area.. because nothing to fill
457460
if (wasDoubleClick == true)
458461
{
459462
// remove previous pixel by using old color (could take from undo also..)
@@ -466,10 +469,18 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
466469
currentColor = previousColor;
467470
}
468471

469-
FloodFill(x, y, (int)currentColor.ColorBGRA);
470-
if (chkMirrorX.IsChecked == true)
472+
// non-contiguous fill, fills all pixels that match target pixel color
473+
if (leftCtrlDown == true)
471474
{
472-
FloodFill(canvasResolutionX - x, y, (int)currentColor.ColorBGRA);
475+
ReplacePixels(previousPixelColor, currentColor);
476+
}
477+
else
478+
{
479+
FloodFill(x, y, (int)currentColor.ColorBGRA);
480+
if (chkMirrorX.IsChecked == true)
481+
{
482+
FloodFill(canvasResolutionX - x, y, (int)currentColor.ColorBGRA);
483+
}
473484
}
474485
break;
475486
default:
@@ -718,6 +729,9 @@ void OnKeyDown(object sender, KeyEventArgs e)
718729
lblToolInfo.Content = "Straight Lines";
719730
leftShiftDown = true;
720731
break;
732+
case Key.LeftCtrl: // left control
733+
leftCtrlDown = true;
734+
break;
721735
default:
722736
break;
723737
}
@@ -731,6 +745,9 @@ private void OnKeyUp(object sender, KeyEventArgs e)
731745
lblToolInfo.Content = "";
732746
leftShiftDown = false;
733747
break;
748+
case Key.LeftCtrl:
749+
leftCtrlDown = false;
750+
break;
734751
default:
735752
break;
736753
}
@@ -910,6 +927,8 @@ void OnCopyImageToClipboard()
910927
*/
911928
}
912929

930+
931+
913932
void FloodFill(int x, int y, int fillColor)
914933
{
915934
// get hit color pixel
@@ -1167,7 +1186,7 @@ private void rectHueBar_MouseDown(object sender, MouseButtonEventArgs e)
11671186

11681187
private void OnGetTransparentColorButton(object sender, MouseButtonEventArgs e)
11691188
{
1170-
var c = new PixelColor(255,255,255,0);
1189+
var c = new PixelColor(255, 255, 255, 0);
11711190
currentColor = c;
11721191
rectCurrentColor.Fill = c.AsSolidColorBrush();
11731192
ResetCurrentBrightnessPreview(currentColor);
@@ -1410,5 +1429,25 @@ private void OnClearButton(object sender, MouseButtonEventArgs e)
14101429
}
14111430

14121431
}
1432+
1433+
public void ReplacePixels(PixelColor find, PixelColor replace)
1434+
{
1435+
for (int x = 0; x < canvasResolutionX; x++)
1436+
{
1437+
for (int y = 0; y < canvasResolutionY; y++)
1438+
{
1439+
var pixel = GetPixelColor(x, y, canvasBitmap);
1440+
1441+
if (pixel == find)
1442+
{
1443+
SetPixel(outlineBitmap, x, y, (int)replace.ColorBGRA);
1444+
}
1445+
1446+
}
1447+
}
1448+
1449+
}
1450+
1451+
14131452
} // class
14141453
} // namespace

0 commit comments

Comments
 (0)