# HG changeset patch # User phoku@33b003aa-7bff-0310-803a-e67f0ece8222 # Date 1224093892 0 # Node ID fbc55c6f57cfe608de33d884ef0d0c9ffaf7b0b7 # Parent 5b04a7d3ded6f51cccda64218543c55a3dbf7eee (Convenient) Timers. diff -r 5b04a7d3ded6 -r fbc55c6f57cf clients/pychan_demo/pychan_test.py --- a/clients/pychan_demo/pychan_test.py Wed Oct 15 14:57:04 2008 +0000 +++ b/clients/pychan_demo/pychan_test.py Wed Oct 15 18:04:52 2008 +0000 @@ -36,6 +36,19 @@ self.widget.hide() self.widget = None +def testTimer(): + import timer + timer.init( pychan.manager.engine.getTimeManager() ) + def spam(): + print "SPAM SPAM" + return 1 + repeater = timer.repeatCall(500,spam) + def stop_spam(): + repeater.stop() + print "BACON EGGS AND SPAM" + timer.delayCall(5000,stop_spam) + + class DemoApplication(basicapplication.ApplicationBase): def __init__(self): super(DemoApplication,self).__init__() @@ -75,6 +88,7 @@ self.currentExample = None self.creditsWidget = None + testTimer() def selectExample(self): if self.demoList.selected_item is None: return diff -r 5b04a7d3ded6 -r fbc55c6f57cf engine/extensions/timer.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/engine/extensions/timer.py Wed Oct 15 18:04:52 2008 +0000 @@ -0,0 +1,107 @@ +import fife + +""" +Convenient timers +================= + +Usage:: + import timer + timer.init( my_fife_engine.getTimeManager() ) + def spam(): + print "SPAM SPAM ", + return "More spam?" # a string is a true value, so it's repeated. + repeater = timer.repeatCall(500,spam) + def stop_spam(): + repeater.stop() + print "BACON EGGS AND SPAM" + timer.delayCall(50000,stop_spam) + +""" + + +_manager = None +_alltimers = {} + +def init(timemanager): + """ + Initialize timers. + + @param timemanager: A L{fife.TimeManager} as retuned by L{fife.Engine.getTimeManager}. + """ + global _manager + _manager = timemanager + +class Timer(fife.TimeEvent): + def __init__(self,delay=0,callback=None): + super(Timer,self).__init__(0) + self.is_registered = False + self.callback = callback + self.setPeriod(delay) + + def start(self): + if self.is_registered: + return + self.is_registered = True + global _alltimers + _alltimers[self]=1 + + _manager.registerEvent(self) + + def stop(self): + if not self.is_registered: + return + self.is_registered = False + global _alltimers + del _alltimers[self] + + _manager.unregisterEvent(self) + + def updateEvent(self,delta): + if callable(self.callback): + self.callback() + +def delayCall(delay,callback): + """ + Delay a function call by a number of milliseconds. + + @param delay Delay in milliseconds. + @param callback The function to call. + + @return The timer. + """ + timer = Timer(delay) + def real_callback(): + timer.stop() + callback() + timer.callback = real_callback + timer.start() + return timer + +from traceback import print_exc +def repeatCall(period,callback): + """ + Repeat a function call. + + @param period Period between calls in milliseconds. + @param callback The function to call. + + @return The timer. + + The call is repeated until the callback returns a False + value (i.e. None) or the timer is stopped. + """ + timer = Timer(period) + def real_callback(): + try: + if not callback(): + timer.stop() + except Exception: + print_exc() + timer.stop() + + timer.callback = real_callback + timer.start() + return timer + +__all__ = [init,Timer,delayCall,repeatCall] +