bastd.ui.configerror

UI for dealing with broken config files.

 1# Released under the MIT License. See LICENSE for details.
 2#
 3"""UI for dealing with broken config files."""
 4
 5from __future__ import annotations
 6
 7from typing import TYPE_CHECKING
 8
 9import _ba
10import ba
11
12if TYPE_CHECKING:
13    pass
14
15
16class ConfigErrorWindow(ba.Window):
17    """Window for dealing with a broken config."""
18
19    def __init__(self) -> None:
20        self._config_file_path = ba.app.config_file_path
21        width = 800
22        super().__init__(
23            ba.containerwidget(size=(width, 300), transition='in_right'))
24        padding = 20
25        ba.textwidget(
26            parent=self._root_widget,
27            position=(padding, 220),
28            size=(width - 2 * padding, 100 - 2 * padding),
29            h_align='center',
30            v_align='top',
31            scale=0.73,
32            text=(f'Error reading {_ba.appnameupper()} config file'
33                  ':\n\n\nCheck the console'
34                  ' (press ~ twice) for details.\n\nWould you like to quit and'
35                  ' try to fix it by hand\nor overwrite it with defaults?\n\n'
36                  '(high scores, player profiles, etc will be lost if you'
37                  ' overwrite)'))
38        ba.textwidget(parent=self._root_widget,
39                      position=(padding, 198),
40                      size=(width - 2 * padding, 100 - 2 * padding),
41                      h_align='center',
42                      v_align='top',
43                      scale=0.5,
44                      text=self._config_file_path)
45        quit_button = ba.buttonwidget(parent=self._root_widget,
46                                      position=(35, 30),
47                                      size=(240, 54),
48                                      label='Quit and Edit',
49                                      on_activate_call=self._quit)
50        ba.buttonwidget(parent=self._root_widget,
51                        position=(width - 370, 30),
52                        size=(330, 54),
53                        label='Overwrite with Defaults',
54                        on_activate_call=self._defaults)
55        ba.containerwidget(edit=self._root_widget,
56                           cancel_button=quit_button,
57                           selected_child=quit_button)
58
59    def _quit(self) -> None:
60        ba.timer(0.001, self._edit_and_quit, timetype=ba.TimeType.REAL)
61        _ba.lock_all_input()
62
63    def _edit_and_quit(self) -> None:
64        _ba.open_file_externally(self._config_file_path)
65        ba.timer(0.1, ba.quit, timetype=ba.TimeType.REAL)
66
67    def _defaults(self) -> None:
68        from ba.internal import commit_app_config
69        ba.containerwidget(edit=self._root_widget, transition='out_left')
70        ba.playsound(ba.getsound('gunCocking'))
71        ba.screenmessage('settings reset.', color=(1, 1, 0))
72
73        # At this point settings are already set; lets just commit them
74        # to disk.
75        commit_app_config(force=True)
class ConfigErrorWindow(ba.ui.Window):
17class ConfigErrorWindow(ba.Window):
18    """Window for dealing with a broken config."""
19
20    def __init__(self) -> None:
21        self._config_file_path = ba.app.config_file_path
22        width = 800
23        super().__init__(
24            ba.containerwidget(size=(width, 300), transition='in_right'))
25        padding = 20
26        ba.textwidget(
27            parent=self._root_widget,
28            position=(padding, 220),
29            size=(width - 2 * padding, 100 - 2 * padding),
30            h_align='center',
31            v_align='top',
32            scale=0.73,
33            text=(f'Error reading {_ba.appnameupper()} config file'
34                  ':\n\n\nCheck the console'
35                  ' (press ~ twice) for details.\n\nWould you like to quit and'
36                  ' try to fix it by hand\nor overwrite it with defaults?\n\n'
37                  '(high scores, player profiles, etc will be lost if you'
38                  ' overwrite)'))
39        ba.textwidget(parent=self._root_widget,
40                      position=(padding, 198),
41                      size=(width - 2 * padding, 100 - 2 * padding),
42                      h_align='center',
43                      v_align='top',
44                      scale=0.5,
45                      text=self._config_file_path)
46        quit_button = ba.buttonwidget(parent=self._root_widget,
47                                      position=(35, 30),
48                                      size=(240, 54),
49                                      label='Quit and Edit',
50                                      on_activate_call=self._quit)
51        ba.buttonwidget(parent=self._root_widget,
52                        position=(width - 370, 30),
53                        size=(330, 54),
54                        label='Overwrite with Defaults',
55                        on_activate_call=self._defaults)
56        ba.containerwidget(edit=self._root_widget,
57                           cancel_button=quit_button,
58                           selected_child=quit_button)
59
60    def _quit(self) -> None:
61        ba.timer(0.001, self._edit_and_quit, timetype=ba.TimeType.REAL)
62        _ba.lock_all_input()
63
64    def _edit_and_quit(self) -> None:
65        _ba.open_file_externally(self._config_file_path)
66        ba.timer(0.1, ba.quit, timetype=ba.TimeType.REAL)
67
68    def _defaults(self) -> None:
69        from ba.internal import commit_app_config
70        ba.containerwidget(edit=self._root_widget, transition='out_left')
71        ba.playsound(ba.getsound('gunCocking'))
72        ba.screenmessage('settings reset.', color=(1, 1, 0))
73
74        # At this point settings are already set; lets just commit them
75        # to disk.
76        commit_app_config(force=True)

Window for dealing with a broken config.

ConfigErrorWindow()
20    def __init__(self) -> None:
21        self._config_file_path = ba.app.config_file_path
22        width = 800
23        super().__init__(
24            ba.containerwidget(size=(width, 300), transition='in_right'))
25        padding = 20
26        ba.textwidget(
27            parent=self._root_widget,
28            position=(padding, 220),
29            size=(width - 2 * padding, 100 - 2 * padding),
30            h_align='center',
31            v_align='top',
32            scale=0.73,
33            text=(f'Error reading {_ba.appnameupper()} config file'
34                  ':\n\n\nCheck the console'
35                  ' (press ~ twice) for details.\n\nWould you like to quit and'
36                  ' try to fix it by hand\nor overwrite it with defaults?\n\n'
37                  '(high scores, player profiles, etc will be lost if you'
38                  ' overwrite)'))
39        ba.textwidget(parent=self._root_widget,
40                      position=(padding, 198),
41                      size=(width - 2 * padding, 100 - 2 * padding),
42                      h_align='center',
43                      v_align='top',
44                      scale=0.5,
45                      text=self._config_file_path)
46        quit_button = ba.buttonwidget(parent=self._root_widget,
47                                      position=(35, 30),
48                                      size=(240, 54),
49                                      label='Quit and Edit',
50                                      on_activate_call=self._quit)
51        ba.buttonwidget(parent=self._root_widget,
52                        position=(width - 370, 30),
53                        size=(330, 54),
54                        label='Overwrite with Defaults',
55                        on_activate_call=self._defaults)
56        ba.containerwidget(edit=self._root_widget,
57                           cancel_button=quit_button,
58                           selected_child=quit_button)
Inherited Members
ba.ui.Window
get_root_widget