bauiv1lib.config

Functionality for editing config values and applying them to the game.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""Functionality for editing config values and applying them to the game."""
  4
  5from __future__ import annotations
  6
  7from typing import TYPE_CHECKING
  8
  9import bauiv1 as bui
 10
 11if TYPE_CHECKING:
 12    from typing import Any, Callable
 13
 14
 15class ConfigCheckBox:
 16    """A checkbox wired up to control a config value.
 17
 18    It will automatically save and apply the config when its
 19    value changes.
 20    """
 21
 22    widget: bui.Widget
 23    """The underlying bui.Widget instance."""
 24
 25    def __init__(
 26        self,
 27        parent: bui.Widget,
 28        configkey: str,
 29        position: tuple[float, float],
 30        size: tuple[float, float],
 31        displayname: str | bui.Lstr | None = None,
 32        scale: float | None = None,
 33        maxwidth: float | None = None,
 34        autoselect: bool = True,
 35        value_change_call: Callable[[Any], Any] | None = None,
 36    ):
 37        if displayname is None:
 38            displayname = configkey
 39        self._value_change_call = value_change_call
 40        self._configkey = configkey
 41        self.widget = bui.checkboxwidget(
 42            parent=parent,
 43            autoselect=autoselect,
 44            position=position,
 45            size=size,
 46            text=displayname,
 47            textcolor=(0.8, 0.8, 0.8),
 48            value=bui.app.config.resolve(configkey),
 49            on_value_change_call=self._value_changed,
 50            scale=scale,
 51            maxwidth=maxwidth,
 52        )
 53        # complain if we outlive our checkbox
 54        bui.uicleanupcheck(self, self.widget)
 55
 56    def _value_changed(self, val: bool) -> None:
 57        cfg = bui.app.config
 58        cfg[self._configkey] = val
 59        if self._value_change_call is not None:
 60            self._value_change_call(val)
 61        cfg.apply_and_commit()
 62
 63
 64class ConfigNumberEdit:
 65    """A set of controls for editing a numeric config value.
 66
 67    It will automatically save and apply the config when its
 68    value changes.
 69    """
 70
 71    nametext: bui.Widget
 72    """The text widget displaying the name."""
 73
 74    valuetext: bui.Widget
 75    """The text widget displaying the current value."""
 76
 77    minusbutton: bui.Widget
 78    """The button widget used to reduce the value."""
 79
 80    plusbutton: bui.Widget
 81    """The button widget used to increase the value."""
 82
 83    def __init__(
 84        self,
 85        parent: bui.Widget,
 86        configkey: str,
 87        position: tuple[float, float],
 88        minval: float = 0.0,
 89        maxval: float = 100.0,
 90        increment: float = 1.0,
 91        callback: Callable[[float], Any] | None = None,
 92        xoffset: float = 0.0,
 93        displayname: str | bui.Lstr | None = None,
 94        changesound: bool = True,
 95        textscale: float = 1.0,
 96        as_percent: bool = False,
 97        fallback_value: float = 0.0,
 98        f: int = 1,
 99    ):
100        if displayname is None:
101            displayname = configkey
102
103        self._configkey = configkey
104        self._minval = minval
105        self._maxval = maxval
106        self._increment = increment
107        self._callback = callback
108        try:
109            self._value = bui.app.config.resolve(configkey)
110        except ValueError:
111            self._value = bui.app.config.get(configkey, fallback_value)
112        self._as_percent = as_percent
113        self._f = f
114
115        self.nametext = bui.textwidget(
116            parent=parent,
117            position=position,
118            size=(100, 30),
119            text=displayname,
120            maxwidth=160 + xoffset,
121            color=(0.8, 0.8, 0.8, 1.0),
122            h_align='left',
123            v_align='center',
124            scale=textscale,
125        )
126        self.valuetext = bui.textwidget(
127            parent=parent,
128            position=(246 + xoffset, position[1]),
129            size=(60, 28),
130            editable=False,
131            color=(0.3, 1.0, 0.3, 1.0),
132            h_align='right',
133            v_align='center',
134            text=str(self._value),
135            padding=2,
136        )
137        self.minusbutton = bui.buttonwidget(
138            parent=parent,
139            position=(330 + xoffset, position[1]),
140            size=(28, 28),
141            label='-',
142            autoselect=True,
143            on_activate_call=bui.Call(self._down),
144            repeat=True,
145            enable_sound=changesound,
146        )
147        self.plusbutton = bui.buttonwidget(
148            parent=parent,
149            position=(380 + xoffset, position[1]),
150            size=(28, 28),
151            label='+',
152            autoselect=True,
153            on_activate_call=bui.Call(self._up),
154            repeat=True,
155            enable_sound=changesound,
156        )
157        # Complain if we outlive our widgets.
158        bui.uicleanupcheck(self, self.nametext)
159        self._update_display()
160
161    def _up(self) -> None:
162        self._value = min(self._maxval, self._value + self._increment)
163        self._changed()
164
165    def _down(self) -> None:
166        self._value = max(self._minval, self._value - self._increment)
167        self._changed()
168
169    def _changed(self) -> None:
170        self._update_display()
171        if self._callback:
172            self._callback(self._value)
173        bui.app.config[self._configkey] = self._value
174        bui.app.config.apply_and_commit()
175
176    def _update_display(self) -> None:
177        if self._as_percent:
178            val = f'{round(self._value*100.0)}%'
179        else:
180            val = f'{self._value:.{self._f}f}'
181        bui.textwidget(edit=self.valuetext, text=val)
class ConfigCheckBox:
16class ConfigCheckBox:
17    """A checkbox wired up to control a config value.
18
19    It will automatically save and apply the config when its
20    value changes.
21    """
22
23    widget: bui.Widget
24    """The underlying bui.Widget instance."""
25
26    def __init__(
27        self,
28        parent: bui.Widget,
29        configkey: str,
30        position: tuple[float, float],
31        size: tuple[float, float],
32        displayname: str | bui.Lstr | None = None,
33        scale: float | None = None,
34        maxwidth: float | None = None,
35        autoselect: bool = True,
36        value_change_call: Callable[[Any], Any] | None = None,
37    ):
38        if displayname is None:
39            displayname = configkey
40        self._value_change_call = value_change_call
41        self._configkey = configkey
42        self.widget = bui.checkboxwidget(
43            parent=parent,
44            autoselect=autoselect,
45            position=position,
46            size=size,
47            text=displayname,
48            textcolor=(0.8, 0.8, 0.8),
49            value=bui.app.config.resolve(configkey),
50            on_value_change_call=self._value_changed,
51            scale=scale,
52            maxwidth=maxwidth,
53        )
54        # complain if we outlive our checkbox
55        bui.uicleanupcheck(self, self.widget)
56
57    def _value_changed(self, val: bool) -> None:
58        cfg = bui.app.config
59        cfg[self._configkey] = val
60        if self._value_change_call is not None:
61            self._value_change_call(val)
62        cfg.apply_and_commit()

A checkbox wired up to control a config value.

It will automatically save and apply the config when its value changes.

ConfigCheckBox( parent: _bauiv1.Widget, configkey: str, position: tuple[float, float], size: tuple[float, float], displayname: str | babase._language.Lstr | None = None, scale: float | None = None, maxwidth: float | None = None, autoselect: bool = True, value_change_call: Optional[Callable[[Any], Any]] = None)
26    def __init__(
27        self,
28        parent: bui.Widget,
29        configkey: str,
30        position: tuple[float, float],
31        size: tuple[float, float],
32        displayname: str | bui.Lstr | None = None,
33        scale: float | None = None,
34        maxwidth: float | None = None,
35        autoselect: bool = True,
36        value_change_call: Callable[[Any], Any] | None = None,
37    ):
38        if displayname is None:
39            displayname = configkey
40        self._value_change_call = value_change_call
41        self._configkey = configkey
42        self.widget = bui.checkboxwidget(
43            parent=parent,
44            autoselect=autoselect,
45            position=position,
46            size=size,
47            text=displayname,
48            textcolor=(0.8, 0.8, 0.8),
49            value=bui.app.config.resolve(configkey),
50            on_value_change_call=self._value_changed,
51            scale=scale,
52            maxwidth=maxwidth,
53        )
54        # complain if we outlive our checkbox
55        bui.uicleanupcheck(self, self.widget)
widget: _bauiv1.Widget

The underlying bui.Widget instance.

class ConfigNumberEdit:
 65class ConfigNumberEdit:
 66    """A set of controls for editing a numeric config value.
 67
 68    It will automatically save and apply the config when its
 69    value changes.
 70    """
 71
 72    nametext: bui.Widget
 73    """The text widget displaying the name."""
 74
 75    valuetext: bui.Widget
 76    """The text widget displaying the current value."""
 77
 78    minusbutton: bui.Widget
 79    """The button widget used to reduce the value."""
 80
 81    plusbutton: bui.Widget
 82    """The button widget used to increase the value."""
 83
 84    def __init__(
 85        self,
 86        parent: bui.Widget,
 87        configkey: str,
 88        position: tuple[float, float],
 89        minval: float = 0.0,
 90        maxval: float = 100.0,
 91        increment: float = 1.0,
 92        callback: Callable[[float], Any] | None = None,
 93        xoffset: float = 0.0,
 94        displayname: str | bui.Lstr | None = None,
 95        changesound: bool = True,
 96        textscale: float = 1.0,
 97        as_percent: bool = False,
 98        fallback_value: float = 0.0,
 99        f: int = 1,
100    ):
101        if displayname is None:
102            displayname = configkey
103
104        self._configkey = configkey
105        self._minval = minval
106        self._maxval = maxval
107        self._increment = increment
108        self._callback = callback
109        try:
110            self._value = bui.app.config.resolve(configkey)
111        except ValueError:
112            self._value = bui.app.config.get(configkey, fallback_value)
113        self._as_percent = as_percent
114        self._f = f
115
116        self.nametext = bui.textwidget(
117            parent=parent,
118            position=position,
119            size=(100, 30),
120            text=displayname,
121            maxwidth=160 + xoffset,
122            color=(0.8, 0.8, 0.8, 1.0),
123            h_align='left',
124            v_align='center',
125            scale=textscale,
126        )
127        self.valuetext = bui.textwidget(
128            parent=parent,
129            position=(246 + xoffset, position[1]),
130            size=(60, 28),
131            editable=False,
132            color=(0.3, 1.0, 0.3, 1.0),
133            h_align='right',
134            v_align='center',
135            text=str(self._value),
136            padding=2,
137        )
138        self.minusbutton = bui.buttonwidget(
139            parent=parent,
140            position=(330 + xoffset, position[1]),
141            size=(28, 28),
142            label='-',
143            autoselect=True,
144            on_activate_call=bui.Call(self._down),
145            repeat=True,
146            enable_sound=changesound,
147        )
148        self.plusbutton = bui.buttonwidget(
149            parent=parent,
150            position=(380 + xoffset, position[1]),
151            size=(28, 28),
152            label='+',
153            autoselect=True,
154            on_activate_call=bui.Call(self._up),
155            repeat=True,
156            enable_sound=changesound,
157        )
158        # Complain if we outlive our widgets.
159        bui.uicleanupcheck(self, self.nametext)
160        self._update_display()
161
162    def _up(self) -> None:
163        self._value = min(self._maxval, self._value + self._increment)
164        self._changed()
165
166    def _down(self) -> None:
167        self._value = max(self._minval, self._value - self._increment)
168        self._changed()
169
170    def _changed(self) -> None:
171        self._update_display()
172        if self._callback:
173            self._callback(self._value)
174        bui.app.config[self._configkey] = self._value
175        bui.app.config.apply_and_commit()
176
177    def _update_display(self) -> None:
178        if self._as_percent:
179            val = f'{round(self._value*100.0)}%'
180        else:
181            val = f'{self._value:.{self._f}f}'
182        bui.textwidget(edit=self.valuetext, text=val)

A set of controls for editing a numeric config value.

It will automatically save and apply the config when its value changes.

ConfigNumberEdit( parent: _bauiv1.Widget, configkey: str, position: tuple[float, float], minval: float = 0.0, maxval: float = 100.0, increment: float = 1.0, callback: Optional[Callable[[float], Any]] = None, xoffset: float = 0.0, displayname: str | babase._language.Lstr | None = None, changesound: bool = True, textscale: float = 1.0, as_percent: bool = False, fallback_value: float = 0.0, f: int = 1)
 84    def __init__(
 85        self,
 86        parent: bui.Widget,
 87        configkey: str,
 88        position: tuple[float, float],
 89        minval: float = 0.0,
 90        maxval: float = 100.0,
 91        increment: float = 1.0,
 92        callback: Callable[[float], Any] | None = None,
 93        xoffset: float = 0.0,
 94        displayname: str | bui.Lstr | None = None,
 95        changesound: bool = True,
 96        textscale: float = 1.0,
 97        as_percent: bool = False,
 98        fallback_value: float = 0.0,
 99        f: int = 1,
100    ):
101        if displayname is None:
102            displayname = configkey
103
104        self._configkey = configkey
105        self._minval = minval
106        self._maxval = maxval
107        self._increment = increment
108        self._callback = callback
109        try:
110            self._value = bui.app.config.resolve(configkey)
111        except ValueError:
112            self._value = bui.app.config.get(configkey, fallback_value)
113        self._as_percent = as_percent
114        self._f = f
115
116        self.nametext = bui.textwidget(
117            parent=parent,
118            position=position,
119            size=(100, 30),
120            text=displayname,
121            maxwidth=160 + xoffset,
122            color=(0.8, 0.8, 0.8, 1.0),
123            h_align='left',
124            v_align='center',
125            scale=textscale,
126        )
127        self.valuetext = bui.textwidget(
128            parent=parent,
129            position=(246 + xoffset, position[1]),
130            size=(60, 28),
131            editable=False,
132            color=(0.3, 1.0, 0.3, 1.0),
133            h_align='right',
134            v_align='center',
135            text=str(self._value),
136            padding=2,
137        )
138        self.minusbutton = bui.buttonwidget(
139            parent=parent,
140            position=(330 + xoffset, position[1]),
141            size=(28, 28),
142            label='-',
143            autoselect=True,
144            on_activate_call=bui.Call(self._down),
145            repeat=True,
146            enable_sound=changesound,
147        )
148        self.plusbutton = bui.buttonwidget(
149            parent=parent,
150            position=(380 + xoffset, position[1]),
151            size=(28, 28),
152            label='+',
153            autoselect=True,
154            on_activate_call=bui.Call(self._up),
155            repeat=True,
156            enable_sound=changesound,
157        )
158        # Complain if we outlive our widgets.
159        bui.uicleanupcheck(self, self.nametext)
160        self._update_display()
nametext: _bauiv1.Widget

The text widget displaying the name.

valuetext: _bauiv1.Widget

The text widget displaying the current value.

minusbutton: _bauiv1.Widget

The button widget used to reduce the value.

plusbutton: _bauiv1.Widget

The button widget used to increase the value.