And as thus we sat in darkness, Each one busy in his prayers, “We are lost!” the captain shouted, As he staggered down the stairs. But his little daughter whispered, As she took his icy hand, “Isn’t God upon the ocean, Just the same as on the land?” Then we kissed the little maiden, And we spoke in better cheer; And we anchored safe in harbor When the morn was shining clear.
39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
# event system
|
|
|
|
EVENT_CALLBACKS = {}
|
|
|
|
def add_listener(event, callback, priority = 5):
|
|
if event not in EVENT_CALLBACKS:
|
|
EVENT_CALLBACKS[event] = []
|
|
|
|
if (priority, callback) not in EVENT_CALLBACKS[event]:
|
|
EVENT_CALLBACKS[event].append((priority, callback))
|
|
EVENT_CALLBACKS[event].sort(key = lambda x: x[0])
|
|
|
|
def remove_listener(event, callback, priority = 5):
|
|
if event in EVENT_CALLBACKS and (priority, callback) in EVENT_CALLBACKS[event]:
|
|
EVENT_CALLBACKS[event].remove((priority, callback))
|
|
|
|
if event in EVENT_CALLBACKS and not EVENT_CALLBACKS[event]:
|
|
del EVENT_CALLBACKS[event]
|
|
|
|
class Event:
|
|
def __init__(self, name, data):
|
|
self.stop_processing = False
|
|
self.prevent_default = False
|
|
self.name = name
|
|
self.data = data
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
if self.name not in EVENT_CALLBACKS:
|
|
return True
|
|
|
|
for item in list(EVENT_CALLBACKS[self.name]):
|
|
item[1](self, *args, **kwargs)
|
|
if self.stop_processing:
|
|
break
|
|
|
|
return not self.prevent_default
|
|
|
|
# vim: set expandtab:sw=4:ts=4:
|