Mercurial > repos > IBBoard
annotate Commands/CommandStack.cs @ 108:27b6aa1e98e8
Re #12: Document classes and methods
* Document the Constants
(Also - test new Hg repo and hooks)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 14 Aug 2011 14:34:12 +0100 |
parents | 2baf3f949cfb |
children |
rev | line source |
---|---|
16 | 1 // This file (CommandStack.cs) is a part of the IBBoard library and is copyright 2009 IBBoard. |
2 // | |
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. | |
4 | |
37 | 5 using System; |
41
d5dcd1c09c28
* Make sure we reset "cleanPos" in the command stack when we clear it
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
6 using System.Collections.Generic; |
37 | 7 |
8 namespace IBBoard.Commands | |
9 { | |
10 /// <summary> | |
11 /// Summary description for CommandStack. | |
12 /// </summary> | |
13 public class CommandStack | |
14 { | |
41
d5dcd1c09c28
* Make sure we reset "cleanPos" in the command stack when we clear it
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
15 private List<Command> commandStack; |
37 | 16 private int listPos = -1; |
17 private int listMax = -1; | |
18 private int cleanPos; | |
19 | |
20 public event MethodInvoker CommandStackUpdated; | |
21 | |
22 public CommandStack() | |
23 { | |
41
d5dcd1c09c28
* Make sure we reset "cleanPos" in the command stack when we clear it
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
24 commandStack = new List<Command>(10); |
37 | 25 cleanPos = -1; |
26 } | |
27 | |
28 public bool IsDirty() | |
29 { | |
30 return listPos!=cleanPos; | |
31 } | |
32 | |
33 public void setCleanMark() | |
34 { | |
35 cleanPos = listPos; | |
36 } | |
37 | |
38 public bool IsEmpty() | |
39 { | |
40 return listMax == -1; | |
41 } | |
42 | |
43 public bool CanUndo() | |
44 { | |
45 return listPos > -1; | |
46 } | |
47 | |
48 public int UndoLength | |
49 { | |
50 get { return listPos + 1; } | |
51 } | |
52 | |
53 public int RedoLength | |
54 { | |
55 get { return listMax - listPos; } | |
56 } | |
57 | |
58 public bool CanRedo() | |
59 { | |
60 return listPos < listMax; | |
61 } | |
62 | |
63 public void Reset() | |
64 { | |
65 commandStack.Clear(); | |
66 listMax = -1; | |
43
2baf3f949cfb
* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe)
IBBoard <dev@ibboard.co.uk>
parents:
41
diff
changeset
|
67 listPos = -1; |
41
d5dcd1c09c28
* Make sure we reset "cleanPos" in the command stack when we clear it
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
68 cleanPos = -1; |
37 | 69 DoCommandStackUpdated(); |
70 } | |
71 | |
72 public void Undo() | |
73 { | |
74 if (CanUndo()) | |
75 { | |
41
d5dcd1c09c28
* Make sure we reset "cleanPos" in the command stack when we clear it
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
76 commandStack[listPos].Undo(); |
37 | 77 listPos--; |
78 DoCommandStackUpdated(); | |
79 } | |
80 else | |
81 { | |
82 throw new InvalidOperationException("Cannot undo action on empty command stack"); | |
83 } | |
84 } | |
85 | |
86 public void Redo() | |
87 { | |
88 if (CanRedo()) | |
89 { | |
41
d5dcd1c09c28
* Make sure we reset "cleanPos" in the command stack when we clear it
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
90 commandStack[listPos+1].Redo(); |
37 | 91 listPos++; |
92 DoCommandStackUpdated(); | |
93 } | |
94 else | |
95 { | |
96 throw new InvalidOperationException("No actions to redo"); | |
97 } | |
98 } | |
99 | |
100 public bool Execute(Command cmd) | |
101 { | |
102 if (cmd.CanExecute()) | |
103 { | |
104 if (cmd.Execute()) | |
105 { | |
106 //if we can't redo, i.e. there's no commands beyond our current point, add to the end else insert | |
107 if (!CanRedo()) | |
108 { | |
109 commandStack.Add(cmd); | |
110 listPos++; | |
111 } | |
112 else | |
113 { | |
114 if (cleanPos>listPos) | |
115 { | |
116 cleanPos = -2; | |
117 } | |
118 | |
119 //else overwrite at our current position, setting the listMax value will ignore any commands we could have redone beyond here | |
120 commandStack[++listPos] = cmd; | |
121 } | |
122 | |
123 listMax = listPos; | |
124 DoCommandStackUpdated(); | |
125 return true; | |
126 } | |
127 else | |
128 { | |
129 throw new InvalidOperationException("Executable command failed to execute"); | |
130 } | |
131 } | |
132 else | |
133 { | |
134 return false; | |
135 } | |
136 } | |
137 | |
138 protected void DoCommandStackUpdated() | |
139 { | |
140 if (CommandStackUpdated!=null) | |
141 { | |
142 CommandStackUpdated(); | |
143 } | |
144 } | |
145 | |
146 public Command PeekUndoCommand() | |
147 { | |
148 return PeekUndoCommand(1); | |
149 } | |
150 | |
151 public Command PeekUndoCommand(int backCount) | |
152 { | |
153 backCount = backCount - 1; | |
154 if (backCount > -1 && backCount <= listPos) | |
155 { | |
41
d5dcd1c09c28
* Make sure we reset "cleanPos" in the command stack when we clear it
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
156 return commandStack[listPos-backCount]; |
37 | 157 } |
158 else | |
159 { | |
160 return null; | |
161 } | |
162 } | |
163 | |
164 public Command PeekRedoCommand() | |
165 { | |
166 return PeekRedoCommand(1); | |
167 } | |
168 | |
169 public Command PeekRedoCommand(int forwardCount) | |
170 { | |
171 if (forwardCount > 0 && listPos+forwardCount <= listMax) | |
172 { | |
41
d5dcd1c09c28
* Make sure we reset "cleanPos" in the command stack when we clear it
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
173 return commandStack[listPos+forwardCount]; |
37 | 174 } |
175 else | |
176 { | |
177 return null; | |
178 } | |
179 } | |
180 } | |
181 } |