widget_controller.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. """Controller class.
  4. Represents a Gamepad or Joystick controller.
  5. """
  6. from .valuewidget import ValueWidget
  7. from .widget import register, widget_serialization
  8. from .domwidget import DOMWidget
  9. from .widget_core import CoreWidget
  10. from .trait_types import TypedTuple
  11. from traitlets import Bool, Int, Float, Unicode, Instance
  12. @register
  13. class Button(DOMWidget, ValueWidget, CoreWidget):
  14. """Represents a gamepad or joystick button."""
  15. value = Float(min=0.0, max=1.0, read_only=True, help="The value of the button.").tag(sync=True)
  16. pressed = Bool(read_only=True, help="Whether the button is pressed.").tag(sync=True)
  17. _view_name = Unicode('ControllerButtonView').tag(sync=True)
  18. _model_name = Unicode('ControllerButtonModel').tag(sync=True)
  19. @register
  20. class Axis(DOMWidget, ValueWidget, CoreWidget):
  21. """Represents a gamepad or joystick axis."""
  22. value = Float(min=-1.0, max=1.0, read_only=True, help="The value of the axis.").tag(sync=True)
  23. _view_name = Unicode('ControllerAxisView').tag(sync=True)
  24. _model_name = Unicode('ControllerAxisModel').tag(sync=True)
  25. @register
  26. class Controller(DOMWidget, CoreWidget):
  27. """Represents a game controller."""
  28. index = Int(help="The id number of the controller.").tag(sync=True)
  29. # General information about the gamepad, button and axes mapping, name.
  30. # These values are all read-only and set by the JavaScript side.
  31. name = Unicode(read_only=True, help="The name of the controller.").tag(sync=True)
  32. mapping = Unicode(read_only=True, help="The name of the control mapping.").tag(sync=True)
  33. connected = Bool(read_only=True, help="Whether the gamepad is connected.").tag(sync=True)
  34. timestamp = Float(read_only=True, help="The last time the data from this gamepad was updated.").tag(sync=True)
  35. # Buttons and axes - read-only
  36. buttons = TypedTuple(trait=Instance(Button), read_only=True, help="The buttons on the gamepad.").tag(sync=True, **widget_serialization)
  37. axes = TypedTuple(trait=Instance(Axis), read_only=True, help="The axes on the gamepad.").tag(sync=True, **widget_serialization)
  38. _view_name = Unicode('ControllerView').tag(sync=True)
  39. _model_name = Unicode('ControllerModel').tag(sync=True)