bastd.ui.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 ba 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: ba.Widget 23 """The underlying ba.Widget instance.""" 24 25 def __init__(self, 26 parent: ba.Widget, 27 configkey: str, 28 position: tuple[float, float], 29 size: tuple[float, float], 30 displayname: str | ba.Lstr | None = None, 31 scale: float | None = None, 32 maxwidth: float | None = None, 33 autoselect: bool = True, 34 value_change_call: Callable[[Any], Any] | None = None): 35 if displayname is None: 36 displayname = configkey 37 self._value_change_call = value_change_call 38 self._configkey = configkey 39 self.widget = ba.checkboxwidget( 40 parent=parent, 41 autoselect=autoselect, 42 position=position, 43 size=size, 44 text=displayname, 45 textcolor=(0.8, 0.8, 0.8), 46 value=ba.app.config.resolve(configkey), 47 on_value_change_call=self._value_changed, 48 scale=scale, 49 maxwidth=maxwidth) 50 # complain if we outlive our checkbox 51 ba.uicleanupcheck(self, self.widget) 52 53 def _value_changed(self, val: bool) -> None: 54 cfg = ba.app.config 55 cfg[self._configkey] = val 56 if self._value_change_call is not None: 57 self._value_change_call(val) 58 cfg.apply_and_commit() 59 60 61class ConfigNumberEdit: 62 """A set of controls for editing a numeric config value. 63 64 It will automatically save and apply the config when its 65 value changes. 66 """ 67 68 nametext: ba.Widget 69 """The text widget displaying the name.""" 70 71 valuetext: ba.Widget 72 """The text widget displaying the current value.""" 73 74 minusbutton: ba.Widget 75 """The button widget used to reduce the value.""" 76 77 plusbutton: ba.Widget 78 """The button widget used to increase the value.""" 79 80 def __init__(self, 81 parent: ba.Widget, 82 configkey: str, 83 position: tuple[float, float], 84 minval: float = 0.0, 85 maxval: float = 100.0, 86 increment: float = 1.0, 87 callback: Callable[[float], Any] | None = None, 88 xoffset: float = 0.0, 89 displayname: str | ba.Lstr | None = None, 90 changesound: bool = True, 91 textscale: float = 1.0): 92 if displayname is None: 93 displayname = configkey 94 95 self._configkey = configkey 96 self._minval = minval 97 self._maxval = maxval 98 self._increment = increment 99 self._callback = callback 100 self._value = ba.app.config.resolve(configkey) 101 102 self.nametext = ba.textwidget(parent=parent, 103 position=position, 104 size=(100, 30), 105 text=displayname, 106 maxwidth=160 + xoffset, 107 color=(0.8, 0.8, 0.8, 1.0), 108 h_align='left', 109 v_align='center', 110 scale=textscale) 111 self.valuetext = ba.textwidget(parent=parent, 112 position=(246 + xoffset, position[1]), 113 size=(60, 28), 114 editable=False, 115 color=(0.3, 1.0, 0.3, 1.0), 116 h_align='right', 117 v_align='center', 118 text=str(self._value), 119 padding=2) 120 self.minusbutton = ba.buttonwidget( 121 parent=parent, 122 position=(330 + xoffset, position[1]), 123 size=(28, 28), 124 label='-', 125 autoselect=True, 126 on_activate_call=ba.Call(self._down), 127 repeat=True, 128 enable_sound=changesound) 129 self.plusbutton = ba.buttonwidget(parent=parent, 130 position=(380 + xoffset, 131 position[1]), 132 size=(28, 28), 133 label='+', 134 autoselect=True, 135 on_activate_call=ba.Call(self._up), 136 repeat=True, 137 enable_sound=changesound) 138 # Complain if we outlive our widgets. 139 ba.uicleanupcheck(self, self.nametext) 140 self._update_display() 141 142 def _up(self) -> None: 143 self._value = min(self._maxval, self._value + self._increment) 144 self._changed() 145 146 def _down(self) -> None: 147 self._value = max(self._minval, self._value - self._increment) 148 self._changed() 149 150 def _changed(self) -> None: 151 self._update_display() 152 if self._callback: 153 self._callback(self._value) 154 ba.app.config[self._configkey] = self._value 155 ba.app.config.apply_and_commit() 156 157 def _update_display(self) -> None: 158 ba.textwidget(edit=self.valuetext, text=f'{self._value:.1f}')
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: ba.Widget 24 """The underlying ba.Widget instance.""" 25 26 def __init__(self, 27 parent: ba.Widget, 28 configkey: str, 29 position: tuple[float, float], 30 size: tuple[float, float], 31 displayname: str | ba.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 if displayname is None: 37 displayname = configkey 38 self._value_change_call = value_change_call 39 self._configkey = configkey 40 self.widget = ba.checkboxwidget( 41 parent=parent, 42 autoselect=autoselect, 43 position=position, 44 size=size, 45 text=displayname, 46 textcolor=(0.8, 0.8, 0.8), 47 value=ba.app.config.resolve(configkey), 48 on_value_change_call=self._value_changed, 49 scale=scale, 50 maxwidth=maxwidth) 51 # complain if we outlive our checkbox 52 ba.uicleanupcheck(self, self.widget) 53 54 def _value_changed(self, val: bool) -> None: 55 cfg = ba.app.config 56 cfg[self._configkey] = val 57 if self._value_change_call is not None: 58 self._value_change_call(val) 59 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: _ba.Widget, configkey: str, position: tuple[float, float], size: tuple[float, float], displayname: str | ba._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__(self, 27 parent: ba.Widget, 28 configkey: str, 29 position: tuple[float, float], 30 size: tuple[float, float], 31 displayname: str | ba.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 if displayname is None: 37 displayname = configkey 38 self._value_change_call = value_change_call 39 self._configkey = configkey 40 self.widget = ba.checkboxwidget( 41 parent=parent, 42 autoselect=autoselect, 43 position=position, 44 size=size, 45 text=displayname, 46 textcolor=(0.8, 0.8, 0.8), 47 value=ba.app.config.resolve(configkey), 48 on_value_change_call=self._value_changed, 49 scale=scale, 50 maxwidth=maxwidth) 51 # complain if we outlive our checkbox 52 ba.uicleanupcheck(self, self.widget)
class
ConfigNumberEdit:
62class ConfigNumberEdit: 63 """A set of controls for editing a numeric config value. 64 65 It will automatically save and apply the config when its 66 value changes. 67 """ 68 69 nametext: ba.Widget 70 """The text widget displaying the name.""" 71 72 valuetext: ba.Widget 73 """The text widget displaying the current value.""" 74 75 minusbutton: ba.Widget 76 """The button widget used to reduce the value.""" 77 78 plusbutton: ba.Widget 79 """The button widget used to increase the value.""" 80 81 def __init__(self, 82 parent: ba.Widget, 83 configkey: str, 84 position: tuple[float, float], 85 minval: float = 0.0, 86 maxval: float = 100.0, 87 increment: float = 1.0, 88 callback: Callable[[float], Any] | None = None, 89 xoffset: float = 0.0, 90 displayname: str | ba.Lstr | None = None, 91 changesound: bool = True, 92 textscale: float = 1.0): 93 if displayname is None: 94 displayname = configkey 95 96 self._configkey = configkey 97 self._minval = minval 98 self._maxval = maxval 99 self._increment = increment 100 self._callback = callback 101 self._value = ba.app.config.resolve(configkey) 102 103 self.nametext = ba.textwidget(parent=parent, 104 position=position, 105 size=(100, 30), 106 text=displayname, 107 maxwidth=160 + xoffset, 108 color=(0.8, 0.8, 0.8, 1.0), 109 h_align='left', 110 v_align='center', 111 scale=textscale) 112 self.valuetext = ba.textwidget(parent=parent, 113 position=(246 + xoffset, position[1]), 114 size=(60, 28), 115 editable=False, 116 color=(0.3, 1.0, 0.3, 1.0), 117 h_align='right', 118 v_align='center', 119 text=str(self._value), 120 padding=2) 121 self.minusbutton = ba.buttonwidget( 122 parent=parent, 123 position=(330 + xoffset, position[1]), 124 size=(28, 28), 125 label='-', 126 autoselect=True, 127 on_activate_call=ba.Call(self._down), 128 repeat=True, 129 enable_sound=changesound) 130 self.plusbutton = ba.buttonwidget(parent=parent, 131 position=(380 + xoffset, 132 position[1]), 133 size=(28, 28), 134 label='+', 135 autoselect=True, 136 on_activate_call=ba.Call(self._up), 137 repeat=True, 138 enable_sound=changesound) 139 # Complain if we outlive our widgets. 140 ba.uicleanupcheck(self, self.nametext) 141 self._update_display() 142 143 def _up(self) -> None: 144 self._value = min(self._maxval, self._value + self._increment) 145 self._changed() 146 147 def _down(self) -> None: 148 self._value = max(self._minval, self._value - self._increment) 149 self._changed() 150 151 def _changed(self) -> None: 152 self._update_display() 153 if self._callback: 154 self._callback(self._value) 155 ba.app.config[self._configkey] = self._value 156 ba.app.config.apply_and_commit() 157 158 def _update_display(self) -> None: 159 ba.textwidget(edit=self.valuetext, text=f'{self._value:.1f}')
A set of controls for editing a numeric config value.
It will automatically save and apply the config when its value changes.
ConfigNumberEdit( parent: _ba.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 | ba._language.Lstr | None = None, changesound: bool = True, textscale: float = 1.0)
81 def __init__(self, 82 parent: ba.Widget, 83 configkey: str, 84 position: tuple[float, float], 85 minval: float = 0.0, 86 maxval: float = 100.0, 87 increment: float = 1.0, 88 callback: Callable[[float], Any] | None = None, 89 xoffset: float = 0.0, 90 displayname: str | ba.Lstr | None = None, 91 changesound: bool = True, 92 textscale: float = 1.0): 93 if displayname is None: 94 displayname = configkey 95 96 self._configkey = configkey 97 self._minval = minval 98 self._maxval = maxval 99 self._increment = increment 100 self._callback = callback 101 self._value = ba.app.config.resolve(configkey) 102 103 self.nametext = ba.textwidget(parent=parent, 104 position=position, 105 size=(100, 30), 106 text=displayname, 107 maxwidth=160 + xoffset, 108 color=(0.8, 0.8, 0.8, 1.0), 109 h_align='left', 110 v_align='center', 111 scale=textscale) 112 self.valuetext = ba.textwidget(parent=parent, 113 position=(246 + xoffset, position[1]), 114 size=(60, 28), 115 editable=False, 116 color=(0.3, 1.0, 0.3, 1.0), 117 h_align='right', 118 v_align='center', 119 text=str(self._value), 120 padding=2) 121 self.minusbutton = ba.buttonwidget( 122 parent=parent, 123 position=(330 + xoffset, position[1]), 124 size=(28, 28), 125 label='-', 126 autoselect=True, 127 on_activate_call=ba.Call(self._down), 128 repeat=True, 129 enable_sound=changesound) 130 self.plusbutton = ba.buttonwidget(parent=parent, 131 position=(380 + xoffset, 132 position[1]), 133 size=(28, 28), 134 label='+', 135 autoselect=True, 136 on_activate_call=ba.Call(self._up), 137 repeat=True, 138 enable_sound=changesound) 139 # Complain if we outlive our widgets. 140 ba.uicleanupcheck(self, self.nametext) 141 self._update_display()