comparison pyink/domview.py @ 1469:c1e70540541c

Drawing functions for states and transitions
author Thinker K.F. Li <thinker@codemud.net>
date Sun, 24 Apr 2011 12:30:47 +0800
parents c586981ecf1a
children 2a9b9c281f80
comparison
equal deleted inserted replaced
1468:c586981ecf1a 1469:c1e70540541c
51 class Transition(object): 51 class Transition(object):
52 node = None 52 node = None
53 condition = None 53 condition = None
54 target = None 54 target = None
55 action = None 55 action = None
56 path = None
56 57
57 def __init__(self, node=None): 58 def __init__(self, node=None):
58 self.node = node 59 self.node = node
59 pass 60 pass
60 61
67 68
68 def set_action(self, action): 69 def set_action(self, action):
69 self.action = action 70 self.action = action
70 node = self.node 71 node = self.node
71 node.setAttribute('action', action) 72 node.setAttribute('action', action)
73 pass
74
75 def set_path(self, path):
76 self.paht = path
77 node = self.node
78 path_txt = ' '.join([str(c) for c in path.strip().split()])
79 node.setAttribute('path', path_txt)
72 pass 80 pass
73 81
74 def reparse(self): 82 def reparse(self):
75 condition = node.getAttribute('condition') 83 condition = node.getAttribute('condition')
76 target = node.getAttribute('target') 84 target = node.getAttribute('target')
77 try: 85 try:
78 action = node.getAttribute('action') 86 action = node.getAttribute('action')
79 except: 87 except:
80 action = None 88 action = None
81 pass 89 pass
90 try:
91 path = node.getAttribute('path').strip().split()
92 path = [float(c) for c in path]
93 except:
94 path = None
95 pass
82 96
83 self.condition = condition 97 self.condition = condition
84 self.target = target 98 self.target = target
85 self.action = action 99 self.action = action
100 self.path = path
86 pass 101 pass
87 102
88 def update_node(self): 103 def update_node(self):
89 node = self.node 104 node = self.node
90 node.setAttribute('condition', self.condition) 105 node.setAttribute('condition', self.condition)
91 node.setAttribute('target', self.target) 106 node.setAttribute('target', self.target)
92 if self.action: 107 if self.action:
93 node.setAttribute('action', self.action) 108 node.setAttribute('action', self.action)
109 pass
110 if self.path:
111 node.setAttribute('path', self.path)
94 pass 112 pass
95 pass 113 pass
96 114
97 ## \brief Create a node according status of the object. 115 ## \brief Create a node according status of the object.
98 # 116 #
134 # 152 #
135 class State(object): 153 class State(object):
136 name = None 154 name = None
137 entry_action = None 155 entry_action = None
138 transitions = None 156 transitions = None
157 r = None
158 x = 0
159 y = 0
139 160
140 def __init__(self, node=None): 161 def __init__(self, node=None):
141 self.node = node 162 self.node = node
142 self.transitions = {} 163 self.transitions = {}
143 pass 164 pass
148 pass 169 pass
149 170
150 def set_entry_action(self, action): 171 def set_entry_action(self, action):
151 self.entry_action = action 172 self.entry_action = action
152 self.node.setAttribute('entry_action', action) 173 self.node.setAttribute('entry_action', action)
174 pass
175
176 def set_r(self, r):
177 self.r = r
178 self.node.setAttribute('r', str(r))
179 pass
180
181 def set_xy(self, x, y):
182 self.x = x
183 self.y = y
184 self.node.setAttribute('x', str(x))
185 self.node.setAttribute('y', str(y))
153 pass 186 pass
154 187
155 def reparse(self): 188 def reparse(self):
156 node = self.node 189 node = self.node
157 190
159 try: 192 try:
160 entry_action = node.getAttribute('entry_action') 193 entry_action = node.getAttribute('entry_action')
161 except: 194 except:
162 entry_action = None 195 entry_action = None
163 pass 196 pass
197 try:
198 r = float(node.getAttribute('r'))
199 except:
200 r = None
201 pass
202 try:
203 x = float(node.getAttribute('x'))
204 except:
205 x = 0
206 pass
207 try:
208 y = float(node.getAttribute('y'))
209 except:
210 y = 0
211 pass
164 212
165 all_transitions = [Transition.parse_transition(child) 213 all_transitions = [Transition.parse_transition(child)
166 for child in node.childList() 214 for child in node.childList()
167 if child.name() == 'ns0:transition'] 215 if child.name() == 'ns0:transition']
168 transitions = dict([(trn.condition, trn) 216 transitions = dict([(trn.condition, trn)
169 for trn in all_transitions]) 217 for trn in all_transitions])
170 218
171 self.name = name 219 self.name = name
172 self.transitions = transitions 220 self.transitions = transitions
173 self.entry_action = entry_action 221 self.entry_action = entry_action
222 self.r = r
223 self.x = x
224 self.y = y
174 pass 225 pass
175 226
176 def update_node(self): 227 def update_node(self):
177 node = self.node 228 node = self.node
178 229
179 node.setAttribute('name', self.name) 230 node.setAttribute('name', self.name)
180 if self.entry_action: 231 if self.entry_action:
181 node.setAttribute('entry_action', self.entry_action) 232 node.setAttribute('entry_action', self.entry_action)
233 pass
234 if self.r:
235 node.setAttribute('r', self.r)
236 pass
237 if self.x:
238 node.setAttribute('x', self.x)
239 pass
240 if self.y:
241 node.setAttribute('y', self.y)
182 pass 242 pass
183 243
184 transitions = self.transitions 244 transitions = self.transitions
185 for trn in transitions: 245 for trn in transitions:
186 trn.update_node() 246 trn.update_node()
1048 def set_state_entry_action(self, state_name, entry_action): 1108 def set_state_entry_action(self, state_name, entry_action):
1049 state = self._get_state(state_name) 1109 state = self._get_state(state_name)
1050 state.set_entry_action(entry_action) 1110 state.set_entry_action(entry_action)
1051 pass 1111 pass
1052 1112
1113 def set_state_r(self, state_name, r):
1114 state = self._get_state(state_name)
1115 state.set_r(r)
1116 pass
1117
1118 def set_state_xy(self, state_name, x, y):
1119 state = self._get_state(state_name)
1120 state.set_xy(x, y)
1121 pass
1122
1123 def get_state_entry_action(self, state_name):
1124 state = self._get_state(state_name)
1125 action = state.get_entry_action()
1126 return action
1127
1128 def get_state_r(self, state_name):
1129 state = self._get_state(state_name)
1130 r = state.r
1131 return r
1132
1133 def get_state_xy(self, state_name):
1134 state = self._get_state(state_name)
1135 xy = state.x, state.y
1136 return xy
1137
1053 def all_transitions(self, state_name): 1138 def all_transitions(self, state_name):
1054 state = self._get_state(state_name) 1139 state = self._get_state(state_name)
1055 trn_names = state.all_transitions() 1140 trn_names = state.all_transitions()
1056 return trn_names 1141 return trn_names
1057 1142
1075 trn = state.get_transition(cond) 1160 trn = state.get_transition(cond)
1076 1161
1077 cond = trn.condition 1162 cond = trn.condition
1078 target = trn.target 1163 target = trn.target
1079 action = trn.action 1164 action = trn.action
1080 1165 path = trn.path
1081 return cond, target, action 1166
1167 return cond, target, action, path
1082 1168
1083 def set_transition_action(self, state_name, cond, action): 1169 def set_transition_action(self, state_name, cond, action):
1084 trn = state.get_transition(state_name, cond) 1170 trn = state.get_transition(state_name, cond)
1085 trn.set_action(action) 1171 trn.set_action(action)
1172 pass
1173
1174 def set_transition_path(self, state_name, cond, path):
1175 trn = state.get_transition(state_name, cond)
1176 trn.set_path(path)
1086 pass 1177 pass
1087 pass 1178 pass
1088 1179
1089 1180
1090 ## \brief Parser for scenes nodes. 1181 ## \brief Parser for scenes nodes.