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

Respond to a gamepad button press during config selection.

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

Window for selecting a gamepad to configure.

Inherited Members
bauiv1._uitypes.Window
get_root_widget