forked from LegacyNsfw/TimingEditor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CommandHistory.cs
203 lines (169 loc) · 5.1 KB
/
CommandHistory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NSFW.TimingEditor
{
public abstract class Command
{
public abstract void Execute();
public abstract void Undo();
}
public class EditCell : Command
{
private ITable table;
private double oldValue;
private double newValue;
private int x;
private int y;
public ITable Table { get { return this.table; } }
public int Y { get { return this.y; } }
public int X { get { return this.x; } }
public double OldValue { get { return this.oldValue; } }
public double NewValue { get { return this.newValue; } }
public EditCell(ITable table, int x, int y, double newValue)
{
this.table = table;
this.x = x;
this.y = y;
this.newValue = newValue;
this.oldValue = this.table.GetCell(this.x, this.y);
}
public override void Execute()
{
this.table.SetCell(this.x, this.y, this.newValue);
}
public override void Undo()
{
this.table.SetCell(this.x, this.y, this.oldValue);
}
}
/* public class EditMultipleCells : Command
{
private IList<EditCell> cells;
public EditMultipleCells(IList<EditCell> cells)
{
this.cells = cells;
}
public override void Execute()
{
foreach (EditCell cell in this.cells)
{
cell.Execute();
}
}
public override void Undo()
{
foreach (EditCell cell in this.cells)
{
cell.Undo();
}
}
}
public class Paste : Command
{
private ITable source;
private ITable destination;
private ITable backup;
public Paste(ITable source, ITable destination)
{
this.source = source;
this.destination = destination;
this.backup = destination.Clone();
}
public override void Execute()
{
this.source.CopyTo(destination);
}
public override void Undo()
{
this.backup.CopyTo(this.destination);
}
}
public class DoublePaste : Command
{
private Paste initial;
private Paste modified;
public DoublePaste(Paste initial, Paste modified)
{
this.initial = initial;
this.modified = modified;
}
public override void Execute()
{
this.initial.Execute();
this.modified.Execute();
}
public override void Undo()
{
this.initial.Undo();
this.modified.Undo();
}
}
*/
public delegate void UpdateCommandHistoryButtons(object sender, EventArgs args);
public class CommandHistory
{
private static CommandHistory instance = new CommandHistory();
private List<Command> commands;
private List<Command> undone;
public event UpdateCommandHistoryButtons UpdateCommandHistoryButtons;
private CommandHistory()
{
this.commands = new List<Command>();
this.undone = new List<Command>();
}
public static CommandHistory Instance
{
[System.Diagnostics.DebuggerStepThrough]
get
{
return instance;
}
}
public bool CanUndo { get { return this.commands.Count > 0; } }
public bool CanRedo { get { return this.undone.Count > 0; } }
public void Execute(Command command)
{
command.Execute();
this.commands.Add(command);
this.undone.Clear();
this.UpdateButtons();
}
public Command Undo()
{
if (this.commands.Count == 0)
{
return null;
}
int lastIndex = this.commands.Count - 1;
Command command = this.commands[lastIndex];
this.commands.RemoveAt(lastIndex);
command.Undo();
this.undone.Add(command);
this.UpdateButtons();
return command;
}
public Command Redo()
{
if (this.undone.Count == 0)
{
return null;
}
int lastIndex = this.undone.Count - 1;
Command command = this.undone[lastIndex];
this.undone.RemoveAt(lastIndex);
command.Execute();
this.commands.Add(command);
this.UpdateButtons();
return command;
}
private void UpdateButtons()
{
if (this.UpdateCommandHistoryButtons != null)
{
this.UpdateCommandHistoryButtons(this, new EventArgs());
}
}
}
}