mouse.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. from panda3d.core import WindowProperties
  22. class MouseEventHandler:
  23. def __init__(self, eventName):
  24. self._eventName = eventName
  25. self._callbacks = {}
  26. base.accept(eventName, self.__event)
  27. def __event(self):
  28. # LUI goes for everything..
  29. if base.uiManager.hasMouse(): return
  30. for priority, callbackList in sorted(self._callbacks.items()):
  31. for callback in callbackList:
  32. if callback[0](*callback[1]):
  33. return # Callback accepted
  34. def addCallback(self, callback, args=[], priority=0):
  35. if priority not in self._callbacks:
  36. self._callbacks.update({ priority : [] })
  37. self._callbacks[priority].append([callback, args])
  38. class MouseHandler:
  39. """ Note: callbacks must return True if they used the event and
  40. False when they don't.
  41. """
  42. def __init__(self):
  43. self._eventHandlers = {
  44. 'mouse1' : MouseEventHandler('mouse1'),
  45. 'mouse1-up' : MouseEventHandler('mouse1-up'),
  46. 'mouse3' : MouseEventHandler('mouse3'),
  47. 'mouse3-up' : MouseEventHandler('mouse3-up'),
  48. 'wheel_up' : MouseEventHandler('wheel_up'),
  49. 'wheel_down': MouseEventHandler('wheel_down')
  50. }
  51. def connect(self, eventName, callback, args=[], priority=0):
  52. self._eventHandlers[eventName].addCallback(callback, args=args, priority=priority)
  53. def showCursor(self, state=True):
  54. wprops = WindowProperties()
  55. wprops.setCursorHidden(not state)
  56. base.win.requestProperties(wprops)
  57. def cursorVisible(self):
  58. return not WindowProperties().getCursorHidden()