/usr/share/pyshared/pymt/clock.py is in python-pymt 0.5.1-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | '''
Clock: a clock with scheduled events
You can add new event like this ::
def my_callback(dt):
pass
# call my_callback every 0.5 seconds
getClock().schedule_interval(my_callback, 0.5)
# call my_callback in 5 seconds
getClock().schedule_once(my_callback, 5)
If the callback return False, the schedule will be removed.
'''
__all__ = ('Clock', 'getClock')
import time
from pymt.weakmethod import WeakMethod
class _Event(object):
def __init__(self, loop, callback, timeout, starttime):
self.loop = loop
self.callback = WeakMethod(callback)
self.timeout = timeout
self._last_dt = starttime
self._dt = 0.
def do(self, dt):
if self.callback.is_dead():
return False
self.callback()(dt)
def tick(self, curtime):
# timeout happen ?
if curtime - self._last_dt < self.timeout:
return True
# calculate current timediff for this event
self._dt = curtime - self._last_dt
self._last_dt = curtime
# call the callback
if self.callback.is_dead():
return False
ret = self.callback()(self._dt)
# if it's a once event, don't care about the result
# just remove the event
if not self.loop:
return False
# if user return an explicit false,
# remove the event
if ret == False:
return False
return True
class Clock(object):
'''A clock object, that support events'''
__slots__ = ('_dt', '_last_fps_tick', '_last_tick', '_fps',
'_fps_counter', '_events')
def __init__(self):
self._dt = 0
self._last_tick = time.time()
self._fps = 0
self._fps_counter = 0
self._last_fps_tick = None
self._events = []
def tick(self):
'''Advance clock to the next step. Must be called every frame.
The default clock have the tick() function called by PyMT'''
# tick the current time
current = time.time()
self._dt = current - self._last_tick
self._fps_counter += 1
self._last_tick = current
# calculate fps things
if self._last_fps_tick == None:
self._last_fps_tick = current
elif current - self._last_fps_tick > 1:
self._fps = self._fps_counter / float(current - self._last_fps_tick)
self._last_fps_tick = current
self._fps_counter = 0
# process event
self._process_events()
return self._dt
def get_fps(self):
'''Get the current FPS calculated by the clock'''
return self._fps
def get_time(self):
'''Get the last tick made by the clock'''
return self._last_tick
def schedule_once(self, callback, timeout=0):
'''Schedule an event in <timeout> seconds'''
event = _Event(False, callback, timeout, self._last_tick)
self._events.append(event)
return event
def schedule_interval(self, callback, timeout):
'''Schedule a event to be call every <timeout> seconds'''
event = _Event(True, callback, timeout, self._last_tick)
self._events.append(event)
return event
def unschedule(self, callback):
'''Remove a previous schedule event'''
self._events = [x for x in self._events if x.callback() != callback]
def _process_events(self):
for event in self._events[:]:
if event.tick(self._last_tick) == False:
# event may be already removed by the callback
if event in self._events:
self._events.remove(event)
# create a default clock
_default_clock = Clock()
# make it available
def getClock():
'''Return the clock instance used by PyMT'''
return _default_clock
|