Skip to content

Commit 272b439

Browse files
committed
add save icon, adjust toolbar colors, add clear icon, fix ClearImage, update hue and saturation color gradients
1 parent ea6706f commit 272b439

File tree

5 files changed

+96
-24
lines changed

5 files changed

+96
-24
lines changed

PixelArtTool/MainWindow.xaml

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<VisualBrush x:Key="LevelSaturationBrush" TileMode="None">
3535
<VisualBrush.Visual>
3636
<Canvas Background="Black" Width="1" Height="1" SnapsToDevicePixels="True">
37-
<Rectangle Width="1" Height="1" SnapsToDevicePixels="True">
37+
<Rectangle x:Name="rectGradientSingle" Width="1" Height="1" SnapsToDevicePixels="True">
3838
<Rectangle.Fill>
3939
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
4040
<LinearGradientBrush.GradientStops>
@@ -63,28 +63,28 @@
6363
<local:EnumBooleanConverter x:Key="ComparisonConverter" />
6464
</Grid.Resources>
6565

66-
<ToolBarTray Background="White" Height="32" VerticalAlignment="Top">
67-
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top">
66+
<ToolBarTray Background="#FF1F1F1F" Height="32" VerticalAlignment="Top">
67+
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top" Background="#FF9C9C9C">
6868
<Button x:Name="btnNew" ToolTip="New (clear image)" Click="OnClearButton">
69-
<Image Source="/Resources/Buttons/emptybutton.png" />
69+
<Image Source="/Resources/Buttons/newimage.png" Width="24" Height="24" RenderOptions.BitmapScalingMode="NearestNeighbor" />
7070
</Button>
7171
<Button x:Name="btnSave" ToolTip="Save as.." Click="OnSaveButton">
72-
<Image Source="/Resources/Buttons/emptybutton.png" />
72+
<Image Source="/Resources/Buttons/save.png" Width="24" Height="24" RenderOptions.BitmapScalingMode="NearestNeighbor" />
7373
</Button>
7474
</ToolBar>
7575

76-
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top">
77-
<RadioButton GroupName="Toolbar" Tag="Draw" ToolTip="Pencil" Style="{StaticResource {x:Type ToggleButton}}"
76+
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top" Background="#FF9C9C9C">
77+
<RadioButton GroupName="Toolbar" Tag="Draw" ToolTip="Brush (D)" Style="{StaticResource {x:Type ToggleButton}}"
7878
IsChecked="{Binding Path=CurrentTool, Converter={StaticResource ComparisonConverter}, ConverterParameter={x:Static local:ToolMode.Draw},Mode=TwoWay}">
7979
<Image Source="/Resources/Buttons/drawmode.png" Width="24" Height="24" RenderOptions.BitmapScalingMode="NearestNeighbor" />
8080
</RadioButton>
81-
<RadioButton GroupName="Toolbar" Tag="Fill" ToolTip="Flood Fill" Style="{StaticResource {x:Type ToggleButton}}"
81+
<RadioButton GroupName="Toolbar" Tag="Fill" ToolTip="Flood Fill (F)" Style="{StaticResource {x:Type ToggleButton}}"
8282
IsChecked="{Binding Path=CurrentTool, Converter={StaticResource ComparisonConverter}, ConverterParameter={x:Static local:ToolMode.Fill},Mode=TwoWay}">
8383
<Image Source="/Resources/Buttons/emptybutton.png" RenderOptions.BitmapScalingMode="NearestNeighbor" />
8484
</RadioButton>
8585
</ToolBar>
8686

87-
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top">
87+
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top" Background="#FF9C9C9C">
8888
<RadioButton GroupName="Toolbar2" ToolTip="Test" Style="{StaticResource {x:Type ToggleButton}}">
8989
<Image Source="/Resources/Buttons/emptybutton.png" />
9090
</RadioButton>
@@ -93,11 +93,11 @@
9393
</RadioButton>
9494
</ToolBar>
9595

96-
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top">
96+
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top" Background="#FF9C9C9C">
9797
<Slider x:Name="sliderOpacity" ToolTip="Opacity" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="50" Height="28" Value="255" Maximum="255" LargeChange="0" SmallChange="1" ValueChanged="OpacitySliderValueChanged"/>
9898
</ToolBar>
9999

100-
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top">
100+
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top" Background="#FF9C9C9C">
101101
<Button x:Name="btnUndo" ToolTip="Undo" Click="OnUndoButtonDown">
102102
<Image Source="/Resources/Buttons/emptybutton.png" />
103103
</Button>
@@ -106,7 +106,7 @@
106106
</Button>
107107
</ToolBar>
108108

109-
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top">
109+
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top" Background="#FF9C9C9C">
110110
<ComboBox x:Name="cmbDrawMode" HorizontalAlignment="Left" VerticalAlignment="Top" Width="55" IsReadOnly="True" SelectedIndex="0" SelectionChanged="OnModeSelectionChanged">
111111
<System:String>Default</System:String>
112112
<System:String>Additive</System:String>
@@ -171,16 +171,15 @@
171171
<Button x:Name="btnFlipY" Click="OnFlipYButtonDown" Content="FlipY" HorizontalAlignment="Left" Margin="390,316,0,0" VerticalAlignment="Top" Width="32"/>
172172
<Button x:Name="btnLoadPalette" Click="OnLoadPaletteButton" Content="Load Palette" HorizontalAlignment="Left" Margin="10,321,0,0" VerticalAlignment="Top" Width="71"/>
173173
</Grid>
174-
<CheckBox x:Name="chkOutline" Content="Outline" HorizontalAlignment="Left" Margin="648,50,0,0" VerticalAlignment="Top" Width="64"/>
174+
<CheckBox x:Name="chkOutline" Content="Outline" HorizontalAlignment="Left" Margin="638,50,0,0" VerticalAlignment="Top" Width="64" Click="chkOutline_Click"/>
175175
<Rectangle x:Name="rectCurrentColor" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="28" Margin="10,276,0,0" Stroke="Black" VerticalAlignment="Top" Width="28"/>
176-
<CheckBox x:Name="chkMirrorX" Content="MirrorX" HorizontalAlignment="Left" Margin="648,80,0,0" VerticalAlignment="Top" Width="64"/>
176+
<CheckBox x:Name="chkMirrorX" Content="Symmetry X" HorizontalAlignment="Left" Margin="638,71,0,0" VerticalAlignment="Top" Width="93"/>
177177
<Rectangle x:Name="rectSecondaryColor" Fill="Black" HorizontalAlignment="Left" Height="28" Margin="47,276,0,0" Stroke="Black" VerticalAlignment="Top" Width="28"/>
178178

179179
<!-- https://stackoverflow.com/a/32514853/5452781 -->
180-
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="452,113,0,0">
181-
<Rectangle Fill="{StaticResource LevelSaturationBrush}" Width="200" Height="200" Margin="10" Stroke="Black" StrokeThickness="1" SnapsToDevicePixels="True" />
182-
<Rectangle Fill="{StaticResource HueBrush}" Width="20" Height="200" Margin="10" Stroke="Black" StrokeThickness="1" SnapsToDevicePixels="True" />
183-
</StackPanel>
180+
<Rectangle x:Name="tempRect" Width="200" Height="200" Margin="459,130,0,0" SnapsToDevicePixels="True" HorizontalAlignment="Left" VerticalAlignment="Top" UseLayoutRounding="False" Fill="Black" />
181+
<Rectangle x:Name="rectSaturation" Fill="{StaticResource LevelSaturationBrush}" Width="200" Height="200" Margin="459,130,0,0" StrokeThickness="1" SnapsToDevicePixels="True" HorizontalAlignment="Left" VerticalAlignment="Top" />
182+
<Rectangle x:Name="rectHueBar" Fill="{StaticResource HueBrush}" Width="20" Height="200" Margin="664,130,0,0" Stroke="Black" StrokeThickness="1" SnapsToDevicePixels="True" MouseDown="rectHueBar_MouseDown" HorizontalAlignment="Left" VerticalAlignment="Top" />
184183

185184
</Grid>
186185
</Window>

PixelArtTool/MainWindow.xaml.cs

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public static implicit operator Point(POINT point)
9797
// modes
9898
BlendMode blendMode;
9999

100+
// clear buffers
101+
Int32Rect emptyRect;
102+
int bytesPerPixel;
103+
byte[] emptyPixels;
104+
int emptyStride;
105+
100106
private ToolMode _currentTool = ToolMode.Draw;
101107
public ToolMode CurrentTool
102108
{
@@ -128,7 +134,6 @@ void Start()
128134
RenderOptions.SetBitmapScalingMode(gridImage, BitmapScalingMode.NearestNeighbor);
129135
RenderOptions.SetEdgeMode(gridImage, EdgeMode.Aliased);
130136
w = (MainWindow)Application.Current.MainWindow;
131-
//var gridScaleX = (int)gridImage.Width / canvasResolutionX;
132137
gridBitmap = new WriteableBitmap(canvasResolutionX, canvasResolutionY, dpiX, dpiY, PixelFormats.Bgra32, null);
133138
gridImage.Source = gridBitmap;
134139
DrawBackgroundGrid();
@@ -138,7 +143,6 @@ void Start()
138143
RenderOptions.SetBitmapScalingMode(outlineImage, BitmapScalingMode.NearestNeighbor);
139144
RenderOptions.SetEdgeMode(outlineImage, EdgeMode.Aliased);
140145
w = (MainWindow)Application.Current.MainWindow;
141-
//var gridScaleX = (int)gridImage.Width / canvasResolutionX;
142146
outlineBitmap = new WriteableBitmap(canvasResolutionX, canvasResolutionY, dpiX, dpiY, PixelFormats.Bgra32, null);
143147
outlineImage.Source = outlineBitmap;
144148

@@ -151,6 +155,12 @@ void Start()
151155
canvasBitmap = new WriteableBitmap(canvasResolutionX, canvasResolutionY, dpiX, dpiY, PixelFormats.Bgra32, null);
152156
drawingImage.Source = canvasBitmap;
153157

158+
// init clear buffers
159+
emptyRect = new Int32Rect(0, 0, canvasBitmap.PixelWidth, canvasBitmap.PixelHeight);
160+
bytesPerPixel = canvasBitmap.Format.BitsPerPixel / 8;
161+
emptyPixels = new byte[emptyRect.Width * emptyRect.Height * bytesPerPixel];
162+
emptyStride = emptyRect.Width * bytesPerPixel;
163+
154164
// setup preview area
155165
RenderOptions.SetBitmapScalingMode(imgPreview1x, BitmapScalingMode.NearestNeighbor);
156166
imgPreview1x.Source = canvasBitmap;
@@ -663,13 +673,13 @@ void drawingMouseWheel(object sender, MouseWheelEventArgs e)
663673
private void OnClearButton(object sender, RoutedEventArgs e)
664674
{
665675
ClearImage(canvasBitmap);
676+
UpdateOutline();
666677
}
667678

668679
// clears bitmap by re-creating it
669-
void ClearImage(WriteableBitmap target)
680+
void ClearImage(WriteableBitmap targetBitmap)
670681
{
671-
canvasBitmap = new WriteableBitmap(canvasResolutionX, canvasResolutionY, dpiX, dpiY, PixelFormats.Bgra32, null);
672-
drawingImage.Source = canvasBitmap;
682+
targetBitmap.WritePixels(emptyRect, emptyPixels, emptyStride, 0);
673683
}
674684

675685
private void OnSaveButton(object sender, RoutedEventArgs e)
@@ -747,7 +757,7 @@ void OnKeyDown(object sender, KeyEventArgs e)
747757
c2.Blue = c1.B;
748758
currentColor = c2;
749759
rectCurrentColor.Fill = new SolidColorBrush(Color.FromArgb(c2.Alpha, c2.Red, c2.Green, c2.Blue));
750-
// Console.WriteLine(cursor.X + "," + cursor.Y + " = " + c1);
760+
// Console.WriteLine(cursor.X + "," + cursor.Y + " = " + c1);
751761
break;
752762
case Key.X: // swap current/secondary colors
753763
var tempcolor = rectCurrentColor.Fill;
@@ -1058,6 +1068,63 @@ public static Point GetCursorPosition()
10581068
return lpPoint;
10591069
}
10601070

1071+
private void chkOutline_Click(object sender, RoutedEventArgs e)
1072+
{
1073+
if (chkOutline.IsChecked == true)
1074+
{
1075+
UpdateOutline();
1076+
}
1077+
else // clear
1078+
{
1079+
ClearImage(outlineBitmap);
1080+
}
1081+
}
1082+
1083+
private void rectHueBar_MouseDown(object sender, MouseButtonEventArgs e)
1084+
{
1085+
POINT cursor;
1086+
GetCursorPos(out cursor);
1087+
var c = Win32GetScreenPixel((int)cursor.X, (int)cursor.Y);
1088+
//Console.WriteLine("color:"+c);
1089+
var f = rectSaturation.Fill;
1090+
1091+
// build hue gradient
1092+
LinearGradientBrush myBrush = new LinearGradientBrush();
1093+
var c1 = new Color();
1094+
c1.R = 255;
1095+
c1.G = 255;
1096+
c1.B = 255;
1097+
c1.A = 255;
1098+
var c2 = new Color();
1099+
c2.R = c.R;
1100+
c2.G = c.G;
1101+
c2.B = c.B;
1102+
c2.A = 255;
1103+
myBrush.StartPoint = new Point(0, 0);
1104+
myBrush.EndPoint = new Point(1, 0);
1105+
1106+
var g1 = new GradientStop(c1, 0.0);
1107+
myBrush.GradientStops.Add(g1);
1108+
1109+
var g2 = new GradientStop(c2, 1.0);
1110+
myBrush.GradientStops.Add(g2);
1111+
rectSaturation.Fill = myBrush;
1112+
1113+
// set opacity mask
1114+
var opacityBrush = new LinearGradientBrush();
1115+
opacityBrush.StartPoint = new Point(0, 0);
1116+
opacityBrush.EndPoint = new Point(0, 1);
1117+
var g1b = new GradientStop(c1, 0.0);
1118+
opacityBrush.GradientStops.Add(g1b);
1119+
c2.A = 0;
1120+
c2.R = 0;
1121+
c2.G = 0;
1122+
c2.B = 0;
1123+
var g2b = new GradientStop(c2, 1.0);
1124+
opacityBrush.GradientStops.Add(g2b);
1125+
rectSaturation.OpacityMask = opacityBrush;
1126+
1127+
}
10611128
} // class
10621129

10631130
// https://stackoverflow.com/a/2908885/5452781

PixelArtTool/PixelArtTool.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,11 @@
127127
<ItemGroup>
128128
<Resource Include="Resources\Buttons\drawmode.png" />
129129
</ItemGroup>
130+
<ItemGroup>
131+
<Resource Include="Resources\Buttons\save.png" />
132+
</ItemGroup>
133+
<ItemGroup>
134+
<Resource Include="Resources\Buttons\newimage.png" />
135+
</ItemGroup>
130136
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
131137
</Project>
1.18 KB
Loading
1.17 KB
Loading

0 commit comments

Comments
 (0)