Mercurial > fife-parpg
comparison engine/python/fife/extensions/fife_timer.py @ 378:64738befdf3b
bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author | vtchill@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 11 Jan 2010 23:34:52 +0000 |
parents | |
children | 7f1c42b66aa4 |
comparison
equal
deleted
inserted
replaced
377:fe6fb0e0ed23 | 378:64738befdf3b |
---|---|
1 # -*- coding: utf-8 -*- | |
2 | |
3 # #################################################################### | |
4 # Copyright (C) 2005-2009 by the FIFE team | |
5 # http://www.fifengine.de | |
6 # This file is part of FIFE. | |
7 # | |
8 # FIFE is free software; you can redistribute it and/or | |
9 # modify it under the terms of the GNU Lesser General Public | |
10 # License as published by the Free Software Foundation; either | |
11 # version 2.1 of the License, or (at your option) any later version. | |
12 # | |
13 # This library is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 # Lesser General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU Lesser General Public | |
19 # License along with this library; if not, write to the | |
20 # Free Software Foundation, Inc., | |
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 # #################################################################### | |
23 | |
24 from fife import fife | |
25 | |
26 """ | |
27 Convenient timers | |
28 ================= | |
29 | |
30 Usage:: | |
31 import timer | |
32 timer.init( my_fife_engine.getTimeManager() ) | |
33 def spam(): | |
34 print "SPAM SPAM ", | |
35 return "More spam?" # a string is a true value, so it's repeated. | |
36 repeater = timer.repeatCall(500,spam) | |
37 def stop_spam(): | |
38 repeater.stop() | |
39 print "BACON EGGS AND SPAM" | |
40 timer.delayCall(50000,stop_spam) | |
41 | |
42 """ | |
43 | |
44 | |
45 _manager = None | |
46 _alltimers = {} | |
47 | |
48 def init(timemanager): | |
49 """ | |
50 Initialize timers. | |
51 | |
52 @param timemanager: A L{fife.TimeManager} as retuned by L{fife.Engine.getTimeManager}. | |
53 """ | |
54 global _manager | |
55 _manager = timemanager | |
56 | |
57 class Timer(fife.TimeEvent): | |
58 def __init__(self,delay=0,callback=None): | |
59 super(Timer,self).__init__(0) | |
60 self.manager = _manager | |
61 self.is_registered = False | |
62 self.callback = callback | |
63 self.setPeriod(delay) | |
64 | |
65 def start(self): | |
66 if self.is_registered: | |
67 return | |
68 self.is_registered = True | |
69 global _alltimers | |
70 _alltimers[self]=1 | |
71 self.manager.registerEvent(self) | |
72 | |
73 def stop(self): | |
74 if not self.is_registered: | |
75 return | |
76 self.is_registered = False | |
77 global _alltimers | |
78 del _alltimers[self] | |
79 self.manager.unregisterEvent(self) | |
80 | |
81 def updateEvent(self,delta): | |
82 if callable(self.callback): | |
83 self.callback() | |
84 | |
85 def delayCall(delay,callback): | |
86 """ | |
87 Delay a function call by a number of milliseconds. | |
88 | |
89 @param delay Delay in milliseconds. | |
90 @param callback The function to call. | |
91 | |
92 @return The timer. | |
93 """ | |
94 timer = Timer(delay) | |
95 def cbwa(c, *args): | |
96 c(*args) | |
97 def real_callback(c, t): | |
98 t.stop() | |
99 c() | |
100 | |
101 timer.callback = cbwa(real_callback, callback, timer) | |
102 timer.start() | |
103 return timer | |
104 | |
105 from traceback import print_exc | |
106 def repeatCall(period,callback): | |
107 """ | |
108 Repeat a function call. | |
109 | |
110 @param period Period between calls in milliseconds. | |
111 @param callback The function to call. | |
112 | |
113 @return The timer. | |
114 | |
115 The call is repeated until the callback returns a False | |
116 value (i.e. None) or the timer is stopped. | |
117 """ | |
118 timer = Timer(period) | |
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() | |
129 return timer | |
130 | |
131 __all__ = [init,Timer,delayCall,repeatCall] | |
132 |