forked from NSMBW-Community/Reggie-Next
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathundo.py
More file actions
265 lines (224 loc) · 7.65 KB
/
undo.py
File metadata and controls
265 lines (224 loc) · 7.65 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import globals_
class UndoStack:
"""
A stack you can push UndoActions on, and stuff.
"""
def __init__(self):
self.pastActions = []
self.futureActions = []
def addAction(self, act):
"""
Adds an action to the stack
"""
self.pastActions.append(act)
self.futureActions = []
self.enableOrDisableMenuItems()
def addOrExtendAction(self, act):
"""
Adds an action to the stack, or extends the current one if applicable
"""
if len(self.pastActions) > 0 and self.pastActions[-1].isExtentionOf(act):
self.pastActions[-1].extend(act)
self.enableOrDisableMenuItems()
else:
self.addAction(act)
def undo(self):
"""
Undoes the last action
"""
if len(self.pastActions) == 0: return
act = self.pastActions.pop()
while act.isNull():
# Keep popping null actions off
if len(self.pastActions) == 0:
return
act = self.pastActions.pop()
act.undo()
self.futureActions.append(act)
self.enableOrDisableMenuItems()
def redo(self):
"""
Redoes the last undone action
"""
if len(self.futureActions) == 0: return
act = self.futureActions.pop()
while act.isNull():
# Keep popping null actions off
act = self.futureActions.pop()
act.redo()
self.pastActions.append(act)
self.enableOrDisableMenuItems()
def enableOrDisableMenuItems(self):
"""
Enables or disables the menu items of mainWindow
"""
globals_.mainWindow.actions['undo'].setEnabled(len(self.pastActions) > 0)
globals_.mainWindow.actions['redo'].setEnabled(len(self.futureActions) > 0)
class UndoAction:
"""
Abstract undo action
"""
def undo(self):
"""
Sets the target to its initial state
"""
pass
def redo(self):
"""
Sets the target to its final state
"""
pass
def isExtentionOf(self, other):
"""
Returns True if this action extends another, else False
"""
return False
def extend(self, other):
"""
Extends this UndoAction with the data from an extention of it.
isExtentionOf must have returned True first!
"""
pass
def isNull(self):
"""
Returns True if this action is effectively a no-op
"""
return True
class MoveItemUndoAction(UndoAction):
"""
An UndoAction for movement of a single level item that is not an object
"""
def __init__(self, target, origX, origY, finalX, finalY):
"""
Initializes the undo action
"""
defType = target.instanceDef
self.origDef = defType(target)
self.finalDef = defType(target)
self.origDef.objx = origX
self.origDef.objy = origY
self.finalDef.objx = finalX
self.finalDef.objy = finalY
def undo(self):
"""
Sets the target object's position to the original position
"""
instance = self.finalDef.findInstance()
if instance:
self.changeObjectPos(instance, self.origDef.objx, self.origDef.objy)
else:
print('Undo Move Item: Cannot find item instance! ' + str(self.finalDef))
def redo(self):
"""
Sets the target object's position to the final position
"""
instance = self.origDef.findInstance()
if instance:
self.changeObjectPos(instance, self.finalDef.objx, self.finalDef.objy)
else:
print('Redo Move Item: Cannot find item instance! ' + str(self.origDef))
@staticmethod
def changeObjectPos(object, newX, newY):
"""
Changes the position of an object
"""
# This causes a circular import
return
# oldBR = object.getFullRect()
# if isinstance(object, SpriteItem):
# # Sprites are weird so they handle this themselves
# object.setNewObjPos(newX, newY)
# elif isinstance(object, ObjectItem):
# # Objects use the objx and objy properties differently
# object.objx, object.objy = newX, newY
# object.setPos(newX * 24, newY * 24)
# else:
# # Everything else is normal
# object.objx, object.objy = newX, newY
# object.setPos(newX * 1.5, newY * 1.5)
# newBR = object.getFullRect()
# globals_.mainWindow.scene.update(oldBR)
# globals_.mainWindow.scene.update(newBR)
# if isinstance(object, PathItem):
# object.updatePos()
# object.pathinfo['peline'].nodePosChanged()
def isExtentionOf(self, other):
"""
Returns True if this MoveItemUndoAction extends another
"""
return hasattr(other, 'origDef') and self.origDef.defMatchesData(other.origDef)
def extend(self, other):
"""
Extends this MoveItemUndoAction with the data from an extention of it.
isExtentionOf must have returned True first!
"""
self.finalDef.objx = other.finalDef.objx
self.finalDef.objy = other.finalDef.objy
def isNull(self):
"""
Returns True if this action is effectively a no-op
"""
matches = True
matches = matches and abs(self.origDef.objx - self.finalDef.objx) <= 2
matches = matches and abs(self.origDef.objy - self.finalDef.objy) <= 2
return matches
class SimultaneousUndoAction(UndoAction):
"""
An undo action that consists of multiple undo actions at once
"""
def __init__(self, children):
"""
Initializes the undo action
"""
self.children = set(children)
def undo(self):
"""
Calls undo() on all children
"""
for c in self.children:
c.undo()
def redo(self):
"""
Calls redo() on all children
"""
for c in self.children:
c.redo()
def isExtentionOf(self, other):
"""
Returns True if this SinultaneousUndoAction and another one have equivalent children
"""
if not hasattr(other, 'children'): return False
searchIn = set(self.children)
searchAgainst = set(other.children)
for searchInObj in searchIn:
found = False
for searchAgainstObj in searchAgainst:
if searchAgainstObj.isExtentionOf(searchInObj):
found = True
searchAgainst.remove(searchAgainstObj)
break # only breaks out of inner loop
if not found:
return False
return True
def extend(self, other):
"""
Extend this SimultaneousUndoAction with the data from an extention of it.
isExtentionOf must have returned True first!
"""
searchMine = set(self.children)
searchOther = set(other.children)
for searchMineObj in searchMine:
for searchOtherObj in searchOther:
if searchOtherObj.isExtentionOf(searchMineObj):
searchMineObj.extend(searchOtherObj)
searchOther.remove(searchOtherObj)
break # only breaks out of inner loop
def isNull(self):
"""
Returns True if this action is effectively a no-op
"""
# Hopefully this code is easy enough for you to follow.
anythingIsDifferent = False
for c in self.children:
anythingIsDifferent = anythingIsDifferent or not c.isNull()
return not anythingIsDifferent