1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
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
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
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
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
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
151
153 return self._callback
154
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
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
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
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
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
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