mouse.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. ########################################################################
  2. # Hello Worlds - Libre 3D RPG game.
  3. # Copyright (C) 2020 CYBERDEViL
  4. #
  5. # This file is part of Hello Worlds.
  6. #
  7. # Hello Worlds is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Hello Worlds is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. class MouseEventHandler:
  22. def __init__(self, eventName):
  23. self._eventName = eventName
  24. self._callbacks = {}
  25. base.accept(eventName, self.__event)
  26. def __event(self):
  27. # LUI goes for everything..
  28. if base.luiHasMouse(): return
  29. for priority, callbackList in sorted(self._callbacks.items()):
  30. for callback in callbackList:
  31. if callback[0](*callback[1]):
  32. return # Callback accepted
  33. def addCallback(self, callback, args=[], priority=0):
  34. if priority not in self._callbacks:
  35. self._callbacks.update({ priority : [] })
  36. self._callbacks[priority].append([callback, args])
  37. class MouseHandler:
  38. """ Note: callbacks must return True if they used the event and
  39. False when they don't.
  40. """
  41. def __init__(self):
  42. self._eventHandlers = {
  43. 'mouse1' : MouseEventHandler('mouse1'),
  44. 'mouse1-up' : MouseEventHandler('mouse1-up'),
  45. 'mouse3' : MouseEventHandler('mouse3'),
  46. 'mouse3-up' : MouseEventHandler('mouse3-up'),
  47. 'wheel_up' : MouseEventHandler('wheel_up'),
  48. 'wheel_down': MouseEventHandler('wheel_down')
  49. }
  50. def connect(self, eventName, callback, args=[], priority=0):
  51. self._eventHandlers[eventName].addCallback(callback, args=args, priority=priority)