Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

70 linhas
1.7KB

  1. import weakref
  2. import pygame
  3. def _getWeakRef(fn):
  4. if not hasattr(fn, "__func__"):
  5. return None
  6. if hasattr(fn, "__self__"):
  7. return weakref.WeakMethod(fn)
  8. return weakref.ref(fn)
  9. class EventError(Exception):
  10. pass
  11. class _Events:
  12. def __init__(self):
  13. self._signals = {}
  14. def _ClearUnreferenced(self):
  15. def isempty(r):
  16. return r() is not None
  17. for signal, sigs in self._signals:
  18. self._signals[signal] = filter(isempty, sigs)
  19. def listen(self, signal, fn):
  20. ref = _getWeakRef(fn)
  21. if ref is None or ref() is None:
  22. raise EventError("Expected a function callback.")
  23. if not signal in self._signals:
  24. self._signals[signal] = []
  25. if not ref in self._signals[signal]:
  26. self._signal[signal].append(ref)
  27. def unlisten(self, signal, fn):
  28. ref = _getWeakRef(fn)
  29. if res is None or ref() is None:
  30. return # Not a function. Nothing to do.
  31. if signal in self._signals:
  32. if ref in self._signals[signal]:
  33. self._signals[signal].remove(ref)
  34. def unlisten_all(self, signal):
  35. if signal in self._signals:
  36. del self._signals[signal]
  37. def emit(self, signal, data):
  38. if signal in self._signals:
  39. for r in self._signals[signal]:
  40. fn = r()
  41. if fn is not None:
  42. fn(signal, data)
  43. self._ClearUnreferenced()
  44. # Create the actual Events instance :)
  45. Events = _Events()
  46. def pollEmitter():
  47. for event in pygame.event.get():
  48. #TODO: For each event obtains, convert it for the above Event dispatcher
  49. pass