X-Git-Url: https://svn.cri.ensmp.fr/git/minwii.git/blobdiff_plain/a1cb64098e2a34d115378fdced1044da37e94395..64f8cd4d684f0be29308516446a85432c6a4d396:/src/app/eventutils.py diff --git a/src/app/eventutils.py b/src/app/eventutils.py index 10ddbad..969ad81 100755 --- a/src/app/eventutils.py +++ b/src/app/eventutils.py @@ -27,28 +27,90 @@ $Id$ $URL$ """ import types +import pygame +from StringIO import StringIO +from logging import getLogger, basicConfig, DEBUG +import sys +basicConfig(level=DEBUG, stream = sys.stdout) +console = getLogger('minwii.eventutils') -def event_handler(eventType) : - def doRename(m) : - m.__name__ = 'eventHandler%s' % eventType +class _EventDispatcher : + def __init__(self) : + self.registry = {} + + def addEventListener(self, eventType, listener) : + if self.registry.has_key(eventType) : + self.registry[eventType][listener] = True + else : + self.registry[eventType] = {listener:True} + + def removeEventListener(self, eventType, listener) : + try : + del self.registry[eventType][listener] + except KeyError : + console.warn("no listener to remove") + + def dispatchEvents(self) : + events = pygame.event.get() + for event in events : + #if event.type == pygame.MOUSEMOTION : + # if hasattr(event, 'wiimoteid') : + # console.info(event) + # #return + + listeners = self.registry.get(event.type, {}) + for listener in listeners.keys() : + listener(event) + def reset(self) : + self.registry = {} + + def __repr__(self) : + out = StringIO() + keys = self.registry.keys() + keys.sort() + for k in keys : + print >> out, "event", k + for listener in self.registry[k].keys() : + print >> out, listener.__name__ + out.seek(0) + return out.read() + + +EventDispatcher = _EventDispatcher() + +def event_handler(*eventTypes) : + def markFunctionAsListener(m) : + m.__islistener__ = True + m.__eventtypes__ = eventTypes return m - return doRename + return markFunctionAsListener -class MetaRenamer(type): - def __new__(mcs, name, bases, dict) : - for k, v in dict.items() : - if isinstance(v, types.FunctionType) : - if k != v.__name__ : - print 'renommage de %s en %s' % (k, v.__name__) - dict[v.__name__] = v - del dict[k] - return type.__new__(mcs, name, bases, dict) +class EventInitializer(type): + + def __init__(cls, name, bases, dict) : + def init_listeners(self) : + for k, v in dict.items() : + if isinstance(v, types.FunctionType) and hasattr(v, '__islistener__') : + listener = getattr(self, k) + for eventType in v.__eventtypes__ : + EventDispatcher.addEventListener(eventType, listener) + + def ctor(self, *args, **kw) : + default_ctor = dict.get('__init__') + if not default_ctor : + super(cls, self).__init__(*args, **kw) + else : + default_ctor(self, *args, **kw) + init_listeners(self) + + cls.__init__ = ctor class EventHandlerMixin(object) : - __metaclass__ = MetaRenamer + __metaclass__ = EventInitializer - def input(self, event) : - handler = getattr(self, 'eventHandler%s' % event.type, lambda e:None) - handler(event) + #def input(self) : + # event = pygame.event.wait() + # handler = getattr(self, 'eventHandler%s' % event.type, lambda e:None) + # handler(event)