joystick.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from typing import Callable, Dict
  2. from .custom_view import CustomView
  3. from .element import Element
  4. class JoystickView(CustomView):
  5. def __init__(self, on_start, on_move, on_end, **options):
  6. super().__init__('joystick', __file__, ['nipplejs.min.js'], **options)
  7. self.on_start = on_start
  8. self.on_move = on_move
  9. self.on_end = on_end
  10. self.allowed_events = ['onStart', 'onMove', 'onEnd']
  11. self.initialize(temp=False,
  12. onStart=self.handle_start,
  13. onMove=self.handle_move,
  14. onEnd=self.handle_end)
  15. def handle_start(self, msg):
  16. if self.on_start is not None:
  17. return self.on_start(msg)
  18. return False
  19. def handle_move(self, msg):
  20. if self.on_move is not None:
  21. return self.on_move(msg)
  22. return False
  23. def handle_end(self, msg):
  24. if self.on_end is not None:
  25. return self.on_end(msg)
  26. return False
  27. class Joystick(Element):
  28. def __init__(self,
  29. *,
  30. on_start: Callable = None,
  31. on_move: Callable = None,
  32. on_end: Callable = None,
  33. **options: Dict,
  34. ):
  35. """Joystick
  36. Create a joystick based on `nipple.js <https://yoannmoi.net/nipplejs/>`_.
  37. :param on_start: callback for when the user toches the joystick
  38. :param on_move: callback for when the user moves the joystick
  39. :param on_end: callback for when the user releases the joystick
  40. :param options: arguments like `color` which should be passed to the `underlying nipple.js library <https://github.com/yoannmoinet/nipplejs#options>`_
  41. """
  42. super().__init__(JoystickView(on_start, on_move, on_end, **options))