"""
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')
class _EventDispatcher :
def __init__(self) :
def addEventListener(self, eventType, listener) :
if self.registry.has_key(eventType) :
- self.registry[eventType].append(listener)
+ self.registry[eventType][listener] = True
else :
- self.registry[eventType] = [listener]
+ 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 :
- listeners = self.registry.get(event.type, [])
- for listener in listeners :
+ 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()
if isinstance(v, types.FunctionType) and hasattr(v, '__islistener__') :
listener = getattr(self, k)
EventDispatcher.addEventListener(v.__eventtype__, listener)
- del v.__islistener__
- del v.__eventtype__
def ctor(self, *args, **kw) :
- init_listeners(self)
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