11using Microsoft . Win32 ;
22using System ;
3+ using System . Collections ;
34using System . Collections . Generic ;
45using System . ComponentModel ;
56using System . IO ;
@@ -53,8 +54,27 @@ public partial class MainWindow : Window, INotifyPropertyChanged
5354
5455 // undo
5556 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>();
5878
5979 // drawing lines
6080 bool firstPixel = true ;
@@ -98,6 +118,10 @@ public MainWindow()
98118 Start ( ) ;
99119 }
100120
121+ Stack < WriteableBitmap > undoStack = new Stack < WriteableBitmap > ( ) ;
122+ Stack < WriteableBitmap > redoStack = new Stack < WriteableBitmap > ( ) ;
123+ WriteableBitmap currentItem ;
124+
101125 void Start ( )
102126 {
103127 // needed for binding
@@ -157,13 +181,6 @@ void Start()
157181 w . MouseWheel += new MouseWheelEventHandler ( DrawingMouseWheel ) ;
158182 drawingImage . MouseUp += new MouseButtonEventHandler ( DrawingMouseUp ) ;
159183
160- // FIXME init undos
161- for ( int i = 0 ; i < maxUndoCount ; i ++ )
162- {
163- undoBufferBitmap [ i ] = canvasBitmap . Clone ( ) ;
164- }
165-
166-
167184 // build palette
168185 paletteImage = imgPalette ;
169186 RenderOptions . SetBitmapScalingMode ( paletteImage , BitmapScalingMode . NearestNeighbor ) ;
@@ -270,8 +287,10 @@ void DrawPixel(int x, int y)
270287 draw = currentColor ;
271288 break ;
272289 case BlendMode . Additive :
290+ /*
273291 // 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());
275294 // mix colors ADDITIVE mode
276295 int r = (int)(oc.Red + currentColor.Red * (opacity / (float)255));
277296 int g = (int)(oc.Green + currentColor.Green * (opacity / (float)255));
@@ -280,6 +299,7 @@ void DrawPixel(int x, int y)
280299 draw.Green = ClampToByte(g);
281300 draw.Blue = ClampToByte(b);
282301 draw.Alpha = opacity;
302+ */
283303 break ;
284304 default :
285305 break ;
@@ -463,22 +483,16 @@ void DrawingMiddleButtonDown(object sender, MouseButtonEventArgs e)
463483 }
464484 }
465485
486+ // clicked, but not moved
466487 void DrawingLeftButtonDown ( object sender , MouseButtonEventArgs e )
467488 {
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 ) ;
477492
478493 int x = ( int ) ( e . GetPosition ( drawingImage ) . X / canvasScaleX ) ;
479494 int y = ( int ) ( e . GetPosition ( drawingImage ) . Y / canvasScaleX ) ;
480495
481-
482496 switch ( CurrentTool )
483497 {
484498 case ToolMode . Draw :
@@ -504,8 +518,8 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
504518 {
505519 UpdateOutline ( ) ;
506520 }
521+ } // DrawingLeftButtonDown
507522
508- }
509523
510524 void DrawingMouseUp ( object sender , MouseButtonEventArgs e )
511525 {
@@ -624,6 +638,7 @@ void DrawingMouseWheel(object sender, MouseWheelEventArgs e)
624638
625639 private void OnClearButton ( object sender , RoutedEventArgs e )
626640 {
641+ //undoRedoWrapper.AddItem(canvasBitmap.Clone());
627642 ClearImage ( canvasBitmap , emptyRect , emptyPixels , emptyStride ) ;
628643 UpdateOutline ( ) ;
629644 }
@@ -659,7 +674,12 @@ private void OpacitySliderValueChanged(object sender, RoutedPropertyChangedEvent
659674
660675 private void OnUndoButtonDown ( object sender , RoutedEventArgs e )
661676 {
662- CallUndo ( ) ;
677+ DoUndo ( ) ;
678+ }
679+
680+ private void OnRedoButtonDown ( object sender , RoutedEventArgs e )
681+ {
682+ DoRedo ( ) ;
663683 }
664684
665685 private void OnModeSelectionChanged ( object sender , SelectionChangedEventArgs e )
@@ -734,14 +754,37 @@ private void OnKeyUp(object sender, KeyEventArgs e)
734754 }
735755 }
736756
737- private void CallUndo ( )
757+
758+ // restore to previous bitmap
759+ private void DoUndo ( )
738760 {
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+ }
743771 }
744772
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+
745788 void CopyBitmapPixels ( WriteableBitmap source , WriteableBitmap target )
746789 {
747790 byte [ ] data = new byte [ source . BackBufferStride * source . PixelHeight ] ;
@@ -752,14 +795,24 @@ void CopyBitmapPixels(WriteableBitmap source, WriteableBitmap target)
752795
753796 public void Executed_Undo ( object sender , ExecutedRoutedEventArgs e )
754797 {
755- CallUndo ( ) ;
798+ DoUndo ( ) ;
756799 }
757800
758801 public void CanExecute_Undo ( object sender , CanExecuteRoutedEventArgs e )
759802 {
760803 e . CanExecute = true ;
761804 }
762805
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+
763816 void FloodFill ( int x , int y , int fillColor )
764817 {
765818 // get hit color pixel
0 commit comments