|
| 1 | +import warnings |
| 2 | + |
| 3 | +from pyglet.input import Controller |
| 4 | + |
| 5 | +import arcade |
| 6 | +from arcade import ControllerManager |
| 7 | + |
| 8 | + |
| 9 | +class _WindowControllerBridge: |
| 10 | + """Translates controller events to UIEvents and passes them to the UIManager. |
| 11 | +
|
| 12 | + Controller are automatically connected and disconnected. |
| 13 | +
|
| 14 | + Controller events are consumed by the UIControllerBridge, |
| 15 | + if the UIEvent is consumed by the UIManager. |
| 16 | +
|
| 17 | + This implicates, that the UIControllerBridge should be the first listener in the chain and |
| 18 | + that other systems should be aware, when not to act on events (like when the UI is active). |
| 19 | + """ |
| 20 | + |
| 21 | + |
| 22 | + def __init__(self, window: arcade.Window): |
| 23 | + self.window = window |
| 24 | + |
| 25 | + self.cm = ControllerManager() |
| 26 | + self.cm.push_handlers(self) |
| 27 | + |
| 28 | + # bind to existing controllers |
| 29 | + for controller in self.cm.get_controllers(): |
| 30 | + self.on_connect(controller) |
| 31 | + |
| 32 | + def on_connect(self, controller: Controller): |
| 33 | + controller.push_handlers(self) |
| 34 | + |
| 35 | + try: |
| 36 | + controller.open() |
| 37 | + except Exception as e: |
| 38 | + warnings.warn(f"Failed to open controller {controller}: {e}") |
| 39 | + |
| 40 | + def on_disconnect(self, controller: Controller): |
| 41 | + controller.remove_handlers(self) |
| 42 | + |
| 43 | + try: |
| 44 | + controller.close() |
| 45 | + except Exception as e: |
| 46 | + warnings.warn(f"Failed to close controller {controller}: {e}") |
| 47 | + |
| 48 | + # Controller event mapping |
| 49 | + def on_stick_motion(self, controller: Controller, name, value): |
| 50 | + return self.window.dispatch_event("on_stick_motion", controller, name, value) |
| 51 | + |
| 52 | + def on_trigger_motion(self, controller: Controller, name, value): |
| 53 | + return self.window.dispatch_event("on_trigger_motion", controller, name, value) |
| 54 | + |
| 55 | + def on_button_press(self, controller: Controller, button): |
| 56 | + return self.window.dispatch_event("on_button_press", controller, button) |
| 57 | + |
| 58 | + def on_button_release(self, controller: Controller, button): |
| 59 | + return self.window.dispatch_event("on_button_release", controller, button) |
| 60 | + |
| 61 | + def on_dpad_motion(self, controller: Controller, value): |
| 62 | + return self.window.dispatch_event("on_dpad_motion", controller, value) |
| 63 | + |
| 64 | + |
| 65 | +class ControllerWindow(arcade.Window): |
| 66 | + """A window that listens to controller events and dispatches them via on_... hooks.""" |
| 67 | + |
| 68 | + def __init__(self, *args, **kwargs): |
| 69 | + super().__init__(*args, **kwargs) |
| 70 | + self.cb = _WindowControllerBridge(self) |
| 71 | + |
| 72 | + # Controller event mapping |
| 73 | + def on_stick_motion(self, controller: Controller, name, value): |
| 74 | + pass |
| 75 | + |
| 76 | + def on_trigger_motion(self, controller: Controller, name, value): |
| 77 | + pass |
| 78 | + |
| 79 | + def on_button_press(self, controller: Controller, button): |
| 80 | + pass |
| 81 | + |
| 82 | + def on_button_release(self, controller: Controller, button): |
| 83 | + pass |
| 84 | + |
| 85 | + def on_dpad_motion(self, controller: Controller, value): |
| 86 | + pass |
| 87 | + |
| 88 | + |
| 89 | +ControllerWindow.register_event_type("on_stick_motion") |
| 90 | +ControllerWindow.register_event_type("on_trigger_motion") |
| 91 | +ControllerWindow.register_event_type("on_button_press") |
| 92 | +ControllerWindow.register_event_type("on_button_release") |
| 93 | +ControllerWindow.register_event_type("on_dpad_motion") |
0 commit comments