Bombsquad-Ballistica-Modded.../dist/ba_data/python/bauiv1lib/config.py

187 lines
5.5 KiB
Python
Raw Normal View History

2021-03-29 03:24:13 +05:30
# Released under the MIT License. See LICENSE for details.
#
"""Functionality for editing config values and applying them to the game."""
from __future__ import annotations
from typing import TYPE_CHECKING
2023-08-13 17:21:49 +05:30
import bauiv1 as bui
2021-03-29 03:24:13 +05:30
if TYPE_CHECKING:
2022-06-30 00:31:52 +05:30
from typing import Any, Callable
2021-03-29 03:24:13 +05:30
class ConfigCheckBox:
"""A checkbox wired up to control a config value.
It will automatically save and apply the config when its
value changes.
"""
2023-08-13 17:21:49 +05:30
widget: bui.Widget
"""The underlying bui.Widget instance."""
2022-06-09 01:26:46 +05:30
def __init__(
self,
2023-08-13 17:21:49 +05:30
parent: bui.Widget,
configkey: str,
position: tuple[float, float],
size: tuple[float, float],
2023-08-13 17:21:49 +05:30
displayname: str | bui.Lstr | None = None,
scale: float | None = None,
maxwidth: float | None = None,
autoselect: bool = True,
value_change_call: Callable[[Any], Any] | None = None,
):
2021-03-29 03:24:13 +05:30
if displayname is None:
displayname = configkey
self._value_change_call = value_change_call
self._configkey = configkey
2023-08-13 17:21:49 +05:30
self.widget = bui.checkboxwidget(
2021-03-29 03:24:13 +05:30
parent=parent,
autoselect=autoselect,
position=position,
size=size,
text=displayname,
textcolor=(0.8, 0.8, 0.8),
2023-08-13 17:21:49 +05:30
value=bui.app.config.resolve(configkey),
2021-03-29 03:24:13 +05:30
on_value_change_call=self._value_changed,
scale=scale,
maxwidth=maxwidth,
)
2021-03-29 03:24:13 +05:30
# complain if we outlive our checkbox
2023-08-13 17:21:49 +05:30
bui.uicleanupcheck(self, self.widget)
2021-03-29 03:24:13 +05:30
def _value_changed(self, val: bool) -> None:
2023-08-13 17:21:49 +05:30
cfg = bui.app.config
2021-03-29 03:24:13 +05:30
cfg[self._configkey] = val
if self._value_change_call is not None:
self._value_change_call(val)
cfg.apply_and_commit()
class ConfigNumberEdit:
"""A set of controls for editing a numeric config value.
It will automatically save and apply the config when its
value changes.
2022-06-09 01:26:46 +05:30
"""
2021-03-29 03:24:13 +05:30
2023-08-13 17:21:49 +05:30
nametext: bui.Widget
2022-06-09 01:26:46 +05:30
"""The text widget displaying the name."""
2021-03-29 03:24:13 +05:30
2023-08-13 17:21:49 +05:30
valuetext: bui.Widget
2022-06-09 01:26:46 +05:30
"""The text widget displaying the current value."""
2021-03-29 03:24:13 +05:30
2023-08-13 17:21:49 +05:30
minusbutton: bui.Widget
2022-06-09 01:26:46 +05:30
"""The button widget used to reduce the value."""
2021-03-29 03:24:13 +05:30
2023-08-13 17:21:49 +05:30
plusbutton: bui.Widget
2022-06-09 01:26:46 +05:30
"""The button widget used to increase the value."""
2021-03-29 03:24:13 +05:30
def __init__(
self,
2023-08-13 17:21:49 +05:30
parent: bui.Widget,
configkey: str,
position: tuple[float, float],
minval: float = 0.0,
maxval: float = 100.0,
increment: float = 1.0,
callback: Callable[[float], Any] | None = None,
xoffset: float = 0.0,
2023-08-13 17:21:49 +05:30
displayname: str | bui.Lstr | None = None,
changesound: bool = True,
textscale: float = 1.0,
2023-12-21 15:55:50 +05:30
as_percent: bool = False,
2024-05-19 18:25:43 +05:30
fallback_value: float = 0.0,
f: int = 1,
):
2021-03-29 03:24:13 +05:30
if displayname is None:
displayname = configkey
self._configkey = configkey
self._minval = minval
self._maxval = maxval
self._increment = increment
self._callback = callback
2024-05-19 18:25:43 +05:30
try:
self._value = bui.app.config.resolve(configkey)
except ValueError:
self._value = bui.app.config.get(configkey, fallback_value)
2024-11-28 00:23:35 +05:30
self._value = (
self._minval
if self._minval > self._value
else self._maxval if self._maxval < self._value else self._value
)
2023-12-21 15:55:50 +05:30
self._as_percent = as_percent
2024-05-19 18:25:43 +05:30
self._f = f
2021-03-29 03:24:13 +05:30
2023-08-13 17:21:49 +05:30
self.nametext = bui.textwidget(
parent=parent,
2024-11-28 00:23:35 +05:30
position=(position[0], position[1] + 12.0),
size=(0, 0),
text=displayname,
2024-11-28 00:23:35 +05:30
maxwidth=150 + xoffset,
color=(0.8, 0.8, 0.8, 1.0),
h_align='left',
v_align='center',
scale=textscale,
)
2023-08-13 17:21:49 +05:30
self.valuetext = bui.textwidget(
parent=parent,
2024-11-28 00:23:35 +05:30
position=(position[0] + 216 + xoffset, position[1] + 12.0),
size=(0, 0),
editable=False,
color=(0.3, 1.0, 0.3, 1.0),
h_align='right',
v_align='center',
text=str(self._value),
padding=2,
)
2023-08-13 17:21:49 +05:30
self.minusbutton = bui.buttonwidget(
2021-03-29 03:24:13 +05:30
parent=parent,
2024-11-28 00:23:35 +05:30
position=(position[0] + 230 + xoffset, position[1]),
2021-03-29 03:24:13 +05:30
size=(28, 28),
label='-',
autoselect=True,
2023-08-13 17:21:49 +05:30
on_activate_call=bui.Call(self._down),
2021-03-29 03:24:13 +05:30
repeat=True,
enable_sound=changesound,
)
2023-08-13 17:21:49 +05:30
self.plusbutton = bui.buttonwidget(
parent=parent,
2024-11-28 00:23:35 +05:30
position=(position[0] + 280 + xoffset, position[1]),
size=(28, 28),
label='+',
autoselect=True,
2023-08-13 17:21:49 +05:30
on_activate_call=bui.Call(self._up),
repeat=True,
enable_sound=changesound,
)
2021-03-29 03:24:13 +05:30
# Complain if we outlive our widgets.
2023-08-13 17:21:49 +05:30
bui.uicleanupcheck(self, self.nametext)
2021-03-29 03:24:13 +05:30
self._update_display()
def _up(self) -> None:
self._value = min(self._maxval, self._value + self._increment)
self._changed()
def _down(self) -> None:
self._value = max(self._minval, self._value - self._increment)
self._changed()
def _changed(self) -> None:
self._update_display()
if self._callback:
self._callback(self._value)
2023-08-13 17:21:49 +05:30
bui.app.config[self._configkey] = self._value
bui.app.config.apply_and_commit()
2021-03-29 03:24:13 +05:30
def _update_display(self) -> None:
2023-12-21 15:55:50 +05:30
if self._as_percent:
val = f'{round(self._value*100.0)}%'
else:
2024-05-19 18:25:43 +05:30
val = f'{self._value:.{self._f}f}'
2023-12-21 15:55:50 +05:30
bui.textwidget(edit=self.valuetext, text=val)