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._value = (
113            self._minval
114            if self._minval > self._value
115            else self._maxval if self._maxval < self._value else self._value
116        )
117        self._as_percent = as_percent
118        self._f = f
119
120        self.nametext = bui.textwidget(
121            parent=parent,
122            position=(position[0], position[1] + 12.0),
123            size=(0, 0),
124            text=displayname,
125            maxwidth=150 + xoffset,
126            color=(0.8, 0.8, 0.8, 1.0),
127            h_align='left',
128            v_align='center',
129            scale=textscale,
130        )
131        self.valuetext = bui.textwidget(
132            parent=parent,
133            position=(position[0] + 216 + xoffset, position[1] + 12.0),
134            size=(0, 0),
135            editable=False,
136            color=(0.3, 1.0, 0.3, 1.0),
137            h_align='right',
138            v_align='center',
139            text=str(self._value),
140            padding=2,
141        )
142        self.minusbutton = bui.buttonwidget(
143            parent=parent,
144            position=(position[0] + 230 + xoffset, position[1]),
145            size=(28, 28),
146            label='-',
147            autoselect=True,
148            on_activate_call=bui.Call(self._down),
149            repeat=True,
150            enable_sound=changesound,
151        )
152        self.plusbutton = bui.buttonwidget(
153            parent=parent,
154            position=(position[0] + 280 + xoffset, position[1]),
155            size=(28, 28),
156            label='+',
157            autoselect=True,
158            on_activate_call=bui.Call(self._up),
159            repeat=True,
160            enable_sound=changesound,
161        )
162        # Complain if we outlive our widgets.
163        bui.uicleanupcheck(self, self.nametext)
164        self._update_display()
165
166    def _up(self) -> None:
167        self._value = min(self._maxval, self._value + self._increment)
168        self._changed()
169
170    def _down(self) -> None:
171        self._value = max(self._minval, self._value - self._increment)
172        self._changed()
173
174    def _changed(self) -> None:
175        self._update_display()
176        if self._callback:
177            self._callback(self._value)
178        bui.app.config[self._configkey] = self._value
179        bui.app.config.apply_and_commit()
180
181    def _update_display(self) -> None:
182        if self._as_percent:
183            val = f'{round(self._value*100.0)}%'
184        else:
185            val = f'{self._value:.{self._f}f}'
186        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.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._value = (
114            self._minval
115            if self._minval > self._value
116            else self._maxval if self._maxval < self._value else self._value
117        )
118        self._as_percent = as_percent
119        self._f = f
120
121        self.nametext = bui.textwidget(
122            parent=parent,
123            position=(position[0], position[1] + 12.0),
124            size=(0, 0),
125            text=displayname,
126            maxwidth=150 + xoffset,
127            color=(0.8, 0.8, 0.8, 1.0),
128            h_align='left',
129            v_align='center',
130            scale=textscale,
131        )
132        self.valuetext = bui.textwidget(
133            parent=parent,
134            position=(position[0] + 216 + xoffset, position[1] + 12.0),
135            size=(0, 0),
136            editable=False,
137            color=(0.3, 1.0, 0.3, 1.0),
138            h_align='right',
139            v_align='center',
140            text=str(self._value),
141            padding=2,
142        )
143        self.minusbutton = bui.buttonwidget(
144            parent=parent,
145            position=(position[0] + 230 + xoffset, position[1]),
146            size=(28, 28),
147            label='-',
148            autoselect=True,
149            on_activate_call=bui.Call(self._down),
150            repeat=True,
151            enable_sound=changesound,
152        )
153        self.plusbutton = bui.buttonwidget(
154            parent=parent,
155            position=(position[0] + 280 + xoffset, position[1]),
156            size=(28, 28),
157            label='+',
158            autoselect=True,
159            on_activate_call=bui.Call(self._up),
160            repeat=True,
161            enable_sound=changesound,
162        )
163        # Complain if we outlive our widgets.
164        bui.uicleanupcheck(self, self.nametext)
165        self._update_display()
166
167    def _up(self) -> None:
168        self._value = min(self._maxval, self._value + self._increment)
169        self._changed()
170
171    def _down(self) -> None:
172        self._value = max(self._minval, self._value - self._increment)
173        self._changed()
174
175    def _changed(self) -> None:
176        self._update_display()
177        if self._callback:
178            self._callback(self._value)
179        bui.app.config[self._configkey] = self._value
180        bui.app.config.apply_and_commit()
181
182    def _update_display(self) -> None:
183        if self._as_percent:
184            val = f'{round(self._value*100.0)}%'
185        else:
186            val = f'{self._value:.{self._f}f}'
187        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.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._value = (
114            self._minval
115            if self._minval > self._value
116            else self._maxval if self._maxval < self._value else self._value
117        )
118        self._as_percent = as_percent
119        self._f = f
120
121        self.nametext = bui.textwidget(
122            parent=parent,
123            position=(position[0], position[1] + 12.0),
124            size=(0, 0),
125            text=displayname,
126            maxwidth=150 + xoffset,
127            color=(0.8, 0.8, 0.8, 1.0),
128            h_align='left',
129            v_align='center',
130            scale=textscale,
131        )
132        self.valuetext = bui.textwidget(
133            parent=parent,
134            position=(position[0] + 216 + xoffset, position[1] + 12.0),
135            size=(0, 0),
136            editable=False,
137            color=(0.3, 1.0, 0.3, 1.0),
138            h_align='right',
139            v_align='center',
140            text=str(self._value),
141            padding=2,
142        )
143        self.minusbutton = bui.buttonwidget(
144            parent=parent,
145            position=(position[0] + 230 + xoffset, position[1]),
146            size=(28, 28),
147            label='-',
148            autoselect=True,
149            on_activate_call=bui.Call(self._down),
150            repeat=True,
151            enable_sound=changesound,
152        )
153        self.plusbutton = bui.buttonwidget(
154            parent=parent,
155            position=(position[0] + 280 + xoffset, position[1]),
156            size=(28, 28),
157            label='+',
158            autoselect=True,
159            on_activate_call=bui.Call(self._up),
160            repeat=True,
161            enable_sound=changesound,
162        )
163        # Complain if we outlive our widgets.
164        bui.uicleanupcheck(self, self.nametext)
165        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.