11
11
namespace PixelArtTool
12
12
{
13
13
14
+ public enum DrawMode : byte
15
+ {
16
+ Default = 0 ,
17
+ Additive = 1
18
+ }
19
+
14
20
/// <summary>
15
21
/// Interaction logic for MainWindow.xaml
16
22
/// </summary>
@@ -49,6 +55,9 @@ public partial class MainWindow : Window
49
55
int currentUndoIndex = 0 ;
50
56
WriteableBitmap [ ] undoBufferBitmap = new WriteableBitmap [ maxUndoCount ] ;
51
57
58
+ // modes
59
+ DrawMode drawMode ;
60
+
52
61
public MainWindow ( )
53
62
{
54
63
InitializeComponent ( ) ;
@@ -95,6 +104,9 @@ void Start()
95
104
currentColorIndex = 5 ;
96
105
currentColor = palette [ currentColorIndex ] ;
97
106
UpdateCurrentColor ( ) ;
107
+
108
+ //this.KeyDown += new KeyEventHandler(OnKeyDown);
109
+ //this.keyup += new KeyEventHandler(OnKeyDown);
98
110
}
99
111
100
112
@@ -201,10 +213,17 @@ public struct PixelColor
201
213
PixelColor [ , ] result = new PixelColor [ width , height ] ;
202
214
203
215
CopyPixels2 ( source , result , width * 4 , 0 , false ) ;
204
- //source.CopyPixels(result, width * 4, 0, false);
205
216
return result ;
206
217
}
207
218
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 ;
208
227
209
228
// https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.writeablebitmap?redirectedfrom=MSDN&view=netframework-4.7.2
210
229
// The DrawPixel method updates the WriteableBitmap by using
@@ -214,24 +233,76 @@ void DrawPixel(int x, int y)
214
233
if ( x < 0 || x > canvasResolutionX - 1 ) return ;
215
234
if ( y < 0 || y > canvasResolutionY - 1 ) return ;
216
235
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
+ }
225
280
226
- newc . Red = ClampToByte ( r ) ;
227
- newc . Green = ClampToByte ( g ) ;
228
- newc . Blue = ClampToByte ( b ) ;
229
- newc . Alpha = opacity ;
281
+ PixelColor draw = new PixelColor ( ) ;
230
282
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
+ }
232
303
233
304
// draw
234
- SetPixel ( canvasBitmap , x , y , ( int ) newc . ColorBGRA ) ;
305
+ SetPixel ( canvasBitmap , x , y , ( int ) draw . ColorBGRA ) ;
235
306
236
307
prevX = x ;
237
308
prevY = y ;
@@ -346,7 +417,7 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
346
417
{
347
418
// undo test
348
419
undoBufferBitmap [ ++ currentUndoIndex ] = canvasBitmap . Clone ( ) ;
349
- Console . WriteLine ( "save undo " + currentUndoIndex ) ;
420
+ // Console.WriteLine("save undo " + currentUndoIndex);
350
421
351
422
int x = ( int ) ( e . GetPosition ( drawingImage ) . X / canvasScaleX ) ;
352
423
int y = ( int ) ( e . GetPosition ( drawingImage ) . Y / canvasScaleX ) ;
@@ -483,5 +554,42 @@ private void OnUndoButtonDown(object sender, RoutedEventArgs e)
483
554
imgCanvas . Source = canvasBitmap ;
484
555
}
485
556
}
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
+ }
486
594
} // class
487
595
} // namespace
0 commit comments