Skip to content

Commit a515d89

Browse files
committed
clear old references, Redo undo and redo system
1 parent 04c39d2 commit a515d89

File tree

3 files changed

+84
-53
lines changed

3 files changed

+84
-53
lines changed

PixelArtTool/MainWindow.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
Title="PixelArtTool" Height="412.222" Width="739.444" Background="#FF252526" KeyDown="OnKeyDown" KeyUp="OnKeyUp">
1010
<Window.CommandBindings>
1111
<CommandBinding Command="ApplicationCommands.Undo" Executed="Executed_Undo" CanExecute="CanExecute_Undo"/>
12+
<CommandBinding Command="ApplicationCommands.Redo" Executed="Executed_Redo" CanExecute="CanExecute_Redo"/>
1213
</Window.CommandBindings>
1314
<Window.InputBindings>
1415
<KeyBinding Command="ApplicationCommands.Undo" Gesture="Ctrl+Z"/>
16+
<KeyBinding Command="ApplicationCommands.Redo" Gesture="Ctrl+Y"/>
1517
</Window.InputBindings>
1618
<Window.Resources>
1719

@@ -131,7 +133,7 @@
131133
<Button x:Name="btnUndo" ToolTip="Undo" Click="OnUndoButtonDown">
132134
<Image Source="/Resources/Buttons/emptybutton.png" />
133135
</Button>
134-
<Button x:Name="btnRedo" ToolTip="Redo">
136+
<Button x:Name="btnRedo" ToolTip="Redo" Click="OnRedoButtonDown">
135137
<Image Source="/Resources/Buttons/emptybutton.png" />
136138
</Button>
137139
</ToolBar>

PixelArtTool/MainWindow.xaml.cs

Lines changed: 81 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Win32;
22
using System;
3+
using System.Collections;
34
using System.Collections.Generic;
45
using System.ComponentModel;
56
using 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

PixelArtTool/PixelArtTool.csproj

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,6 @@
4646
<Reference Include="System.Xaml">
4747
<RequiredTargetFramework>4.0</RequiredTargetFramework>
4848
</Reference>
49-
<Reference Include="Telerik.Windows.Controls, Version=2018.3.1016.45, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL">
50-
<HintPath>..\lib\RCWPF\2018.3.1016.45.Trial\Telerik.Windows.Controls.dll</HintPath>
51-
<Private>True</Private>
52-
</Reference>
53-
<Reference Include="Telerik.Windows.Controls.FileDialogs, Version=2018.3.1016.45, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL">
54-
<HintPath>..\lib\RCWPF\2018.3.1016.45.Trial\Telerik.Windows.Controls.FileDialogs.dll</HintPath>
55-
<Private>True</Private>
56-
</Reference>
57-
<Reference Include="Telerik.Windows.Controls.GridView, Version=2018.3.1016.45, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL">
58-
<HintPath>..\lib\RCWPF\2018.3.1016.45.Trial\Telerik.Windows.Controls.GridView.dll</HintPath>
59-
<Private>True</Private>
60-
</Reference>
61-
<Reference Include="Telerik.Windows.Controls.Input, Version=2018.3.1016.45, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL">
62-
<HintPath>..\lib\RCWPF\2018.3.1016.45.Trial\Telerik.Windows.Controls.Input.dll</HintPath>
63-
<Private>True</Private>
64-
</Reference>
65-
<Reference Include="Telerik.Windows.Controls.Navigation, Version=2018.3.1016.45, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL">
66-
<HintPath>..\lib\RCWPF\2018.3.1016.45.Trial\Telerik.Windows.Controls.Navigation.dll</HintPath>
67-
<Private>True</Private>
68-
</Reference>
69-
<Reference Include="Telerik.Windows.Data, Version=2018.3.1016.45, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL">
70-
<HintPath>..\lib\RCWPF\2018.3.1016.45.Trial\Telerik.Windows.Data.dll</HintPath>
71-
<Private>True</Private>
72-
</Reference>
7349
<Reference Include="WindowsBase" />
7450
<Reference Include="PresentationCore" />
7551
<Reference Include="PresentationFramework" />

0 commit comments

Comments
 (0)