-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand.py
144 lines (114 loc) · 3.86 KB
/
command.py
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
"""
This is a module with command pattern, which supports
animation editing in the project.
"""
class CommandList:
"""This class manages commands queue"""
def __init__(self, model):
self.model = model
self.commands = list()
self.last_id = -1
def add_command(self, command):
"""Add command and apply"""
self.commands.append(command)
self.redo()
def undo(self):
"""Revert last command"""
if self.last_id == -1:
return
self.commands[self.last_id].revert()
self.last_id -= 1
self.model.update_views()
def redo(self):
"""Redo next command"""
if self.last_id + 1 == len(self.commands):
return
self.last_id += 1
self.commands[self.last_id].apply(self.model)
self.model.update_views()
def reset(self):
self.commands = list()
self.last_id = -1
class PatchCommand:
"""Command which change an element."""
def __init__(self, opts):
self.opts = opts
self.old_value = None
self.target = None
def apply(self, model):
"""Apply command to model"""
self.target = model.active_element
self.old_value = model.active_element.process_patch(self.opts)
def revert(self):
"""Revert command"""
self.target.process_patch(self.old_value)
class AddBoneCommand:
"""This command add bone to the skeleton"""
def __init__(self, bone):
self.bone = bone
self.target = None
self.added_id = None
def apply(self, model):
"""Apply command to model"""
self.target = model.active_element
self.added_id = model.active_element.number_of_bones
model.active_element.add_bone(self.bone)
def revert(self):
"""Revert command"""
self.target.remove_bone(self.added_id)
class AddSkeletonCommand:
"""This command add a skeleton to the project"""
def __init__(self, skeleton):
self.skeleton = skeleton
self.target = None
self.added_id = None
def apply(self, model):
"""Apply command to model"""
self.target = model.active_element
self.added_id = model.active_element.number_of_skeletons
model.active_element.add_skeleton(self.skeleton)
def revert(self):
"""Revert command"""
self.target.remove_skeleton(self.added_id)
class AddStateCommand:
"""This command add a skeleton to the project"""
def __init__(self, state):
self.state = state
self.target = None
self.added_id = None
def apply(self, model):
"""Apply command to model"""
self.target = model.active_element
self.added_id = model.active_element.number_of_states
model.active_element.add_state(self.state)
def revert(self):
"""Revert command"""
self.target.remove_state(self.added_id)
class AddAnimationCommand:
"""This command add a skeleton to the project"""
def __init__(self, animation):
self.animation = animation
self.target = None
self.added_id = None
def apply(self, model):
"""Apply command to model"""
self.target = model.active_element
self.added_id = model.active_element.number_of_animations
model.active_element.add_animation(self.animation)
def revert(self):
"""Revert command"""
self.target.remove_animation(self.added_id)
class SelectCommand:
"""This command select another element"""
def __init__(self, elem):
self.elem = elem
self.previous = None
self.model = None
def apply(self, model):
"""Apply command to model"""
self.previous = model.active_element
self.model = model
model.active_element = self.elem
def revert(self):
"""Revert command"""
self.model.active_element = self.previous