Package rats :: Module tween
[hide private]
[frames] | no frames]

Source Code for Module rats.tween

 1  """ 
 2  Motion tween equations. 
 3  """ 
 4  #  Easing Equations v1.5 
 5  #  May 1, 2003 
 6  #  (c) 2003 Robert Penner, all rights reserved.  
 7  #  This work is subject to the terms in  
 8  #  http://www.robertpenner.com/easing_terms_of_use.html.   
 9  #   
10  #  These tweening functions provide different flavors of  
11  #  math-based motion under a consistent API.  
12  #   
13  #  Types of easing: 
14  #   
15  #         Linear 
16  #         Quadratic 
17  #         Cubic 
18  #         Quartic 
19  #         Quintic 
20  #         Sinusoidal 
21  #         Exponential 
22  #         Circular 
23  #         Elastic 
24  #         Back 
25  #         Bounce 
26  # 
27  #  Changes: 
28  #  1.5 - added bounce easing 
29  #  1.4 - added elastic and back easing 
30  #  1.3 - tweaked the exponential easing functions to make endpoints exact 
31  #  1.2 - inline optimizations (changing t and multiplying in one step) 
32  #        -- thanks to Tatsuo Kato for the idea 
33  #   
34  #  Discussed in Chapter 7 of  
35  #  Robert Penner's Programming Macromedia Flash MX 
36  #  (including graphs of the easing equations) 
37  #   
38  #  http://www.robertpenner.com/profmx 
39  #  http://www.amazon.com/exec/obidos/ASIN/0072223561/robertpennerc-20 
40  # 
41   
42  LINEAR_TWEEN = 1 
43   
44 -class Tween(object):
45 """ 46 Motion tween tool. 47 48 """
49 - def __init__(self, start_val, end_val, duration, current_time, 50 kind=LINEAR_TWEEN):
51 # TODO: use time from pygame of os ? 52 # self.t = current_time # current time 53 self.b = start_val # beginning value 54 self.c = end_val - start_val # change in value 55 self.d = duration # duration 56 self.kind = kind
57
58 - def tick(self, current_time):
59 """ 60 Simple wrapper for easing methods. 61 62 All the easing methods take those arguments : 63 t: current time 64 b: beginning value 65 c: change in value 66 d: duration 67 """ 68 method = self.linear_tween 69 if self.kind == LINEAR_TWEEN: 70 method = self.linear_tween 71 return method(float(current_time), self.b, self.c, self.d)
72
73 - def linear_tween(self, t, b, c, d):
74 """ 75 simple linear tweening - no easing 76 """ 77 return c * (t / d) + b
78 79 if __name__ == '__main__': 80 print "this test will tween from 0 to 100 in steps of 10" 81 kind = LINEAR_TWEEN 82 start_val = 0.0 83 end_val = 100.0 84 duration = 10.0 85 current_time = 0.0 86 tween = Tween(start_val, end_val, duration, current_time, kind) 87 for t in range(11): 88 print tween.tick(t) 89