annotate pyink/tween.py @ 1147:5cfa73d7e80f

Add the Run button to simulate the animation
author wycc
date Fri, 24 Dec 2010 15:00:28 +0800
parents e14ec6d1a661
children 6586cd10c92f
rev   line source
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
1 # -*- indent-tabs-mode: t; tab-width: 8; python-indent: 4; -*-
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
2 # vim: sw=4:ts=8:sts=4
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
3 import traceback
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
4 import math
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
5 class TweenObject:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
6 def __init__(self,doc,dom):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
7 self.document = doc
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
8 self.dom = dom
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
9 try:
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
10 self.width = float(dom.getAttribute("width"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
11 self.height = float(dom.getAttribute("height"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
12 except:
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
13 self.width = 640
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
14 self.height = 480
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
15
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
16 def updateMapping(self):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
17 self.nodeToItem={}
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
18 root = self.dom
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
19 self.updateMappingNode(root)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
20 def updateMappingNode(self,node):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
21 for c in node.childList():
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
22 self.updateMappingNode(c)
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
23 try:
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
24 self.nodeToItem[c.getAttribute("id")] = c
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
25 except:
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
26 pass
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
27 def updateTweenContent(self,obj, typ, source,dest,cur):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
28 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
29 Update the content of the duplicate scene group. We will use the (start,end) and cur to calculate the percentage of
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
30 the tween motion effect and then use it to update the transform matrix of the duplicated scene group.
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
31 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
32
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
33 start = source.idx
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
34 end = dest.idx
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
35 print cur,start,end
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
36 percent = (cur-start)*1.0/(end-start)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
37 i = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
38 s = source.ref.firstChild()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
39 d = dest.ref.firstChild()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
40 sources={}
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
41 dests={}
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
42 # Collect ref from the obj
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
43 o = obj.firstChild()
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
44 maps={}
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
45 while o:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
46 print "--->",o
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
47 try:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
48 ref = o.getAttribute("ref")
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
49 except:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
50 print o
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
51 ref = None
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
52
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
53 if ref:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
54 maps[ref] = o
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
55 o = o.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
56 # Collect all objects
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
57 while d:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
58 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
59 label = d.getAttribute("inkscape:label")
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
60 except:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
61 d = d.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
62 continue
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
63 dests[label] = d
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
64 d = d.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
65 # Check if the object in the source exists in the destination
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
66 s = source.ref.firstChild()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
67 d = dest.ref.firstChild()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
68 while s:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
69 print s,d
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
70 sid = s.getAttribute("id")
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
71 if maps.has_key(sid):
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
72 o = maps[sid]
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
73 else:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
74 o = None
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
75 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
76 label = s.getAttribute("inkscape:label")
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
77 # Use i8nkscape:label to identidy the equipvalent objects
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
78 if label:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
79 if dests.hasattr(label.value()):
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
80 self.updateTweenObject(obj,typ,s,dests[label.value()],percent,o)
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
81 s = s.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
82 continue
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
83 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
84 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
85 # Search obejcts in the destination
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
86 while d:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
87 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
88 d.getAttribute("inkscape:label")
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
89 d = d.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
90 continue
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
91 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
92 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
93 if s.name() == d.name():
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
94 self.updateTweenObject(obj,typ,s,d,percent,o)
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
95 d = d.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
96 break
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
97 d = d.next()
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
98 s = s.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
99
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
100 def parseTransform(self,obj):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
101 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
102 Return the transform matrix of an object
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
103 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
104 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
105 t = obj.getAttribute("transform")
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
106 print t
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
107 if t[0:9] == 'translate':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
108 print "translate"
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
109 fields = t[10:].split(',')
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
110 x = float(fields[0])
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
111 fields = fields[1].split(')')
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
112 y = float(fields[0])
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
113 return [1,0,0,1,x,y]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
114 elif t[0:6] == 'matrix':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
115 print "matrix"
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
116 fields=t[7:].split(')')
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
117 fields = fields[0].split(',')
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
118 return [float(fields[0]),float(fields[1]),float(fields[2]),float(fields[3]),float(fields[4]),float(fields[5])]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
119 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
120 #traceback.print_exc()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
121 return [1,0,0,1,0,0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
122
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
123 def invA(self,m):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
124 d = m[0]*m[3]-m[2]*m[1]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
125 return [m[3]/d, -m[1]/d, -m[2]/d, m[0]/d, (m[1]*m[5]-m[4]*m[3])/d, (m[4]*m[2]-m[0]*m[5])/d]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
126
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
127 def mulA(self,a,b):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
128 return [a[0]*b[0]+a[1]*b[2],
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
129 a[0]*b[1]+a[1]*b[3],
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
130 a[2]*b[0]+a[3]*b[2],
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
131 a[2]*b[1]+a[3]*b[3],
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
132 a[0]*b[4]+a[1]*b[5]+a[4],
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
133 a[2]*b[4]+a[3]*b[5]+a[5]]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
134
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
135 def decomposition(self,m):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
136 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
137 Decompose the affine matrix into production of translation,rotation,shear and scale.
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
138 The algorithm is documented at http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
139 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
140 if m[0]*m[3] == m[1]*m[2]:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
141 print "The affine matrix is singular"
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
142 return [1,0,0,1,0,0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
143 A=m[0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
144 B=m[2]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
145 C=m[1]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
146 D=m[3]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
147 E=m[4]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
148 F=m[5]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
149 sx = math.sqrt(A*A+B*B)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
150 A = A/sx
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
151 B = B/sx
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
152 shear = m[0]*m[1]+m[2]*m[3]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
153 C = C - A*shear
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
154 D = D - B*shear
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
155 sy = math.sqrt(C*C+D*D)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
156 C = C/sy
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
157 D = D/sy
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
158 r = A*D-B*C
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
159 if r == -1:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
160 shear = -shear
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
161 sy = -sy
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
162 R = math.atan2(B,A)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
163 return [sx,sy, R, E,F]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
164
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
165
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
166 def updateTweenObject(self,obj,typ,s,d,p,newobj):
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
167 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
168 Generate tweened object in the @obj by using s and d in the @p percent
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
169 http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
170 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
171 if typ == 'relocate':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
172 newobj = s.duplicate(self.document)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
173 top = self.document.createElement("svg:g")
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
174 top.setAttribute("ref", s.getAttribute("id"))
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
175 top.appendChild(newobj)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
176 obj.appendChild(top)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
177 if s.name() == 'svg:g':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
178 # Parse the translate or matrix
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
179 sm = self.parseTransform(s)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
180 dm = self.parseTransform(d)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
181 top.setAttribute("transform","translate(%g,%g)" % ((dm[2]-sm[2])*p,(dm[5]-sm[5])*p))
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
182 else:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
183 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
184 sx = float(s.getAttribute("x"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
185 sy = float(s.getAttribute("y"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
186 dx = float(d.getAttribute("x"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
187 dy = float(d.getAttribute("y"))
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
188 tx = (dx-sx)*p
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
189 ty = (dy-sy)*p
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
190 print tx,ty
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
191 top.setAttribute("transform","translate(%g,%g)" % (tx,ty))
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
192 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
193 traceback.print_exc()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
194 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
195 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
196 elif typ == 'scale':
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
197 self.updateTweenObjectScale(obj,s,d,p,newobj)
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
198 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
199 elif typ == 'normal':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
200 newobj = s.duplicate(self.document)
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
201 newobj.setAttribute("ref", s.getAttribute("id"))
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
202 top = self.document.createElement("svg:g")
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
203 top.appendChild(newobj)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
204 obj.appendChild(top)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
205 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
206
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
207 def updateTweenObjectScale(self,obj,s,d,p,newobj):
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
208 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
209 Generate a new group which contains the original group and then
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
210 add the transform matrix to generate a tween frame between the
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
211 origin and destination scene group.
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
212
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
213 We will parse the transform matrix of the @s and @d and then
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
214 generate the matrix which is (1-p) of @s and p percent of @d.
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
215 """
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
216 if newobj == None:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
217 newobj = s.duplicate(self.document)
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
218 top = self.document.createElement("svg:g")
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
219 top.setAttribute("ref",s.getAttribute("id"))
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
220 top.appendChild(newobj)
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
221 obj.appendChild(top)
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
222 else:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
223 top = newobj
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
224 newobj = top.firstChild()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
225
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
226 if s.name() == 'svg:g':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
227 # Parse the translate or matrix
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
228 #
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
229 # D = B inv(A)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
230 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
231 item = self.nodeToItem[s.getAttribute("id")]
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
232 (ox,oy) = item.getCenter()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
233 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
234 ox = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
235 oy = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
236 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
237 item = self.nodeToItem[d.getAttribute("id")]
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
238 (dx,dy) = item.getCenter()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
239 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
240 dx = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
241 dy = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
242
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
243 sm = self.parseTransform(s)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
244 ss = self.decomposition(sm)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
245 dm = self.parseTransform(d)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
246 dd = self.decomposition(dm)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
247 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
248 sy = (ss[1]*(1-p)+dd[1]*p)/ss[0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
249 a = ss[2]*(1-p)+dd[2]*p-ss[2]
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
250 tx = ox*(1-p)+dx*p
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
251 ty = oy*(1-p)+dy*p
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
252 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
253 m = self.mulA([sx,0,0,sy,0,0],m)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
254 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height])
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
255 m = self.mulA([1,0,0,1,tx,self.height-ty],m)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
256
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
257 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5]))
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
258 else:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
259 try:
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
260 try:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
261 sw = float(s.getAttribute("width"))
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
262 except:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
263 sw = 1
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
264 try:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
265 sh = float(s.getAttribute("height"))
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
266 except:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
267 sh = 1
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
268 try:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
269 dw = float(d.getAttribute("width"))
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
270 except:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
271 dw = 1
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
272 try:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
273 dh = float(d.getAttribute("height"))
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
274 except:
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
275 dh = 1
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
276 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
277 item = self.nodeToItem[s.getAttribute("id")]
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
278 (ox,oy) = item.getCenter()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
279 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
280 ox = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
281 oy = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
282 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
283 item = self.nodeToItem[d.getAttribute("id")]
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
284 (dx,dy) = item.getCenter()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
285 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
286 dx = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
287 dy = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
288 try:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
289 sm = self.parseTransform(s)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
290 ss = self.decomposition(sm)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
291 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
292 ss = [1,1,0,0,0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
293 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
294 try:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
295 dm = self.parseTransform(d)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
296 dd = self.decomposition(dm)
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
297 print "dd=",dd
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
298 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
299 dd = [1,1,0,0,0]
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
300 dd[0] = dd[0]*dw/sw
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
301 dd[1] = dd[1]*dh/sh
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
302 print "ss[0]=",ss[0],"dd[0]=",dd[0]
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
303 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
304 sy = (ss[1]*(1-p)+dd[1]*p)/ss[1]
1146
e14ec6d1a661 CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents: 1141
diff changeset
305 print "sx=",sx,"sy=",sy
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
306 a = ss[2]*(1-p)+dd[2]*p-ss[2]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
307 tx = ox*(1-p)+dx*p
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
308 ty = oy*(1-p)+dy*p
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
309 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
310 m = self.mulA([sx,0,0,sy,0,0],m)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
311 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height])
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
312 m = self.mulA([1,0,0,1,tx,self.height-ty],m)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
313
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
314 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5]))
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
315 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
316 traceback.print_exc()