comparison engine/python/fife/extensions/fife_timer.py @ 492:16ceb3228324

Moved the SoundManager and the 2D math function (helpers) from the shooter demo to the fife extensions. Fixed fife_timer as it didn't work in the first place. Modified the SoundManager to use the now working fife_timer and removed the Timer class from the shooter demo entirely
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 07 May 2010 21:07:27 +0000
parents 23d01971e9c2
children e241d7553496
comparison
equal deleted inserted replaced
491:c4168eb47a44 492:16ceb3228324
26 """ 26 """
27 Convenient timers 27 Convenient timers
28 ================= 28 =================
29 29
30 Usage:: 30 Usage::
31 import timer 31 import fife.extensions.fife_timer
32 timer.init( my_fife_engine.getTimeManager() ) 32 fife_timer.init( my_fife_engine.getTimeManager() )
33 def spam(): 33 def spam():
34 print "SPAM SPAM ", 34 print "SPAM SPAM ",
35 return "More spam?" # a string is a true value, so it's repeated. 35 repeater = fife_timer.repeatCall(500,spam)
36 repeater = timer.repeatCall(500,spam)
37 def stop_spam(): 36 def stop_spam():
38 repeater.stop() 37 repeater.stop()
39 print "BACON EGGS AND SPAM" 38 print "BACON EGGS AND SPAM"
40 timer.delayCall(50000,stop_spam) 39 fife_timer.delayCall(50000,stop_spam)
41 40
42 """ 41 """
43 42
44 43
45 _manager = None 44 _manager = None
46 _alltimers = {} 45 _alltimers = []
47 46
48 def init(timemanager): 47 def init(timemanager):
49 """ 48 """
50 Initialize timers. 49 Initialize timers.
51 50
53 """ 52 """
54 global _manager 53 global _manager
55 _manager = timemanager 54 _manager = timemanager
56 55
57 class Timer(fife.TimeEvent): 56 class Timer(fife.TimeEvent):
58 def __init__(self,delay=0,callback=None): 57 """
59 super(Timer,self).__init__(0) 58 Timer
60 self.manager = _manager 59
61 self.is_registered = False 60 This class wraps the fife.TimeEvent class to make it easily usable from Python
62 self.callback = callback 61 It allows for a TimeEvent to be executed once or multiple times.
62 """
63 def __init__(self,delay=0,callback=None,repeat=0):
64 """
65 @param delay The delay in milliseconds to execute the callback
66 @param callback The function to execute
67 @param repeat The number of times to execute the callback. 1=once, 0=forever
68 """
69 super(Timer,self).__init__(delay)
70 self._is_registered = False
71 self._callback = callback
72 self._manager = _manager
63 self.setPeriod(delay) 73 self.setPeriod(delay)
74 self._repeat = repeat
75 self._executed = 0
64 76
65 def start(self): 77 def start(self):
66 if self.is_registered: 78 """
79 Must be called before the timer will start
80 """
81 if self._is_registered:
67 return 82 return
68 self.is_registered = True 83 self._is_registered = True
84 self._executed = 0
85 self._manager.registerEvent(self)
86
69 global _alltimers 87 global _alltimers
70 _alltimers[self]=1 88 _alltimers.append(self)
71 self.manager.registerEvent(self)
72 89
73 def stop(self): 90 def stop(self):
74 if not self.is_registered: 91 """
92 Stops the timer
93 """
94 if not self._is_registered:
75 return 95 return
76 self.is_registered = False 96 self._is_registered = False
97 self._manager.unregisterEvent(self)
98
77 global _alltimers 99 global _alltimers
78 del _alltimers[self] 100 _alltimers.remove(self)
79 self.manager.unregisterEvent(self) 101
80 102
81 def updateEvent(self,delta): 103 def updateEvent(self,delta):
82 if callable(self.callback): 104 """
83 self.callback() 105 Should not be called directly. This is called by FIFE.
106 """
107 if callable(self._callback):
108 self._callback()
109
110 if self._repeat != 0:
111 self._executed += 1
112 if self._executed >= self._repeat:
113 self.stop()
84 114
85 def delayCall(delay,callback): 115 def delayCall(delay,callback):
86 """ 116 """
87 Delay a function call by a number of milliseconds. 117 Delay a function call by a number of milliseconds.
88 118
89 @param delay Delay in milliseconds. 119 @param delay Delay in milliseconds.
90 @param callback The function to call. 120 @param callback The function to call.
91 121
92 @return The timer. 122 @return The timer.
93 """ 123 """
94 timer = Timer(delay) 124 timer = Timer(delay, callback, 1)
95
96 def real_callback(c, t):
97 t.stop()
98 c()
99
100 from fife.extensions.pychan.tools import callbackWithArguments as cbwa
101 timer.callback = cbwa(real_callback, callback, timer)
102 timer.start() 125 timer.start()
103 return timer 126 return timer
104 127
105 from traceback import print_exc 128
106 def repeatCall(period,callback): 129 def repeatCall(period,callback):
107 """ 130 """
108 Repeat a function call. 131 Repeat a function call.
109 132
110 @param period Period between calls in milliseconds. 133 @param period Period between calls in milliseconds.
111 @param callback The function to call. 134 @param callback The function to call.
112 135
113 @return The timer. 136 @return The timer.
114 137
115 The call is repeated until the callback returns a False 138 The call is repeated until the timer is stopped.
116 value (i.e. None) or the timer is stopped.
117 """ 139 """
118 timer = Timer(period) 140 timer = Timer(period, callback, 0)
119 def real_callback():
120 try:
121 if not callback():
122 timer.stop()
123 except Exception:
124 print_exc()
125 timer.stop()
126
127 timer.callback = real_callback
128 timer.start() 141 timer.start()
129 return timer 142 return timer
130 143
131 __all__ = [init,Timer,delayCall,repeatCall] 144 __all__ = ['init','Timer','delayCall','repeatCall']
132 145