Skip to content

Commit bbc14dc

Browse files
committed
add background grid to canvas
1 parent da5eb2d commit bbc14dc

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

PixelArtTool/MainWindow.xaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,14 @@
7979
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="128" Margin="10,50,0,0" VerticalAlignment="Top" Width="64">
8080
<Image x:Name="imgPalette" HorizontalAlignment="Left" Height="128" Margin="0,0,0,-30" VerticalAlignment="Top" Width="64" Stretch="Fill"/>
8181
</Border>
82-
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
83-
<Image x:Name="imgCanvas" HorizontalAlignment="Left" Height="256" Margin="-1" VerticalAlignment="Top" Width="256" Stretch="Fill"/>
84-
</Border>
82+
<Grid>
83+
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
84+
<Image x:Name="imgGrid" HorizontalAlignment="Left" Height="256" Margin="-1" VerticalAlignment="Top" Width="256" Stretch="Fill"/>
85+
</Border>
86+
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
87+
<Image x:Name="imgCanvas" HorizontalAlignment="Left" Height="256" Margin="-1" VerticalAlignment="Top" Width="256" Stretch="Fill"/>
88+
</Border>
89+
</Grid>
8590
<Rectangle x:Name="rectCurrentColor" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="24" Margin="10,181,0,0" Stroke="Black" VerticalAlignment="Top" Width="24"/>
8691
</Grid>
8792
</Window>

PixelArtTool/MainWindow.xaml.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ public enum DrawMode : byte
2323
public partial class MainWindow : Window
2424
{
2525
WriteableBitmap canvasBitmap;
26+
WriteableBitmap gridBitmap;
2627
WriteableBitmap paletteBitmap;
2728
Window w;
2829

2930
Image drawingImage;
31+
Image gridImage;
3032
Image paletteImage;
3133

3234
// bitmap settings
@@ -40,6 +42,8 @@ public partial class MainWindow : Window
4042
int dpiX = 96;
4143
int dpiY = 96;
4244

45+
byte gridAlpha = 16;
46+
4347
// colors
4448
PixelColor currentColor;
4549
PixelColor[] palette;
@@ -66,6 +70,18 @@ public MainWindow()
6670

6771
void Start()
6872
{
73+
74+
// setup background grid
75+
gridImage = imgGrid;
76+
RenderOptions.SetBitmapScalingMode(gridImage, BitmapScalingMode.NearestNeighbor);
77+
RenderOptions.SetEdgeMode(gridImage, EdgeMode.Aliased);
78+
w = (MainWindow)Application.Current.MainWindow;
79+
//var gridScaleX = (int)gridImage.Width / canvasResolutionX;
80+
gridBitmap = new WriteableBitmap(canvasResolutionX, canvasResolutionY, dpiX, dpiY, PixelFormats.Bgra32, null);
81+
gridImage.Source = gridBitmap;
82+
DrawBackgroundGrid();
83+
84+
6985
// build drawing area
7086
drawingImage = imgCanvas;
7187
RenderOptions.SetBitmapScalingMode(drawingImage, BitmapScalingMode.NearestNeighbor);
@@ -615,7 +631,6 @@ private void CallUndo()
615631
if (currentUndoIndex > 0)
616632
{
617633
canvasBitmap = undoBufferBitmap[--currentUndoIndex];
618-
Console.WriteLine("restore undo " + currentUndoIndex);
619634
imgCanvas.Source = canvasBitmap;
620635
}
621636
}
@@ -629,5 +644,23 @@ public void CanExecute_Undo(object sender, CanExecuteRoutedEventArgs e)
629644
{
630645
e.CanExecute = true;
631646
}
647+
648+
void DrawBackgroundGrid()
649+
{
650+
for (int x = 0; x < 16; x++)
651+
{
652+
for (int y = 0; y < 16; y++)
653+
{
654+
PixelColor c = new PixelColor();
655+
c.Alpha = gridAlpha;
656+
byte v = (byte)(((x % 2) == (y % 2)) ? 255 : 0);
657+
c.Red = v;
658+
c.Green = v;
659+
c.Blue = v;
660+
SetPixel(gridBitmap, x, y, (int)c.ColorBGRA);
661+
}
662+
}
663+
}
664+
632665
} // class
633666
} // namespace

0 commit comments

Comments
 (0)