Package dtk :: Package ui :: Module timeline

Source Code for Module dtk.ui.timeline

 1  #! /usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3   
 4  # Copyright (C) 2011 ~ 2012 Deepin, Inc. 
 5  #               2011 ~ 2012 Wang Yong 
 6  #  
 7  # Author:     Wang Yong <lazycat.manatee@gmail.com> 
 8  # Maintainer: Wang Yong <lazycat.manatee@gmail.com> 
 9  #  
10  # This program is free software: you can redistribute it and/or modify 
11  # it under the terms of the GNU General Public License as published by 
12  # the Free Software Foundation, either version 3 of the License, or 
13  # any later version. 
14  #  
15  # This program is distributed in the hope that it will be useful, 
16  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
18  # GNU General Public License for more details. 
19  #  
20  # You should have received a copy of the GNU General Public License 
21  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
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   
30 -class Timeline(gobject.GObject):
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
41 - def __init__(self, duration, curve):
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
56 - def run(self):
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
68 - def stop(self):
69 ''' 70 Stop. 71 ''' 72 self._stopped = True
73
74 - def update(self):
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