bastd.ui.settings.testing

Provides UI for test settings.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""Provides UI for test settings."""
  4
  5from __future__ import annotations
  6
  7import copy
  8from typing import TYPE_CHECKING
  9
 10import ba
 11import ba.internal
 12
 13if TYPE_CHECKING:
 14    from typing import Any, Callable
 15
 16
 17class TestingWindow(ba.Window):
 18    """Window for conveniently testing various settings."""
 19
 20    def __init__(
 21        self,
 22        title: ba.Lstr,
 23        entries: list[dict[str, Any]],
 24        transition: str = 'in_right',
 25        back_call: Callable[[], ba.Window] | None = None,
 26    ):
 27        uiscale = ba.app.ui.uiscale
 28        self._width = 600
 29        self._height = 324 if uiscale is ba.UIScale.SMALL else 400
 30        self._entries = copy.deepcopy(entries)
 31        self._back_call = back_call
 32        super().__init__(
 33            root_widget=ba.containerwidget(
 34                size=(self._width, self._height),
 35                transition=transition,
 36                scale=(
 37                    2.5
 38                    if uiscale is ba.UIScale.SMALL
 39                    else 1.2
 40                    if uiscale is ba.UIScale.MEDIUM
 41                    else 1.0
 42                ),
 43                stack_offset=(0, -28)
 44                if uiscale is ba.UIScale.SMALL
 45                else (0, 0),
 46            )
 47        )
 48        self._back_button = btn = ba.buttonwidget(
 49            parent=self._root_widget,
 50            autoselect=True,
 51            position=(65, self._height - 59),
 52            size=(130, 60),
 53            scale=0.8,
 54            text_scale=1.2,
 55            label=ba.Lstr(resource='backText'),
 56            button_type='back',
 57            on_activate_call=self._do_back,
 58        )
 59        ba.textwidget(
 60            parent=self._root_widget,
 61            position=(self._width * 0.5, self._height - 35),
 62            size=(0, 0),
 63            color=ba.app.ui.title_color,
 64            h_align='center',
 65            v_align='center',
 66            maxwidth=245,
 67            text=title,
 68        )
 69
 70        ba.buttonwidget(
 71            edit=self._back_button,
 72            button_type='backSmall',
 73            size=(60, 60),
 74            label=ba.charstr(ba.SpecialChar.BACK),
 75        )
 76
 77        ba.textwidget(
 78            parent=self._root_widget,
 79            position=(self._width * 0.5, self._height - 75),
 80            size=(0, 0),
 81            color=ba.app.ui.infotextcolor,
 82            h_align='center',
 83            v_align='center',
 84            maxwidth=self._width * 0.75,
 85            text=ba.Lstr(resource='settingsWindowAdvanced.forTestingText'),
 86        )
 87        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
 88        self._scroll_width = self._width - 130
 89        self._scroll_height = self._height - 140
 90        self._scrollwidget = ba.scrollwidget(
 91            parent=self._root_widget,
 92            size=(self._scroll_width, self._scroll_height),
 93            highlight=False,
 94            position=((self._width - self._scroll_width) * 0.5, 40),
 95        )
 96        ba.containerwidget(edit=self._scrollwidget, claims_left_right=True)
 97
 98        self._spacing = 50
 99
100        self._sub_width = self._scroll_width * 0.95
101        self._sub_height = 50 + len(self._entries) * self._spacing + 60
102        self._subcontainer = ba.containerwidget(
103            parent=self._scrollwidget,
104            size=(self._sub_width, self._sub_height),
105            background=False,
106        )
107
108        h = 230
109        v = self._sub_height - 48
110
111        for i, entry in enumerate(self._entries):
112
113            entry_name = entry['name']
114
115            # If we haven't yet, record the default value for this name so
116            # we can reset if we want..
117            if entry_name not in ba.app.value_test_defaults:
118                ba.app.value_test_defaults[entry_name] = ba.internal.value_test(
119                    entry_name
120                )
121
122            ba.textwidget(
123                parent=self._subcontainer,
124                position=(h, v),
125                size=(0, 0),
126                h_align='right',
127                v_align='center',
128                maxwidth=200,
129                text=entry['label'],
130            )
131            btn = ba.buttonwidget(
132                parent=self._subcontainer,
133                position=(h + 20, v - 19),
134                size=(40, 40),
135                autoselect=True,
136                repeat=True,
137                left_widget=self._back_button,
138                button_type='square',
139                label='-',
140                on_activate_call=ba.Call(self._on_minus_press, entry['name']),
141            )
142            if i == 0:
143                ba.widget(edit=btn, up_widget=self._back_button)
144            # pylint: disable=consider-using-f-string
145            entry['widget'] = ba.textwidget(
146                parent=self._subcontainer,
147                position=(h + 100, v),
148                size=(0, 0),
149                h_align='center',
150                v_align='center',
151                maxwidth=60,
152                text='%.4g' % ba.internal.value_test(entry_name),
153            )
154            btn = ba.buttonwidget(
155                parent=self._subcontainer,
156                position=(h + 140, v - 19),
157                size=(40, 40),
158                autoselect=True,
159                repeat=True,
160                button_type='square',
161                label='+',
162                on_activate_call=ba.Call(self._on_plus_press, entry['name']),
163            )
164            if i == 0:
165                ba.widget(edit=btn, up_widget=self._back_button)
166            v -= self._spacing
167        v -= 35
168        ba.buttonwidget(
169            parent=self._subcontainer,
170            autoselect=True,
171            size=(200, 50),
172            position=(self._sub_width * 0.5 - 100, v),
173            label=ba.Lstr(resource='settingsWindowAdvanced.resetText'),
174            right_widget=btn,
175            on_activate_call=self._on_reset_press,
176        )
177
178    def _get_entry(self, name: str) -> dict[str, Any]:
179        for entry in self._entries:
180            if entry['name'] == name:
181                return entry
182        raise ba.NotFoundError(f'Entry not found: {name}')
183
184    def _on_reset_press(self) -> None:
185        for entry in self._entries:
186            ba.internal.value_test(
187                entry['name'],
188                absolute=ba.app.value_test_defaults[entry['name']],
189            )
190            # pylint: disable=consider-using-f-string
191            ba.textwidget(
192                edit=entry['widget'],
193                text='%.4g' % ba.internal.value_test(entry['name']),
194            )
195
196    def _on_minus_press(self, entry_name: str) -> None:
197        entry = self._get_entry(entry_name)
198        ba.internal.value_test(entry['name'], change=-entry['increment'])
199        # pylint: disable=consider-using-f-string
200        ba.textwidget(
201            edit=entry['widget'],
202            text='%.4g' % ba.internal.value_test(entry['name']),
203        )
204
205    def _on_plus_press(self, entry_name: str) -> None:
206        entry = self._get_entry(entry_name)
207        ba.internal.value_test(entry['name'], change=entry['increment'])
208        # pylint: disable=consider-using-f-string
209        ba.textwidget(
210            edit=entry['widget'],
211            text='%.4g' % ba.internal.value_test(entry['name']),
212        )
213
214    def _do_back(self) -> None:
215        # pylint: disable=cyclic-import
216        from bastd.ui.settings.advanced import AdvancedSettingsWindow
217
218        ba.containerwidget(edit=self._root_widget, transition='out_right')
219        backwin = (
220            self._back_call()
221            if self._back_call is not None
222            else AdvancedSettingsWindow(transition='in_left')
223        )
224        ba.app.ui.set_main_menu_window(backwin.get_root_widget())
class TestingWindow(ba.ui.Window):
 18class TestingWindow(ba.Window):
 19    """Window for conveniently testing various settings."""
 20
 21    def __init__(
 22        self,
 23        title: ba.Lstr,
 24        entries: list[dict[str, Any]],
 25        transition: str = 'in_right',
 26        back_call: Callable[[], ba.Window] | None = None,
 27    ):
 28        uiscale = ba.app.ui.uiscale
 29        self._width = 600
 30        self._height = 324 if uiscale is ba.UIScale.SMALL else 400
 31        self._entries = copy.deepcopy(entries)
 32        self._back_call = back_call
 33        super().__init__(
 34            root_widget=ba.containerwidget(
 35                size=(self._width, self._height),
 36                transition=transition,
 37                scale=(
 38                    2.5
 39                    if uiscale is ba.UIScale.SMALL
 40                    else 1.2
 41                    if uiscale is ba.UIScale.MEDIUM
 42                    else 1.0
 43                ),
 44                stack_offset=(0, -28)
 45                if uiscale is ba.UIScale.SMALL
 46                else (0, 0),
 47            )
 48        )
 49        self._back_button = btn = ba.buttonwidget(
 50            parent=self._root_widget,
 51            autoselect=True,
 52            position=(65, self._height - 59),
 53            size=(130, 60),
 54            scale=0.8,
 55            text_scale=1.2,
 56            label=ba.Lstr(resource='backText'),
 57            button_type='back',
 58            on_activate_call=self._do_back,
 59        )
 60        ba.textwidget(
 61            parent=self._root_widget,
 62            position=(self._width * 0.5, self._height - 35),
 63            size=(0, 0),
 64            color=ba.app.ui.title_color,
 65            h_align='center',
 66            v_align='center',
 67            maxwidth=245,
 68            text=title,
 69        )
 70
 71        ba.buttonwidget(
 72            edit=self._back_button,
 73            button_type='backSmall',
 74            size=(60, 60),
 75            label=ba.charstr(ba.SpecialChar.BACK),
 76        )
 77
 78        ba.textwidget(
 79            parent=self._root_widget,
 80            position=(self._width * 0.5, self._height - 75),
 81            size=(0, 0),
 82            color=ba.app.ui.infotextcolor,
 83            h_align='center',
 84            v_align='center',
 85            maxwidth=self._width * 0.75,
 86            text=ba.Lstr(resource='settingsWindowAdvanced.forTestingText'),
 87        )
 88        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
 89        self._scroll_width = self._width - 130
 90        self._scroll_height = self._height - 140
 91        self._scrollwidget = ba.scrollwidget(
 92            parent=self._root_widget,
 93            size=(self._scroll_width, self._scroll_height),
 94            highlight=False,
 95            position=((self._width - self._scroll_width) * 0.5, 40),
 96        )
 97        ba.containerwidget(edit=self._scrollwidget, claims_left_right=True)
 98
 99        self._spacing = 50
100
101        self._sub_width = self._scroll_width * 0.95
102        self._sub_height = 50 + len(self._entries) * self._spacing + 60
103        self._subcontainer = ba.containerwidget(
104            parent=self._scrollwidget,
105            size=(self._sub_width, self._sub_height),
106            background=False,
107        )
108
109        h = 230
110        v = self._sub_height - 48
111
112        for i, entry in enumerate(self._entries):
113
114            entry_name = entry['name']
115
116            # If we haven't yet, record the default value for this name so
117            # we can reset if we want..
118            if entry_name not in ba.app.value_test_defaults:
119                ba.app.value_test_defaults[entry_name] = ba.internal.value_test(
120                    entry_name
121                )
122
123            ba.textwidget(
124                parent=self._subcontainer,
125                position=(h, v),
126                size=(0, 0),
127                h_align='right',
128                v_align='center',
129                maxwidth=200,
130                text=entry['label'],
131            )
132            btn = ba.buttonwidget(
133                parent=self._subcontainer,
134                position=(h + 20, v - 19),
135                size=(40, 40),
136                autoselect=True,
137                repeat=True,
138                left_widget=self._back_button,
139                button_type='square',
140                label='-',
141                on_activate_call=ba.Call(self._on_minus_press, entry['name']),
142            )
143            if i == 0:
144                ba.widget(edit=btn, up_widget=self._back_button)
145            # pylint: disable=consider-using-f-string
146            entry['widget'] = ba.textwidget(
147                parent=self._subcontainer,
148                position=(h + 100, v),
149                size=(0, 0),
150                h_align='center',
151                v_align='center',
152                maxwidth=60,
153                text='%.4g' % ba.internal.value_test(entry_name),
154            )
155            btn = ba.buttonwidget(
156                parent=self._subcontainer,
157                position=(h + 140, v - 19),
158                size=(40, 40),
159                autoselect=True,
160                repeat=True,
161                button_type='square',
162                label='+',
163                on_activate_call=ba.Call(self._on_plus_press, entry['name']),
164            )
165            if i == 0:
166                ba.widget(edit=btn, up_widget=self._back_button)
167            v -= self._spacing
168        v -= 35
169        ba.buttonwidget(
170            parent=self._subcontainer,
171            autoselect=True,
172            size=(200, 50),
173            position=(self._sub_width * 0.5 - 100, v),
174            label=ba.Lstr(resource='settingsWindowAdvanced.resetText'),
175            right_widget=btn,
176            on_activate_call=self._on_reset_press,
177        )
178
179    def _get_entry(self, name: str) -> dict[str, Any]:
180        for entry in self._entries:
181            if entry['name'] == name:
182                return entry
183        raise ba.NotFoundError(f'Entry not found: {name}')
184
185    def _on_reset_press(self) -> None:
186        for entry in self._entries:
187            ba.internal.value_test(
188                entry['name'],
189                absolute=ba.app.value_test_defaults[entry['name']],
190            )
191            # pylint: disable=consider-using-f-string
192            ba.textwidget(
193                edit=entry['widget'],
194                text='%.4g' % ba.internal.value_test(entry['name']),
195            )
196
197    def _on_minus_press(self, entry_name: str) -> None:
198        entry = self._get_entry(entry_name)
199        ba.internal.value_test(entry['name'], change=-entry['increment'])
200        # pylint: disable=consider-using-f-string
201        ba.textwidget(
202            edit=entry['widget'],
203            text='%.4g' % ba.internal.value_test(entry['name']),
204        )
205
206    def _on_plus_press(self, entry_name: str) -> None:
207        entry = self._get_entry(entry_name)
208        ba.internal.value_test(entry['name'], change=entry['increment'])
209        # pylint: disable=consider-using-f-string
210        ba.textwidget(
211            edit=entry['widget'],
212            text='%.4g' % ba.internal.value_test(entry['name']),
213        )
214
215    def _do_back(self) -> None:
216        # pylint: disable=cyclic-import
217        from bastd.ui.settings.advanced import AdvancedSettingsWindow
218
219        ba.containerwidget(edit=self._root_widget, transition='out_right')
220        backwin = (
221            self._back_call()
222            if self._back_call is not None
223            else AdvancedSettingsWindow(transition='in_left')
224        )
225        ba.app.ui.set_main_menu_window(backwin.get_root_widget())

Window for conveniently testing various settings.

TestingWindow( title: ba._language.Lstr, entries: list[dict[str, typing.Any]], transition: str = 'in_right', back_call: Optional[Callable[[], ba.ui.Window]] = None)
 21    def __init__(
 22        self,
 23        title: ba.Lstr,
 24        entries: list[dict[str, Any]],
 25        transition: str = 'in_right',
 26        back_call: Callable[[], ba.Window] | None = None,
 27    ):
 28        uiscale = ba.app.ui.uiscale
 29        self._width = 600
 30        self._height = 324 if uiscale is ba.UIScale.SMALL else 400
 31        self._entries = copy.deepcopy(entries)
 32        self._back_call = back_call
 33        super().__init__(
 34            root_widget=ba.containerwidget(
 35                size=(self._width, self._height),
 36                transition=transition,
 37                scale=(
 38                    2.5
 39                    if uiscale is ba.UIScale.SMALL
 40                    else 1.2
 41                    if uiscale is ba.UIScale.MEDIUM
 42                    else 1.0
 43                ),
 44                stack_offset=(0, -28)
 45                if uiscale is ba.UIScale.SMALL
 46                else (0, 0),
 47            )
 48        )
 49        self._back_button = btn = ba.buttonwidget(
 50            parent=self._root_widget,
 51            autoselect=True,
 52            position=(65, self._height - 59),
 53            size=(130, 60),
 54            scale=0.8,
 55            text_scale=1.2,
 56            label=ba.Lstr(resource='backText'),
 57            button_type='back',
 58            on_activate_call=self._do_back,
 59        )
 60        ba.textwidget(
 61            parent=self._root_widget,
 62            position=(self._width * 0.5, self._height - 35),
 63            size=(0, 0),
 64            color=ba.app.ui.title_color,
 65            h_align='center',
 66            v_align='center',
 67            maxwidth=245,
 68            text=title,
 69        )
 70
 71        ba.buttonwidget(
 72            edit=self._back_button,
 73            button_type='backSmall',
 74            size=(60, 60),
 75            label=ba.charstr(ba.SpecialChar.BACK),
 76        )
 77
 78        ba.textwidget(
 79            parent=self._root_widget,
 80            position=(self._width * 0.5, self._height - 75),
 81            size=(0, 0),
 82            color=ba.app.ui.infotextcolor,
 83            h_align='center',
 84            v_align='center',
 85            maxwidth=self._width * 0.75,
 86            text=ba.Lstr(resource='settingsWindowAdvanced.forTestingText'),
 87        )
 88        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
 89        self._scroll_width = self._width - 130
 90        self._scroll_height = self._height - 140
 91        self._scrollwidget = ba.scrollwidget(
 92            parent=self._root_widget,
 93            size=(self._scroll_width, self._scroll_height),
 94            highlight=False,
 95            position=((self._width - self._scroll_width) * 0.5, 40),
 96        )
 97        ba.containerwidget(edit=self._scrollwidget, claims_left_right=True)
 98
 99        self._spacing = 50
100
101        self._sub_width = self._scroll_width * 0.95
102        self._sub_height = 50 + len(self._entries) * self._spacing + 60
103        self._subcontainer = ba.containerwidget(
104            parent=self._scrollwidget,
105            size=(self._sub_width, self._sub_height),
106            background=False,
107        )
108
109        h = 230
110        v = self._sub_height - 48
111
112        for i, entry in enumerate(self._entries):
113
114            entry_name = entry['name']
115
116            # If we haven't yet, record the default value for this name so
117            # we can reset if we want..
118            if entry_name not in ba.app.value_test_defaults:
119                ba.app.value_test_defaults[entry_name] = ba.internal.value_test(
120                    entry_name
121                )
122
123            ba.textwidget(
124                parent=self._subcontainer,
125                position=(h, v),
126                size=(0, 0),
127                h_align='right',
128                v_align='center',
129                maxwidth=200,
130                text=entry['label'],
131            )
132            btn = ba.buttonwidget(
133                parent=self._subcontainer,
134                position=(h + 20, v - 19),
135                size=(40, 40),
136                autoselect=True,
137                repeat=True,
138                left_widget=self._back_button,
139                button_type='square',
140                label='-',
141                on_activate_call=ba.Call(self._on_minus_press, entry['name']),
142            )
143            if i == 0:
144                ba.widget(edit=btn, up_widget=self._back_button)
145            # pylint: disable=consider-using-f-string
146            entry['widget'] = ba.textwidget(
147                parent=self._subcontainer,
148                position=(h + 100, v),
149                size=(0, 0),
150                h_align='center',
151                v_align='center',
152                maxwidth=60,
153                text='%.4g' % ba.internal.value_test(entry_name),
154            )
155            btn = ba.buttonwidget(
156                parent=self._subcontainer,
157                position=(h + 140, v - 19),
158                size=(40, 40),
159                autoselect=True,
160                repeat=True,
161                button_type='square',
162                label='+',
163                on_activate_call=ba.Call(self._on_plus_press, entry['name']),
164            )
165            if i == 0:
166                ba.widget(edit=btn, up_widget=self._back_button)
167            v -= self._spacing
168        v -= 35
169        ba.buttonwidget(
170            parent=self._subcontainer,
171            autoselect=True,
172            size=(200, 50),
173            position=(self._sub_width * 0.5 - 100, v),
174            label=ba.Lstr(resource='settingsWindowAdvanced.resetText'),
175            right_widget=btn,
176            on_activate_call=self._on_reset_press,
177        )
Inherited Members
ba.ui.Window
get_root_widget