Mercurial > fife-parpg
comparison engine/extensions/fife_timer.py @ 255:51cc05d862f2
Merged editor_rewrite branch to trunk.
This contains changes that may break compatibility against existing clients.
For a list of changes that may affect your client, see: http://wiki.fifengine.de/Changes_to_pychan_and_FIFE_in_editor_rewrite_branch
author | cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 08 Jun 2009 16:00:02 +0000 |
parents | |
children | 48c99636453e |
comparison
equal
deleted
inserted
replaced
254:10b5f7f36dd4 | 255:51cc05d862f2 |
---|---|
1 # -*- coding: utf-8 -*- | |
2 import fife | |
3 | |
4 """ | |
5 Convenient timers | |
6 ================= | |
7 | |
8 Usage:: | |
9 import timer | |
10 timer.init( my_fife_engine.getTimeManager() ) | |
11 def spam(): | |
12 print "SPAM SPAM ", | |
13 return "More spam?" # a string is a true value, so it's repeated. | |
14 repeater = timer.repeatCall(500,spam) | |
15 def stop_spam(): | |
16 repeater.stop() | |
17 print "BACON EGGS AND SPAM" | |
18 timer.delayCall(50000,stop_spam) | |
19 | |
20 """ | |
21 | |
22 | |
23 _manager = None | |
24 _alltimers = {} | |
25 | |
26 def init(timemanager): | |
27 """ | |
28 Initialize timers. | |
29 | |
30 @param timemanager: A L{fife.TimeManager} as retuned by L{fife.Engine.getTimeManager}. | |
31 """ | |
32 global _manager | |
33 _manager = timemanager | |
34 | |
35 class Timer(fife.TimeEvent): | |
36 def __init__(self,delay=0,callback=None): | |
37 super(Timer,self).__init__(0) | |
38 self.manager = _manager | |
39 self.is_registered = False | |
40 self.callback = callback | |
41 self.setPeriod(delay) | |
42 | |
43 def start(self): | |
44 if self.is_registered: | |
45 return | |
46 self.is_registered = True | |
47 global _alltimers | |
48 _alltimers[self]=1 | |
49 self.manager.registerEvent(self) | |
50 | |
51 def stop(self): | |
52 if not self.is_registered: | |
53 return | |
54 self.is_registered = False | |
55 global _alltimers | |
56 del _alltimers[self] | |
57 self.manager.unregisterEvent(self) | |
58 | |
59 def updateEvent(self,delta): | |
60 if callable(self.callback): | |
61 self.callback() | |
62 | |
63 def delayCall(delay,callback): | |
64 """ | |
65 Delay a function call by a number of milliseconds. | |
66 | |
67 @param delay Delay in milliseconds. | |
68 @param callback The function to call. | |
69 | |
70 @return The timer. | |
71 """ | |
72 timer = Timer(delay) | |
73 def cbwa(c, *args): | |
74 c(*args) | |
75 def real_callback(c, t): | |
76 t.stop() | |
77 c() | |
78 | |
79 timer.callback = cbwa(real_callback, callback, timer) | |
80 timer.start() | |
81 return timer | |
82 | |
83 from traceback import print_exc | |
84 def repeatCall(period,callback): | |
85 """ | |
86 Repeat a function call. | |
87 | |
88 @param period Period between calls in milliseconds. | |
89 @param callback The function to call. | |
90 | |
91 @return The timer. | |
92 | |
93 The call is repeated until the callback returns a False | |
94 value (i.e. None) or the timer is stopped. | |
95 """ | |
96 timer = Timer(period) | |
97 def real_callback(): | |
98 try: | |
99 if not callback(): | |
100 timer.stop() | |
101 except Exception: | |
102 print_exc() | |
103 timer.stop() | |
104 | |
105 timer.callback = real_callback | |
106 timer.start() | |
107 return timer | |
108 | |
109 __all__ = [init,Timer,delayCall,repeatCall] | |
110 |