Skip to content

Commit 55dec66

Browse files
committed
testing undo (not working yet)
1 parent a00c8db commit 55dec66

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

PixelArtTool/MainWindow.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939
<Slider x:Name="sliderOpacity" ToolTip="Opacity" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="50" Height="28" Value="255" Maximum="255" LargeChange="0" SmallChange="1" ValueChanged="OpacitySliderValueChanged"/>
4040
</ToolBar>
4141

42+
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top">
43+
<Button x:Name="btnUndo" ToolTip="Undo" Click="OnUndoButtonDown">
44+
<Image Source="/Resources/Buttons/emptybutton.png" />
45+
</Button>
46+
<Button x:Name="btnRedo" ToolTip="Redo">
47+
<Image Source="/Resources/Buttons/emptybutton.png" />
48+
</Button>
49+
</ToolBar>
50+
4251
</ToolBarTray>
4352

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

PixelArtTool/MainWindow.xaml.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public partial class MainWindow : Window
4444
int prevX;
4545
int prevY;
4646

47+
// undo
48+
const int maxUndoCount = 100;
49+
int currentUndoIndex = 0;
50+
WriteableBitmap[] undoBufferBitmap = new WriteableBitmap[maxUndoCount];
4751

4852
public MainWindow()
4953
{
@@ -68,6 +72,7 @@ void Start()
6872
drawingImage.MouseRightButtonDown += new MouseButtonEventHandler(DrawingRightButtonDown);
6973
drawingImage.MouseDown += new MouseButtonEventHandler(DrawingMiddleButtonDown);
7074
w.MouseWheel += new MouseWheelEventHandler(drawingMouseWheel);
75+
drawingImage.MouseUp += new MouseButtonEventHandler(DrawingMouseUp);
7176

7277
// build palette
7378
paletteImage = imgPalette;
@@ -221,7 +226,6 @@ void DrawPixel(int x, int y)
221226

222227
prevX = x;
223228
prevY = y;
224-
225229
}
226230

227231
void ErasePixel(MouseEventArgs e)
@@ -306,6 +310,14 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
306310
DrawPixel(x, y);
307311
}
308312

313+
void DrawingMouseUp(object sender, MouseButtonEventArgs e)
314+
{
315+
// undo test
316+
undoBufferBitmap[++currentUndoIndex] = canvasBitmap.Clone();
317+
Console.WriteLine("save undo " + currentUndoIndex);
318+
}
319+
320+
309321
void DrawingAreaMouseMoved(object sender, MouseEventArgs e)
310322
{
311323
// update mousepos info
@@ -421,5 +433,15 @@ private void OpacitySliderValueChanged(object sender, RoutedPropertyChangedEvent
421433
// ... Set Window Title.
422434
//this.Title = "Value: " + value.ToString("0.0") + "/" + slider.Maximum;
423435
}
436+
437+
private void OnUndoButtonDown(object sender, RoutedEventArgs e)
438+
{
439+
if (currentUndoIndex > 0)
440+
{
441+
canvasBitmap = undoBufferBitmap[--currentUndoIndex];
442+
Console.WriteLine("restore undo " + currentUndoIndex);
443+
imgCanvas.Source = canvasBitmap;
444+
}
445+
}
424446
} // class
425447
} // namespace

0 commit comments

Comments
 (0)