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

169 lines
5.2 KiB
Python
Raw Permalink Normal View History

2021-03-29 03:24:13 +05:30
# Released under the MIT License. See LICENSE for details.
#
"""Dialog window controlled by the master server."""
from __future__ import annotations
2022-10-01 14:51:35 +05:30
import logging
from dataclasses import dataclass, field
2023-08-13 17:21:49 +05:30
from typing import Annotated
2022-10-01 14:51:35 +05:30
from efro.dataclassio import ioprepped, IOAttrs
2021-03-29 03:24:13 +05:30
2023-08-13 17:21:49 +05:30
import bauiv1 as bui
2022-10-01 14:51:35 +05:30
@ioprepped
@dataclass
class ServerDialogData:
"""Data for ServerDialog."""
2022-10-01 14:51:35 +05:30
dialog_id: Annotated[str, IOAttrs('dialogID')]
text: Annotated[str, IOAttrs('text')]
subs: Annotated[list[tuple[str, str]], IOAttrs('subs')] = field(
default_factory=list
)
2022-10-01 14:51:35 +05:30
show_cancel: Annotated[bool, IOAttrs('showCancel')] = True
copy_text: Annotated[str | None, IOAttrs('copyText')] = None
2021-03-29 03:24:13 +05:30
2023-08-13 17:21:49 +05:30
class ServerDialogWindow(bui.Window):
2021-03-29 03:24:13 +05:30
"""A dialog window driven by the master-server."""
2022-10-01 14:51:35 +05:30
def __init__(self, data: ServerDialogData):
self._data = data
2023-08-13 17:21:49 +05:30
txt = bui.Lstr(
translate=('serverResponses', data.text), subs=data.subs
).evaluate()
2021-03-29 03:24:13 +05:30
txt = txt.strip()
txt_scale = 1.5
2022-10-01 14:51:35 +05:30
txt_height = (
2023-08-13 17:21:49 +05:30
bui.get_string_height(txt, suppress_warning=True) * txt_scale
)
2021-03-29 03:24:13 +05:30
self._width = 500
2022-10-01 14:51:35 +05:30
self._height = 160 + min(200, txt_height)
2023-08-13 17:21:49 +05:30
assert bui.app.classic is not None
uiscale = bui.app.ui_v1.uiscale
super().__init__(
2023-08-13 17:21:49 +05:30
root_widget=bui.containerwidget(
size=(self._width, self._height),
transition='in_scale',
scale=(
1.8
2023-08-13 17:21:49 +05:30
if uiscale is bui.UIScale.SMALL
2024-05-19 18:25:43 +05:30
else 1.35 if uiscale is bui.UIScale.MEDIUM else 1.0
),
)
)
2023-08-13 17:21:49 +05:30
self._starttime = bui.apptime()
2021-03-29 03:24:13 +05:30
2023-08-13 17:21:49 +05:30
bui.getsound('swish').play()
bui.textwidget(
parent=self._root_widget,
position=(self._width * 0.5, 70 + (self._height - 70) * 0.5),
size=(0, 0),
color=(1.0, 3.0, 1.0),
scale=txt_scale,
h_align='center',
v_align='center',
text=txt,
maxwidth=self._width * 0.85,
max_height=(self._height - 110),
)
2022-10-01 14:51:35 +05:30
2023-08-13 17:21:49 +05:30
show_copy = data.copy_text is not None and bui.clipboard_is_supported()
2022-10-01 14:51:35 +05:30
# Currently can't do both copy and cancel since they go in the same
# spot. Cancel wins in this case since it is important functionality
# and copy is just for convenience (and not even always available).
if show_copy and data.show_cancel:
logging.warning(
'serverdialog requesting both copy and cancel;'
' copy will not be shown.'
)
2022-10-01 14:51:35 +05:30
show_copy = False
self._cancel_button = (
None
if not data.show_cancel
2023-08-13 17:21:49 +05:30
else bui.buttonwidget(
parent=self._root_widget,
position=(30, 30),
size=(160, 60),
autoselect=True,
2023-08-13 17:21:49 +05:30
label=bui.Lstr(resource='cancelText'),
on_activate_call=self._cancel_press,
)
)
self._copy_button = (
None
if not show_copy
2023-08-13 17:21:49 +05:30
else bui.buttonwidget(
parent=self._root_widget,
position=(30, 30),
size=(160, 60),
autoselect=True,
2023-08-13 17:21:49 +05:30
label=bui.Lstr(resource='copyText'),
on_activate_call=self._copy_press,
)
)
2022-10-01 14:51:35 +05:30
2023-08-13 17:21:49 +05:30
self._ok_button = bui.buttonwidget(
2021-03-29 03:24:13 +05:30
parent=self._root_widget,
position=(
2024-05-19 18:25:43 +05:30
(
(self._width - 182)
if (data.show_cancel or show_copy)
else (self._width * 0.5 - 80)
),
30,
),
2021-03-29 03:24:13 +05:30
size=(160, 60),
autoselect=True,
2023-08-13 17:21:49 +05:30
label=bui.Lstr(resource='okText'),
on_activate_call=self._ok_press,
)
2022-10-01 14:51:35 +05:30
2023-08-13 17:21:49 +05:30
bui.containerwidget(
edit=self._root_widget,
cancel_button=self._cancel_button,
start_button=self._ok_button,
selected_child=self._ok_button,
)
2021-03-29 03:24:13 +05:30
2022-10-01 14:51:35 +05:30
def _copy_press(self) -> None:
assert self._data.copy_text is not None
2023-08-13 17:21:49 +05:30
bui.clipboard_set_text(self._data.copy_text)
bui.screenmessage(bui.Lstr(resource='copyConfirmText'), color=(0, 1, 0))
2022-10-01 14:51:35 +05:30
2021-03-29 03:24:13 +05:30
def _ok_press(self) -> None:
2023-08-13 17:21:49 +05:30
plus = bui.app.plus
assert plus is not None
if bui.apptime() - self._starttime < 1.0:
bui.getsound('error').play()
2021-03-29 03:24:13 +05:30
return
2023-08-13 17:21:49 +05:30
plus.add_v1_account_transaction(
{
'type': 'DIALOG_RESPONSE',
'dialogID': self._data.dialog_id,
'response': 1,
}
)
2023-08-13 17:21:49 +05:30
bui.containerwidget(edit=self._root_widget, transition='out_scale')
2021-03-29 03:24:13 +05:30
def _cancel_press(self) -> None:
2023-08-13 17:21:49 +05:30
plus = bui.app.plus
assert plus is not None
if bui.apptime() - self._starttime < 1.0:
bui.getsound('error').play()
2021-03-29 03:24:13 +05:30
return
2023-08-13 17:21:49 +05:30
plus.add_v1_account_transaction(
{
'type': 'DIALOG_RESPONSE',
'dialogID': self._data.dialog_id,
'response': 0,
}
)
2023-08-13 17:21:49 +05:30
bui.containerwidget(edit=self._root_widget, transition='out_scale')