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
  6import bauiv1 as bui
  7import bascenev1 as bs
  8
  9
 10class TouchscreenSettingsWindow(bui.Window):
 11    """Settings window for touchscreens."""
 12
 13    def __del__(self) -> None:
 14        # Note - this happens in 'back' too;
 15        # we just do it here too in case the window is closed by other means.
 16
 17        # FIXME: Could switch to a UI destroy callback now that those are a
 18        #  thing that exists.
 19        bs.set_touchscreen_editing(False)
 20
 21    def __init__(self) -> None:
 22        self._width = 650
 23        self._height = 380
 24        self._spacing = 40
 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                transition='in_right',
 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            )
 41        )
 42
 43        btn = bui.buttonwidget(
 44            parent=self._root_widget,
 45            position=(55, self._height - 60),
 46            size=(120, 60),
 47            label=bui.Lstr(resource='backText'),
 48            button_type='back',
 49            scale=0.8,
 50            on_activate_call=self._back,
 51        )
 52        bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 53
 54        bui.textwidget(
 55            parent=self._root_widget,
 56            position=(25, self._height - 50),
 57            size=(self._width, 25),
 58            text=bui.Lstr(resource=self._r + '.titleText'),
 59            color=bui.app.ui_v1.title_color,
 60            maxwidth=280,
 61            h_align='center',
 62            v_align='center',
 63        )
 64
 65        bui.buttonwidget(
 66            edit=btn,
 67            button_type='backSmall',
 68            size=(60, 60),
 69            label=bui.charstr(bui.SpecialChar.BACK),
 70        )
 71
 72        self._scroll_width = self._width - 100
 73        self._scroll_height = self._height - 110
 74        self._sub_width = self._scroll_width - 20
 75        self._sub_height = 360
 76
 77        self._scrollwidget = bui.scrollwidget(
 78            parent=self._root_widget,
 79            position=(
 80                (self._width - self._scroll_width) * 0.5,
 81                self._height - 65 - self._scroll_height,
 82            ),
 83            size=(self._scroll_width, self._scroll_height),
 84            claims_left_right=True,
 85            claims_tab=True,
 86            selection_loops_to_parent=True,
 87        )
 88        self._subcontainer = bui.containerwidget(
 89            parent=self._scrollwidget,
 90            size=(self._sub_width, self._sub_height),
 91            background=False,
 92            claims_left_right=True,
 93            claims_tab=True,
 94            selection_loops_to_parent=True,
 95        )
 96        self._build_gui()
 97
 98    def _build_gui(self) -> None:
 99        from bauiv1lib.config import ConfigNumberEdit, ConfigCheckBox
100        from bauiv1lib.radiogroup import make_radio_group
101
102        # Clear anything already there.
103        children = self._subcontainer.get_children()
104        for child in children:
105            child.delete()
106        h = 30
107        v = self._sub_height - 85
108        clr = (0.8, 0.8, 0.8, 1.0)
109        clr2 = (0.8, 0.8, 0.8)
110        bui.textwidget(
111            parent=self._subcontainer,
112            position=(-10, v + 43),
113            size=(self._sub_width, 25),
114            text=bui.Lstr(resource=self._r + '.swipeInfoText'),
115            flatness=1.0,
116            color=(0, 0.9, 0.1, 0.7),
117            maxwidth=self._sub_width * 0.9,
118            scale=0.55,
119            h_align='center',
120            v_align='center',
121        )
122        cur_val = bui.app.config.get('Touch Movement Control Type', 'swipe')
123        bui.textwidget(
124            parent=self._subcontainer,
125            position=(h, v - 2),
126            size=(0, 30),
127            text=bui.Lstr(resource=self._r + '.movementText'),
128            maxwidth=190,
129            color=clr,
130            v_align='center',
131        )
132        cb1 = bui.checkboxwidget(
133            parent=self._subcontainer,
134            position=(h + 220, v),
135            size=(170, 30),
136            text=bui.Lstr(resource=self._r + '.joystickText'),
137            maxwidth=100,
138            textcolor=clr2,
139            scale=0.9,
140        )
141        cb2 = bui.checkboxwidget(
142            parent=self._subcontainer,
143            position=(h + 357, v),
144            size=(170, 30),
145            text=bui.Lstr(resource=self._r + '.swipeText'),
146            maxwidth=100,
147            textcolor=clr2,
148            value=False,
149            scale=0.9,
150        )
151        make_radio_group(
152            (cb1, cb2), ('joystick', 'swipe'), cur_val, self._movement_changed
153        )
154        v -= 50
155        ConfigNumberEdit(
156            parent=self._subcontainer,
157            position=(h, v),
158            xoffset=65,
159            configkey='Touch Controls Scale Movement',
160            displayname=bui.Lstr(
161                resource=self._r + '.movementControlScaleText'
162            ),
163            changesound=False,
164            minval=0.1,
165            maxval=4.0,
166            increment=0.1,
167        )
168        v -= 50
169        cur_val = bui.app.config.get('Touch Action Control Type', 'buttons')
170        bui.textwidget(
171            parent=self._subcontainer,
172            position=(h, v - 2),
173            size=(0, 30),
174            text=bui.Lstr(resource=self._r + '.actionsText'),
175            maxwidth=190,
176            color=clr,
177            v_align='center',
178        )
179        cb1 = bui.checkboxwidget(
180            parent=self._subcontainer,
181            position=(h + 220, v),
182            size=(170, 30),
183            text=bui.Lstr(resource=self._r + '.buttonsText'),
184            maxwidth=100,
185            textcolor=clr2,
186            scale=0.9,
187        )
188        cb2 = bui.checkboxwidget(
189            parent=self._subcontainer,
190            position=(h + 357, v),
191            size=(170, 30),
192            text=bui.Lstr(resource=self._r + '.swipeText'),
193            maxwidth=100,
194            textcolor=clr2,
195            scale=0.9,
196        )
197        make_radio_group(
198            (cb1, cb2), ('buttons', 'swipe'), cur_val, self._actions_changed
199        )
200        v -= 50
201        ConfigNumberEdit(
202            parent=self._subcontainer,
203            position=(h, v),
204            xoffset=65,
205            configkey='Touch Controls Scale Actions',
206            displayname=bui.Lstr(resource=self._r + '.actionControlScaleText'),
207            changesound=False,
208            minval=0.1,
209            maxval=4.0,
210            increment=0.1,
211        )
212
213        v -= 50
214        ConfigCheckBox(
215            parent=self._subcontainer,
216            position=(h, v),
217            size=(400, 30),
218            maxwidth=400,
219            configkey='Touch Controls Swipe Hidden',
220            displayname=bui.Lstr(resource=self._r + '.swipeControlsHiddenText'),
221        )
222        v -= 65
223
224        bui.buttonwidget(
225            parent=self._subcontainer,
226            position=(self._sub_width * 0.5 - 70, v),
227            size=(170, 60),
228            label=bui.Lstr(resource=self._r + '.resetText'),
229            scale=0.75,
230            on_activate_call=self._reset,
231        )
232
233        bui.textwidget(
234            parent=self._root_widget,
235            position=(self._width * 0.5, 38),
236            size=(0, 0),
237            h_align='center',
238            text=bui.Lstr(resource=self._r + '.dragControlsText'),
239            maxwidth=self._width * 0.8,
240            scale=0.65,
241            color=(1, 1, 1, 0.4),
242        )
243
244    def _actions_changed(self, v: str) -> None:
245        cfg = bui.app.config
246        cfg['Touch Action Control Type'] = v
247        cfg.apply_and_commit()
248
249    def _movement_changed(self, v: str) -> None:
250        cfg = bui.app.config
251        cfg['Touch Movement Control Type'] = v
252        cfg.apply_and_commit()
253
254    def _reset(self) -> None:
255        cfg = bui.app.config
256        cfgkeys = [
257            'Touch Movement Control Type',
258            'Touch Action Control Type',
259            'Touch Controls Scale',
260            'Touch Controls Scale Movement',
261            'Touch Controls Scale Actions',
262            'Touch Controls Swipe Hidden',
263            'Touch DPad X',
264            'Touch DPad Y',
265            'Touch Buttons X',
266            'Touch Buttons Y',
267        ]
268        for cfgkey in cfgkeys:
269            if cfgkey in cfg:
270                del cfg[cfgkey]
271        cfg.apply_and_commit()
272        bui.apptimer(0, self._build_gui)
273
274    def _back(self) -> None:
275        from bauiv1lib.settings import controls
276
277        # no-op if our underlying widget is dead or on its way out.
278        if not self._root_widget or self._root_widget.transitioning_out:
279            return
280
281        bui.containerwidget(edit=self._root_widget, transition='out_right')
282        assert bui.app.classic is not None
283        bui.app.ui_v1.set_main_menu_window(
284            controls.ControlsSettingsWindow(
285                transition='in_left'
286            ).get_root_widget(),
287            from_window=self._root_widget,
288        )
289        bs.set_touchscreen_editing(False)
class TouchscreenSettingsWindow(bauiv1._uitypes.Window):
 11class TouchscreenSettingsWindow(bui.Window):
 12    """Settings window for touchscreens."""
 13
 14    def __del__(self) -> None:
 15        # Note - this happens in 'back' too;
 16        # we just do it here too in case the window is closed by other means.
 17
 18        # FIXME: Could switch to a UI destroy callback now that those are a
 19        #  thing that exists.
 20        bs.set_touchscreen_editing(False)
 21
 22    def __init__(self) -> None:
 23        self._width = 650
 24        self._height = 380
 25        self._spacing = 40
 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                transition='in_right',
 36                scale=(
 37                    1.9
 38                    if uiscale is bui.UIScale.SMALL
 39                    else 1.55 if uiscale is bui.UIScale.MEDIUM else 1.2
 40                ),
 41            )
 42        )
 43
 44        btn = bui.buttonwidget(
 45            parent=self._root_widget,
 46            position=(55, self._height - 60),
 47            size=(120, 60),
 48            label=bui.Lstr(resource='backText'),
 49            button_type='back',
 50            scale=0.8,
 51            on_activate_call=self._back,
 52        )
 53        bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 54
 55        bui.textwidget(
 56            parent=self._root_widget,
 57            position=(25, self._height - 50),
 58            size=(self._width, 25),
 59            text=bui.Lstr(resource=self._r + '.titleText'),
 60            color=bui.app.ui_v1.title_color,
 61            maxwidth=280,
 62            h_align='center',
 63            v_align='center',
 64        )
 65
 66        bui.buttonwidget(
 67            edit=btn,
 68            button_type='backSmall',
 69            size=(60, 60),
 70            label=bui.charstr(bui.SpecialChar.BACK),
 71        )
 72
 73        self._scroll_width = self._width - 100
 74        self._scroll_height = self._height - 110
 75        self._sub_width = self._scroll_width - 20
 76        self._sub_height = 360
 77
 78        self._scrollwidget = bui.scrollwidget(
 79            parent=self._root_widget,
 80            position=(
 81                (self._width - self._scroll_width) * 0.5,
 82                self._height - 65 - self._scroll_height,
 83            ),
 84            size=(self._scroll_width, self._scroll_height),
 85            claims_left_right=True,
 86            claims_tab=True,
 87            selection_loops_to_parent=True,
 88        )
 89        self._subcontainer = bui.containerwidget(
 90            parent=self._scrollwidget,
 91            size=(self._sub_width, self._sub_height),
 92            background=False,
 93            claims_left_right=True,
 94            claims_tab=True,
 95            selection_loops_to_parent=True,
 96        )
 97        self._build_gui()
 98
 99    def _build_gui(self) -> None:
100        from bauiv1lib.config import ConfigNumberEdit, ConfigCheckBox
101        from bauiv1lib.radiogroup import make_radio_group
102
103        # Clear anything already there.
104        children = self._subcontainer.get_children()
105        for child in children:
106            child.delete()
107        h = 30
108        v = self._sub_height - 85
109        clr = (0.8, 0.8, 0.8, 1.0)
110        clr2 = (0.8, 0.8, 0.8)
111        bui.textwidget(
112            parent=self._subcontainer,
113            position=(-10, v + 43),
114            size=(self._sub_width, 25),
115            text=bui.Lstr(resource=self._r + '.swipeInfoText'),
116            flatness=1.0,
117            color=(0, 0.9, 0.1, 0.7),
118            maxwidth=self._sub_width * 0.9,
119            scale=0.55,
120            h_align='center',
121            v_align='center',
122        )
123        cur_val = bui.app.config.get('Touch Movement Control Type', 'swipe')
124        bui.textwidget(
125            parent=self._subcontainer,
126            position=(h, v - 2),
127            size=(0, 30),
128            text=bui.Lstr(resource=self._r + '.movementText'),
129            maxwidth=190,
130            color=clr,
131            v_align='center',
132        )
133        cb1 = bui.checkboxwidget(
134            parent=self._subcontainer,
135            position=(h + 220, v),
136            size=(170, 30),
137            text=bui.Lstr(resource=self._r + '.joystickText'),
138            maxwidth=100,
139            textcolor=clr2,
140            scale=0.9,
141        )
142        cb2 = bui.checkboxwidget(
143            parent=self._subcontainer,
144            position=(h + 357, v),
145            size=(170, 30),
146            text=bui.Lstr(resource=self._r + '.swipeText'),
147            maxwidth=100,
148            textcolor=clr2,
149            value=False,
150            scale=0.9,
151        )
152        make_radio_group(
153            (cb1, cb2), ('joystick', 'swipe'), cur_val, self._movement_changed
154        )
155        v -= 50
156        ConfigNumberEdit(
157            parent=self._subcontainer,
158            position=(h, v),
159            xoffset=65,
160            configkey='Touch Controls Scale Movement',
161            displayname=bui.Lstr(
162                resource=self._r + '.movementControlScaleText'
163            ),
164            changesound=False,
165            minval=0.1,
166            maxval=4.0,
167            increment=0.1,
168        )
169        v -= 50
170        cur_val = bui.app.config.get('Touch Action Control Type', 'buttons')
171        bui.textwidget(
172            parent=self._subcontainer,
173            position=(h, v - 2),
174            size=(0, 30),
175            text=bui.Lstr(resource=self._r + '.actionsText'),
176            maxwidth=190,
177            color=clr,
178            v_align='center',
179        )
180        cb1 = bui.checkboxwidget(
181            parent=self._subcontainer,
182            position=(h + 220, v),
183            size=(170, 30),
184            text=bui.Lstr(resource=self._r + '.buttonsText'),
185            maxwidth=100,
186            textcolor=clr2,
187            scale=0.9,
188        )
189        cb2 = bui.checkboxwidget(
190            parent=self._subcontainer,
191            position=(h + 357, v),
192            size=(170, 30),
193            text=bui.Lstr(resource=self._r + '.swipeText'),
194            maxwidth=100,
195            textcolor=clr2,
196            scale=0.9,
197        )
198        make_radio_group(
199            (cb1, cb2), ('buttons', 'swipe'), cur_val, self._actions_changed
200        )
201        v -= 50
202        ConfigNumberEdit(
203            parent=self._subcontainer,
204            position=(h, v),
205            xoffset=65,
206            configkey='Touch Controls Scale Actions',
207            displayname=bui.Lstr(resource=self._r + '.actionControlScaleText'),
208            changesound=False,
209            minval=0.1,
210            maxval=4.0,
211            increment=0.1,
212        )
213
214        v -= 50
215        ConfigCheckBox(
216            parent=self._subcontainer,
217            position=(h, v),
218            size=(400, 30),
219            maxwidth=400,
220            configkey='Touch Controls Swipe Hidden',
221            displayname=bui.Lstr(resource=self._r + '.swipeControlsHiddenText'),
222        )
223        v -= 65
224
225        bui.buttonwidget(
226            parent=self._subcontainer,
227            position=(self._sub_width * 0.5 - 70, v),
228            size=(170, 60),
229            label=bui.Lstr(resource=self._r + '.resetText'),
230            scale=0.75,
231            on_activate_call=self._reset,
232        )
233
234        bui.textwidget(
235            parent=self._root_widget,
236            position=(self._width * 0.5, 38),
237            size=(0, 0),
238            h_align='center',
239            text=bui.Lstr(resource=self._r + '.dragControlsText'),
240            maxwidth=self._width * 0.8,
241            scale=0.65,
242            color=(1, 1, 1, 0.4),
243        )
244
245    def _actions_changed(self, v: str) -> None:
246        cfg = bui.app.config
247        cfg['Touch Action Control Type'] = v
248        cfg.apply_and_commit()
249
250    def _movement_changed(self, v: str) -> None:
251        cfg = bui.app.config
252        cfg['Touch Movement Control Type'] = v
253        cfg.apply_and_commit()
254
255    def _reset(self) -> None:
256        cfg = bui.app.config
257        cfgkeys = [
258            'Touch Movement Control Type',
259            'Touch Action Control Type',
260            'Touch Controls Scale',
261            'Touch Controls Scale Movement',
262            'Touch Controls Scale Actions',
263            'Touch Controls Swipe Hidden',
264            'Touch DPad X',
265            'Touch DPad Y',
266            'Touch Buttons X',
267            'Touch Buttons Y',
268        ]
269        for cfgkey in cfgkeys:
270            if cfgkey in cfg:
271                del cfg[cfgkey]
272        cfg.apply_and_commit()
273        bui.apptimer(0, self._build_gui)
274
275    def _back(self) -> None:
276        from bauiv1lib.settings import controls
277
278        # no-op if our underlying widget is dead or on its way out.
279        if not self._root_widget or self._root_widget.transitioning_out:
280            return
281
282        bui.containerwidget(edit=self._root_widget, transition='out_right')
283        assert bui.app.classic is not None
284        bui.app.ui_v1.set_main_menu_window(
285            controls.ControlsSettingsWindow(
286                transition='in_left'
287            ).get_root_widget(),
288            from_window=self._root_widget,
289        )
290        bs.set_touchscreen_editing(False)

Settings window for touchscreens.

Inherited Members
bauiv1._uitypes.Window
get_root_widget