forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsceneitems.py
More file actions
137 lines (104 loc) · 2.71 KB
/
Copy pathsceneitems.py
File metadata and controls
137 lines (104 loc) · 2.71 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
from collections import deque
class Item(object):
def __init__(self, name, color='red', pos=(0, 0)):
self.name = name
self.color = color
self.pos = pos
def __repr__(self):
return "<Item {0} {1} {2} {3}>".format(self.name, self.color, *self.pos)
def setColor(self, color):
self.color = color
def setPos(self, pos):
self.pos = pos
class Scene(object):
def __init__(self):
self.items = []
def addItem(self, item):
self.items.append(item)
def removeItem(self, item):
self.items.remove(item)
##
class CommandQueue(object):
def __init__(self):
self._undo = deque()
self._redo = deque()
def undo_queue(self, command):
self._undo.append(command)
print self._undo
def redo_queue(self, command):
self._redo.append(command)
print self._redo
def undo(self):
command = self._undo.pop()
print self._undo
self.redo_queue(command)
return command
def redo(self):
command = self._redo.pop()
print self._redo
self.undo_queue(command)
return command
cq = CommandQueue()
cq.undo()
cq.redo()
cq.undo_queue("add-item")
cq.undo_queue("set-color")
cq.undo_queue("set-pos")
cq.undo()
cq.redo()
##
class Command(object):
def __repr__(self):
return "<Cmd {0}>".format(self.cmd_type)
class AddItemCommand(Command):
cmd_type = 'add-item'
def __init__(self, ctx):
self.ctx = ctx
self.item = None
def redo(self):
self.item = Item('unnamed')
self.ctx.scene.addItem(self.item)
self.ctx.item = self.item
def undo(self):
self.ctx.scene.removeItem(self.item)
self.item = None
self.ctx.item = None
class SetColorCommand(Command):
cmd_type = 'set-color'
def __init__(self, ctx, color):
self.ctx = ctx
self.color = color
self.prevColor = self.ctx.item.color
def redo(self):
self.ctx.item.setColor(self.color)
def undo(self):
self.ctx.item.setColor(self.prevColor)
##
scene = Scene()
ctx = Context()
commandqueue = CommandQueue()
ctx.scene = scene
addItemCmd = AddItemCommand(ctx)
addItemCmd.redo()
ctx.scene.items
ctx.item
commandqueue.undo_queue(addItemCmd)
# ctx.scene.items
# ctx.item = ctx.scene.items[0]
setColorCmd = SetColorCommand(ctx, 'blue')
setColorCmd.redo()
ctx.item
commandqueue.undo_queue(setColorCmd)
cmd = commandqueue.undo()
cmd.undo()
ctx.item
cmd = commandqueue.undo()
cmd.undo()
ctx.scene.items
ctx.item
commandqueue.undo()
commandqueue.redo().redo()
ctx.item
ctx.scene.items
commandqueue.redo().redo()
ctx.item