Skip to content

Commit 403fdeb

Browse files
committed
load settings at start, allow drawing lines with symmetry mode, add ConvertSystemDrawingColorToPixelColor, show/hide pixel rectangle on mouse enter/exit, quick clear image with shift down or right click clear
1 parent 86a33c0 commit 403fdeb

File tree

3 files changed

+100
-54
lines changed

3 files changed

+100
-54
lines changed

PixelArtTool/MainWindow.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@
107107

108108
<ToolBarTray Background="#FF1F1F1F" Height="32" VerticalAlignment="Top">
109109
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top" Background="#FF9C9C9C">
110-
<Button x:Name="btnNew" ToolTip="New (Ctrl+N)" Click="OnClearButton">
111-
<Image Source="/Resources/Buttons/newimage.png" Width="24" Height="24" RenderOptions.BitmapScalingMode="NearestNeighbor" />
110+
<Button x:Name="btnNew" ToolTip="New (Ctrl+N)" PreviewMouseDown="OnClearButton">
111+
<Image Source="/Resources/Buttons/newimage.png" Width="24" Height="24" RenderOptions.BitmapScalingMode="NearestNeighbor" IsHitTestVisible="False" />
112112
</Button>
113113
<Button x:Name="btnSave" ToolTip="Save (Ctrl+S)" Click="OnSaveButton">
114114
<Image Source="/Resources/Buttons/save.png" Width="24" Height="24" RenderOptions.BitmapScalingMode="NearestNeighbor" />
@@ -176,7 +176,7 @@
176176
<Image x:Name="gridImage" HorizontalAlignment="Left" Height="256" Margin="-1" VerticalAlignment="Top" Width="256" Stretch="Fill" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
177177
</Border>
178178
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
179-
<Image x:Name="drawingImage" HorizontalAlignment="Left" Height="256" Margin="-1" VerticalAlignment="Top" Width="256" Stretch="Fill" MouseMove="DrawingAreaMouseMoved" MouseRightButtonDown="DrawingRightButtonDown" MouseLeftButtonDown="DrawingLeftButtonDown" MouseDown="DrawingMiddleButtonDown" MouseUp="DrawingMouseUp" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
179+
<Image x:Name="drawingImage" HorizontalAlignment="Left" Height="256" Margin="-1" VerticalAlignment="Top" Width="256" Stretch="Fill" MouseMove="DrawingAreaMouseMoved" MouseRightButtonDown="DrawingRightButtonDown" MouseLeftButtonDown="DrawingLeftButtonDown" MouseDown="DrawingMiddleButtonDown" MouseUp="DrawingMouseUp" RenderOptions.BitmapScalingMode="NearestNeighbor" MouseLeave="drawingImage_MouseLeave" MouseEnter="drawingImage_MouseEnter"/>
180180
</Border>
181181
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
182182
<Image x:Name="outlineImage" IsHitTestVisible="False" HorizontalAlignment="Left" Height="256" Margin="-1" VerticalAlignment="Top" Width="256" Stretch="Fill" IsEnabled="False" RenderOptions.BitmapScalingMode="NearestNeighbor"/>

PixelArtTool/MainWindow.xaml.cs

Lines changed: 87 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void Start()
133133
// needed for binding
134134
DataContext = this;
135135

136-
// TODO read settings
136+
// defaults
137137
lightColor.Red = 255;
138138
lightColor.Green = 255;
139139
lightColor.Blue = 255;
@@ -144,6 +144,9 @@ void Start()
144144
darkColor.Blue = 0;
145145
darkColor.Alpha = gridAlpha;
146146

147+
// get values from settings
148+
LoadSettings();
149+
147150
// setup background grid
148151
gridBitmap = new WriteableBitmap(canvasResolutionX, canvasResolutionY, dpiX, dpiY, PixelFormats.Bgra32, null);
149152
gridImage.Source = gridBitmap;
@@ -237,8 +240,8 @@ void DrawPixel(int x, int y)
237240
// draw
238241
SetPixel(canvasBitmap, x, y, (int)draw.ColorBGRA);
239242

240-
prevX = x;
241-
prevY = y;
243+
// prevX = x;
244+
// prevY = y;
242245
} // drawpixel
243246

244247
void ErasePixel(int x, int y)
@@ -426,22 +429,27 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
426429
switch (CurrentTool)
427430
{
428431
case ToolMode.Draw:
429-
430432
// check if shift is down, then do line to previous point
431433
if (leftShiftDown == true)
432434
{
433-
//DrawLine(prevX, prevY, x, y);
434435
DrawLine(prevX, prevY, x, y);
435436
}
436-
437-
DrawPixel(x, y);
438-
prevX = x;
439-
prevY = y;
437+
else
438+
{
439+
DrawPixel(x, y);
440+
}
440441

441442
// mirror
442443
if (chkMirrorX.IsChecked == true)
443444
{
444-
DrawPixel(canvasResolutionX - x, y);
445+
if (leftShiftDown == true)
446+
{
447+
DrawLine(canvasResolutionX - prevX, prevY, canvasResolutionX - x, y);
448+
}
449+
else
450+
{
451+
DrawPixel(canvasResolutionX - x, y);
452+
}
445453
}
446454
break;
447455
case ToolMode.Fill:
@@ -466,7 +474,10 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
466474
break;
467475
default:
468476
break;
469-
}
477+
} // switch-currenttool
478+
479+
prevX = x;
480+
prevY = y;
470481

471482
if (wasDoubleClick == true)
472483
{
@@ -523,7 +534,8 @@ void DrawingAreaMouseMoved(object sender, MouseEventArgs e)
523534
default:
524535
break;
525536
}
526-
537+
prevX = x;
538+
prevY = y;
527539
}
528540
else if (e.RightButton == MouseButtonState.Pressed)
529541
{
@@ -612,38 +624,6 @@ void WindowMouseWheel(object sender, MouseWheelEventArgs e)
612624
lineCurrentHueLine.Margin = new Thickness(offset, 0, offset, 0);
613625
}
614626

615-
private void OnClearButton(object sender, RoutedEventArgs e)
616-
{
617-
// show dialog for new resolution
618-
NewImageDialog dlg = new NewImageDialog();
619-
dlg.Owner = this;
620-
dlg.sliderResolution.Value = canvasResolutionX;
621-
var result = dlg.ShowDialog();
622-
switch (result)
623-
{
624-
case true:
625-
RegisterUndo();
626-
ClearImage(canvasBitmap, emptyRect, emptyPixels, emptyStride);
627-
UpdateOutline();
628-
// reset title
629-
window.Title = windowTitle;
630-
saveFile = null;
631-
632-
canvasResolutionX = (int)dlg.sliderResolution.Value;
633-
canvasResolutionY = (int)dlg.sliderResolution.Value;
634-
635-
// re-init everything!!??
636-
Start();
637-
638-
break;
639-
case false: // cancelled
640-
break;
641-
default:
642-
Console.WriteLine("Unknown error..");
643-
break;
644-
}
645-
}
646-
647627
// if unsaved, this is same as save as.., if already saved, then overwrite current
648628
private void OnSaveButton(object sender, RoutedEventArgs e)
649629
{
@@ -1398,6 +1378,68 @@ private void btnSettings_Click(object sender, RoutedEventArgs e)
13981378
break;
13991379
}
14001380
}
1401-
} // class
14021381

1382+
void LoadSettings()
1383+
{
1384+
lightColor = ConvertSystemDrawingColorToPixelColor(Properties.Settings.Default.gridLightColor);
1385+
darkColor = ConvertSystemDrawingColorToPixelColor(Properties.Settings.Default.gridDarkColor);
1386+
gridAlpha = Properties.Settings.Default.gridAlpha;
1387+
canvasResolutionX = canvasResolutionY = Properties.Settings.Default.defaultResolution;
1388+
}
1389+
1390+
private void drawingImage_MouseLeave(object sender, MouseEventArgs e)
1391+
{
1392+
rectPixelPos.Visibility = Visibility.Hidden;
1393+
}
1394+
1395+
private void drawingImage_MouseEnter(object sender, MouseEventArgs e)
1396+
{
1397+
rectPixelPos.Visibility = Visibility.Visible;
1398+
}
1399+
1400+
private void OnClearButton(object sender, MouseButtonEventArgs e)
1401+
{
1402+
// shiftdown or right button, just clear without dialog
1403+
if (leftShiftDown == true || e.RightButton == MouseButtonState.Pressed)
1404+
{
1405+
ClearImage(canvasBitmap, emptyRect, emptyPixels, emptyStride);
1406+
UpdateOutline();
1407+
// reset title
1408+
window.Title = windowTitle;
1409+
saveFile = null;
1410+
1411+
return;
1412+
}
1413+
1414+
// show dialog for new resolution
1415+
NewImageDialog dlg = new NewImageDialog();
1416+
dlg.Owner = this;
1417+
dlg.sliderResolution.Value = canvasResolutionX;
1418+
var result = dlg.ShowDialog();
1419+
switch (result)
1420+
{
1421+
case true:
1422+
RegisterUndo();
1423+
ClearImage(canvasBitmap, emptyRect, emptyPixels, emptyStride);
1424+
UpdateOutline();
1425+
// reset title
1426+
window.Title = windowTitle;
1427+
saveFile = null;
1428+
1429+
canvasResolutionX = (int)dlg.sliderResolution.Value;
1430+
canvasResolutionY = (int)dlg.sliderResolution.Value;
1431+
1432+
// TODO no need to do full start
1433+
Start();
1434+
1435+
break;
1436+
case false: // cancelled
1437+
break;
1438+
default:
1439+
Console.WriteLine("Unknown error..");
1440+
break;
1441+
}
1442+
1443+
}
1444+
} // class
14031445
} // namespace

PixelArtTool/Tools.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,12 @@ public static int Repeat(int val, int max)
193193

194194
public static void DrawBackgroundGrid(WriteableBitmap targetBitmap, int canvasResolutionX, int canvasResolutionY, PixelColor c1, PixelColor c2, byte alpha)
195195
{
196-
Console.WriteLine(123);
197196
PixelColor c = new PixelColor();
198197
for (int x = 0; x < canvasResolutionX; x++)
199198
{
200199
for (int y = 0; y < canvasResolutionY; y++)
201200
{
202-
// c.Alpha = gridAlpha;
203-
// byte v = (byte)(((x % 2) == (y % 2)) ? 255 : 0);
204201
var v = ((x % 2) == (y % 2)) ? c1 : c2;
205-
// c.Red = v;
206-
// c.Green = v;
207-
// c.Blue = v;
208202
v.Alpha = alpha;
209203
SetPixel(targetBitmap, x, y, (int)v.ColorBGRA);
210204
}
@@ -527,6 +521,16 @@ public static SolidColorBrush ConvertSystemDrawingColorToSolidColorBrush(System.
527521
return new SolidColorBrush(Color.FromArgb(c.A, c.R, c.G, c.B));
528522
}
529523

524+
public static PixelColor ConvertSystemDrawingColorToPixelColor(System.Drawing.Color c)
525+
{
526+
var pc = new PixelColor();
527+
pc.Alpha = c.A;
528+
pc.Red = c.R;
529+
pc.Green = c.G;
530+
pc.Blue = c.B;
531+
return pc;
532+
}
533+
530534
public static System.Drawing.Color ConvertBrushToSystemDrawingColor(Brush c)
531535
{
532536
var bc = ((SolidColorBrush)c);

0 commit comments

Comments
 (0)