|
5 | 5 | from pyglet.math import Vec2 |
6 | 6 |
|
7 | 7 | import arcade |
8 | | -from arcade import MOUSE_BUTTON_LEFT |
9 | 8 | from arcade.gui.events import ( |
10 | | - UIControllerButtonPressEvent, |
11 | | - UIControllerButtonReleaseEvent, |
12 | 9 | UIControllerDpadEvent, |
13 | 10 | UIControllerEvent, |
14 | 11 | UIEvent, |
15 | 12 | UIKeyPressEvent, |
16 | | - UIKeyReleaseEvent, |
17 | | - UIMousePressEvent, |
18 | | - UIMouseReleaseEvent, |
19 | 13 | ) |
20 | 14 | from arcade.gui.property import ListProperty, Property, bind |
21 | 15 | from arcade.gui.surface import Surface |
22 | | -from arcade.gui.widgets import FocusMode, UIInteractiveWidget, UIWidget |
| 16 | +from arcade.gui.widgets import FocusMode, UIWidget |
23 | 17 | from arcade.gui.widgets.layout import UIAnchorLayout |
24 | | -from arcade.gui.widgets.slider import UIBaseSlider |
25 | 18 |
|
26 | 19 |
|
27 | 20 | class UIFocusable(UIWidget): |
@@ -100,54 +93,22 @@ def on_event(self, event: UIEvent) -> bool | None: |
100 | 93 |
|
101 | 94 | return EVENT_HANDLED |
102 | 95 |
|
103 | | - elif event.symbol == arcade.key.SPACE: |
104 | | - self._start_interaction() |
| 96 | + elif isinstance(event, UIControllerDpadEvent): |
| 97 | + # switch focus |
| 98 | + if event.vector.x == 1: |
| 99 | + self.focus_right() |
105 | 100 | return EVENT_HANDLED |
106 | 101 |
|
107 | | - elif isinstance(event, UIKeyReleaseEvent): |
108 | | - if event.symbol == arcade.key.SPACE: |
109 | | - self._end_interaction() |
| 102 | + elif event.vector.y == 1: |
| 103 | + self.focus_up() |
110 | 104 | return EVENT_HANDLED |
111 | 105 |
|
112 | | - elif isinstance(event, UIControllerDpadEvent): |
113 | | - if self._interacting: |
114 | | - # TODO this should be handled in the slider! |
115 | | - # pass dpad events to the interacting widget |
116 | | - if event.vector.x == 1 and isinstance(self._interacting, UIBaseSlider): |
117 | | - self._interacting.norm_value += 0.1 |
118 | | - return EVENT_HANDLED |
119 | | - |
120 | | - elif event.vector.x == -1 and isinstance(self._interacting, UIBaseSlider): |
121 | | - self._interacting.norm_value -= 0.1 |
122 | | - return EVENT_HANDLED |
123 | | - |
| 106 | + elif event.vector.x == -1: |
| 107 | + self.focus_left() |
124 | 108 | return EVENT_HANDLED |
125 | 109 |
|
126 | | - else: |
127 | | - # switch focus |
128 | | - if event.vector.x == 1: |
129 | | - self.focus_right() |
130 | | - return EVENT_HANDLED |
131 | | - |
132 | | - elif event.vector.y == 1: |
133 | | - self.focus_up() |
134 | | - return EVENT_HANDLED |
135 | | - |
136 | | - elif event.vector.x == -1: |
137 | | - self.focus_left() |
138 | | - return EVENT_HANDLED |
139 | | - |
140 | | - elif event.vector.y == -1: |
141 | | - self.focus_down() |
142 | | - return EVENT_HANDLED |
143 | | - |
144 | | - elif isinstance(event, UIControllerButtonPressEvent): |
145 | | - if event.button == "a": |
146 | | - self._start_interaction() |
147 | | - return EVENT_HANDLED |
148 | | - elif isinstance(event, UIControllerButtonReleaseEvent): |
149 | | - if event.button == "a": |
150 | | - self._end_interaction() |
| 110 | + elif event.vector.y == -1: |
| 111 | + self.focus_down() |
151 | 112 | return EVENT_HANDLED |
152 | 113 |
|
153 | 114 | return EVENT_UNHANDLED |
@@ -278,48 +239,6 @@ def focus_previous(self): |
278 | 239 | # automatically wrap around via index -1 |
279 | 240 | self.set_focus(self._focusable_widgets[focused_index]) |
280 | 241 |
|
281 | | - def _start_interaction(self): |
282 | | - # TODO this should be handled in the widget |
283 | | - |
284 | | - widget = self.focused_widget |
285 | | - |
286 | | - if isinstance(widget, UIInteractiveWidget): |
287 | | - widget.dispatch_ui_event( |
288 | | - UIMousePressEvent( |
289 | | - source=self, |
290 | | - x=int(widget.rect.center_x), |
291 | | - y=int(widget.rect.center_y), |
292 | | - button=MOUSE_BUTTON_LEFT, |
293 | | - modifiers=0, |
294 | | - ) |
295 | | - ) |
296 | | - self._interacting = widget |
297 | | - else: |
298 | | - print("Cannot interact widget") |
299 | | - |
300 | | - def _end_interaction(self): |
301 | | - widget = self.focused_widget |
302 | | - |
303 | | - if isinstance(widget, UIInteractiveWidget): |
304 | | - if isinstance(self._interacting, UIBaseSlider): |
305 | | - # if slider, release outside the slider |
306 | | - x = self._interacting.rect.left - 1 |
307 | | - y = self._interacting.rect.bottom - 1 |
308 | | - else: |
309 | | - x = widget.rect.center_x |
310 | | - y = widget.rect.center_y |
311 | | - |
312 | | - self._interacting = None |
313 | | - widget.dispatch_ui_event( |
314 | | - UIMouseReleaseEvent( |
315 | | - source=self, |
316 | | - x=int(x), |
317 | | - y=int(y), |
318 | | - button=MOUSE_BUTTON_LEFT, |
319 | | - modifiers=0, |
320 | | - ) |
321 | | - ) |
322 | | - |
323 | 242 | def _do_render(self, surface: Surface, force=False) -> bool: |
324 | 243 | rendered = super()._do_render(surface, force) |
325 | 244 |
|
@@ -373,4 +292,5 @@ def is_focusable(widget): |
373 | 292 |
|
374 | 293 |
|
375 | 294 | class UIFocusGroup(UIFocusMixin, UIAnchorLayout): |
376 | | - pass |
| 295 | + """This will be removed in the future. |
| 296 | + UIFocusMixin is planned to be integrated into UILayout.""" |
0 commit comments