Mercurial > MadButterfly
comparison pyink/MBScene.py @ 1236:2eeac97853eb
Running animation successful
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Mon, 10 Jan 2011 11:45:13 +0800 |
parents | 84e7b1c1892b |
children | b5cceb2b87bb |
comparison
equal
deleted
inserted
replaced
1235:84e7b1c1892b | 1236:2eeac97853eb |
---|---|
114 addEventListener(doc, 'DOMNodeRemoved', self._on_remove_node, None) | 114 addEventListener(doc, 'DOMNodeRemoved', self._on_remove_node, None) |
115 addEventListener(doc, 'DOMAttrModified', self._on_attr_modified, None) | 115 addEventListener(doc, 'DOMAttrModified', self._on_attr_modified, None) |
116 pass | 116 pass |
117 | 117 |
118 def _on_insert_node(self, node, child): | 118 def _on_insert_node(self, node, child): |
119 for cchild in child.childList(): | |
120 self._on_insert_node(child, cchild) | |
121 pass | |
122 | |
119 try: | 123 try: |
120 child_id = child.getAttribute('id') | 124 child_id = child.getAttribute('id') |
121 except: | 125 except: |
122 pass | 126 pass |
123 else: | 127 else: |
168 pass | 172 pass |
169 pass | 173 pass |
170 return maxframe | 174 return maxframe |
171 | 175 |
172 def _on_remove_node(self, node, child): | 176 def _on_remove_node(self, node, child): |
177 for cchild in child.childList(): | |
178 self._on_remove_node(child, cchild) | |
179 pass | |
180 | |
173 try: | 181 try: |
174 child_id = child.getAttribute('id') | 182 child_id = child.getAttribute('id') |
175 except: | 183 except: |
176 pass | 184 pass |
177 else: | 185 else: |
632 else: # this scene is covered by removing range | 640 else: # this scene is covered by removing range |
633 self.rm_scene_node_n_group(scene_node) | 641 self.rm_scene_node_n_group(scene_node) |
634 pass | 642 pass |
635 pass | 643 pass |
636 pass | 644 pass |
645 | |
646 def get_max_frame(self): | |
647 return self._maxframe | |
637 pass | 648 pass |
638 | 649 |
639 ## \brief Maintain frameline list for MBScene. | 650 ## \brief Maintain frameline list for MBScene. |
640 # | 651 # |
641 class MBScene_framelines(object): | 652 class MBScene_framelines(object): |
642 _frameline_tween_types = (frameline.TWEEN_TYPE_NONE, | 653 _frameline_tween_types = (frameline.TWEEN_TYPE_NONE, |
643 frameline.TWEEN_TYPE_SHAPE) | 654 frameline.TWEEN_TYPE_SHAPE) |
644 | 655 _num_frames_of_line = 100 |
656 | |
645 _framelines = None | 657 _framelines = None |
646 | 658 |
647 def __init__(self, *args, **kws): | 659 def __init__(self, *args, **kws): |
648 super(MBScene_framelines, self).__init__(*args, **kws) | 660 super(MBScene_framelines, self).__init__(*args, **kws) |
649 | 661 |
824 return start, end, tween_type | 836 return start, end, tween_type |
825 | 837 |
826 def get_all_key_tween_of_layer(self, layer_idx): | 838 def get_all_key_tween_of_layer(self, layer_idx): |
827 frameline = self._framelines[layer_idx] | 839 frameline = self._framelines[layer_idx] |
828 info = frameline.get_frame_blocks() | 840 info = frameline.get_frame_blocks() |
829 return info | 841 tweens = [(tween[0], tween[1], |
842 self._frameline_tween_types.index(tween[2])) | |
843 for tween in info] | |
844 return tweens | |
830 | 845 |
831 ## \brief Tweening key frame to a give size | 846 ## \brief Tweening key frame to a give size |
832 # | 847 # |
833 # The tween can be changed by tweening it again. | 848 # The tween can be changed by tweening it again. |
834 def tween(self, layer_idx, key_frame_idx, tween_len, | 849 def tween(self, layer_idx, key_frame_idx, tween_len, |
836 assert tween_len > 0 | 851 assert tween_len > 0 |
837 frameline = self._framelines[layer_idx] | 852 frameline = self._framelines[layer_idx] |
838 right_frame_idx = key_frame_idx + tween_len - 1 | 853 right_frame_idx = key_frame_idx + tween_len - 1 |
839 fl_tween_type = self._frameline_tween_types[tween_type] | 854 fl_tween_type = self._frameline_tween_types[tween_type] |
840 | 855 |
841 start, end, fl_tween_type = frameline.get_frame_block(frame_idx) | 856 start, end, old_fl_tween_type = \ |
857 frameline.get_frame_block(key_frame_idx) | |
842 if start != key_frame_idx: | 858 if start != key_frame_idx: |
843 ValueError, 'invalid key frame (%d)' % (key_frame_idx) | 859 ValueError, 'invalid key frame (%d)' % (key_frame_idx) |
844 if start < end: | 860 if start < end: |
845 frameline.rm_keyframe(end) | 861 frameline.rm_keyframe(end) |
846 pass | 862 pass |
964 | 980 |
965 ## \brief Update content of a frameline from scenes of respective layer. | 981 ## \brief Update content of a frameline from scenes of respective layer. |
966 # | 982 # |
967 def _update_frameline_content(self, layer_idx): | 983 def _update_frameline_content(self, layer_idx): |
968 fl_mgr = self._fl_mgr | 984 fl_mgr = self._fl_mgr |
969 scene_nodes = fl_mgr.get_all_scene_node_of_layer(layer_idx) | 985 scene_nodes = self._dom.get_all_scene_node_of_layer(layer_idx) |
970 for scene_node in scene_nodes: | 986 for scene_node in scene_nodes: |
971 start, end, tween_name = self._parse_one_scene(scene_node) | 987 start, end, tween_name = self._dom._parse_one_scene(scene_node) |
972 | 988 |
973 fl_mgr.add_keyframe(layer_idx, start) | 989 fl_mgr.add_keyframe(layer_idx, start) |
974 fl_mgr.set_keyframe_data(layer_idx, start, scene_node) | 990 fl_mgr.set_keyframe_data(layer_idx, start, scene_node) |
975 if start != end: | 991 if start != end: |
976 tween_type = self._tween_type_names.index(tween_name) | 992 tween_type = self._tween_type_names.index(tween_name) |
977 fl_mgr.tween(start, end, tween_type) | 993 tween_len = end - start + 1 |
994 fl_mgr.tween(layer_idx, start, tween_len, tween_type) | |
978 pass | 995 pass |
979 pass | 996 pass |
980 pass | 997 pass |
981 | 998 |
982 ## \brief Add a frameline for every found layer. | 999 ## \brief Add a frameline for every found layer. |
1099 ## \brief Return associated group node for a key frame. | 1116 ## \brief Return associated group node for a key frame. |
1100 # | 1117 # |
1101 # The given frame index must be exactly a key frame. | 1118 # The given frame index must be exactly a key frame. |
1102 # | 1119 # |
1103 def get_keyframe_group(self, layer_idx, frame_idx): | 1120 def get_keyframe_group(self, layer_idx, frame_idx): |
1104 scene_node = self._fl_mgr.get_keyframe_data(frame_idx) | 1121 scene_node = self._fl_mgr.get_keyframe_data(layer_idx, frame_idx) |
1105 scene_group_id = scene_node.getAttribute('ref') | 1122 scene_group_id = scene_node.getAttribute('ref') |
1106 scene_group_node = self._dom.get_node(scene_group_id) | 1123 scene_group_node = self._dom.get_node(scene_group_id) |
1107 return scene_group_node | 1124 return scene_group_node |
1108 | 1125 |
1109 ## \brief Find an associated key frame and tween info for a group ID. | 1126 ## \brief Find an associated key frame and tween info for a group ID. |
1127 def get_left_key(self, layer_idx, frame_idx): | 1144 def get_left_key(self, layer_idx, frame_idx): |
1128 start, end, tween_type = \ | 1145 start, end, tween_type = \ |
1129 self._fl_mgr.get_left_key_tween(layer_idx, frame_idx) | 1146 self._fl_mgr.get_left_key_tween(layer_idx, frame_idx) |
1130 return start, end, tween_type | 1147 return start, end, tween_type |
1131 | 1148 |
1149 def get_layer_keys(self, layer_idx): | |
1150 tweens = self._fl_mgr.get_all_key_tween_of_layer(layer_idx) | |
1151 return tweens | |
1152 | |
1132 ## \brief Return widget showing frames and layers. | 1153 ## \brief Return widget showing frames and layers. |
1133 # | 1154 # |
1134 def get_frame_ui_widget(self): | 1155 def get_frame_ui_widget(self): |
1135 return self._fl_mgr._frameline_box | 1156 return self._fl_mgr._frameline_box |
1136 | 1157 |
1137 def register_active_frame_cb(self, cb): | 1158 def register_active_frame_callback(self, cb): |
1138 self._fl_mgr.register_active_frame_cb(cb) | 1159 self._fl_mgr.register_active_frame_callback(cb) |
1139 pass | 1160 pass |
1140 | 1161 |
1141 def set_layer_label(self, txt): | 1162 def set_layer_label(self, txt): |
1142 self._fl_mgr.set_layer_label(txt) | 1163 self._fl_mgr.set_layer_label(txt) |
1143 pass | 1164 pass |
1145 ## \brief Get duplicate group of a layer. | 1166 ## \brief Get duplicate group of a layer. |
1146 # | 1167 # |
1147 def get_layer_dup_group(self, layer_idx): | 1168 def get_layer_dup_group(self, layer_idx): |
1148 data = self._dom.get_layer_data(layer_idx) | 1169 data = self._dom.get_layer_data(layer_idx) |
1149 if not data: | 1170 if not data: |
1150 data = object() | 1171 data = dict() |
1151 self._dom.set_layer_data(layer_idx, data) | 1172 self._dom.set_layer_data(layer_idx, data) |
1152 pass | 1173 pass |
1153 | 1174 |
1154 if hasattr(data, 'dup_group_id'): | 1175 dup_group = None |
1176 if data.has_key('dup_group_id'): | |
1155 try: | 1177 try: |
1156 dup_group = self._dom.get_node(data.dup_group_id) | 1178 dup_group = self._dom.get_node(data['dup_group_id']) |
1157 except KeyError: | 1179 except KeyError: |
1158 dup_group = None | |
1159 pass | 1180 pass |
1160 pass | 1181 pass |
1161 | 1182 |
1162 if not dup_group: | 1183 if not dup_group: |
1184 # Search dup group from children of the layer group | |
1185 layer_group = self._dom.get_layer_group(layer_idx) | |
1186 for child in layer_group.childList(): | |
1187 try: | |
1188 label = child.getAttribute('inkscape:label') | |
1189 except: | |
1190 pass | |
1191 else: | |
1192 if label == 'dup': | |
1193 data['dup_group_id'] = child | |
1194 return child | |
1195 pass | |
1196 pass | |
1197 | |
1198 # Or create a new dup group for the layer | |
1163 dup_group = self._dom.add_layer_dup_group(layer_idx) | 1199 dup_group = self._dom.add_layer_dup_group(layer_idx) |
1164 data.dup_group_id = dup_group.getAttribute('id') | 1200 data['dup_group_id'] = dup_group.getAttribute('id') |
1165 pass | 1201 pass |
1166 | 1202 |
1167 return dup_group | 1203 return dup_group |
1204 | |
1205 def get_max_frame(self): | |
1206 max_frame = self._dom.get_max_frame() | |
1207 return max_frame | |
1168 pass | 1208 pass |
1169 | 1209 |
1170 ## \brief MBScene connect GUI and DOM-tree | 1210 ## \brief MBScene connect GUI and DOM-tree |
1171 # | 1211 # |
1172 # This class connect behavior of GUI to the DOM-tree. All about GUI is | 1212 # This class connect behavior of GUI to the DOM-tree. All about GUI is |
1193 self.tween = None | 1233 self.tween = None |
1194 self.document = None | 1234 self.document = None |
1195 self.root = root | 1235 self.root = root |
1196 self.framerate = 12 | 1236 self.framerate = 12 |
1197 self._disable_tween_type_selector = False | 1237 self._disable_tween_type_selector = False |
1238 self.current = 0 | |
1198 | 1239 |
1199 self._dom = MBDOM_UI() | 1240 self._dom = MBDOM_UI() |
1200 pass | 1241 pass |
1201 | 1242 |
1202 def change_active_frame(self, node): | 1243 def change_active_frame(self, node): |
1250 | 1291 |
1251 def extendScene(self): | 1292 def extendScene(self): |
1252 layer_idx, frame_idx = self._dom.get_active_layer_frame() | 1293 layer_idx, frame_idx = self._dom.get_active_layer_frame() |
1253 start, end, tween_type = \ | 1294 start, end, tween_type = \ |
1254 self._dom.get_left_key(layer_idx, frame_idx) | 1295 self._dom.get_left_key(layer_idx, frame_idx) |
1255 tween_len = frame_idx - start | 1296 tween_len = frame_idx - start + 1 |
1256 self._dom.tween(layer_idx, start, tween_len, tween_type) | 1297 self._dom.tween(layer_idx, start, tween_len, tween_type) |
1257 | 1298 |
1258 scene_group = self._dom.get_keyframe_group(layer_idx, start) | 1299 scene_group = self._dom.get_keyframe_group(layer_idx, start) |
1259 self._enterGroup(scene_gorup) | 1300 self._enterGroup(scene_gorup) |
1260 pass | 1301 pass |
1280 """ | 1321 """ |
1281 self.current = idx | 1322 self.current = idx |
1282 self.tween.updateMapping() | 1323 self.tween.updateMapping() |
1283 for layer_idx in range(self._dom.get_layer_num()): | 1324 for layer_idx in range(self._dom.get_layer_num()): |
1284 dup_group = self._dom.get_layer_dup_group(layer_idx) | 1325 dup_group = self._dom.get_layer_dup_group(layer_idx) |
1285 | 1326 dup_group.setAttribute('style', 'display: none') |
1286 all_key_tweens = self._dom.get_all_key_tween_of_layer(layer_idx) | 1327 |
1287 for start, stop, tween_type in all_key_tweens: | 1328 all_key_tweens = self._dom.get_layer_keys(layer_idx) |
1329 for start, end, tween_type in all_key_tweens: | |
1288 if start == idx: # at key frame | 1330 if start == idx: # at key frame |
1289 dup_group.setAttribute('style', 'display: none') | |
1290 scene_group = \ | 1331 scene_group = \ |
1291 self._dom.get_keyframe_group(layer_idx, idx) | 1332 self._dom.get_keyframe_group(layer_idx, start) |
1292 scene_group.setAttribute('style', '') | 1333 scene_group.setAttribute('style', '') |
1293 elif start < idx and end >= idx: # in Tween | 1334 elif start < idx and end >= idx: # in Tween |
1335 dup_group.setAttribute('style', '') | |
1294 scene_group = \ | 1336 scene_group = \ |
1295 self._dom.get_keyframe_group(layer_idx, idx) | 1337 self._dom.get_keyframe_group(layer_idx, start) |
1338 scene_group.setAttribute('style', 'display: none') | |
1296 | 1339 |
1297 try: | 1340 try: |
1298 next_scene_group = \ | 1341 next_scene_group = \ |
1299 self._dom.get_keyframe_group(layer_idx, end + 1) | 1342 self._dom.get_keyframe_group(layer_idx, end + 1) |
1300 except: # no next key frame | 1343 except: # no next key frame |
1301 next_scene_group = scene_group | 1344 next_scene_group = scene_group |
1302 pass | 1345 pass |
1303 | 1346 |
1304 tween_obj_type = self._tween_obj_tween_types[tween_type] | 1347 tween_obj_type = self._tween_obj_tween_types[tween_type] |
1305 nframes = stop - start + 1 | 1348 nframes = end - start + 1 |
1306 percent = float(idx - start) / nframes | 1349 percent = float(idx - start) / nframes |
1307 self.tween.updateTweenContent(dup_group, | 1350 self.tween.updateTweenContent(dup_group, |
1308 tween_obj_type, | 1351 tween_obj_type, |
1309 scene_group, | 1352 scene_group, |
1310 next_scene_group, | 1353 next_scene_group, |
1311 percent) | 1354 percent) |
1312 pass | 1355 pass |
1313 else: # this scene should not be showed. | 1356 else: # this scene should not be showed. |
1314 scene_group = \ | 1357 scene_group = \ |
1315 self._dom.get_keyframe_group(layer_idx, idx) | 1358 self._dom.get_keyframe_group(layer_idx, start) |
1316 scene_group.setAttribute('style', '') | 1359 scene_group.setAttribute('style', 'display: none') |
1317 pass | 1360 pass |
1318 pass | 1361 pass |
1319 pass | 1362 pass |
1320 pass | 1363 pass |
1321 | 1364 |
1489 self._lockui = False | 1532 self._lockui = False |
1490 pass | 1533 pass |
1491 pass | 1534 pass |
1492 | 1535 |
1493 def doRunNext(self): | 1536 def doRunNext(self): |
1494 if self.current > self._maxframe: | 1537 if self.current > self._dom.get_max_frame(): |
1495 self.current = 0 | 1538 self.current = 0 |
1496 pass | 1539 pass |
1497 try: | 1540 try: |
1498 self.setCurrentScene(self.current) | 1541 self.setCurrentScene(self.current) |
1499 except: | 1542 except: |
1582 pass | 1625 pass |
1583 | 1626 |
1584 self.document = self.desktop.doc().rdoc | 1627 self.document = self.desktop.doc().rdoc |
1585 self.tween = TweenObject(self.document, self.root) | 1628 self.tween = TweenObject(self.document, self.root) |
1586 self._dom.handle_doc_root(self.document, self.root) | 1629 self._dom.handle_doc_root(self.document, self.root) |
1587 self._dom.register_active_frame_cb(self.onCellClick) | 1630 self._dom.register_active_frame_callback(self.onCellClick) |
1588 | 1631 |
1589 if self.top == None: | 1632 if self.top == None: |
1590 self.top = gtk.VBox(False, 0) | 1633 self.top = gtk.VBox(False, 0) |
1591 toplevel = self.desktop.getToplevel() | 1634 toplevel = self.desktop.getToplevel() |
1592 toplevel.child.child.pack_end(self.top, expand=False) | 1635 toplevel.child.child.pack_end(self.top, expand=False) |