bauiv1lib.settings.touchscreen

UI settings functionality related to touchscreens.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""UI settings functionality related to touchscreens."""
  4from __future__ import annotations
  5
  6from typing import override
  7
  8import bauiv1 as bui
  9import bascenev1 as bs
 10
 11
 12class TouchscreenSettingsWindow(bui.MainWindow):
 13    """Settings window for touchscreens."""
 14
 15    def __del__(self) -> None:
 16        bs.set_touchscreen_editing(False)
 17
 18    def __init__(
 19        self,
 20        transition: str | None = 'in_right',
 21        origin_widget: bui.Widget | None = None,
 22    ) -> None:
 23        self._width = 780
 24        self._height = 380
 25        self._r = 'configTouchscreenWindow'
 26
 27        bs.set_touchscreen_editing(True)
 28
 29        assert bui.app.classic is not None
 30        uiscale = bui.app.ui_v1.uiscale
 31        super().__init__(
 32            root_widget=bui.containerwidget(
 33                size=(self._width, self._height),
 34                scale=(
 35                    1.9
 36                    if uiscale is bui.UIScale.SMALL
 37                    else 1.55 if uiscale is bui.UIScale.MEDIUM else 1.2
 38                ),
 39                toolbar_visibility=(
 40                    'menu_minimal'
 41                    if uiscale is bui.UIScale.SMALL
 42                    else 'menu_full'
 43                ),
 44                stack_offset=(
 45                    (0, -20) if uiscale is bui.UIScale.SMALL else (0, 0)
 46                ),
 47            ),
 48            transition=transition,
 49            origin_widget=origin_widget,
 50        )
 51
 52        if uiscale is bui.UIScale.SMALL:
 53            bui.containerwidget(
 54                edit=self._root_widget, on_cancel_call=self.main_window_back
 55            )
 56        else:
 57            btn = bui.buttonwidget(
 58                parent=self._root_widget,
 59                position=(55, self._height - 60),
 60                size=(60, 60),
 61                label=bui.charstr(bui.SpecialChar.BACK),
 62                button_type='backSmall',
 63                scale=0.8,
 64                on_activate_call=self.main_window_back,
 65            )
 66            bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 67
 68        bui.textwidget(
 69            parent=self._root_widget,
 70            position=(25, self._height - 57),
 71            size=(self._width, 25),
 72            text=bui.Lstr(resource=f'{self._r}.titleText'),
 73            color=bui.app.ui_v1.title_color,
 74            maxwidth=280,
 75            h_align='center',
 76            v_align='center',
 77        )
 78
 79        self._scroll_width = self._width - 100
 80        self._scroll_height = self._height - 110
 81        self._sub_width = self._scroll_width - 20
 82        self._sub_height = 360
 83
 84        self._scrollwidget = bui.scrollwidget(
 85            parent=self._root_widget,
 86            position=(
 87                (self._width - self._scroll_width) * 0.5,
 88                self._height - 65 - self._scroll_height,
 89            ),
 90            size=(self._scroll_width, self._scroll_height),
 91            claims_left_right=True,
 92            claims_tab=True,
 93            selection_loops_to_parent=True,
 94        )
 95        self._subcontainer = bui.containerwidget(
 96            parent=self._scrollwidget,
 97            size=(self._sub_width, self._sub_height),
 98            background=False,
 99            claims_left_right=True,
100            claims_tab=True,
101            selection_loops_to_parent=True,
102        )
103        self._build_gui()
104
105    @override
106    def get_main_window_state(self) -> bui.MainWindowState:
107        # Support recreating our window for back/refresh purposes.
108        cls = type(self)
109        return bui.BasicMainWindowState(
110            create_call=lambda transition, origin_widget: cls(
111                transition=transition, origin_widget=origin_widget
112            )
113        )
114
115    def _build_gui(self) -> None:
116        # pylint: disable=too-many-locals
117        from bauiv1lib.config import ConfigNumberEdit, ConfigCheckBox
118        from bauiv1lib.radiogroup import make_radio_group
119
120        # Clear anything already there.
121        children = self._subcontainer.get_children()
122        for child in children:
123            child.delete()
124        h = 30
125        hoffs = 100
126        hoffs2 = 70
127        hoffs3 = 320
128        v = self._sub_height - 85
129        clr = (0.8, 0.8, 0.8, 1.0)
130        clr2 = (0.8, 0.8, 0.8)
131        bui.textwidget(
132            parent=self._subcontainer,
133            position=(self._sub_width * 0.5, v + 63),
134            size=(0, 0),
135            text=bui.Lstr(resource=f'{self._r}.swipeInfoText'),
136            flatness=1.0,
137            color=(0, 0.9, 0.1, 0.7),
138            maxwidth=self._sub_width * 0.9,
139            scale=0.55,
140            h_align='center',
141            v_align='center',
142        )
143        cur_val = bui.app.config.get('Touch Movement Control Type', 'swipe')
144        bui.textwidget(
145            parent=self._subcontainer,
146            position=(h, v - 2),
147            size=(0, 30),
148            text=bui.Lstr(resource=f'{self._r}.movementText'),
149            maxwidth=190,
150            color=clr,
151            v_align='center',
152        )
153        cb1 = bui.checkboxwidget(
154            parent=self._subcontainer,
155            position=(h + hoffs + 220, v),
156            size=(170, 30),
157            text=bui.Lstr(resource=f'{self._r}.joystickText'),
158            maxwidth=100,
159            textcolor=clr2,
160            scale=0.9,
161        )
162        cb2 = bui.checkboxwidget(
163            parent=self._subcontainer,
164            position=(h + hoffs + 357, v),
165            size=(170, 30),
166            text=bui.Lstr(resource=f'{self._r}.swipeText'),
167            maxwidth=100,
168            textcolor=clr2,
169            value=False,
170            scale=0.9,
171        )
172        make_radio_group(
173            (cb1, cb2), ('joystick', 'swipe'), cur_val, self._movement_changed
174        )
175        v -= 50
176        ConfigNumberEdit(
177            parent=self._subcontainer,
178            position=(h, v),
179            xoffset=hoffs2 + 65,
180            configkey='Touch Controls Scale Movement',
181            displayname=bui.Lstr(
182                resource=f'{self._r}.movementControlScaleText'
183            ),
184            changesound=False,
185            minval=0.1,
186            maxval=4.0,
187            increment=0.1,
188        )
189        v -= 50
190        cur_val = bui.app.config.get('Touch Action Control Type', 'buttons')
191        bui.textwidget(
192            parent=self._subcontainer,
193            position=(h, v - 2),
194            size=(0, 30),
195            text=bui.Lstr(resource=f'{self._r}.actionsText'),
196            maxwidth=190,
197            color=clr,
198            v_align='center',
199        )
200        cb1 = bui.checkboxwidget(
201            parent=self._subcontainer,
202            position=(h + hoffs + 220, v),
203            size=(170, 30),
204            text=bui.Lstr(resource=f'{self._r}.buttonsText'),
205            maxwidth=100,
206            textcolor=clr2,
207            scale=0.9,
208        )
209        cb2 = bui.checkboxwidget(
210            parent=self._subcontainer,
211            position=(h + hoffs + 357, v),
212            size=(170, 30),
213            text=bui.Lstr(resource=f'{self._r}.swipeText'),
214            maxwidth=100,
215            textcolor=clr2,
216            scale=0.9,
217        )
218        make_radio_group(
219            (cb1, cb2), ('buttons', 'swipe'), cur_val, self._actions_changed
220        )
221        v -= 50
222        ConfigNumberEdit(
223            parent=self._subcontainer,
224            position=(h, v),
225            xoffset=hoffs2 + 65,
226            configkey='Touch Controls Scale Actions',
227            displayname=bui.Lstr(resource=f'{self._r}.actionControlScaleText'),
228            changesound=False,
229            minval=0.1,
230            maxval=4.0,
231            increment=0.1,
232        )
233
234        v -= 50
235        bui.textwidget(
236            parent=self._subcontainer,
237            position=(h, v - 2),
238            size=(0, 30),
239            text=bui.Lstr(resource=f'{self._r}.swipeControlsHiddenText'),
240            maxwidth=190,
241            color=clr,
242            v_align='center',
243        )
244
245        ConfigCheckBox(
246            parent=self._subcontainer,
247            position=(h + hoffs3, v),
248            size=(100, 30),
249            maxwidth=400,
250            configkey='Touch Controls Swipe Hidden',
251            displayname='',
252        )
253        v -= 65
254
255        bui.buttonwidget(
256            parent=self._subcontainer,
257            position=(self._sub_width * 0.5 - 70, v),
258            size=(170, 60),
259            label=bui.Lstr(resource=f'{self._r}.resetText'),
260            scale=0.75,
261            on_activate_call=self._reset,
262        )
263
264        bui.textwidget(
265            parent=self._root_widget,
266            position=(self._width * 0.5, 38),
267            size=(0, 0),
268            h_align='center',
269            text=bui.Lstr(resource=f'{self._r}.dragControlsText'),
270            maxwidth=self._width * 0.8,
271            scale=0.65,
272            color=(1, 1, 1, 0.4),
273        )
274
275    def _actions_changed(self, v: str) -> None:
276        cfg = bui.app.config
277        cfg['Touch Action Control Type'] = v
278        cfg.apply_and_commit()
279
280    def _movement_changed(self, v: str) -> None:
281        cfg = bui.app.config
282        cfg['Touch Movement Control Type'] = v
283        cfg.apply_and_commit()
284
285    def _reset(self) -> None:
286        cfg = bui.app.config
287        cfgkeys = [
288            'Touch Movement Control Type',
289            'Touch Action Control Type',
290            'Touch Controls Scale',
291            'Touch Controls Scale Movement',
292            'Touch Controls Scale Actions',
293            'Touch Controls Swipe Hidden',
294            'Touch DPad X',
295            'Touch DPad Y',
296            'Touch Buttons X',
297            'Touch Buttons Y',
298        ]
299        for cfgkey in cfgkeys:
300            if cfgkey in cfg:
301                del cfg[cfgkey]
302        cfg.apply_and_commit()
303        bui.apptimer(0, self._build_gui)
class TouchscreenSettingsWindow(bauiv1._uitypes.MainWindow):
 13class TouchscreenSettingsWindow(bui.MainWindow):
 14    """Settings window for touchscreens."""
 15
 16    def __del__(self) -> None:
 17        bs.set_touchscreen_editing(False)
 18
 19    def __init__(
 20        self,
 21        transition: str | None = 'in_right',
 22        origin_widget: bui.Widget | None = None,
 23    ) -> None:
 24        self._width = 780
 25        self._height = 380
 26        self._r = 'configTouchscreenWindow'
 27
 28        bs.set_touchscreen_editing(True)
 29
 30        assert bui.app.classic is not None
 31        uiscale = bui.app.ui_v1.uiscale
 32        super().__init__(
 33            root_widget=bui.containerwidget(
 34                size=(self._width, self._height),
 35                scale=(
 36                    1.9
 37                    if uiscale is bui.UIScale.SMALL
 38                    else 1.55 if uiscale is bui.UIScale.MEDIUM else 1.2
 39                ),
 40                toolbar_visibility=(
 41                    'menu_minimal'
 42                    if uiscale is bui.UIScale.SMALL
 43                    else 'menu_full'
 44                ),
 45                stack_offset=(
 46                    (0, -20) if uiscale is bui.UIScale.SMALL else (0, 0)
 47                ),
 48            ),
 49            transition=transition,
 50            origin_widget=origin_widget,
 51        )
 52
 53        if uiscale is bui.UIScale.SMALL:
 54            bui.containerwidget(
 55                edit=self._root_widget, on_cancel_call=self.main_window_back
 56            )
 57        else:
 58            btn = bui.buttonwidget(
 59                parent=self._root_widget,
 60                position=(55, self._height - 60),
 61                size=(60, 60),
 62                label=bui.charstr(bui.SpecialChar.BACK),
 63                button_type='backSmall',
 64                scale=0.8,
 65                on_activate_call=self.main_window_back,
 66            )
 67            bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 68
 69        bui.textwidget(
 70            parent=self._root_widget,
 71            position=(25, self._height - 57),
 72            size=(self._width, 25),
 73            text=bui.Lstr(resource=f'{self._r}.titleText'),
 74            color=bui.app.ui_v1.title_color,
 75            maxwidth=280,
 76            h_align='center',
 77            v_align='center',
 78        )
 79
 80        self._scroll_width = self._width - 100
 81        self._scroll_height = self._height - 110
 82        self._sub_width = self._scroll_width - 20
 83        self._sub_height = 360
 84
 85        self._scrollwidget = bui.scrollwidget(
 86            parent=self._root_widget,
 87            position=(
 88                (self._width - self._scroll_width) * 0.5,
 89                self._height - 65 - self._scroll_height,
 90            ),
 91            size=(self._scroll_width, self._scroll_height),
 92            claims_left_right=True,
 93            claims_tab=True,
 94            selection_loops_to_parent=True,
 95        )
 96        self._subcontainer = bui.containerwidget(
 97            parent=self._scrollwidget,
 98            size=(self._sub_width, self._sub_height),
 99            background=False,
100            claims_left_right=True,
101            claims_tab=True,
102            selection_loops_to_parent=True,
103        )
104        self._build_gui()
105
106    @override
107    def get_main_window_state(self) -> bui.MainWindowState:
108        # Support recreating our window for back/refresh purposes.
109        cls = type(self)
110        return bui.BasicMainWindowState(
111            create_call=lambda transition, origin_widget: cls(
112                transition=transition, origin_widget=origin_widget
113            )
114        )
115
116    def _build_gui(self) -> None:
117        # pylint: disable=too-many-locals
118        from bauiv1lib.config import ConfigNumberEdit, ConfigCheckBox
119        from bauiv1lib.radiogroup import make_radio_group
120
121        # Clear anything already there.
122        children = self._subcontainer.get_children()
123        for child in children:
124            child.delete()
125        h = 30
126        hoffs = 100
127        hoffs2 = 70
128        hoffs3 = 320
129        v = self._sub_height - 85
130        clr = (0.8, 0.8, 0.8, 1.0)
131        clr2 = (0.8, 0.8, 0.8)
132        bui.textwidget(
133            parent=self._subcontainer,
134            position=(self._sub_width * 0.5, v + 63),
135            size=(0, 0),
136            text=bui.Lstr(resource=f'{self._r}.swipeInfoText'),
137            flatness=1.0,
138            color=(0, 0.9, 0.1, 0.7),
139            maxwidth=self._sub_width * 0.9,
140            scale=0.55,
141            h_align='center',
142            v_align='center',
143        )
144        cur_val = bui.app.config.get('Touch Movement Control Type', 'swipe')
145        bui.textwidget(
146            parent=self._subcontainer,
147            position=(h, v - 2),
148            size=(0, 30),
149            text=bui.Lstr(resource=f'{self._r}.movementText'),
150            maxwidth=190,
151            color=clr,
152            v_align='center',
153        )
154        cb1 = bui.checkboxwidget(
155            parent=self._subcontainer,
156            position=(h + hoffs + 220, v),
157            size=(170, 30),
158            text=bui.Lstr(resource=f'{self._r}.joystickText'),
159            maxwidth=100,
160            textcolor=clr2,
161            scale=0.9,
162        )
163        cb2 = bui.checkboxwidget(
164            parent=self._subcontainer,
165            position=(h + hoffs + 357, v),
166            size=(170, 30),
167            text=bui.Lstr(resource=f'{self._r}.swipeText'),
168            maxwidth=100,
169            textcolor=clr2,
170            value=False,
171            scale=0.9,
172        )
173        make_radio_group(
174            (cb1, cb2), ('joystick', 'swipe'), cur_val, self._movement_changed
175        )
176        v -= 50
177        ConfigNumberEdit(
178            parent=self._subcontainer,
179            position=(h, v),
180            xoffset=hoffs2 + 65,
181            configkey='Touch Controls Scale Movement',
182            displayname=bui.Lstr(
183                resource=f'{self._r}.movementControlScaleText'
184            ),
185            changesound=False,
186            minval=0.1,
187            maxval=4.0,
188            increment=0.1,
189        )
190        v -= 50
191        cur_val = bui.app.config.get('Touch Action Control Type', 'buttons')
192        bui.textwidget(
193            parent=self._subcontainer,
194            position=(h, v - 2),
195            size=(0, 30),
196            text=bui.Lstr(resource=f'{self._r}.actionsText'),
197            maxwidth=190,
198            color=clr,
199            v_align='center',
200        )
201        cb1 = bui.checkboxwidget(
202            parent=self._subcontainer,
203            position=(h + hoffs + 220, v),
204            size=(170, 30),
205            text=bui.Lstr(resource=f'{self._r}.buttonsText'),
206            maxwidth=100,
207            textcolor=clr2,
208            scale=0.9,
209        )
210        cb2 = bui.checkboxwidget(
211            parent=self._subcontainer,
212            position=(h + hoffs + 357, v),
213            size=(170, 30),
214            text=bui.Lstr(resource=f'{self._r}.swipeText'),
215            maxwidth=100,
216            textcolor=clr2,
217            scale=0.9,
218        )
219        make_radio_group(
220            (cb1, cb2), ('buttons', 'swipe'), cur_val, self._actions_changed
221        )
222        v -= 50
223        ConfigNumberEdit(
224            parent=self._subcontainer,
225            position=(h, v),
226            xoffset=hoffs2 + 65,
227            configkey='Touch Controls Scale Actions',
228            displayname=bui.Lstr(resource=f'{self._r}.actionControlScaleText'),
229            changesound=False,
230            minval=0.1,
231            maxval=4.0,
232            increment=0.1,
233        )
234
235        v -= 50
236        bui.textwidget(
237            parent=self._subcontainer,
238            position=(h, v - 2),
239            size=(0, 30),
240            text=bui.Lstr(resource=f'{self._r}.swipeControlsHiddenText'),
241            maxwidth=190,
242            color=clr,
243            v_align='center',
244        )
245
246        ConfigCheckBox(
247            parent=self._subcontainer,
248            position=(h + hoffs3, v),
249            size=(100, 30),
250            maxwidth=400,
251            configkey='Touch Controls Swipe Hidden',
252            displayname='',
253        )
254        v -= 65
255
256        bui.buttonwidget(
257            parent=self._subcontainer,
258            position=(self._sub_width * 0.5 - 70, v),
259            size=(170, 60),
260            label=bui.Lstr(resource=f'{self._r}.resetText'),
261            scale=0.75,
262            on_activate_call=self._reset,
263        )
264
265        bui.textwidget(
266            parent=self._root_widget,
267            position=(self._width * 0.5, 38),
268            size=(0, 0),
269            h_align='center',
270            text=bui.Lstr(resource=f'{self._r}.dragControlsText'),
271            maxwidth=self._width * 0.8,
272            scale=0.65,
273            color=(1, 1, 1, 0.4),
274        )
275
276    def _actions_changed(self, v: str) -> None:
277        cfg = bui.app.config
278        cfg['Touch Action Control Type'] = v
279        cfg.apply_and_commit()
280
281    def _movement_changed(self, v: str) -> None:
282        cfg = bui.app.config
283        cfg['Touch Movement Control Type'] = v
284        cfg.apply_and_commit()
285
286    def _reset(self) -> None:
287        cfg = bui.app.config
288        cfgkeys = [
289            'Touch Movement Control Type',
290            'Touch Action Control Type',
291            'Touch Controls Scale',
292            'Touch Controls Scale Movement',
293            'Touch Controls Scale Actions',
294            'Touch Controls Swipe Hidden',
295            'Touch DPad X',
296            'Touch DPad Y',
297            'Touch Buttons X',
298            'Touch Buttons Y',
299        ]
300        for cfgkey in cfgkeys:
301            if cfgkey in cfg:
302                del cfg[cfgkey]
303        cfg.apply_and_commit()
304        bui.apptimer(0, self._build_gui)

Settings window for touchscreens.

TouchscreenSettingsWindow( transition: str | None = 'in_right', origin_widget: _bauiv1.Widget | None = None)
 19    def __init__(
 20        self,
 21        transition: str | None = 'in_right',
 22        origin_widget: bui.Widget | None = None,
 23    ) -> None:
 24        self._width = 780
 25        self._height = 380
 26        self._r = 'configTouchscreenWindow'
 27
 28        bs.set_touchscreen_editing(True)
 29
 30        assert bui.app.classic is not None
 31        uiscale = bui.app.ui_v1.uiscale
 32        super().__init__(
 33            root_widget=bui.containerwidget(
 34                size=(self._width, self._height),
 35                scale=(
 36                    1.9
 37                    if uiscale is bui.UIScale.SMALL
 38                    else 1.55 if uiscale is bui.UIScale.MEDIUM else 1.2
 39                ),
 40                toolbar_visibility=(
 41                    'menu_minimal'
 42                    if uiscale is bui.UIScale.SMALL
 43                    else 'menu_full'
 44                ),
 45                stack_offset=(
 46                    (0, -20) if uiscale is bui.UIScale.SMALL else (0, 0)
 47                ),
 48            ),
 49            transition=transition,
 50            origin_widget=origin_widget,
 51        )
 52
 53        if uiscale is bui.UIScale.SMALL:
 54            bui.containerwidget(
 55                edit=self._root_widget, on_cancel_call=self.main_window_back
 56            )
 57        else:
 58            btn = bui.buttonwidget(
 59                parent=self._root_widget,
 60                position=(55, self._height - 60),
 61                size=(60, 60),
 62                label=bui.charstr(bui.SpecialChar.BACK),
 63                button_type='backSmall',
 64                scale=0.8,
 65                on_activate_call=self.main_window_back,
 66            )
 67            bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 68
 69        bui.textwidget(
 70            parent=self._root_widget,
 71            position=(25, self._height - 57),
 72            size=(self._width, 25),
 73            text=bui.Lstr(resource=f'{self._r}.titleText'),
 74            color=bui.app.ui_v1.title_color,
 75            maxwidth=280,
 76            h_align='center',
 77            v_align='center',
 78        )
 79
 80        self._scroll_width = self._width - 100
 81        self._scroll_height = self._height - 110
 82        self._sub_width = self._scroll_width - 20
 83        self._sub_height = 360
 84
 85        self._scrollwidget = bui.scrollwidget(
 86            parent=self._root_widget,
 87            position=(
 88                (self._width - self._scroll_width) * 0.5,
 89                self._height - 65 - self._scroll_height,
 90            ),
 91            size=(self._scroll_width, self._scroll_height),
 92            claims_left_right=True,
 93            claims_tab=True,
 94            selection_loops_to_parent=True,
 95        )
 96        self._subcontainer = bui.containerwidget(
 97            parent=self._scrollwidget,
 98            size=(self._sub_width, self._sub_height),
 99            background=False,
100            claims_left_right=True,
101            claims_tab=True,
102            selection_loops_to_parent=True,
103        )
104        self._build_gui()

Create a MainWindow given a root widget and transition info.

Automatically handles in and out transitions on the provided widget, so there is no need to set transitions when creating it.

@override
def get_main_window_state(self) -> bauiv1.MainWindowState:
106    @override
107    def get_main_window_state(self) -> bui.MainWindowState:
108        # Support recreating our window for back/refresh purposes.
109        cls = type(self)
110        return bui.BasicMainWindowState(
111            create_call=lambda transition, origin_widget: cls(
112                transition=transition, origin_widget=origin_widget
113            )
114        )

Return a WindowState to recreate this window, if supported.

Inherited Members
bauiv1._uitypes.MainWindow
main_window_back_state
main_window_is_top_level
main_window_close
main_window_has_control
main_window_back
main_window_replace
on_main_window_close
bauiv1._uitypes.Window
get_root_widget