comparison pyink/tween.py @ 1141:8f0ee167c5b2

Fix the issue of the new DOM implementation
author wycc
date Thu, 23 Dec 2010 00:00:46 +0800
parents d4dbcb93aee0
children e14ec6d1a661
comparison
equal deleted inserted replaced
1140:d4dbcb93aee0 1141:8f0ee167c5b2
4 import math 4 import math
5 class TweenObject: 5 class TweenObject:
6 def __init__(self,doc,dom): 6 def __init__(self,doc,dom):
7 self.document = doc 7 self.document = doc
8 self.dom = dom 8 self.dom = dom
9 self.width = float(dom.attribute("width")) 9 try:
10 self.height = float(dom.attribute("height")) 10 self.width = float(dom.getAttribute("width"))
11 self.height = float(dom.getAttribute("height"))
12 except:
13 self.width = 640
14 self.height = 480
11 15
12 def updateMapping(self): 16 def updateMapping(self):
13 self.nodeToItem={} 17 self.nodeToItem={}
14 root = self.dom 18 root = self.dom
15 self.updateMappingNode(root) 19 self.updateMappingNode(root)
16 def updateMappingNode(self,node): 20 def updateMappingNode(self,node):
17 for c in node.childList(): 21 for c in node.childList():
18 self.updateMappingNode(c) 22 self.updateMappingNode(c)
19 self.nodeToItem[c.getId()] = c 23 try:
24 self.nodeToItem[c.getAttribute("id")] = c
25 except:
26 pass
20 def updateTweenContent(self,obj, typ, source,dest,cur): 27 def updateTweenContent(self,obj, typ, source,dest,cur):
21 """ 28 """
22 Update the content of the duplicate scene group. We will use the (start,end) and cur to calculate the percentage of 29 Update the content of the duplicate scene group. We will use the (start,end) and cur to calculate the percentage of
23 the tween motion effect and then use it to update the transform matrix of the duplicated scene group. 30 the tween motion effect and then use it to update the transform matrix of the duplicated scene group.
24 """ 31 """
34 dests={} 41 dests={}
35 42
36 # Collect all objects 43 # Collect all objects
37 while d: 44 while d:
38 try: 45 try:
39 label = d.attribute("inkscape:label") 46 label = d.getAttribute("inkscape:label")
40 except: 47 except:
41 d = d.getNext() 48 d = d.next()
42 continue 49 continue
43 dests[label] = d 50 dests[label] = d
44 d = d.getNext() 51 d = d.next()
45 # Check if the object in the source exists in the destination 52 # Check if the object in the source exists in the destination
46 s = source.ref.firstChild() 53 s = source.ref.firstChild()
47 d = dest.ref.firstChild() 54 d = dest.ref.firstChild()
48 while s: 55 while s:
49 print s,d 56 print s,d
50 try: 57 try:
51 label = s.attribute("inkscape:label") 58 label = s.getAttribute("inkscape:label")
52 # Use i8nkscape:label to identidy the equipvalent objects 59 # Use i8nkscape:label to identidy the equipvalent objects
53 if label: 60 if label:
54 if dests.hasattr(label.value()): 61 if dests.hasattr(label.value()):
55 self.updateTweenObject(obj,typ,s,dests[label.value()],percent) 62 self.updateTweenObject(obj,typ,s,dests[label.value()],percent)
56 s = s.getNext() 63 s = s.next()
57 continue 64 continue
58 except: 65 except:
59 pass 66 pass
60 # Search obejcts in the destination 67 # Search obejcts in the destination
61 while d: 68 while d:
62 try: 69 try:
63 d.attribute("inkscape:label") 70 d.getAttribute("inkscape:label")
64 d = d.getNext() 71 d = d.next()
65 continue 72 continue
66 except: 73 except:
67 pass 74 pass
68 if s.name() == d.name(): 75 if s.name() == d.name():
69 self.updateTweenObject(obj,typ,s,d,percent) 76 self.updateTweenObject(obj,typ,s,d,percent)
70 d = d.getNext() 77 d = d.next()
71 break 78 break
72 d = d.getNext() 79 d = d.next()
73 s = s.getNext() 80 s = s.next()
74 81
75 def parseTransform(self,obj): 82 def parseTransform(self,obj):
76 """ 83 """
77 Return the transform matrix of an object 84 Return the transform matrix of an object
78 """ 85 """
79 try: 86 try:
80 t = obj.attribute("transform") 87 t = obj.getAttribute("transform")
81 print t 88 print t
82 if t[0:9] == 'translate': 89 if t[0:9] == 'translate':
83 print "translate" 90 print "translate"
84 fields = t[10:].split(',') 91 fields = t[10:].split(',')
85 x = float(fields[0]) 92 x = float(fields[0])
143 Generate tweened object in the @obj by using s and d in the @p percent 150 Generate tweened object in the @obj by using s and d in the @p percent
144 http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html 151 http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html
145 """ 152 """
146 if typ == 'relocate': 153 if typ == 'relocate':
147 newobj = s.duplicate(self.document) 154 newobj = s.duplicate(self.document)
148 newobj.setAttribute("ref", s.getId()) 155 newobj.setAttribute("ref", s.getAttribute("id"))
149 top = self.document.createElement("svg:g") 156 top = self.document.createElement("svg:g")
150 top.appendChild(newobj) 157 top.appendChild(newobj)
151 obj.appendChild(top) 158 obj.appendChild(top)
152 if s.name() == 'svg:g': 159 if s.name() == 'svg:g':
153 # Parse the translate or matrix 160 # Parse the translate or matrix
154 sm = self.parseTransform(s) 161 sm = self.parseTransform(s)
155 dm = self.parseTransform(d) 162 dm = self.parseTransform(d)
156 top.setAttribute("transform","translate(%g,%g)" % ((dm[2]-sm[2])*p,(dm[5]-sm[5])*p)) 163 top.setAttribute("transform","translate(%g,%g)" % ((dm[2]-sm[2])*p,(dm[5]-sm[5])*p))
157 else: 164 else:
158 try: 165 try:
159 sx = float(s.attribute("x")) 166 sx = float(s.getAttribute("x"))
160 sy = float(s.attribute("y")) 167 sy = float(s.getAttribute("y"))
161 dx = float(d.attribute("x")) 168 dx = float(d.getAttribute("x"))
162 dy = float(d.attribute("y")) 169 dy = float(d.getAttribute("y"))
163 tx = (dx-sx)*p 170 tx = (dx-sx)*p
164 ty = (dy-sy)*p 171 ty = (dy-sy)*p
165 print tx,ty 172 print tx,ty
166 top.setAttribute("transform","translate(%g,%g)" % (tx,ty)) 173 top.setAttribute("transform","translate(%g,%g)" % (tx,ty))
167 except: 174 except:
171 elif typ == 'scale': 178 elif typ == 'scale':
172 self.updateTweenObjectScale(obj,s,d,p) 179 self.updateTweenObjectScale(obj,s,d,p)
173 pass 180 pass
174 elif typ == 'normal': 181 elif typ == 'normal':
175 newobj = s.duplicate(self.document) 182 newobj = s.duplicate(self.document)
176 newobj.setAttribute("ref", s.getId()) 183 newobj.setAttribute("ref", s.getAttribute("id"))
177 top = self.document.createElement("svg:g") 184 top = self.document.createElement("svg:g")
178 top.appendChild(newobj) 185 top.appendChild(newobj)
179 obj.appendChild(top) 186 obj.appendChild(top)
180 pass 187 pass
181 188
196 if s.name() == 'svg:g': 203 if s.name() == 'svg:g':
197 # Parse the translate or matrix 204 # Parse the translate or matrix
198 # 205 #
199 # D = B inv(A) 206 # D = B inv(A)
200 try: 207 try:
201 item = self.nodeToItem[s.attribute("id")] 208 item = self.nodeToItem[s.getAttribute("id")]
202 (ox,oy) = item.getCenter() 209 (ox,oy) = item.getCenter()
203 except: 210 except:
204 ox = 0 211 ox = 0
205 oy = 0 212 oy = 0
206 try: 213 try:
207 item = self.nodeToItem[d.attribute("id")] 214 item = self.nodeToItem[d.getAttribute("id")]
208 (dx,dy) = item.getCenter() 215 (dx,dy) = item.getCenter()
209 except: 216 except:
210 dx = 0 217 dx = 0
211 dy = 0 218 dy = 0
212 219
225 m = self.mulA([1,0,0,1,tx,self.height-ty],m) 232 m = self.mulA([1,0,0,1,tx,self.height-ty],m)
226 233
227 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5])) 234 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5]))
228 else: 235 else:
229 try: 236 try:
230 sw = float(s.attribute("width")) 237 sw = float(s.getAttribute("width"))
231 sh = float(s.attribute("height")) 238 sh = float(s.getAttribute("height"))
232 dw = float(d.attribute("width")) 239 dw = float(d.getAttribute("width"))
233 dh = float(d.attribute("height")) 240 dh = float(d.getAttribute("height"))
234 try: 241 try:
235 item = self.nodeToItem[s.attribute("id")] 242 item = self.nodeToItem[s.getAttribute("id")]
236 (ox,oy) = item.getCenter() 243 (ox,oy) = item.getCenter()
237 except: 244 except:
238 ox = 0 245 ox = 0
239 oy = 0 246 oy = 0
240 try: 247 try:
241 item = self.nodeToItem[d.attribute("id")] 248 item = self.nodeToItem[d.getAttribute("id")]
242 (dx,dy) = item.getCenter() 249 (dx,dy) = item.getCenter()
243 except: 250 except:
244 dx = 0 251 dx = 0
245 dy = 0 252 dy = 0
246 try: 253 try: