comparison pyink/MBScene.py @ 1151:71c72e8d6755

Refactory cod eof TweenObject.updateTweenContent and MBScene.setCurrentScene(). - updateTweenContent() use frameline.get_frame_blocks(), instead of frameline._keys, to get information of tweens and key frames. - MBScene.setCurrentScene() use 'ns0:duplicate-src' attribute to map nodes from a start scene to a stop scene.
author Thinker K.F. Li <thinker@codemud.net>
date Sun, 26 Dec 2010 23:40:09 +0800
parents 6586cd10c92f
children 5db4d769387c
comparison
equal deleted inserted replaced
1150:6586cd10c92f 1151:71c72e8d6755
139 139
140 def notifyAttributeChanged(self,node, name, old_value, new_value): 140 def notifyAttributeChanged(self,node, name, old_value, new_value):
141 self.ui.updateUI() 141 self.ui.updateUI()
142 pass 142 pass
143 143
144 def _travel_DOM(node):
145 nodes = [node]
146 while nodes:
147 node = nodes.pop(0)
148 child = node.firstChild()
149 while child:
150 nodes.append(child)
151 child = child.next()
152 pass
153 yield node
154 pass
155 pass
156
144 class MBScene(): 157 class MBScene():
145 _tween_types = (frameline.TWEEN_TYPE_NONE, 158 _frameline_tween_types = (frameline.TWEEN_TYPE_NONE,
146 frameline.TWEEN_TYPE_MOVE, 159 frameline.TWEEN_TYPE_MOVE,
147 frameline.TWEEN_TYPE_SHAPE) 160 frameline.TWEEN_TYPE_SHAPE)
161 _tween_obj_tween_types = (TweenObject.TWEEN_TYPE_NORMAL,
162 TweenObject.TWEEN_TYPE_RELOCATE,
163 TweenObject.TWEEN_TYPE_SCALE)
148 _tween_type_names = ('normal', 'relocate', 'scale') 164 _tween_type_names = ('normal', 'relocate', 'scale')
149 165
150 def __init__(self, desktop, win, root=None): 166 def __init__(self, desktop, win, root=None):
151 self.desktop = desktop 167 self.desktop = desktop
152 self.window = win 168 self.window = win
548 create this group if it is not 564 create this group if it is not
549 available. 565 available.
550 """ 566 """
551 self.current = nth 567 self.current = nth
552 self.tween.updateMapping() 568 self.tween.updateMapping()
569 idx = nth - 1
553 for layer in self._framelines: 570 for layer in self._framelines:
554 i=0 571 i=0
555 572
556 # Check the duplicated scene group and create it if it is not available 573 # Check the duplicated scene group and create it if it is not available
557 try: 574 try:
563 layer.duplicateGroup.setAttribute("sodipodi:insensitive","1") 580 layer.duplicateGroup.setAttribute("sodipodi:insensitive","1")
564 layer.duplicateGroup.setAttribute("style","") 581 layer.duplicateGroup.setAttribute("style","")
565 layer.layer.node.appendChild(layer.duplicateGroup) 582 layer.layer.node.appendChild(layer.duplicateGroup)
566 pass 583 pass
567 # Create a new group 584 # Create a new group
568 #layer.duplicateGroup = None 585 for start_idx, stop_idx, tween_type in layer.get_frame_blocks():
569 while i < len(layer._keys): 586 if start_idx == stop_idx:
570 s = layer._keys[i] 587 scene_group = layer.get_frame_data(start_idx)
571 print s.ref.getAttribute("id"),s.idx,s.left_tween,s.right_tween 588 if idx == start_idx:
572 if s.right_tween is False: 589 scene_group.setAttribute('style', '')
573 if nth == s.idx+1:
574 s.ref.setAttribute("style","")
575 else: 590 else:
576 s.ref.setAttribute("style","display:none") 591 scene_group.setAttribute('style', 'display: none')
577 i = i + 1 592 pass
578 continue 593 elif start_idx <= idx and stop_idx >= idx:
579 594 scene_group = layer.get_frame_data(start_idx)
580 if nth == s.idx + 1: 595 scene_group.setAttribute("style","display:none")
581 s.ref.setAttribute("style","") 596 layer.duplicateGroup.setAttribute("style","")
597 tween_type_idx = \
598 self._frameline_tween_types.index(tween_type)
599 tween_obj_tween_type = \
600 self._tween_obj_tween_types[tween_type_idx]
601
602 next_idx, next_stop_idx, next_tween_type = \
603 layer.get_frame_block(stop_idx + 1)
604 next_scene_group = layer.get_frame_data(next_idx)
605
606 nframes = next_idx - start_idx + 1
607 percent = float(idx - start_idx) / nframes
608 self.tween.updateTweenContent(layer.duplicateGroup,
609 tween_obj_tween_type,
610 scene_group,
611 next_scene_group,
612 percent)
582 else: 613 else:
583 if nth > (s.idx+1) and nth <= (layer._keys[i+1].idx+1): 614 scene_group = layer.get_frame_data(start_idx)
584 if i+2 < len(layer._keys): 615 scene_group.setAttribute("style","display:none")
585 s.ref.setAttribute("style","display:none") 616 pass
586 layer.duplicateGroup.setAttribute("style","")
587 d = layer._keys[i + 2]
588 self.tween.\
589 updateTweenContent(layer.duplicateGroup,
590 layer.get_tween_type(s.idx),
591 s, d, nth)
592 else:
593 layer.duplicateGroup.setAttribute("style","")
594 s.ref.setAttribute("style","display:none")
595 pass
596 else:
597 s.ref.setAttribute("style","display:none")
598 i = i + 2
599 pass 617 pass
600 pass 618 pass
601 pass 619 pass
602 620
603 def enterGroup(self,obj): 621 def enterGroup(self,obj):
633 i = i + 2 651 i = i + 2
634 pass 652 pass
635 pass 653 pass
636 654
637 def setTweenType(self, tween_type): 655 def setTweenType(self, tween_type):
638 sel_type = MBScene._tween_types.index(tween_type) 656 sel_type = MBScene._frameline_tween_types.index(tween_type)
639 self._disable_tween_type_selector = True 657 self._disable_tween_type_selector = True
640 self.tweenTypeSelector.set_active(sel_type) 658 self.tweenTypeSelector.set_active(sel_type)
641 self._disable_tween_type_selector = False 659 self._disable_tween_type_selector = False
642 pass 660 pass
643 661
727 for scene in layer.scenes: 745 for scene in layer.scenes:
728 frameline.add_keyframe(scene.start-1,scene.node) 746 frameline.add_keyframe(scene.start-1,scene.node)
729 if scene.start != scene.end: 747 if scene.start != scene.end:
730 frameline.add_keyframe(scene.end-1,scene.node) 748 frameline.add_keyframe(scene.end-1,scene.node)
731 tween_type_idx = self._tween_type_names.index(scene.type) 749 tween_type_idx = self._tween_type_names.index(scene.type)
732 tween_type = self._tween_types[tween_type_idx] 750 tween_type = self._frameline_tween_types[tween_type_idx]
733 frameline.tween(scene.start-1, tween_type) 751 frameline.tween(scene.start-1, tween_type)
734 pass 752 pass
735 pass 753 pass
736 pass 754 pass
737 755
785 pass 803 pass
786 pass 804 pass
787 if orig == None: 805 if orig == None:
788 return None 806 return None
789 ns = orig.duplicate(rdoc) 807 ns = orig.duplicate(rdoc)
808
809 old_nodes = _travel_DOM(orig)
810 new_nodes = _travel_DOM(ns)
811 for old_node in old_nodes:
812 old_node_id = old_node.getAttribute('id')
813 new_node = new_nodes.next()
814 new_node.setAttribute('ns0:duplicate-src', old_node_id)
815 pass
816
790 gid = self.last_line.node.getAttribute("inkscape:label")+self.newID() 817 gid = self.last_line.node.getAttribute("inkscape:label")+self.newID()
791 self.ID[gid]=1 818 self.ID[gid]=1
792 ns.setAttribute("id",gid) 819 ns.setAttribute("id",gid)
793 ns.setAttribute("inkscape:groupmode","layer") 820 ns.setAttribute("inkscape:groupmode","layer")
794 self.last_line.node.appendChild(ns) 821 self.last_line.node.appendChild(ns)
891 i = 0 918 i = 0
892 found = -1 919 found = -1
893 for start_idx, stop_idx, tween_type in frameline.get_frame_blocks(): 920 for start_idx, stop_idx, tween_type in frameline.get_frame_blocks():
894 if start_idx < stop_idx: 921 if start_idx < stop_idx:
895 n = self.tweenTypeSelector.get_active() 922 n = self.tweenTypeSelector.get_active()
896 new_tween_type = MBScene._tween_types[n] 923 new_tween_type = MBScene._frameline_tween_types[n]
897 self.last_line.set_tween_type(start_idx, new_tween_type) 924 self.last_line.set_tween_type(start_idx, new_tween_type)
898 self.update() 925 self.update()
899 break 926 break
900 pass 927 pass
901 pass 928 pass
927 954
928 def addScenes(self, frameline, scenes_node): 955 def addScenes(self, frameline, scenes_node):
929 doc = self.document 956 doc = self.document
930 for start_idx, stop_idx, tween_type in frameline.get_frame_blocks(): 957 for start_idx, stop_idx, tween_type in frameline.get_frame_blocks():
931 ref = frameline.get_frame_data(start_idx) 958 ref = frameline.get_frame_data(start_idx)
932 tween_type_idx = self._tween_types.index(tween_type) 959 tween_type_idx = self._frameline_tween_types.index(tween_type)
933 tween_type_name = self._tween_type_names[tween_type_idx] 960 tween_type_name = self._tween_type_names[tween_type_idx]
934 961
935 scene_node = doc.createElement("ns0:scene") 962 scene_node = doc.createElement("ns0:scene")
936 scenes_node.appendChild(scene_node) 963 scenes_node.appendChild(scene_node)
937 scene_node.setAttribute("start", str(start_idx + 1)) 964 scene_node.setAttribute("start", str(start_idx + 1))