|
|
|
|
|
|
|
|
import pygame |
|
|
import pygame |
|
|
|
|
|
|
|
|
def _getWeakRef(fn): |
|
|
def _getWeakRef(fn): |
|
|
if not hasattr(fn, "__func__"): |
|
|
|
|
|
|
|
|
if not hasattr(fn, "__call__"): |
|
|
return None |
|
|
return None |
|
|
if hasattr(fn, "__self__"): |
|
|
if hasattr(fn, "__self__"): |
|
|
return weakref.WeakMethod(fn) |
|
|
return weakref.WeakMethod(fn) |
|
|
|
|
|
|
|
|
self._signals = {} |
|
|
self._signals = {} |
|
|
|
|
|
|
|
|
def _ClearUnreferenced(self): |
|
|
def _ClearUnreferenced(self): |
|
|
def isempty(r): |
|
|
|
|
|
return r() is not None |
|
|
|
|
|
|
|
|
|
|
|
for signal, sigs in self._signals: |
|
|
|
|
|
self._signals[signal] = filter(isempty, sigs) |
|
|
|
|
|
|
|
|
for signal in self._signals: |
|
|
|
|
|
removal = [s for s in self._signals[signal] if s() is None] |
|
|
|
|
|
for r in removal: |
|
|
|
|
|
self._signals[signal].remove(r) |
|
|
|
|
|
|
|
|
def listen(self, signal, fn): |
|
|
def listen(self, signal, fn): |
|
|
ref = _getWeakRef(fn) |
|
|
ref = _getWeakRef(fn) |
|
|
|
|
|
|
|
|
if not signal in self._signals: |
|
|
if not signal in self._signals: |
|
|
self._signals[signal] = [] |
|
|
self._signals[signal] = [] |
|
|
if not ref in self._signals[signal]: |
|
|
if not ref in self._signals[signal]: |
|
|
self._signal[signal].append(ref) |
|
|
|
|
|
|
|
|
self._signals[signal].append(ref) |
|
|
|
|
|
|
|
|
def unlisten(self, signal, fn): |
|
|
def unlisten(self, signal, fn): |
|
|
ref = _getWeakRef(fn) |
|
|
ref = _getWeakRef(fn) |
|
|
|
|
|
|
|
|
del self._signals[signal] |
|
|
del self._signals[signal] |
|
|
|
|
|
|
|
|
def emit(self, signal, data): |
|
|
def emit(self, signal, data): |
|
|
if signal in self._signals: |
|
|
|
|
|
for r in self._signals[signal]: |
|
|
|
|
|
|
|
|
if signal in self._signals: |
|
|
|
|
|
for r in self._signals[signal]: |
|
|
fn = r() |
|
|
fn = r() |
|
|
if fn is not None: |
|
|
if fn is not None: |
|
|
fn(signal, data) |
|
|
|
|
|
self._ClearUnreferenced() |
|
|
|
|
|
|
|
|
fn(signal, data) |
|
|
|
|
|
self._ClearUnreferenced() |
|
|
|
|
|
|
|
|
# Create the actual Events instance :) |
|
|
# Create the actual Events instance :) |
|
|
Events = _Events() |
|
|
Events = _Events() |
|
|
|
|
|
|
|
|
lastTick = k[1] |
|
|
lastTick = k[1] |
|
|
_DOWNKEYS.remove(k) |
|
|
_DOWNKEYS.remove(k) |
|
|
if tick - lastTick <= _ClickDelayMax: |
|
|
if tick - lastTick <= _ClickDelayMax: |
|
|
Events.emit("KEYPRESSED", {key:key, mod:pygame.key.get_mods()}) |
|
|
|
|
|
|
|
|
Events.emit("KEYPRESSED", {"key":key, "mod":pygame.key.get_mods()}) |
|
|
return # Done. |
|
|
return # Done. |
|
|
# We found nothing, boss. |
|
|
# We found nothing, boss. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
btnsrc.remove(b) |
|
|
btnsrc.remove(b) |
|
|
if tick - lastTick <= _ClickDelayMax: |
|
|
if tick - lastTick <= _ClickDelayMax: |
|
|
if device >= 0: |
|
|
if device >= 0: |
|
|
Events.emit("JOYBUTTONPRESSED", {joy:device, button:button}) |
|
|
|
|
|
|
|
|
Events.emit("JOYBUTTONPRESSED", {"joy":device, "button":button}) |
|
|
else: |
|
|
else: |
|
|
Events.emit("MOUSEBUTTONPRESSED", {pos:pygame.mouse.get_pos(), button:button}) |
|
|
|
|
|
|
|
|
Events.emit("MOUSEBUTTONPRESSED", {"pos":pygame.mouse.get_pos(), "button":button}) |
|
|
return # Done. |
|
|
return # Done. |
|
|
# We found nothing, boss. |
|
|
# We found nothing, boss. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Events.emit("QUIT", {}) |
|
|
Events.emit("QUIT", {}) |
|
|
elif event.type == pygame.KEYDOWN: |
|
|
elif event.type == pygame.KEYDOWN: |
|
|
_WatchKey(event.key) |
|
|
_WatchKey(event.key) |
|
|
Events.emit("KEYDOWN", {unicode:event.unicode, key:event.key, mod:event.mod}) |
|
|
|
|
|
|
|
|
Events.emit("KEYDOWN", {"unicode":event.unicode, "key":event.key, "mod":event.mod}) |
|
|
elif event.type == pygame.KEYUP: |
|
|
elif event.type == pygame.KEYUP: |
|
|
Events.emit("KEYUP", {key:event.key, mod:event.mod}) |
|
|
|
|
|
|
|
|
Events.emit("KEYUP", {"key":event.key, "mod":event.mod}) |
|
|
_ReleaseKey(event.key) |
|
|
_ReleaseKey(event.key) |
|
|
elif event.type == pygame.MOUSEMOTION: |
|
|
|
|
|
Events.emit("MOUSEMOTION", {pos:event.pos, rel:event.rel, buttons:event.buttons}) |
|
|
|
|
|
|
|
|
elif event.type == pygame.MOUSEMOTION: |
|
|
|
|
|
Events.emit("MOUSEMOTION", {"pos":event.pos, "rel":event.rel, "buttons":event.buttons}) |
|
|
elif event.type == pygame.MOUSEBUTTONUP: |
|
|
elif event.type == pygame.MOUSEBUTTONUP: |
|
|
Events.emit("MOUSEBUTTONUP", {pos:event.pos, button:event.button}) |
|
|
|
|
|
|
|
|
Events.emit("MOUSEBUTTONUP", {"pos":event.pos, "button":event.button}) |
|
|
_ReleaseButton(-1, event.button) |
|
|
_ReleaseButton(-1, event.button) |
|
|
elif event.type == pygame.MOUSEBUTTONDOWN: |
|
|
elif event.type == pygame.MOUSEBUTTONDOWN: |
|
|
_WatchButton(-1, event.button) |
|
|
_WatchButton(-1, event.button) |
|
|
Events.emit("MOUSEBUTTONDOWN", {pos:event.pos, button:event.button}) |
|
|
|
|
|
|
|
|
Events.emit("MOUSEBUTTONDOWN", {"pos":event.pos, "button":event.button}) |
|
|
elif event.type == pygame.VIDEORESIZE: |
|
|
elif event.type == pygame.VIDEORESIZE: |
|
|
Events.emit("VIDEORESIZE", {size:event.size, w:event.w, h:event.h}) |
|
|
|
|
|
|
|
|
Events.emit("VIDEORESIZE", {"size":event.size, "w":event.w, "h":event.h}) |
|
|
elif event.type == pygame.VIDEOEXPOSE: |
|
|
elif event.type == pygame.VIDEOEXPOSE: |
|
|
Events.emit("VIDEOEXPOSE", {}) |
|
|
Events.emit("VIDEOEXPOSE", {}) |
|
|
elif event.type == pygame.JOYAXISMOTION: |
|
|
elif event.type == pygame.JOYAXISMOTION: |
|
|
Events.emit("JOYAXISMOTION", {joy:event.joy, axis:event.axis, value:event.value}) |
|
|
|
|
|
|
|
|
Events.emit("JOYAXISMOTION", {"joy":event.joy, "axis":event.axis, "value":event.value}) |
|
|
elif event.type == pygame.JOYBALLMOTION: |
|
|
elif event.type == pygame.JOYBALLMOTION: |
|
|
Events.emit("JOYBALLMOTION", {joy:event.joy, ball:event.ball, res:event.rel}) |
|
|
|
|
|
|
|
|
Events.emit("JOYBALLMOTION", {"joy":event.joy, "ball":event.ball, "res":event.rel}) |
|
|
elif event.type == pygame.JOYHATMOTION: |
|
|
elif event.type == pygame.JOYHATMOTION: |
|
|
Events.emit("JOYHATMOTION", {joy:event.joy, hat:event.hat, value:event.value}) |
|
|
|
|
|
|
|
|
Events.emit("JOYHATMOTION", {"joy":event.joy, "hat":event.hat, "value":event.value}) |
|
|
elif event.type == pygame.JOYBUTTONUP: |
|
|
elif event.type == pygame.JOYBUTTONUP: |
|
|
Events.emit("JOYBUTTONUP", {joy:event.joy, button:event.button}) |
|
|
|
|
|
|
|
|
Events.emit("JOYBUTTONUP", {"joy":event.joy, "button":event.button}) |
|
|
_ReleaseButton(event.joy, event.button) |
|
|
_ReleaseButton(event.joy, event.button) |
|
|
elif event.type == pygame.JOYBUTTONDOWN: |
|
|
elif event.type == pygame.JOYBUTTONDOWN: |
|
|
_WatchButton(event.joy, event.button) |
|
|
_WatchButton(event.joy, event.button) |
|
|
Events.emit("JOYBUTTONDOWN", {joy:event.joy, button:event.button}) |
|
|
|
|
|
|
|
|
Events.emit("JOYBUTTONDOWN", {"joy":event.joy, "button":event.button}) |
|
|
else: |
|
|
else: |
|
|
Events.emit("PYGUSER_{}".format(event.code), {}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if hasattr(event, "code"): |
|
|
|
|
|
Events.emit("PYGUSER_{}".format(event.code), {}) |
|
|
|
|
|
else: |
|
|
|
|
|
print("Unkown pygame event type '{}'".format(event.type)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|