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

Settings window for touchscreens.

Inherited Members
bauiv1._uitypes.Window
get_root_widget