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
 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.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    else:
 37        width = 700
 38        height = 200
 39        button_width = 100
 40        uiscale = ba.app.ui.uiscale
 41        dlg = (ba.containerwidget(
 42            scale=(1.7 if uiscale is ba.UIScale.SMALL else
 43                   1.4 if uiscale is ba.UIScale.MEDIUM else 1.0),
 44            size=(width, height),
 45            transition='in_right'))
 46        ba.app.ui.set_main_menu_window(dlg)
 47        device_name = inputdevice.name
 48        if device_name == 'iDevice':
 49            msg = ba.Lstr(resource='bsRemoteConfigureInAppText',
 50                          subs=[('${REMOTE_APP_NAME}', get_remote_app_name())])
 51        else:
 52            msg = ba.Lstr(resource='cantConfigureDeviceText',
 53                          subs=[('${DEVICE}', device_name)])
 54        ba.textwidget(parent=dlg,
 55                      position=(0, height - 80),
 56                      size=(width, 25),
 57                      text=msg,
 58                      scale=0.8,
 59                      h_align='center',
 60                      v_align='top')
 61
 62        def _ok() -> None:
 63            from bastd.ui.settings import controls
 64            ba.containerwidget(edit=dlg, transition='out_right')
 65            ba.app.ui.set_main_menu_window(
 66                controls.ControlsSettingsWindow(
 67                    transition='in_left').get_root_widget())
 68
 69        ba.buttonwidget(parent=dlg,
 70                        position=((width - button_width) / 2, 20),
 71                        size=(button_width, 60),
 72                        label=ba.Lstr(resource='okText'),
 73                        on_activate_call=_ok)
 74
 75
 76class GamepadSelectWindow(ba.Window):
 77    """Window for selecting a gamepad to configure."""
 78
 79    def __init__(self) -> None:
 80        from typing import cast
 81        width = 480
 82        height = 170
 83        spacing = 40
 84        self._r = 'configGamepadSelectWindow'
 85
 86        uiscale = ba.app.ui.uiscale
 87        super().__init__(root_widget=ba.containerwidget(
 88            scale=(2.3 if uiscale is ba.UIScale.SMALL else
 89                   1.5 if uiscale is ba.UIScale.MEDIUM else 1.0),
 90            size=(width, height),
 91            transition='in_right',
 92        ))
 93
 94        btn = ba.buttonwidget(parent=self._root_widget,
 95                              position=(20, height - 60),
 96                              size=(130, 60),
 97                              label=ba.Lstr(resource='backText'),
 98                              button_type='back',
 99                              scale=0.8,
100                              on_activate_call=self._back)
101        # Let's not have anything selected by default; its misleading looking
102        # for the controller getting configured.
103        ba.containerwidget(edit=self._root_widget,
104                           cancel_button=btn,
105                           selected_child=cast(ba.Widget, 0))
106        ba.textwidget(parent=self._root_widget,
107                      position=(20, height - 50),
108                      size=(width, 25),
109                      text=ba.Lstr(resource=self._r + '.titleText'),
110                      maxwidth=250,
111                      color=ba.app.ui.title_color,
112                      h_align='center',
113                      v_align='center')
114
115        ba.buttonwidget(edit=btn,
116                        button_type='backSmall',
117                        size=(60, 60),
118                        label=ba.charstr(ba.SpecialChar.BACK))
119
120        v: float = height - 60
121        v -= spacing
122        ba.textwidget(parent=self._root_widget,
123                      position=(15, v),
124                      size=(width - 30, 30),
125                      scale=0.8,
126                      text=ba.Lstr(resource=self._r + '.pressAnyButtonText'),
127                      maxwidth=width * 0.95,
128                      color=ba.app.ui.infotextcolor,
129                      h_align='center',
130                      v_align='top')
131        v -= spacing * 1.24
132        if ba.app.platform == 'android':
133            ba.textwidget(parent=self._root_widget,
134                          position=(15, v),
135                          size=(width - 30, 30),
136                          scale=0.46,
137                          text=ba.Lstr(resource=self._r + '.androidNoteText'),
138                          maxwidth=width * 0.95,
139                          color=(0.7, 0.9, 0.7, 0.5),
140                          h_align='center',
141                          v_align='top')
142
143        _ba.capture_gamepad_input(gamepad_configure_callback)
144
145    def _back(self) -> None:
146        from bastd.ui.settings import controls
147        _ba.release_gamepad_input()
148        ba.containerwidget(edit=self._root_widget, transition='out_right')
149        ba.app.ui.set_main_menu_window(
150            controls.ControlsSettingsWindow(
151                transition='in_left').get_root_widget())
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.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    else:
38        width = 700
39        height = 200
40        button_width = 100
41        uiscale = ba.app.ui.uiscale
42        dlg = (ba.containerwidget(
43            scale=(1.7 if uiscale is ba.UIScale.SMALL else
44                   1.4 if uiscale is ba.UIScale.MEDIUM else 1.0),
45            size=(width, height),
46            transition='in_right'))
47        ba.app.ui.set_main_menu_window(dlg)
48        device_name = inputdevice.name
49        if device_name == 'iDevice':
50            msg = ba.Lstr(resource='bsRemoteConfigureInAppText',
51                          subs=[('${REMOTE_APP_NAME}', get_remote_app_name())])
52        else:
53            msg = ba.Lstr(resource='cantConfigureDeviceText',
54                          subs=[('${DEVICE}', device_name)])
55        ba.textwidget(parent=dlg,
56                      position=(0, height - 80),
57                      size=(width, 25),
58                      text=msg,
59                      scale=0.8,
60                      h_align='center',
61                      v_align='top')
62
63        def _ok() -> None:
64            from bastd.ui.settings import controls
65            ba.containerwidget(edit=dlg, transition='out_right')
66            ba.app.ui.set_main_menu_window(
67                controls.ControlsSettingsWindow(
68                    transition='in_left').get_root_widget())
69
70        ba.buttonwidget(parent=dlg,
71                        position=((width - button_width) / 2, 20),
72                        size=(button_width, 60),
73                        label=ba.Lstr(resource='okText'),
74                        on_activate_call=_ok)

Respond to a gamepad button press during config selection.

class GamepadSelectWindow(ba.ui.Window):
 77class GamepadSelectWindow(ba.Window):
 78    """Window for selecting a gamepad to configure."""
 79
 80    def __init__(self) -> None:
 81        from typing import cast
 82        width = 480
 83        height = 170
 84        spacing = 40
 85        self._r = 'configGamepadSelectWindow'
 86
 87        uiscale = ba.app.ui.uiscale
 88        super().__init__(root_widget=ba.containerwidget(
 89            scale=(2.3 if uiscale is ba.UIScale.SMALL else
 90                   1.5 if uiscale is ba.UIScale.MEDIUM else 1.0),
 91            size=(width, height),
 92            transition='in_right',
 93        ))
 94
 95        btn = ba.buttonwidget(parent=self._root_widget,
 96                              position=(20, height - 60),
 97                              size=(130, 60),
 98                              label=ba.Lstr(resource='backText'),
 99                              button_type='back',
100                              scale=0.8,
101                              on_activate_call=self._back)
102        # Let's not have anything selected by default; its misleading looking
103        # for the controller getting configured.
104        ba.containerwidget(edit=self._root_widget,
105                           cancel_button=btn,
106                           selected_child=cast(ba.Widget, 0))
107        ba.textwidget(parent=self._root_widget,
108                      position=(20, height - 50),
109                      size=(width, 25),
110                      text=ba.Lstr(resource=self._r + '.titleText'),
111                      maxwidth=250,
112                      color=ba.app.ui.title_color,
113                      h_align='center',
114                      v_align='center')
115
116        ba.buttonwidget(edit=btn,
117                        button_type='backSmall',
118                        size=(60, 60),
119                        label=ba.charstr(ba.SpecialChar.BACK))
120
121        v: float = height - 60
122        v -= spacing
123        ba.textwidget(parent=self._root_widget,
124                      position=(15, v),
125                      size=(width - 30, 30),
126                      scale=0.8,
127                      text=ba.Lstr(resource=self._r + '.pressAnyButtonText'),
128                      maxwidth=width * 0.95,
129                      color=ba.app.ui.infotextcolor,
130                      h_align='center',
131                      v_align='top')
132        v -= spacing * 1.24
133        if ba.app.platform == 'android':
134            ba.textwidget(parent=self._root_widget,
135                          position=(15, v),
136                          size=(width - 30, 30),
137                          scale=0.46,
138                          text=ba.Lstr(resource=self._r + '.androidNoteText'),
139                          maxwidth=width * 0.95,
140                          color=(0.7, 0.9, 0.7, 0.5),
141                          h_align='center',
142                          v_align='top')
143
144        _ba.capture_gamepad_input(gamepad_configure_callback)
145
146    def _back(self) -> None:
147        from bastd.ui.settings import controls
148        _ba.release_gamepad_input()
149        ba.containerwidget(edit=self._root_widget, transition='out_right')
150        ba.app.ui.set_main_menu_window(
151            controls.ControlsSettingsWindow(
152                transition='in_left').get_root_widget())

Window for selecting a gamepad to configure.

GamepadSelectWindow()
 80    def __init__(self) -> None:
 81        from typing import cast
 82        width = 480
 83        height = 170
 84        spacing = 40
 85        self._r = 'configGamepadSelectWindow'
 86
 87        uiscale = ba.app.ui.uiscale
 88        super().__init__(root_widget=ba.containerwidget(
 89            scale=(2.3 if uiscale is ba.UIScale.SMALL else
 90                   1.5 if uiscale is ba.UIScale.MEDIUM else 1.0),
 91            size=(width, height),
 92            transition='in_right',
 93        ))
 94
 95        btn = ba.buttonwidget(parent=self._root_widget,
 96                              position=(20, height - 60),
 97                              size=(130, 60),
 98                              label=ba.Lstr(resource='backText'),
 99                              button_type='back',
100                              scale=0.8,
101                              on_activate_call=self._back)
102        # Let's not have anything selected by default; its misleading looking
103        # for the controller getting configured.
104        ba.containerwidget(edit=self._root_widget,
105                           cancel_button=btn,
106                           selected_child=cast(ba.Widget, 0))
107        ba.textwidget(parent=self._root_widget,
108                      position=(20, height - 50),
109                      size=(width, 25),
110                      text=ba.Lstr(resource=self._r + '.titleText'),
111                      maxwidth=250,
112                      color=ba.app.ui.title_color,
113                      h_align='center',
114                      v_align='center')
115
116        ba.buttonwidget(edit=btn,
117                        button_type='backSmall',
118                        size=(60, 60),
119                        label=ba.charstr(ba.SpecialChar.BACK))
120
121        v: float = height - 60
122        v -= spacing
123        ba.textwidget(parent=self._root_widget,
124                      position=(15, v),
125                      size=(width - 30, 30),
126                      scale=0.8,
127                      text=ba.Lstr(resource=self._r + '.pressAnyButtonText'),
128                      maxwidth=width * 0.95,
129                      color=ba.app.ui.infotextcolor,
130                      h_align='center',
131                      v_align='top')
132        v -= spacing * 1.24
133        if ba.app.platform == 'android':
134            ba.textwidget(parent=self._root_widget,
135                          position=(15, v),
136                          size=(width - 30, 30),
137                          scale=0.46,
138                          text=ba.Lstr(resource=self._r + '.androidNoteText'),
139                          maxwidth=width * 0.95,
140                          color=(0.7, 0.9, 0.7, 0.5),
141                          h_align='center',
142                          v_align='top')
143
144        _ba.capture_gamepad_input(gamepad_configure_callback)
Inherited Members
ba.ui.Window
get_root_widget