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