Package libavg :: Module anim

Source Code for Module libavg.anim

  1  # TODO: 
  2  # - loops 
  3  # - Folgen, Gruppen 
  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   
19 -def getNumRunningAnims():
20 return len(g_ActiveAnimations)
21
22 -def abortAnim(node, attrName):
23 global g_ActiveAnimations 24 if g_ActiveAnimations.has_key((node, attrName)): 25 curAnim = g_ActiveAnimations.get((node, attrName)) 26 curAnim._remove()
27 28
29 -class SimpleAnim:
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):
41 global g_Player 42 global g_ActiveAnimations 43 abortAnim(node, attrName) 44 g_ActiveAnimations[(node, attrName)] = self 45 46 g_Player = avg.Player.get() 47 self.node = node 48 self.attrName = attrName 49 self.duration = duration 50 self.startTime = g_Player.getFrameTime() 51 self.onStop = onStop 52 self.useInt = useInt 53 if duration != 0: 54 self.__stopTimeout = g_Player.setTimeout(duration, self._regularStop) 55 self.__interval = g_Player.setOnFrameHandler(self._step) 56 self.__done = False
57 - def abort(self):
58 """ 59 Stops the animation. Does not call onStop() 60 """ 61 if not(self.isDone()): 62 self._remove()
63 - def isDone(self):
64 """ 65 Returns True if the animation has run its course. 66 """ 67 return self.__done
68 - def _remove(self):
69 global g_ActiveAnimations 70 self.__done = True 71 g_ActiveAnimations.pop((self.node, self.attrName)) 72 g_Player.clearInterval(self.__interval) 73 if self.duration != 0: 74 g_Player.clearInterval(self.__stopTimeout)
75
76 -class LinearAnim(SimpleAnim):
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()
98 - def _step(self):
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)
107 - def _regularStop(self):
108 setattr(self.node, self.attrName, self.__endValue) 109 self._remove() 110 if self.onStop != None: 111 self.onStop()
112
113 -class EaseInOutAnim(SimpleAnim):
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()
122 - def _step(self):
123 def ease(t, easeInDuration, easeOutDuration): 124 # All times here are normalized to be between 0 and 1 125 if t > 1: 126 t=1 127 accelDist = easeInDuration*2/math.pi 128 decelDist = easeOutDuration*2/math.pi 129 if t<easeInDuration: 130 # Acceleration stage 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 # Deceleration stage 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 # Linear stage 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)
150 - def _regularStop(self):
151 setattr(self.node, self.attrName, self.__endValue) 152 self._remove() 153 if self.onStop != None: 154 self.onStop()
155
156 -class SplineAnim(SimpleAnim):
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()
187 - def _step(self):
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)
196 - def _regularStop(self):
197 setattr(self.node, self.attrName, self.__endValue) 198 self._remove() 199 if self.onStop != None: 200 self.onStop()
201
202 -def fadeOut(node, duration):
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
222 -class ContinuousAnim(SimpleAnim):
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()
242 - def _step(self):
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
249 -def init(g_avg):
250 global avg 251 global g_ActiveAnimations 252 avg = g_avg 253 g_ActiveAnimations = {}
254