joystick.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from typing import Any, Callable, Optional
  2. from ..dependencies import register_component
  3. from ..element import Element
  4. from ..events import JoystickEventArguments, handle_event
  5. register_component('joystick', __file__, 'joystick.vue', ['lib/nipplejs.min.js'])
  6. class Joystick(Element):
  7. def __init__(self, *,
  8. on_start: Optional[Callable[..., Any]] = None,
  9. on_move: Optional[Callable[..., Any]] = None,
  10. on_end: Optional[Callable[..., Any]] = None,
  11. throttle: float = 0.05,
  12. ** options: Any) -> None:
  13. """Joystick
  14. Create a joystick based on `nipple.js <https://yoannmoi.net/nipplejs/>`_.
  15. :param on_start: callback for when the user touches the joystick
  16. :param on_move: callback for when the user moves the joystick
  17. :param on_end: callback for when the user releases the joystick
  18. :param throttle: throttle interval in seconds for the move event (default: 0.05)
  19. :param options: arguments like `color` which should be passed to the `underlying nipple.js library <https://github.com/yoannmoinet/nipplejs#options>`_
  20. """
  21. super().__init__('joystick')
  22. self.on('start',
  23. lambda _: handle_event(on_start, JoystickEventArguments(sender=self,
  24. client=self.client,
  25. action='start')))
  26. self.on('move',
  27. lambda msg: handle_event(on_move, JoystickEventArguments(sender=self,
  28. client=self.client,
  29. action='move',
  30. x=msg['args']['data']['vector']['x'],
  31. y=msg['args']['data']['vector']['y'])),
  32. args=['data'],
  33. throttle=throttle)
  34. self.on('end',
  35. lambda _: handle_event(on_end, JoystickEventArguments(sender=self,
  36. client=self.client,
  37. action='end')))
  38. self._props['options'] = options