bastd.ui.settings.gamepadselect

Settings UI related to gamepad functionality.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""Settings UI related to gamepad functionality."""
  4
  5from __future__ import annotations
  6
  7from typing import TYPE_CHECKING
  8
  9import ba
 10import ba.internal
 11
 12if TYPE_CHECKING:
 13    from typing import Any
 14
 15
 16def gamepad_configure_callback(event: dict[str, Any]) -> None:
 17    """Respond to a gamepad button press during config selection."""
 18    from ba.internal import get_remote_app_name
 19    from bastd.ui.settings import gamepad
 20
 21    # Ignore all but button-presses.
 22    if event['type'] not in ['BUTTONDOWN', 'HATMOTION']:
 23        return
 24    ba.internal.release_gamepad_input()
 25    try:
 26        ba.app.ui.clear_main_menu_window(transition='out_left')
 27    except Exception:
 28        ba.print_exception('Error transitioning out main_menu_window.')
 29    ba.playsound(ba.getsound('activateBeep'))
 30    ba.playsound(ba.getsound('swish'))
 31    inputdevice = event['input_device']
 32    assert isinstance(inputdevice, ba.InputDevice)
 33    if inputdevice.allows_configuring:
 34        ba.app.ui.set_main_menu_window(
 35            gamepad.GamepadSettingsWindow(inputdevice).get_root_widget()
 36        )
 37    else:
 38        width = 700
 39        height = 200
 40        button_width = 100
 41        uiscale = ba.app.ui.uiscale
 42        dlg = ba.containerwidget(
 43            scale=(
 44                1.7
 45                if uiscale is ba.UIScale.SMALL
 46                else 1.4
 47                if uiscale is ba.UIScale.MEDIUM
 48                else 1.0
 49            ),
 50            size=(width, height),
 51            transition='in_right',
 52        )
 53        ba.app.ui.set_main_menu_window(dlg)
 54        device_name = inputdevice.name
 55        if device_name == 'iDevice':
 56            msg = ba.Lstr(
 57                resource='bsRemoteConfigureInAppText',
 58                subs=[('${REMOTE_APP_NAME}', get_remote_app_name())],
 59            )
 60        else:
 61            msg = ba.Lstr(
 62                resource='cantConfigureDeviceText',
 63                subs=[('${DEVICE}', device_name)],
 64            )
 65        ba.textwidget(
 66            parent=dlg,
 67            position=(0, height - 80),
 68            size=(width, 25),
 69            text=msg,
 70            scale=0.8,
 71            h_align='center',
 72            v_align='top',
 73        )
 74
 75        def _ok() -> None:
 76            from bastd.ui.settings import controls
 77
 78            ba.containerwidget(edit=dlg, transition='out_right')
 79            ba.app.ui.set_main_menu_window(
 80                controls.ControlsSettingsWindow(
 81                    transition='in_left'
 82                ).get_root_widget()
 83            )
 84
 85        ba.buttonwidget(
 86            parent=dlg,
 87            position=((width - button_width) / 2, 20),
 88            size=(button_width, 60),
 89            label=ba.Lstr(resource='okText'),
 90            on_activate_call=_ok,
 91        )
 92
 93
 94class GamepadSelectWindow(ba.Window):
 95    """Window for selecting a gamepad to configure."""
 96
 97    def __init__(self) -> None:
 98        from typing import cast
 99
100        width = 480
101        height = 170
102        spacing = 40
103        self._r = 'configGamepadSelectWindow'
104
105        uiscale = ba.app.ui.uiscale
106        super().__init__(
107            root_widget=ba.containerwidget(
108                scale=(
109                    2.3
110                    if uiscale is ba.UIScale.SMALL
111                    else 1.5
112                    if uiscale is ba.UIScale.MEDIUM
113                    else 1.0
114                ),
115                size=(width, height),
116                transition='in_right',
117            )
118        )
119
120        btn = ba.buttonwidget(
121            parent=self._root_widget,
122            position=(20, height - 60),
123            size=(130, 60),
124            label=ba.Lstr(resource='backText'),
125            button_type='back',
126            scale=0.8,
127            on_activate_call=self._back,
128        )
129        # Let's not have anything selected by default; its misleading looking
130        # for the controller getting configured.
131        ba.containerwidget(
132            edit=self._root_widget,
133            cancel_button=btn,
134            selected_child=cast(ba.Widget, 0),
135        )
136        ba.textwidget(
137            parent=self._root_widget,
138            position=(20, height - 50),
139            size=(width, 25),
140            text=ba.Lstr(resource=self._r + '.titleText'),
141            maxwidth=250,
142            color=ba.app.ui.title_color,
143            h_align='center',
144            v_align='center',
145        )
146
147        ba.buttonwidget(
148            edit=btn,
149            button_type='backSmall',
150            size=(60, 60),
151            label=ba.charstr(ba.SpecialChar.BACK),
152        )
153
154        v: float = height - 60
155        v -= spacing
156        ba.textwidget(
157            parent=self._root_widget,
158            position=(15, v),
159            size=(width - 30, 30),
160            scale=0.8,
161            text=ba.Lstr(resource=self._r + '.pressAnyButtonText'),
162            maxwidth=width * 0.95,
163            color=ba.app.ui.infotextcolor,
164            h_align='center',
165            v_align='top',
166        )
167        v -= spacing * 1.24
168        if ba.app.platform == 'android':
169            ba.textwidget(
170                parent=self._root_widget,
171                position=(15, v),
172                size=(width - 30, 30),
173                scale=0.46,
174                text=ba.Lstr(resource=self._r + '.androidNoteText'),
175                maxwidth=width * 0.95,
176                color=(0.7, 0.9, 0.7, 0.5),
177                h_align='center',
178                v_align='top',
179            )
180
181        ba.internal.capture_gamepad_input(gamepad_configure_callback)
182
183    def _back(self) -> None:
184        from bastd.ui.settings import controls
185
186        ba.internal.release_gamepad_input()
187        ba.containerwidget(edit=self._root_widget, transition='out_right')
188        ba.app.ui.set_main_menu_window(
189            controls.ControlsSettingsWindow(
190                transition='in_left'
191            ).get_root_widget()
192        )
def gamepad_configure_callback(event: dict[str, typing.Any]) -> None:
17def gamepad_configure_callback(event: dict[str, Any]) -> None:
18    """Respond to a gamepad button press during config selection."""
19    from ba.internal import get_remote_app_name
20    from bastd.ui.settings import gamepad
21
22    # Ignore all but button-presses.
23    if event['type'] not in ['BUTTONDOWN', 'HATMOTION']:
24        return
25    ba.internal.release_gamepad_input()
26    try:
27        ba.app.ui.clear_main_menu_window(transition='out_left')
28    except Exception:
29        ba.print_exception('Error transitioning out main_menu_window.')
30    ba.playsound(ba.getsound('activateBeep'))
31    ba.playsound(ba.getsound('swish'))
32    inputdevice = event['input_device']
33    assert isinstance(inputdevice, ba.InputDevice)
34    if inputdevice.allows_configuring:
35        ba.app.ui.set_main_menu_window(
36            gamepad.GamepadSettingsWindow(inputdevice).get_root_widget()
37        )
38    else:
39        width = 700
40        height = 200
41        button_width = 100
42        uiscale = ba.app.ui.uiscale
43        dlg = ba.containerwidget(
44            scale=(
45                1.7
46                if uiscale is ba.UIScale.SMALL
47                else 1.4
48                if uiscale is ba.UIScale.MEDIUM
49                else 1.0
50            ),
51            size=(width, height),
52            transition='in_right',
53        )
54        ba.app.ui.set_main_menu_window(dlg)
55        device_name = inputdevice.name
56        if device_name == 'iDevice':
57            msg = ba.Lstr(
58                resource='bsRemoteConfigureInAppText',
59                subs=[('${REMOTE_APP_NAME}', get_remote_app_name())],
60            )
61        else:
62            msg = ba.Lstr(
63                resource='cantConfigureDeviceText',
64                subs=[('${DEVICE}', device_name)],
65            )
66        ba.textwidget(
67            parent=dlg,
68            position=(0, height - 80),
69            size=(width, 25),
70            text=msg,
71            scale=0.8,
72            h_align='center',
73            v_align='top',
74        )
75
76        def _ok() -> None:
77            from bastd.ui.settings import controls
78
79            ba.containerwidget(edit=dlg, transition='out_right')
80            ba.app.ui.set_main_menu_window(
81                controls.ControlsSettingsWindow(
82                    transition='in_left'
83                ).get_root_widget()
84            )
85
86        ba.buttonwidget(
87            parent=dlg,
88            position=((width - button_width) / 2, 20),
89            size=(button_width, 60),
90            label=ba.Lstr(resource='okText'),
91            on_activate_call=_ok,
92        )

Respond to a gamepad button press during config selection.

class GamepadSelectWindow(ba.ui.Window):
 95class GamepadSelectWindow(ba.Window):
 96    """Window for selecting a gamepad to configure."""
 97
 98    def __init__(self) -> None:
 99        from typing import cast
100
101        width = 480
102        height = 170
103        spacing = 40
104        self._r = 'configGamepadSelectWindow'
105
106        uiscale = ba.app.ui.uiscale
107        super().__init__(
108            root_widget=ba.containerwidget(
109                scale=(
110                    2.3
111                    if uiscale is ba.UIScale.SMALL
112                    else 1.5
113                    if uiscale is ba.UIScale.MEDIUM
114                    else 1.0
115                ),
116                size=(width, height),
117                transition='in_right',
118            )
119        )
120
121        btn = ba.buttonwidget(
122            parent=self._root_widget,
123            position=(20, height - 60),
124            size=(130, 60),
125            label=ba.Lstr(resource='backText'),
126            button_type='back',
127            scale=0.8,
128            on_activate_call=self._back,
129        )
130        # Let's not have anything selected by default; its misleading looking
131        # for the controller getting configured.
132        ba.containerwidget(
133            edit=self._root_widget,
134            cancel_button=btn,
135            selected_child=cast(ba.Widget, 0),
136        )
137        ba.textwidget(
138            parent=self._root_widget,
139            position=(20, height - 50),
140            size=(width, 25),
141            text=ba.Lstr(resource=self._r + '.titleText'),
142            maxwidth=250,
143            color=ba.app.ui.title_color,
144            h_align='center',
145            v_align='center',
146        )
147
148        ba.buttonwidget(
149            edit=btn,
150            button_type='backSmall',
151            size=(60, 60),
152            label=ba.charstr(ba.SpecialChar.BACK),
153        )
154
155        v: float = height - 60
156        v -= spacing
157        ba.textwidget(
158            parent=self._root_widget,
159            position=(15, v),
160            size=(width - 30, 30),
161            scale=0.8,
162            text=ba.Lstr(resource=self._r + '.pressAnyButtonText'),
163            maxwidth=width * 0.95,
164            color=ba.app.ui.infotextcolor,
165            h_align='center',
166            v_align='top',
167        )
168        v -= spacing * 1.24
169        if ba.app.platform == 'android':
170            ba.textwidget(
171                parent=self._root_widget,
172                position=(15, v),
173                size=(width - 30, 30),
174                scale=0.46,
175                text=ba.Lstr(resource=self._r + '.androidNoteText'),
176                maxwidth=width * 0.95,
177                color=(0.7, 0.9, 0.7, 0.5),
178                h_align='center',
179                v_align='top',
180            )
181
182        ba.internal.capture_gamepad_input(gamepad_configure_callback)
183
184    def _back(self) -> None:
185        from bastd.ui.settings import controls
186
187        ba.internal.release_gamepad_input()
188        ba.containerwidget(edit=self._root_widget, transition='out_right')
189        ba.app.ui.set_main_menu_window(
190            controls.ControlsSettingsWindow(
191                transition='in_left'
192            ).get_root_widget()
193        )

Window for selecting a gamepad to configure.

GamepadSelectWindow()
 98    def __init__(self) -> None:
 99        from typing import cast
100
101        width = 480
102        height = 170
103        spacing = 40
104        self._r = 'configGamepadSelectWindow'
105
106        uiscale = ba.app.ui.uiscale
107        super().__init__(
108            root_widget=ba.containerwidget(
109                scale=(
110                    2.3
111                    if uiscale is ba.UIScale.SMALL
112                    else 1.5
113                    if uiscale is ba.UIScale.MEDIUM
114                    else 1.0
115                ),
116                size=(width, height),
117                transition='in_right',
118            )
119        )
120
121        btn = ba.buttonwidget(
122            parent=self._root_widget,
123            position=(20, height - 60),
124            size=(130, 60),
125            label=ba.Lstr(resource='backText'),
126            button_type='back',
127            scale=0.8,
128            on_activate_call=self._back,
129        )
130        # Let's not have anything selected by default; its misleading looking
131        # for the controller getting configured.
132        ba.containerwidget(
133            edit=self._root_widget,
134            cancel_button=btn,
135            selected_child=cast(ba.Widget, 0),
136        )
137        ba.textwidget(
138            parent=self._root_widget,
139            position=(20, height - 50),
140            size=(width, 25),
141            text=ba.Lstr(resource=self._r + '.titleText'),
142            maxwidth=250,
143            color=ba.app.ui.title_color,
144            h_align='center',
145            v_align='center',
146        )
147
148        ba.buttonwidget(
149            edit=btn,
150            button_type='backSmall',
151            size=(60, 60),
152            label=ba.charstr(ba.SpecialChar.BACK),
153        )
154
155        v: float = height - 60
156        v -= spacing
157        ba.textwidget(
158            parent=self._root_widget,
159            position=(15, v),
160            size=(width - 30, 30),
161            scale=0.8,
162            text=ba.Lstr(resource=self._r + '.pressAnyButtonText'),
163            maxwidth=width * 0.95,
164            color=ba.app.ui.infotextcolor,
165            h_align='center',
166            v_align='top',
167        )
168        v -= spacing * 1.24
169        if ba.app.platform == 'android':
170            ba.textwidget(
171                parent=self._root_widget,
172                position=(15, v),
173                size=(width - 30, 30),
174                scale=0.46,
175                text=ba.Lstr(resource=self._r + '.androidNoteText'),
176                maxwidth=width * 0.95,
177                color=(0.7, 0.9, 0.7, 0.5),
178                h_align='center',
179                v_align='top',
180            )
181
182        ba.internal.capture_gamepad_input(gamepad_configure_callback)
Inherited Members
ba.ui.Window
get_root_widget