1
2
3
4
5 import math
6
7 avg = None
8 g_Player = None
9
10 g_ActiveAnimations = {}
11
12
13 try:
14 from . import avg
15 except ValueError:
16 pass
17
18
21
27
28
30 """
31 Base class for animations that change libavg node attributes by interpolating
32 over a set amount of time. Constructing an animation object starts the
33 animation. If abort() isn't needed, there is no need to hold on to the object -
34 it will exist exactly as long as the animation lasts and then disappear.
35
36 The animation framework makes sure that only one animation per attribute of a
37 node runs at any given time. If a second one is started, the first one is
38 silently aborted.
39 """
40 - def __init__(self, node, attrName, duration, useInt, onStop):
58 """
59 Stops the animation. Does not call onStop()
60 """
61 if not(self.isDone()):
62 self._remove()
64 """
65 Returns True if the animation has run its course.
66 """
67 return self.__done
75
77 """
78 Class that animates an attribute of a libavg node by interpolating linearly
79 between start and end values.
80 """
81 - def __init__(self, node, attrName, duration, startValue, endValue, useInt=False, onStop=None):
82 """
83 @param node: The libavg node object to animate.
84 @param attrName: The name of the attribute to change. Must be a numeric
85 attribute.
86 @param duration: The length of the animation in milliseconds.
87 @param startValue: Initial value of the attribute.
88 @param endValue: Value of the attribute after duration has elapsed.
89 @param useInt: If True, the attribute is always set to an integer value.
90 @param onStop: Python callable to invoke when duration has elapsed and
91 the animation has finished. This can be used to chain
92 animations together by using lambda to create a second animation.
93 """
94 SimpleAnim.__init__(self, node, attrName, duration, useInt, onStop)
95 self.__startValue = startValue
96 self.__endValue = endValue
97 self._step()
99 if not(self.isDone()):
100 part = ((float(g_Player.getFrameTime())-self.startTime)/self.duration)
101 if part > 1.0:
102 part = 1.0
103 curValue = self.__startValue+(self.__endValue-self.__startValue)*part
104 if self.useInt:
105 curValue = int(curValue+0.5)
106 setattr(self.node, self.attrName, curValue)
108 setattr(self.node, self.attrName, self.__endValue)
109 self._remove()
110 if self.onStop != None:
111 self.onStop()
112
114 - def __init__(self, node, attrName, duration, startValue, endValue,
115 easeInDuration, easeOutDuration, useInt=False, onStop=None):
116 SimpleAnim.__init__(self, node, attrName, duration, useInt, onStop)
117 self.__startValue = startValue
118 self.__endValue = endValue
119 self.__easeInDuration = float(easeInDuration)/duration
120 self.__easeOutDuration = float(easeOutDuration)/duration
121 self._step()
123 def ease(t, easeInDuration, easeOutDuration):
124
125 if t > 1:
126 t=1
127 accelDist = easeInDuration*2/math.pi
128 decelDist = easeOutDuration*2/math.pi
129 if t<easeInDuration:
130
131 nt=t/easeInDuration
132 s=math.sin(-math.pi/2+nt*math.pi/2)+1;
133 dist=s*accelDist;
134 elif t > 1-easeOutDuration:
135
136 nt = (t-(1-easeOutDuration))/easeOutDuration
137 s = math.sin(nt*math.pi/2)
138 dist = accelDist+(1-easeInDuration-easeOutDuration)+s*decelDist
139 else:
140
141 dist = accelDist+t-easeInDuration
142 return dist/(accelDist+(1-easeInDuration-easeOutDuration)+decelDist)
143 if not(self.isDone()):
144 t = (float(g_Player.getFrameTime())-self.startTime)/self.duration;
145 part = ease(t, self.__easeInDuration, self.__easeOutDuration)
146 curValue = self.__startValue+(self.__endValue-self.__startValue)*part
147 if self.useInt:
148 curValue = int(curValue+0.5)
149 setattr(self.node, self.attrName, curValue)
151 setattr(self.node, self.attrName, self.__endValue)
152 self._remove()
153 if self.onStop != None:
154 self.onStop()
155
157 """
158 Class that animates an attribute of a libavg node by interpolating
159 between start and end values using a cubic spline.
160 """
161 - def __init__(self, node, attrName, duration,
162 startValue, startSpeed, endValue, endSpeed, useInt=False, onStop=None):
163 """
164 @param node: The libavg node object to animate.
165 @param attrName: The name of the attribute to change. Must be a numeric
166 attribute.
167 @param duration: The length of the animation in milliseconds.
168 @param startValue: Initial value of the attribute.
169 @param startSpeed: Initial speed of the animation.
170 @param endValue: Value of the attribute after duration has elapsed.
171 @param endSpeed: Final speed of the animation.
172 @param useInt: If True, the attribute is always set to an integer value.
173 @param onStop: Python callable to invoke when duration has elapsed and
174 the animation has finished. This can be used to chain
175 animations together by using lambda to create a second animation.
176 """
177 SimpleAnim.__init__(self, node, attrName, duration, useInt, onStop)
178 self.__startValue = startValue+0.0
179 self.__startSpeed = startSpeed
180 self.__endValue = endValue
181 self.__endSpeed = endSpeed
182 self.__a = -2*(self.__endValue-self.__startValue)+self.__startSpeed+self.__endSpeed
183 self.__b = 3*(self.__endValue-self.__startValue)-2*self.__startSpeed-self.__endSpeed
184 self.__c = self.__startSpeed
185 self.__d = self.__startValue
186 self._step()
188 if not(self.isDone()):
189 part = ((float(g_Player.getFrameTime())-self.startTime)/self.duration)
190 if part > 1.0:
191 part = 1.0
192 curValue = ((self.__a*part+self.__b)*part+self.__c)*part+self.__d
193 if self.useInt:
194 curValue = int(curValue+0.5)
195 setattr(self.node, self.attrName, curValue)
197 setattr(self.node, self.attrName, self.__endValue)
198 self._remove()
199 if self.onStop != None:
200 self.onStop()
201
203 """
204 Fades the opacity of a node to zero.
205 @param node: The node to fade.
206 @param duration: Length of the fade in milliseconds.
207 """
208 curValue = getattr(node, "opacity")
209 return LinearAnim(node, "opacity", duration, curValue, 0)
210
211 -def fadeIn(node, duration, max=1.0):
212 """
213 Fades the opacity of a node.
214 @param node: The node to fade.
215 @param duration: Length of the fade in milliseconds.
216 @param max: The opacity of the node at the end of the fade.
217 """
218 curValue = getattr(node, "opacity")
219 return LinearAnim(node, "opacity", duration, curValue, max)
220
221
223 """
224 Class that animates an attribute of a libavg node continuously and
225 linearly. The animation will not stop until the abort() method is called.
226 A possible use case is the continuous rotation of an object.
227
228 """
229 - def __init__(self, node, attrName, startValue, speed, useInt=False):
230 """
231 @param node: The libavg node object to animate.
232 @param attrName: The name of the attribute to change. Must be a numeric
233 attribute.
234 @param startValue: Initial value of the attribute.
235 @param speed: Animation speed, value to be added per second.
236 @param useInt: If True, the attribute is always set to an integer value.
237 """
238 SimpleAnim.__init__(self, node, attrName, 0, useInt, None)
239 self.__startValue = startValue
240 self.__speed = speed
241 self._step()
243 time = (float(g_Player.getFrameTime())-self.startTime)/1000
244 curValue = self.__startValue+time*self.__speed
245 if self.useInt:
246 curValue = int(curValue+0.5)
247 setattr(self.node, self.attrName, curValue)
248
254