annotate engine/extensions/timer.py @ 172:3fe0e68f4269

* Fixing funny typo. Chewie works on the RGB Zero-Projekt.
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 22 Jan 2009 15:35:22 +0000
parents fbc55c6f57cf
children
rev   line source
165
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
1 import fife
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
2
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
3 """
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
4 Convenient timers
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
5 =================
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
6
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
7 Usage::
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
8 import timer
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
9 timer.init( my_fife_engine.getTimeManager() )
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
10 def spam():
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
11 print "SPAM SPAM ",
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
12 return "More spam?" # a string is a true value, so it's repeated.
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
13 repeater = timer.repeatCall(500,spam)
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
14 def stop_spam():
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
15 repeater.stop()
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
16 print "BACON EGGS AND SPAM"
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
17 timer.delayCall(50000,stop_spam)
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
18
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
19 """
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
20
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
21
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
22 _manager = None
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
23 _alltimers = {}
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
24
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
25 def init(timemanager):
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
26 """
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
27 Initialize timers.
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
28
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
29 @param timemanager: A L{fife.TimeManager} as retuned by L{fife.Engine.getTimeManager}.
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
30 """
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
31 global _manager
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
32 _manager = timemanager
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
33
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
34 class Timer(fife.TimeEvent):
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
35 def __init__(self,delay=0,callback=None):
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
36 super(Timer,self).__init__(0)
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
37 self.is_registered = False
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
38 self.callback = callback
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
39 self.setPeriod(delay)
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
40
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
41 def start(self):
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
42 if self.is_registered:
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
43 return
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
44 self.is_registered = True
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
45 global _alltimers
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
46 _alltimers[self]=1
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
47
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
48 _manager.registerEvent(self)
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
49
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
50 def stop(self):
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
51 if not self.is_registered:
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
52 return
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
53 self.is_registered = False
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
54 global _alltimers
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
55 del _alltimers[self]
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
56
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
57 _manager.unregisterEvent(self)
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
58
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
59 def updateEvent(self,delta):
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
60 if callable(self.callback):
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
61 self.callback()
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
62
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
63 def delayCall(delay,callback):
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
64 """
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
65 Delay a function call by a number of milliseconds.
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
66
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
67 @param delay Delay in milliseconds.
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
68 @param callback The function to call.
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
69
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
70 @return The timer.
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
71 """
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
72 timer = Timer(delay)
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
73 def real_callback():
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
74 timer.stop()
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
75 callback()
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
76 timer.callback = real_callback
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
77 timer.start()
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
78 return timer
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
79
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
80 from traceback import print_exc
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
81 def repeatCall(period,callback):
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
82 """
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
83 Repeat a function call.
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
84
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
85 @param period Period between calls in milliseconds.
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
86 @param callback The function to call.
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
87
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
88 @return The timer.
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
89
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
90 The call is repeated until the callback returns a False
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
91 value (i.e. None) or the timer is stopped.
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
92 """
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
93 timer = Timer(period)
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
94 def real_callback():
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
95 try:
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
96 if not callback():
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
97 timer.stop()
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
98 except Exception:
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
99 print_exc()
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
100 timer.stop()
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
101
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
102 timer.callback = real_callback
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
103 timer.start()
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
104 return timer
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
105
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
106 __all__ = [init,Timer,delayCall,repeatCall]
fbc55c6f57cf (Convenient) Timers.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
107