1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import gobject
24 import math
25
26 CURVE_LINEAR = lambda x: x
27 CURVE_SINE = lambda x: math.sin(math.pi / 2 * x)
28 FRAMERATE = 30.0
29
31 '''
32 Timeline.
33 '''
34
35 __gtype_name__ = 'Timeline'
36 __gsignals__ = {
37 'update': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_FLOAT,)),
38 'completed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())
39 }
40
42 '''
43 Initialize Timeline class.
44
45 @param duration: Animation duration.
46 @param curve: Animation curve.
47 '''
48 gobject.GObject.__init__(self)
49
50 self.duration = duration
51 self.curve = curve
52
53 self._states = []
54 self._stopped = False
55
57 '''
58 Run.
59 '''
60 n_frames = (self.duration / 1000.0) * FRAMERATE
61
62 while len(self._states) <= n_frames:
63 self._states.append(self.curve(len(self._states) * (1.0 / n_frames)))
64 self._states.reverse()
65
66 gobject.timeout_add(int(self.duration / n_frames), self.update)
67
69 '''
70 Stop.
71 '''
72 self._stopped = True
73
75 '''
76 Update.
77 '''
78 if self._stopped:
79 self.emit('completed')
80 return False
81
82 self.emit('update', self._states.pop())
83 if len(self._states) == 0:
84 self.emit('completed')
85 return False
86 return True
87
88 gobject.type_register(Timeline)
89