Skip to content

Commit ea0f656

Browse files
committed
add default drawmode (replace), add drawmode selector, add straight lines mode (left shift down)
1 parent 373d35e commit ea0f656

File tree

2 files changed

+134
-18
lines changed

2 files changed

+134
-18
lines changed

PixelArtTool/MainWindow.xaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
<Window x:Class="PixelArtTool.MainWindow"
1+
<Window
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:PixelArtTool"
7+
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="PixelArtTool.MainWindow"
78
mc:Ignorable="d"
8-
Title="PixelArtTool" Height="412.222" Width="739.444" Background="#FF252526">
9+
Title="PixelArtTool" Height="412.222" Width="739.444" Background="#FF252526" KeyDown="OnKeyDown" KeyUp="OnKeyUp">
910
<Grid>
1011
<ToolBarTray Background="White" Height="36" VerticalAlignment="Top">
1112
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top">
@@ -48,6 +49,13 @@
4849
</Button>
4950
</ToolBar>
5051

52+
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top">
53+
<ComboBox x:Name="cmbDrawMode" HorizontalAlignment="Left" VerticalAlignment="Top" Width="55" IsReadOnly="True" SelectedIndex="0" SelectionChanged="OnModeSelectionChanged">
54+
<System:String>Default</System:String>
55+
<System:String>Additive</System:String>
56+
</ComboBox>
57+
</ToolBar>
58+
5159
</ToolBarTray>
5260

5361
<StatusBar Height="30" Margin="0,281,0,0" VerticalAlignment="Bottom">

PixelArtTool/MainWindow.xaml.cs

Lines changed: 124 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
namespace PixelArtTool
1212
{
1313

14+
public enum DrawMode : byte
15+
{
16+
Default = 0,
17+
Additive = 1
18+
}
19+
1420
/// <summary>
1521
/// Interaction logic for MainWindow.xaml
1622
/// </summary>
@@ -49,6 +55,9 @@ public partial class MainWindow : Window
4955
int currentUndoIndex = 0;
5056
WriteableBitmap[] undoBufferBitmap = new WriteableBitmap[maxUndoCount];
5157

58+
// modes
59+
DrawMode drawMode;
60+
5261
public MainWindow()
5362
{
5463
InitializeComponent();
@@ -95,6 +104,9 @@ void Start()
95104
currentColorIndex = 5;
96105
currentColor = palette[currentColorIndex];
97106
UpdateCurrentColor();
107+
108+
//this.KeyDown += new KeyEventHandler(OnKeyDown);
109+
//this.keyup += new KeyEventHandler(OnKeyDown);
98110
}
99111

100112

@@ -201,10 +213,17 @@ public struct PixelColor
201213
PixelColor[,] result = new PixelColor[width, height];
202214

203215
CopyPixels2(source, result, width * 4, 0, false);
204-
//source.CopyPixels(result, width * 4, 0, false);
205216
return result;
206217
}
207218

219+
bool firstPixel = true;
220+
int startPixelX = 0;
221+
int startPixelY = 0;
222+
bool verticalLine = false;
223+
bool horizontalLine = false;
224+
// bool diagonalLines = false; // TODO allow diagonal straigh lines
225+
int lockedX = 0;
226+
int lockedY = 0;
208227

209228
// https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.writeablebitmap?redirectedfrom=MSDN&view=netframework-4.7.2
210229
// The DrawPixel method updates the WriteableBitmap by using
@@ -214,24 +233,76 @@ void DrawPixel(int x, int y)
214233
if (x < 0 || x > canvasResolutionX - 1) return;
215234
if (y < 0 || y > canvasResolutionY - 1) return;
216235

217-
// get old color
218-
var oc = GetPixelColor(x, y, undoBufferBitmap[currentUndoIndex]);
219-
220-
// mix colors ADDITIVE mode
221-
var newc = new PixelColor();
222-
int r = (int)(oc.Red + currentColor.Red * ((float)opacity / (float)255));
223-
int g = (int)(oc.Green + currentColor.Green * ((float)opacity / (float)255));
224-
int b = (int)(oc.Blue + currentColor.Blue * ((float)opacity / (float)255));
236+
// is using straight lines
237+
if (leftShiftDown == true)
238+
{
239+
// get first pixel, to measure direction
240+
if (firstPixel == true)
241+
{
242+
startPixelX = x;
243+
startPixelY = y;
244+
firstPixel = false;
245+
}
246+
else // already drew before
247+
{
248+
if (horizontalLine == false && verticalLine == false)
249+
{
250+
// vertical
251+
if (x == startPixelX && y != startPixelY)
252+
{
253+
verticalLine = true;
254+
lockedX = x;
255+
}
256+
else if (y == startPixelY && x != startPixelX)
257+
{
258+
horizontalLine = true;
259+
lockedY = y;
260+
}
261+
}
262+
263+
// lock coordinates if straight lines
264+
if (verticalLine == true)
265+
{
266+
x = lockedX;
267+
}
268+
else if (horizontalLine == true)
269+
{
270+
y = lockedY;
271+
}
272+
}
273+
}
274+
else // left shift not down
275+
{
276+
verticalLine = false;
277+
horizontalLine = false;
278+
firstPixel = true;
279+
}
225280

226-
newc.Red = ClampToByte(r);
227-
newc.Green = ClampToByte(g);
228-
newc.Blue = ClampToByte(b);
229-
newc.Alpha = opacity;
281+
PixelColor draw = new PixelColor();
230282

231-
//Console.WriteLine(oc.Red + "+" + currentColor.Red + "*" + (opacity / (float)255) + " = " + newc.Red);
283+
switch (drawMode)
284+
{
285+
case DrawMode.Default: // replace
286+
draw = currentColor;
287+
break;
288+
case DrawMode.Additive:
289+
// get old color from undo buffer
290+
var oc = GetPixelColor(x, y, undoBufferBitmap[currentUndoIndex]);
291+
// mix colors ADDITIVE mode
292+
int r = (int)(oc.Red + currentColor.Red * (opacity / (float)255));
293+
int g = (int)(oc.Green + currentColor.Green * (opacity / (float)255));
294+
int b = (int)(oc.Blue + currentColor.Blue * (opacity / (float)255));
295+
draw.Red = ClampToByte(r);
296+
draw.Green = ClampToByte(g);
297+
draw.Blue = ClampToByte(b);
298+
draw.Alpha = opacity;
299+
break;
300+
default:
301+
break;
302+
}
232303

233304
// draw
234-
SetPixel(canvasBitmap, x, y, (int)newc.ColorBGRA);
305+
SetPixel(canvasBitmap, x, y, (int)draw.ColorBGRA);
235306

236307
prevX = x;
237308
prevY = y;
@@ -346,7 +417,7 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
346417
{
347418
// undo test
348419
undoBufferBitmap[++currentUndoIndex] = canvasBitmap.Clone();
349-
Console.WriteLine("save undo " + currentUndoIndex);
420+
//Console.WriteLine("save undo " + currentUndoIndex);
350421

351422
int x = (int)(e.GetPosition(drawingImage).X / canvasScaleX);
352423
int y = (int)(e.GetPosition(drawingImage).Y / canvasScaleX);
@@ -483,5 +554,42 @@ private void OnUndoButtonDown(object sender, RoutedEventArgs e)
483554
imgCanvas.Source = canvasBitmap;
484555
}
485556
}
557+
558+
private void OnModeSelectionChanged(object sender, SelectionChangedEventArgs e)
559+
{
560+
var s = sender as ComboBox;
561+
drawMode = (DrawMode)s.SelectedIndex;
562+
}
563+
564+
bool leftShiftDown = false;
565+
566+
// if key is pressed down globally
567+
void OnKeyDown(object sender, KeyEventArgs e)
568+
{
569+
// TODO: add tool shortcut keys
570+
switch (e.Key)
571+
{
572+
case Key.LeftShift:
573+
leftShiftDown = true;
574+
break;
575+
default:
576+
break;
577+
}
578+
}
579+
580+
private void OnKeyUp(object sender, KeyEventArgs e)
581+
{
582+
switch (e.Key)
583+
{
584+
case Key.LeftShift:
585+
leftShiftDown = false;
586+
verticalLine = false;
587+
horizontalLine = false;
588+
firstPixel = true;
589+
break;
590+
default:
591+
break;
592+
}
593+
}
486594
} // class
487595
} // namespace

0 commit comments

Comments
 (0)