comparison pyink/tween.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 ad9c44a08645
comparison
equal deleted inserted replaced
1150:6586cd10c92f 1151:71c72e8d6755
1 # -*- indent-tabs-mode: t; tab-width: 8; python-indent: 4; -*- 1 # -*- indent-tabs-mode: t; tab-width: 8; python-indent: 4; -*-
2 # vim: sw=4:ts=8:sts=4 2 # vim: sw=4:ts=8:sts=4
3 import traceback 3 import traceback
4 import math 4 import math
5
5 class TweenObject: 6 class TweenObject:
7 TWEEN_TYPE_NORMAL = 0
8 TWEEN_TYPE_RELOCATE = 1
9 TWEEN_TYPE_SCALE = 2
10
6 def __init__(self,doc,dom): 11 def __init__(self,doc,dom):
7 self.document = doc 12 self.document = doc
8 self.dom = dom 13 self.dom = dom
9 try: 14 try:
10 self.width = float(dom.getAttribute("width")) 15 self.width = float(dom.getAttribute("width"))
22 self.updateMappingNode(c) 27 self.updateMappingNode(c)
23 try: 28 try:
24 self.nodeToItem[c.getAttribute("id")] = c 29 self.nodeToItem[c.getAttribute("id")] = c
25 except: 30 except:
26 pass 31 pass
27 def updateTweenContent(self,obj, typ, source,dest,cur): 32 def updateTweenContent(self, duplicate_group, tween_type,
28 """ 33 start_scene_group, stop_scene_group, percent):
29 Update the content of the duplicate scene group. We will 34 """
30 use the (start,end) and cur to calculate the percentage of 35 Update the content of the duplicate scene group. We will
31 the tween motion effect and then use it to update the 36 use precent, start_scene_group, stop_scene_group to
32 transform matrix of the duplicated scene group. 37 compute transform matrix and update duplicate scene group
33 """ 38 specified.
34 39 """
35 start = source.idx
36 end = dest.idx
37 print cur,start,end
38 percent = (cur-start)*1.0/(end-start)
39 i = 0
40 s = source.ref.firstChild()
41 d = dest.ref.firstChild()
42 sources={}
43 dests={}
44 # Collect ref from the obj 40 # Collect ref from the obj
45 o = obj.firstChild() 41 node = duplicate_group.firstChild()
46 maps={} 42 dup_nodes = {}
47 while o: 43 while node:
48 print "--->",o 44 try:
49 try: 45 ref = node.getAttribute("ref")
50 ref = o.getAttribute("ref") 46 dup_nodes[ref] = node
51 except: 47 except:
52 print o
53 ref = None 48 ref = None
54 49 pass
55 if ref: 50 node = node.next()
56 maps[ref] = o 51 pass
57 o = o.next() 52
58 # Collect all objects 53 # Collect all nodes in stop scene
59 while d: 54 stop_nodes = {}
60 try: 55 node = stop_scene_group.firstChild()
61 label = d.getAttribute("inkscape:label") 56 while node:
62 except: 57 try:
63 d = d.next() 58 node_label = node.getAttribute("ns0:duplicate-src")
59 stop_nodes[node_label] = node
60 except:
61 pass
62 node = node.next()
63 pass
64
65 #
66 # Node ID of a node of start scene must be mapped to
67 # 'ns0:duplicate-src' attribute of a node of stop scene. The
68 # nodes which can not be mapped to a node of stop scene are
69 # not manipulated by the tween.
70 #
71 # When a scene is duplicated, 'ns0:duplicate-src' attribute of
72 # nodes, in the new scene, must be setted to ID of respective
73 # one in the duplicated scene.
74 #
75 start_node = start_scene_group.firstChild()
76 while start_node:
77 start_node_id = start_node.getAttribute('id')
78 try:
79 stop_node = stop_nodes[start_node_id]
80 except KeyError:
81 start_node = start_node.next()
64 continue 82 continue
65 dests[label] = d 83
66 d = d.next() 84 dup_node = dup_nodes.setdefault(start_node_id, None)
67 # Check if the object in the source exists in the destination 85
68 s = source.ref.firstChild() 86 self.updateTweenObject(duplicate_group, tween_type,
69 d = dest.ref.firstChild() 87 start_node, stop_node,
70 while s: 88 percent, dup_node)
71 print s,d 89 start_node = start_node.next()
72 sid = s.getAttribute("id") 90 pass
73 if maps.has_key(sid): 91 pass
74 o = maps[sid]
75 else:
76 o = None
77 try:
78 label = s.getAttribute("inkscape:label")
79 # Use i8nkscape:label to identidy the equipvalent objects
80 if label:
81 if dests.has_key(label):
82 self.updateTweenObject(obj, typ, s,
83 dests[label], percent, o)
84 s = s.next()
85 continue
86 except:
87 pass
88 # Search obejcts in the destination
89 while d:
90 try:
91 d.getAttribute("inkscape:label")
92 d = d.next()
93 continue
94 except:
95 pass
96 if s.name() == d.name():
97 self.updateTweenObject(obj,typ,s,d,percent,o)
98 d = d.next()
99 break
100 d = d.next()
101 s = s.next()
102 92
103 def parseTransform(self,obj): 93 def parseTransform(self,obj):
104 """ 94 """
105 Return the transform matrix of an object 95 Return the transform matrix of an object
106 """ 96 """
169 def updateTweenObject(self,obj,typ,s,d,p,newobj): 159 def updateTweenObject(self,obj,typ,s,d,p,newobj):
170 """ 160 """
171 Generate tweened object in the @obj by using s and d in the @p percent 161 Generate tweened object in the @obj by using s and d in the @p percent
172 http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html 162 http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html
173 """ 163 """
174 if typ == 1: 164 if typ == self.TWEEN_TYPE_RELOCATE:
175 if s.name() == 'svg:g': 165 if s.name() == 'svg:g':
176 if not newobj: 166 if not newobj:
177 newobj = s.duplicate(self.document) 167 newobj = s.duplicate(self.document)
178 top = self.document.createElement("svg:g") 168 top = self.document.createElement("svg:g")
179 top.setAttribute("ref", s.getAttribute("id")) 169 top.setAttribute("ref", s.getAttribute("id"))
205 top.setAttribute("transform","translate(%g,%g)" % (tx,ty)) 195 top.setAttribute("transform","translate(%g,%g)" % (tx,ty))
206 except: 196 except:
207 traceback.print_exc() 197 traceback.print_exc()
208 pass 198 pass
209 pass 199 pass
210 elif typ == 2: 200 elif typ == self.TWEEN_TYPE_SCALE:
211 self.updateTweenObjectScale(obj,s,d,p,newobj) 201 self.updateTweenObjectScale(obj,s,d,p,newobj)
212 pass 202 pass
213 elif typ == 0: 203 elif typ == self.TWEEN_TYPE_NORMAL:
214 newobj = s.duplicate(self.document) 204 newobj = s.duplicate(self.document)
215 newobj.setAttribute("ref", s.getAttribute("id")) 205 newobj.setAttribute("ref", s.getAttribute("id"))
216 top = self.document.createElement("svg:g") 206 top = self.document.createElement("svg:g")
217 top.appendChild(newobj) 207 top.appendChild(newobj)
218 obj.appendChild(top) 208 obj.appendChild(top)