class EventDispatcher: def __init__(self): from weakref import WeakKeyDictionary self.listeners = WeakKeyDictionary() self.msgs = 0 def register(self, l): self.listeners[ l ] = 1 def unregister(self, l): self.listeners.pop(l) def post(self, e): if isinstance(e, TickEvent): print "Message count : ", self.msgs self.msgs = 0 else: self.msgs += 1 for l in self.listeners.keys(): l.notify(e) ### Events class Event: def __init__(self): pass class TickEvent(Event): def __init__(self): pass class SimulationStartedEvent(Event): def __init__(self, sim): self.sim = sim class QuitEvent(Event): def __init__(self): pass class EnvironmentBuiltEvent(Event): def __init__(self, env): self.env = env class AgentPlacedEvent(Event): def __init__(self, agent): self.agent = agent class AgentMoveRequest(Event): def __init__(self): pass class AgentMovedEvent(Event): def __init__(self, agent): self.agent = agent class AgentKillEvent(Event): def __init__(self, agent): self.agent = agent class AgentAddEvent(Event): def __init__(self, agent): self.agent = agent class ViewSlideRequest(Event): def __init__(self, dir): self.dir = dir class MouseClickEvent(Event): def __init__(self, pos, button): self.pos = pos self.button = button