-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandHandler.cs
More file actions
57 lines (40 loc) · 1.22 KB
/
CommandHandler.cs
File metadata and controls
57 lines (40 loc) · 1.22 KB
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
using GraphicalEditor.Interfaces;
using System.Collections.Generic;
namespace GraphicalEditor
{
class CommandHandler
{
private Stack<ICommand> executeStack = new Stack<ICommand>();
private Stack<ICommand> undoStack = new Stack<ICommand>();
public event OnExecuteDel OnExecute;
public delegate void OnExecuteDel(ICommand command);
public event OnRedoDel OnUndo;
public delegate void OnRedoDel(ICommand command);
public void AddCommand(ICommand commandToAdd)
{
executeStack.Push(commandToAdd);
//Also execute the command when added.
Redo();
}
public void Undo()
{
if (undoStack.Count == 0)
return;
ICommand cmd = undoStack.Pop();
cmd.Undo();
if(OnUndo != null)
OnUndo(cmd);
executeStack.Push(cmd);
}
public void Redo()
{
if (executeStack.Count == 0)
return;
ICommand cmd = executeStack.Pop();
cmd.Execute();
if(OnExecute != null)
OnExecute(cmd);
undoStack.Push(cmd);
}
}
}