Package fife :: Package extensions :: Module fife_timer
[hide private]
[frames] | no frames]

Source Code for Module fife.extensions.fife_timer

  1  # -*- coding: utf-8 -*- 
  2   
  3  # #################################################################### 
  4  #  Copyright (C) 2005-2019 by the FIFE team 
  5  #  http://www.fifengine.net 
  6  #  This file is part of FIFE. 
  7  # 
  8  #  FIFE is free software; you can redistribute it and/or 
  9  #  modify it under the terms of the GNU Lesser General Public 
 10  #  License as published by the Free Software Foundation; either 
 11  #  version 2.1 of the License, or (at your option) any later version. 
 12  # 
 13  #  This library is distributed in the hope that it will be useful, 
 14  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 16  #  Lesser General Public License for more details. 
 17  # 
 18  #  You should have received a copy of the GNU Lesser General Public 
 19  #  License along with this library; if not, write to the 
 20  #  Free Software Foundation, Inc., 
 21  #  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 22  # #################################################################### 
 23   
 24  """ 
 25  Convenient timers 
 26  ================= 
 27   
 28  Usage:: 
 29    import fife.extensions.fife_timer 
 30   
 31    fife_timer.init( my_fife_engine.getTimeManager() ) 
 32   
 33    def spam(): 
 34       print "SPAM SPAM ", 
 35   
 36    repeater = fife_timer.repeatCall(500,spam) 
 37   
 38    def stop_spam(): 
 39       repeater.stop() 
 40       print "BACON EGGS AND SPAM" 
 41   
 42    delayed = fife_timer.delayCall(50000,stop_spam) 
 43   
 44  """ 
 45   
 46  from fife import fife 
 47   
 48  #global time manager 
 49  _manager = None 
 50   
51 -def init(timemanager):
52 """ 53 Initialize timers. 54 55 @param timemanager: A L{fife.TimeManager} as retuned by L{fife.Engine.getTimeManager}. 56 """ 57 global _manager 58 _manager = timemanager
59
60 -class Timer(fife.TimeEvent):
61 """ 62 Timer 63 64 This class wraps the fife.TimeEvent class to make it easily usable from Python 65 It allows for a TimeEvent to be executed once or multiple times. 66 67 Remember FIFE::TimeManager does NOT delete the timer so make sure you keep a reference 68 to this timer to ensure python doesnt delete the timer prematurely. 69 """
70 - def __init__(self,delay=0,callback=None,repeat=0):
71 """ 72 @param delay: The delay in milliseconds to execute the callback 73 @param callback: The function to execute when the time delay has passed 74 @param repeat: The number of times to execute the callback. 1=once, 0=forever 75 """ 76 super(Timer,self).__init__(delay) 77 self._active = False 78 self._callback = callback 79 self._manager = _manager 80 self.setPeriod(delay) 81 self._repeat = repeat 82 self._executed = 0
83
84 - def start(self):
85 """ 86 Call this to start the timer. 87 88 This registers the timer with the time manager. The time manger then 89 calls the timers updateEvent() function when the delay time has passed. 90 """ 91 if self._active: 92 return 93 self._active = True 94 95 self._executed = 0 96 97 self.setLastUpdateTime(self._manager.getTime()) 98 self._manager.registerEvent(self)
99
100 - def stop(self):
101 """ 102 Stops the timer 103 104 This unregisters the timer from the time manager. 105 """ 106 if not self._active: 107 return 108 109 self._active = False 110 self._manager.unregisterEvent(self)
111
112 - def updateEvent(self,delta):
113 """ 114 This is called by FIFE::TimeManager when the delay has passed. 115 116 Should not be called directly. 117 """ 118 119 if self._repeat != 0: 120 self._executed += 1 121 if self._executed >= self._repeat: 122 self.stop() 123 124 if callable(self._callback): 125 self._callback()
126
127 - def _setDelay(self, delay):
128 """ 129 Sets how many milliseconds to wait before executing the callback. 130 131 The timer must not be active to change this value 132 133 @param delay: Number of milliseconds to wait before executing the callback. 134 @type delay: C{integer} 135 """ 136 137 if not self._active: 138 self.setPeriod(delay)
139
140 - def _getDelay(self):
141 """ 142 Returns the number of milliseconds to wait before executing the callback. 143 144 @return: Number of milliseconds. 145 @rtype: C{integer} 146 """ 147 return self.getPeriod()
148
149 - def _setCallback(self, callback):
150 self._callback = callback
151
152 - def _getCallback(self):
153 return self._callback
154
155 - def _setRepeat(self, repeat):
156 """ 157 Sets how many times the timer should be repeated. 158 159 The timer must not be active to change it's repeat value. 160 161 @param repeat: Number of times to repeat the timer. 0=forever, 1=once. 162 @type repeat: C{integer} 163 """ 164 165 if not self._active: 166 self._repeat = repeat
167
168 - def _getRepeat(self, repeat):
169 """ 170 Returns the number of times the timer will be executed. 171 172 @return: Number of times the timer will be executed. 173 @rtype: C{integer} 174 """ 175 return self._repeat
176
177 - def _getActive(self):
178 """ 179 Returns True if the timer is active and False if it is not. 180 181 @return: True if timer is active, False if it is not. 182 @rtype: C{boolean} 183 """ 184 return self._active
185
186 - def _getNumExecuted(self):
187 """ 188 Returns the number of times the timer has been executed 189 190 @return: Number of times the timer has been executed 191 @rtype: C{integer} 192 """ 193 return self._executed
194 195 delay = property(_getDelay, _setDelay) 196 callback = property(_getCallback, _setCallback) 197 repeat = property(_getRepeat, _setRepeat) 198 active = property(_getActive) 199 numexecuted = property(_getNumExecuted)
200 201
202 -def delayCall(delay,callback):
203 """ 204 Delay a function call by a number of milliseconds. 205 206 Remember to keep a reference to the timer this function returns. If you 207 do not python will delete the timer prematurely which may case a segfault. 208 209 @param delay: Delay in milliseconds. 210 @param callback: The function to call. 211 212 @return: The timer. 213 @rtype: L{Timer} 214 """ 215 timer = Timer(delay, callback, 1) 216 timer.start() 217 return timer
218 219
220 -def repeatCall(period,callback):
221 """ 222 Repeat a function call. The call is repeated until the timer is stopped. 223 224 Remember to keep a reference to the timer this function returns. If you 225 do not python will delete the timer prematurely which may case a segfault. 226 227 @param period: Period between calls in milliseconds. 228 @param callback: The function to call. 229 230 @return: The timer. 231 @rtype: L{Timer} 232 """ 233 timer = Timer(period, callback, 0) 234 timer.start() 235 return timer
236 237 __all__ = ['init','Timer','delayCall','repeatCall'] 238