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