Package libavg :: Module anim

Source Code for Module libavg.anim

  1  # TODO: 
  2  # - loops 
  3  # - Folgen, Gruppen 
  4   
5 -class SimpleAnim:
6 """ 7 Base class for animations that change libavg node attributes by interpolating 8 over a set amount of time. Constructing an animation object starts the 9 animation. If abort() isn't needed, there is no need to hold on to the object - 10 it will exist exactly as long as the animation lasts and then disappear. 11 """
12 - def __init__(self, node, attrName, duration, useInt, onStop):
13 self.node = node 14 self.attrName = attrName 15 self.duration = duration 16 self.startTime = g_Player.getFrameTime() 17 self.onStop = onStop 18 self.useInt = useInt
19
20 -class LinearAnim(SimpleAnim):
21 """ 22 Class that animates an attribute of a libavg node by interpolating linearly 23 between start and end values. 24 """
25 - def __init__(self, node, attrName, duration, startValue, endValue, useInt, onStop):
26 """ 27 @param node: The libavg node object to animate. 28 @param attrName: The name of the attribute to change. Must be a numeric 29 attribute. 30 @param duration: The length of the animation in milliseconds. 31 @param startValue: Initial value of the attribute. 32 @param endValue: Value of the attribute after duration has elapsed. 33 @param useInt: If True, the attribute is always set to an integer value. 34 @param onStop: Python callable to invoke when duration has elapsed and 35 the animation has finished. This can be used to chain 36 animations together by using lambda to create a second animation. 37 """ 38 SimpleAnim.__init__(self, node, attrName, duration, useInt, onStop) 39 self.__stopTimeout = g_Player.setTimeout(duration, self.__stop) 40 self.__interval = g_Player.setOnFrameHandler(self.__step) 41 self.__startValue = startValue 42 self.__endValue = endValue 43 self.__done = False 44 self.__step()
45 - def __step(self):
46 if not(self.__done): 47 part = ((float(g_Player.getFrameTime())-self.startTime)/self.duration) 48 if part > 1.0: 49 part = 1.0 50 curValue = self.__startValue+(self.__endValue-self.__startValue)*part 51 if self.useInt: 52 curValue = int(curValue+0.5) 53 setattr(self.node, self.attrName, curValue)
54 - def __stop(self):
55 setattr(self.node, self.attrName, self.__endValue) 56 self.__done = True 57 g_Player.clearInterval(self.__interval) 58 if self.onStop != None: 59 self.onStop()
60 - def abort(self):
61 """ 62 Stops the animation. 63 """ 64 if not(self.__done): 65 self.__done = True 66 g_Player.clearInterval(self.__interval) 67 g_Player.clearInterval(self.__stopTimeout)
68 - def isDone(self):
69 """ 70 Returns True if the animation has run its course. 71 """ 72 return self.__done
73
74 -class SplineAnim(SimpleAnim):
75 """ 76 Class that animates an attribute of a libavg node by interpolating 77 between start and end values using a cubic spline. 78 """
79 - def __init__(self, node, attrName, duration, 80 startValue, startSpeed, endValue, endSpeed, useInt, onStop):
81 """ 82 @param node: The libavg node object to animate. 83 @param attrName: The name of the attribute to change. Must be a numeric 84 attribute. 85 @param duration: The length of the animation in milliseconds. 86 @param startValue: Initial value of the attribute. 87 @param startSpeed: Initial speed of the animation. 88 @param endValue: Value of the attribute after duration has elapsed. 89 @param endSpeed: Final speed of the animation. 90 @param useInt: If True, the attribute is always set to an integer value. 91 @param onStop: Python callable to invoke when duration has elapsed and 92 the animation has finished. This can be used to chain 93 animations together by using lambda to create a second animation. 94 """ 95 SimpleAnim.__init__(self, node, attrName, duration, useInt, onStop) 96 g_Player.setTimeout(duration, self.__stop) 97 self.interval = g_Player.setOnFrameHandler(self.__step) 98 self.__startValue = startValue+0.0 99 self.__startSpeed = startSpeed 100 self.__endValue = endValue 101 self.__endSpeed = endSpeed 102 self.__a = -2*(self.__endValue-self.__startValue)+self.__startSpeed+self.__endSpeed 103 self.__b = 3*(self.__endValue-self.__startValue)-2*self.__startSpeed-self.__endSpeed 104 self.__c = self.__startSpeed 105 self.__d = self.__startValue 106 self.__done = 0 107 self.__step()
108 - def __step(self):
109 if not(self.__done): 110 part = ((float(g_Player.getFrameTime())-self.startTime)/self.duration) 111 if part > 1.0: 112 part = 1.0 113 curValue = ((self.__a*part+self.__b)*part+self.__c)*part+self.__d 114 if self.useInt: 115 curValue = int(curValue+0.5) 116 setattr(self.node, self.attrName, curValue)
117 - def __stop(self):
118 setattr(self.node, self.attrName, self.__endValue) 119 self.__done = 1 120 g_Player.clearInterval(self.interval) 121 if self.onStop != None: 122 self.onStop()
123
124 -def fadeOut(node, duration):
125 """ 126 Fades the opacity of a node to zero. 127 @param node: The node to fade. 128 @param duration: Length of the fade in milliseconds. 129 """ 130 curValue = getattr(node, "opacity") 131 LinearAnim(node, "opacity", duration, curValue, 0, 0, None)
132
133 -def fadeIn(node, duration, max):
134 """ 135 Fades the opacity of a node. 136 @param node: The node to fade. 137 @param duration: Length of the fade in milliseconds. 138 @param max: The opacity of the node at the end of the fade. 139 """ 140 curValue = getattr(node, "opacity") 141 LinearAnim(node, "opacity", duration, curValue, max, 0, None)
142
143 -def init(Player):
144 global g_Player 145 g_Player = Player
146