Skip to content

Commit f4d1873

Browse files
committed
add flip x/y tool, add palette loader, add secondary color, add main-secondary color swap key (x)
1 parent 9f11bf1 commit f4d1873

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

PixelArtTool/MainWindow.xaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@
8282
<Label x:Name="lblToolInfo" Content="-"/>
8383
</StatusBarItem>
8484
</StatusBar>
85-
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="128" Margin="10,50,0,0" VerticalAlignment="Top" Width="64">
86-
<Image x:Name="imgPalette" HorizontalAlignment="Left" Height="128" Margin="0,0,0,-30" VerticalAlignment="Top" Width="64" Stretch="Fill"/>
85+
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="198" Margin="10,50,0,0" VerticalAlignment="Top" Width="64">
86+
<Image x:Name="imgPalette" HorizontalAlignment="Left" Height="198" Margin="0,0,0,-30" VerticalAlignment="Top" Width="64" Stretch="Fill"/>
8787
</Border>
8888
<Grid>
8989
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="256" Margin="89,50,0,0" VerticalAlignment="Top" Width="256">
@@ -99,9 +99,13 @@
9999
<Button x:Name="btnScrollDown" Click="OnScrollButtonDownClicked" Content="\/" HorizontalAlignment="Left" Margin="388,274,0,0" VerticalAlignment="Top" Width="24"/>
100100
<Button x:Name="btnScrollLeft" Click="OnScrollButtonLeftClicked" Content="&lt;" HorizontalAlignment="Left" Margin="361,263,0,0" VerticalAlignment="Top" Width="24"/>
101101
<Button x:Name="btnScrollRight" Click="OnScrollButtonRightClicked" Content="&gt;" HorizontalAlignment="Left" Margin="415,262,0,0" VerticalAlignment="Top" Width="24"/>
102+
<Button x:Name="btnFlipX" Click="OnFlipXButtonDown" Content="FlipX" HorizontalAlignment="Left" Margin="462,251,0,0" VerticalAlignment="Top" Width="32"/>
103+
<Button x:Name="btnFlipY" Click="OnFlipYButtonDown" Content="FlipY" HorizontalAlignment="Left" Margin="462,276,0,0" VerticalAlignment="Top" Width="32"/>
104+
<Button x:Name="btnLoadPalette" Click="OnLoadPaletteButton" Content="Load Palette" HorizontalAlignment="Left" Margin="620,276,0,0" VerticalAlignment="Top" Width="77"/>
102105
</Grid>
103106
<CheckBox x:Name="chkOutline" Content="Outline" HorizontalAlignment="Left" Margin="648,50,0,0" VerticalAlignment="Top" Width="64"/>
104-
<Rectangle x:Name="rectCurrentColor" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="24" Margin="10,181,0,0" Stroke="Black" VerticalAlignment="Top" Width="24"/>
107+
<Rectangle x:Name="rectCurrentColor" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="28" Margin="10,276,0,0" Stroke="Black" VerticalAlignment="Top" Width="28"/>
105108
<CheckBox x:Name="chkMirrorX" Content="MirrorX" HorizontalAlignment="Left" Margin="648,80,0,0" VerticalAlignment="Top" Width="64"/>
109+
<Rectangle x:Name="rectSecondaryColor" Fill="Black" HorizontalAlignment="Left" Height="28" Margin="47,276,0,0" Stroke="Black" VerticalAlignment="Top" Width="28"/>
106110
</Grid>
107111
</Window>

PixelArtTool/MainWindow.xaml.cs

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
using System;
33
using System.Collections.Generic;
44
using System.ComponentModel;
5-
using System.Globalization;
65
using System.IO;
76
using System.Runtime.CompilerServices;
87
using System.Runtime.InteropServices;
98
using System.Windows;
109
using System.Windows.Controls;
1110
using System.Windows.Data;
1211
using System.Windows.Input;
13-
using System.Windows.Markup;
1412
using System.Windows.Media;
1513
using System.Windows.Media.Imaging;
1614

@@ -76,17 +74,16 @@ public partial class MainWindow : Window, INotifyPropertyChanged
7674
// modes
7775
BlendMode blendMode;
7876

79-
// TEST property binding
80-
private ToolMode myVar = ToolMode.Fill;
77+
private ToolMode _currentTool = ToolMode.Draw;
8178
public ToolMode CurrentTool
8279
{
8380
get
8481
{
85-
return myVar;
82+
return _currentTool;
8683
}
8784
set
8885
{
89-
myVar = value;
86+
_currentTool = value;
9087
OnPropertyChanged();
9188
}
9289
}
@@ -157,16 +154,16 @@ void Start()
157154
//paletteImage.MouseRightButtonDown += new MouseButtonEventHandler(PaletteRightButtonDown);
158155

159156
// init
160-
LoadPalette();
157+
LoadPalette("pack://application:,,,/Resources/Palettes/aap-64-1x.png");
161158
currentColorIndex = 5;
162159
currentColor = palette[currentColorIndex];
163160
UpdateCurrentColor();
164161
}
165162

166163

167-
void LoadPalette()
164+
void LoadPalette(string path)
168165
{
169-
Uri uri = new Uri("pack://application:,,,/Resources/Palettes/aap-64-1x.png");
166+
Uri uri = new Uri(path);
170167
var img = new BitmapImage(uri);
171168

172169
// get colors
@@ -699,6 +696,20 @@ void OnKeyDown(object sender, KeyEventArgs e)
699696
// TODO: add tool shortcut keys
700697
switch (e.Key)
701698
{
699+
case Key.X: // swap current/secondary colors
700+
var tempcolor = rectCurrentColor.Fill;
701+
rectCurrentColor.Fill = rectSecondaryColor.Fill;
702+
rectSecondaryColor.Fill = tempcolor;
703+
// TODO move to converter
704+
var c = new PixelColor();
705+
var t = ((SolidColorBrush)rectCurrentColor.Fill).Color;
706+
c.Red = t.R;
707+
c.Green = t.G;
708+
c.Blue = t.B;
709+
c.Alpha = t.A;
710+
currentColor = c;
711+
712+
break;
702713
case Key.B: // brush
703714
CurrentTool = ToolMode.Draw;
704715
break;
@@ -942,6 +953,41 @@ private void OnPropertyChanged([CallerMemberName] string propertyName = null)
942953
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
943954
}
944955

956+
void BitmapFlip(bool horizontal)
957+
{
958+
// clone canvas, FIXME not really needed..could just copy pixels to array or backbuffer directly
959+
var tempCanvasBitmap = new WriteableBitmap(canvasResolutionX, canvasResolutionY, dpiX, dpiY, PixelFormats.Bgra32, null);
960+
tempCanvasBitmap = canvasBitmap.Clone();
961+
for (int x = 0; x < canvasResolutionX; x++)
962+
{
963+
for (int y = 0; y < canvasResolutionY; y++)
964+
{
965+
int xx = horizontal ? (canvasResolutionX - x - 1) : x;
966+
int yy = !horizontal ? (canvasResolutionY - y - 1) : y;
967+
var c = GetPixelColor(xx, yy, tempCanvasBitmap);
968+
SetPixel(canvasBitmap, x, y, (int)c.ColorBGRA);
969+
}
970+
}
971+
}
972+
973+
private void OnFlipXButtonDown(object sender, RoutedEventArgs e)
974+
{
975+
BitmapFlip(horizontal: true);
976+
}
977+
978+
private void OnFlipYButtonDown(object sender, RoutedEventArgs e)
979+
{
980+
BitmapFlip(horizontal: false);
981+
}
982+
983+
private void OnLoadPaletteButton(object sender, RoutedEventArgs e)
984+
{
985+
OpenFileDialog openFileDialog = new OpenFileDialog();
986+
if (openFileDialog.ShowDialog() == true)
987+
{
988+
LoadPalette(openFileDialog.FileName);
989+
}
990+
}
945991
} // class
946992

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

0 commit comments

Comments
 (0)