Mercurial > fife-parpg
comparison engine/python/fife/extensions/fife_timer.py @ 607:eab690c748a3
This is a more permanent fix for the timer issues. Pychan was using the timers incorrectly. This has now been fixed. It has eliminated the need to have global lists of timers in fife_timer. This has not been tested extensivly.
[t:483]
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Fri, 17 Sep 2010 19:55:15 +0000 |
parents | a5c890f0e757 |
children |
comparison
equal
deleted
inserted
replaced
606:a5c890f0e757 | 607:eab690c748a3 |
---|---|
25 Convenient timers | 25 Convenient timers |
26 ================= | 26 ================= |
27 | 27 |
28 Usage:: | 28 Usage:: |
29 import fife.extensions.fife_timer | 29 import fife.extensions.fife_timer |
30 | |
30 fife_timer.init( my_fife_engine.getTimeManager() ) | 31 fife_timer.init( my_fife_engine.getTimeManager() ) |
32 | |
31 def spam(): | 33 def spam(): |
32 print "SPAM SPAM ", | 34 print "SPAM SPAM ", |
35 | |
33 repeater = fife_timer.repeatCall(500,spam) | 36 repeater = fife_timer.repeatCall(500,spam) |
37 | |
34 def stop_spam(): | 38 def stop_spam(): |
35 repeater.stop() | 39 repeater.stop() |
36 print "BACON EGGS AND SPAM" | 40 print "BACON EGGS AND SPAM" |
37 fife_timer.delayCall(50000,stop_spam) | 41 |
42 delayed = fife_timer.delayCall(50000,stop_spam) | |
38 | 43 |
39 """ | 44 """ |
40 | 45 |
41 from fife import fife | 46 from fife import fife |
42 | 47 |
48 #global time manager | |
43 _manager = None | 49 _manager = None |
44 _alltimers = [] | |
45 _deadtimers = [] | |
46 | 50 |
47 def init(timemanager): | 51 def init(timemanager): |
48 """ | 52 """ |
49 Initialize timers. | 53 Initialize timers. |
50 | 54 |
57 """ | 61 """ |
58 Timer | 62 Timer |
59 | 63 |
60 This class wraps the fife.TimeEvent class to make it easily usable from Python | 64 This class wraps the fife.TimeEvent class to make it easily usable from Python |
61 It allows for a TimeEvent to be executed once or multiple times. | 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. | |
62 """ | 69 """ |
63 def __init__(self,delay=0,callback=None,repeat=0): | 70 def __init__(self,delay=0,callback=None,repeat=0): |
64 """ | 71 """ |
65 @param delay: The delay in milliseconds to execute the callback | 72 @param delay: The delay in milliseconds to execute the callback |
66 @param callback: The function to execute | 73 @param callback: The function to execute when the time delay has passed |
67 @param repeat: The number of times to execute the callback. 1=once, 0=forever | 74 @param repeat: The number of times to execute the callback. 1=once, 0=forever |
68 """ | 75 """ |
69 super(Timer,self).__init__(delay) | 76 super(Timer,self).__init__(delay) |
70 self._active = False | 77 self._active = False |
71 self._callback = callback | 78 self._callback = callback |
87 | 94 |
88 self._executed = 0 | 95 self._executed = 0 |
89 | 96 |
90 self.setLastUpdateTime(self._manager.getTime()) | 97 self.setLastUpdateTime(self._manager.getTime()) |
91 self._manager.registerEvent(self) | 98 self._manager.registerEvent(self) |
92 | |
93 global _alltimers | |
94 global _deadtimers | |
95 | |
96 _alltimers.append(self) | |
97 | 99 |
98 def stop(self): | 100 def stop(self): |
99 """ | 101 """ |
100 Stops the timer | 102 Stops the timer |
101 | 103 |
102 This unregisters the timer from the time manager. The time manager does NOT | 104 This unregisters the timer from the time manager. |
103 delete the timer so make sure you keep a reference to this timer and ensure | |
104 python doesnt delete the timer prematurely. | |
105 """ | 105 """ |
106 if not self._active: | 106 if not self._active: |
107 return | 107 return |
108 | 108 |
109 self._active = False | 109 self._active = False |
110 self._manager.unregisterEvent(self) | 110 self._manager.unregisterEvent(self) |
111 | 111 |
112 | |
113 #FIXME: See ticket #483 in trac. This is a temporary solution and needs | |
114 #to be reworked. | |
115 | |
116 global _alltimers | |
117 global _deadtimers | |
118 | |
119 #clean up any dead timers | |
120 del _deadtimers[:] | |
121 | |
122 #Ddd this timer to the dead timers list to be removed next time a timer | |
123 #is stopped. | |
124 _deadtimers.append(self) | |
125 | |
126 #finally remove self from the global timer list | |
127 _alltimers.remove(self) | |
128 | |
129 | |
130 def updateEvent(self,delta): | 112 def updateEvent(self,delta): |
131 """ | 113 """ |
132 This is called by FIFE::TimeManager when the delay has passed. | 114 This is called by FIFE::TimeManager when the delay has passed. |
133 | 115 |
134 Should not be called directly. | 116 Should not be called directly. |
139 if self._executed >= self._repeat: | 121 if self._executed >= self._repeat: |
140 self.stop() | 122 self.stop() |
141 | 123 |
142 if callable(self._callback): | 124 if callable(self._callback): |
143 self._callback() | 125 self._callback() |
144 | 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 | |
145 | 201 |
146 def delayCall(delay,callback): | 202 def delayCall(delay,callback): |
147 """ | 203 """ |
148 Delay a function call by a number of milliseconds. | 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. | |
149 | 208 |
150 @param delay: Delay in milliseconds. | 209 @param delay: Delay in milliseconds. |
151 @param callback: The function to call. | 210 @param callback: The function to call. |
152 | 211 |
153 @return: The timer. | 212 @return: The timer. |
160 | 219 |
161 def repeatCall(period,callback): | 220 def repeatCall(period,callback): |
162 """ | 221 """ |
163 Repeat a function call. The call is repeated until the timer is stopped. | 222 Repeat a function call. The call is repeated until the timer is stopped. |
164 | 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 | |
165 @param period: Period between calls in milliseconds. | 227 @param period: Period between calls in milliseconds. |
166 @param callback: The function to call. | 228 @param callback: The function to call. |
167 | 229 |
168 @return: The timer. | 230 @return: The timer. |
169 @rtype: L{Timer} | 231 @rtype: L{Timer} |