1
1
using Microsoft . Win32 ;
2
2
using System ;
3
+ using System . Collections ;
3
4
using System . Collections . Generic ;
4
5
using System . ComponentModel ;
5
6
using System . IO ;
@@ -53,8 +54,27 @@ public partial class MainWindow : Window, INotifyPropertyChanged
53
54
54
55
// undo
55
56
const int maxUndoCount = 100 ;
56
- int currentUndoIndex = 0 ;
57
- WriteableBitmap [ ] undoBufferBitmap = new WriteableBitmap [ maxUndoCount ] ;
57
+ //int currentUndoIndex = 0;
58
+ private int myVar = 0 ;
59
+
60
+ public int currentUndoIndex
61
+ {
62
+ get
63
+ {
64
+ Console . WriteLine ( "set:" + myVar ) ;
65
+ return myVar ;
66
+ }
67
+ set
68
+ {
69
+ Console . WriteLine ( "get:" + myVar ) ;
70
+ myVar = value ;
71
+ }
72
+ }
73
+
74
+
75
+ //WriteableBitmap[] undoBufferBitmap = new WriteableBitmap[maxUndoCount];
76
+ //List<WriteableBitmap> undoBufferBitmap = new List<WriteableBitmap>();
77
+ //Stack<WriteableBitmap> undoBufferBitmap = new Stack<WriteableBitmap>();
58
78
59
79
// drawing lines
60
80
bool firstPixel = true ;
@@ -98,6 +118,10 @@ public MainWindow()
98
118
Start ( ) ;
99
119
}
100
120
121
+ Stack < WriteableBitmap > undoStack = new Stack < WriteableBitmap > ( ) ;
122
+ Stack < WriteableBitmap > redoStack = new Stack < WriteableBitmap > ( ) ;
123
+ WriteableBitmap currentItem ;
124
+
101
125
void Start ( )
102
126
{
103
127
// needed for binding
@@ -157,13 +181,6 @@ void Start()
157
181
w . MouseWheel += new MouseWheelEventHandler ( DrawingMouseWheel ) ;
158
182
drawingImage . MouseUp += new MouseButtonEventHandler ( DrawingMouseUp ) ;
159
183
160
- // FIXME init undos
161
- for ( int i = 0 ; i < maxUndoCount ; i ++ )
162
- {
163
- undoBufferBitmap [ i ] = canvasBitmap . Clone ( ) ;
164
- }
165
-
166
-
167
184
// build palette
168
185
paletteImage = imgPalette ;
169
186
RenderOptions . SetBitmapScalingMode ( paletteImage , BitmapScalingMode . NearestNeighbor ) ;
@@ -270,8 +287,10 @@ void DrawPixel(int x, int y)
270
287
draw = currentColor ;
271
288
break ;
272
289
case BlendMode . Additive :
290
+ /*
273
291
// get old color from undo buffer
274
- var oc = GetPixelColor ( x , y , undoBufferBitmap [ currentUndoIndex ] ) ;
292
+ // TODO add undo back
293
+ //var oc = GetPixelColor(x, y, undoBufferBitmap.Pop());
275
294
// mix colors ADDITIVE mode
276
295
int r = (int)(oc.Red + currentColor.Red * (opacity / (float)255));
277
296
int g = (int)(oc.Green + currentColor.Green * (opacity / (float)255));
@@ -280,6 +299,7 @@ void DrawPixel(int x, int y)
280
299
draw.Green = ClampToByte(g);
281
300
draw.Blue = ClampToByte(b);
282
301
draw.Alpha = opacity;
302
+ */
283
303
break ;
284
304
default :
285
305
break ;
@@ -463,22 +483,16 @@ void DrawingMiddleButtonDown(object sender, MouseButtonEventArgs e)
463
483
}
464
484
}
465
485
486
+ // clicked, but not moved
466
487
void DrawingLeftButtonDown ( object sender , MouseButtonEventArgs e )
467
488
{
468
- // undo test
469
- //undoBufferBitmap[currentUndoIndex++] = canvasBitmap.Clone();
470
- currentUndoIndex = ++ currentUndoIndex % maxUndoCount ; // wrap
471
- CopyBitmapPixels ( canvasBitmap , undoBufferBitmap [ currentUndoIndex ] ) ;
472
-
473
- // FIXME if undobuffer clone enabled above, sometimes Exception thrown: 'System.IndexOutOfRangeException' in PixelArtTool.exe
474
- // An unhandled exception of type 'System.IndexOutOfRangeException' occurred in PixelArtTool.exe
475
- // Index was outside the bounds of the array.
476
- // Console.WriteLine(drawingImage);
489
+ // take current bitmap as currentimage
490
+ currentItem = canvasBitmap . Clone ( ) ;
491
+ undoStack . Push ( currentItem ) ;
477
492
478
493
int x = ( int ) ( e . GetPosition ( drawingImage ) . X / canvasScaleX ) ;
479
494
int y = ( int ) ( e . GetPosition ( drawingImage ) . Y / canvasScaleX ) ;
480
495
481
-
482
496
switch ( CurrentTool )
483
497
{
484
498
case ToolMode . Draw :
@@ -504,8 +518,8 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
504
518
{
505
519
UpdateOutline ( ) ;
506
520
}
521
+ } // DrawingLeftButtonDown
507
522
508
- }
509
523
510
524
void DrawingMouseUp ( object sender , MouseButtonEventArgs e )
511
525
{
@@ -624,6 +638,7 @@ void DrawingMouseWheel(object sender, MouseWheelEventArgs e)
624
638
625
639
private void OnClearButton ( object sender , RoutedEventArgs e )
626
640
{
641
+ //undoRedoWrapper.AddItem(canvasBitmap.Clone());
627
642
ClearImage ( canvasBitmap , emptyRect , emptyPixels , emptyStride ) ;
628
643
UpdateOutline ( ) ;
629
644
}
@@ -659,7 +674,12 @@ private void OpacitySliderValueChanged(object sender, RoutedPropertyChangedEvent
659
674
660
675
private void OnUndoButtonDown ( object sender , RoutedEventArgs e )
661
676
{
662
- CallUndo ( ) ;
677
+ DoUndo ( ) ;
678
+ }
679
+
680
+ private void OnRedoButtonDown ( object sender , RoutedEventArgs e )
681
+ {
682
+ DoRedo ( ) ;
663
683
}
664
684
665
685
private void OnModeSelectionChanged ( object sender , SelectionChangedEventArgs e )
@@ -734,14 +754,37 @@ private void OnKeyUp(object sender, KeyEventArgs e)
734
754
}
735
755
}
736
756
737
- private void CallUndo ( )
757
+
758
+ // restore to previous bitmap
759
+ private void DoUndo ( )
738
760
{
739
- currentUndoIndex -- ;
740
- // TODO: only wrap to current last active index
741
- if ( currentUndoIndex < 0 ) currentUndoIndex += maxUndoCount ; // wrap
742
- CopyBitmapPixels ( undoBufferBitmap [ currentUndoIndex ] , canvasBitmap ) ;
761
+ if ( undoStack . Count > 0 )
762
+ {
763
+ // TODO: clear redo?
764
+ // save current image in top of redo stack
765
+ redoStack . Push ( canvasBitmap . Clone ( ) ) ;
766
+ // take latest image from top of undo stack
767
+ currentItem = undoStack . Pop ( ) ;
768
+ // show latest image
769
+ CopyBitmapPixels ( currentItem , canvasBitmap ) ;
770
+ }
743
771
}
744
772
773
+ // go to next existing undo buffer, if available
774
+ private void DoRedo ( )
775
+ {
776
+ if ( redoStack . Count > 0 )
777
+ {
778
+ // save current image in top of redo stack
779
+ undoStack . Push ( canvasBitmap . Clone ( ) ) ;
780
+ // take latest image from top of redo stack
781
+ currentItem = redoStack . Pop ( ) ;
782
+ // show latest redo image
783
+ CopyBitmapPixels ( currentItem , canvasBitmap ) ;
784
+ }
785
+ }
786
+
787
+
745
788
void CopyBitmapPixels ( WriteableBitmap source , WriteableBitmap target )
746
789
{
747
790
byte [ ] data = new byte [ source . BackBufferStride * source . PixelHeight ] ;
@@ -752,14 +795,24 @@ void CopyBitmapPixels(WriteableBitmap source, WriteableBitmap target)
752
795
753
796
public void Executed_Undo ( object sender , ExecutedRoutedEventArgs e )
754
797
{
755
- CallUndo ( ) ;
798
+ DoUndo ( ) ;
756
799
}
757
800
758
801
public void CanExecute_Undo ( object sender , CanExecuteRoutedEventArgs e )
759
802
{
760
803
e . CanExecute = true ;
761
804
}
762
805
806
+ public void Executed_Redo ( object sender , ExecutedRoutedEventArgs e )
807
+ {
808
+ DoRedo ( ) ;
809
+ }
810
+
811
+ public void CanExecute_Redo ( object sender , CanExecuteRoutedEventArgs e )
812
+ {
813
+ e . CanExecute = true ;
814
+ }
815
+
763
816
void FloodFill ( int x , int y , int fillColor )
764
817
{
765
818
// get hit color pixel
0 commit comments