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

190 lines
5.7 KiB
Python
Raw Normal View History

2021-03-29 03:24:13 +05:30
# Released under the MIT License. See LICENSE for details.
#
"""UI functionality for entering promo codes."""
from __future__ import annotations
import time
2023-08-13 17:21:49 +05:30
import bauiv1 as bui
2021-03-29 03:24:13 +05:30
2023-08-13 17:21:49 +05:30
class PromoCodeWindow(bui.Window):
2021-03-29 03:24:13 +05:30
"""Window for entering promo codes."""
def __init__(
2023-08-13 17:21:49 +05:30
self, modal: bool = False, origin_widget: bui.Widget | None = None
):
2022-06-30 00:31:52 +05:30
scale_origin: tuple[float, float] | None
2021-03-29 03:24:13 +05:30
if origin_widget is not None:
self._transition_out = 'out_scale'
scale_origin = origin_widget.get_screen_space_center()
transition = 'in_scale'
else:
self._transition_out = 'out_right'
scale_origin = None
transition = 'in_right'
width = 450
2023-12-21 15:55:50 +05:30
height = 330
2021-03-29 03:24:13 +05:30
self._modal = modal
self._r = 'promoCodeWindow'
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=(width, height),
transition=transition,
toolbar_visibility='menu_minimal_no_back',
scale_origin_stack_offset=scale_origin,
scale=(
2.0
2023-08-13 17:21:49 +05:30
if uiscale is bui.UIScale.SMALL
else 1.5
2023-08-13 17:21:49 +05:30
if uiscale is bui.UIScale.MEDIUM
else 1.0
),
)
)
2023-08-13 17:21:49 +05:30
btn = bui.buttonwidget(
parent=self._root_widget,
scale=0.5,
position=(40, height - 40),
size=(60, 60),
label='',
on_activate_call=self._do_back,
autoselect=True,
color=(0.55, 0.5, 0.6),
2023-08-13 17:21:49 +05:30
icon=bui.gettexture('crossOut'),
iconscale=1.2,
)
2023-12-21 15:55:50 +05:30
v = height - 74
bui.textwidget(
parent=self._root_widget,
text=bui.Lstr(resource='codesExplainText'),
maxwidth=width * 0.9,
position=(width * 0.5, v),
color=(0.7, 0.7, 0.7, 1.0),
size=(0, 0),
scale=0.8,
h_align='center',
v_align='center',
)
v -= 60
bui.textwidget(
parent=self._root_widget,
text=bui.Lstr(
resource='supportEmailText',
subs=[('${EMAIL}', 'support@froemling.net')],
),
maxwidth=width * 0.9,
position=(width * 0.5, v),
color=(0.7, 0.7, 0.7, 1.0),
size=(0, 0),
scale=0.65,
h_align='center',
v_align='center',
)
v -= 80
2023-08-13 17:21:49 +05:30
bui.textwidget(
parent=self._root_widget,
2023-08-13 17:21:49 +05:30
text=bui.Lstr(resource=self._r + '.codeText'),
2023-12-21 15:55:50 +05:30
position=(22, v),
color=(0.8, 0.8, 0.8, 1.0),
size=(90, 30),
h_align='right',
)
2023-12-21 15:55:50 +05:30
v -= 8
2023-08-13 17:21:49 +05:30
self._text_field = bui.textwidget(
2021-03-29 03:24:13 +05:30
parent=self._root_widget,
2023-12-21 15:55:50 +05:30
position=(125, v),
2021-03-29 03:24:13 +05:30
size=(280, 46),
text='',
h_align='left',
v_align='center',
max_chars=64,
color=(0.9, 0.9, 0.9, 1.0),
2023-08-13 17:21:49 +05:30
description=bui.Lstr(resource=self._r + '.codeText'),
2021-03-29 03:24:13 +05:30
editable=True,
padding=4,
on_return_press_call=self._activate_enter_button,
)
2023-08-13 17:21:49 +05:30
bui.widget(edit=btn, down_widget=self._text_field)
2021-03-29 03:24:13 +05:30
2023-12-21 15:55:50 +05:30
v -= 79
2021-03-29 03:24:13 +05:30
b_width = 200
2023-08-13 17:21:49 +05:30
self._enter_button = btn2 = bui.buttonwidget(
2021-03-29 03:24:13 +05:30
parent=self._root_widget,
2023-12-21 15:55:50 +05:30
position=(width * 0.5 - b_width * 0.5, v),
2021-03-29 03:24:13 +05:30
size=(b_width, 60),
scale=1.0,
2023-08-13 17:21:49 +05:30
label=bui.Lstr(
resource='submitText', fallback_resource=self._r + '.enterText'
),
on_activate_call=self._do_enter,
)
2023-08-13 17:21:49 +05:30
bui.containerwidget(
edit=self._root_widget,
cancel_button=btn,
start_button=btn2,
selected_child=self._text_field,
)
2021-03-29 03:24:13 +05:30
def _do_back(self) -> None:
# pylint: disable=cyclic-import
2023-08-13 17:21:49 +05:30
from bauiv1lib.settings.advanced import AdvancedSettingsWindow
2023-12-21 15:55:50 +05:30
# no-op if our underlying widget is dead or on its way out.
if not self._root_widget or self._root_widget.transitioning_out:
return
2023-08-13 17:21:49 +05:30
bui.containerwidget(
edit=self._root_widget, transition=self._transition_out
)
2021-03-29 03:24:13 +05:30
if not self._modal:
2023-08-13 17:21:49 +05:30
assert bui.app.classic is not None
bui.app.ui_v1.set_main_menu_window(
2023-12-21 15:55:50 +05:30
AdvancedSettingsWindow(transition='in_left').get_root_widget(),
from_window=self._root_widget,
)
2021-03-29 03:24:13 +05:30
def _activate_enter_button(self) -> None:
self._enter_button.activate()
def _do_enter(self) -> None:
# pylint: disable=cyclic-import
2023-08-13 17:21:49 +05:30
from bauiv1lib.settings.advanced import AdvancedSettingsWindow
2023-12-21 15:55:50 +05:30
# no-op if our underlying widget is dead or on its way out.
if not self._root_widget or self._root_widget.transitioning_out:
return
2023-08-13 17:21:49 +05:30
plus = bui.app.plus
assert plus is not None
2023-08-13 17:21:49 +05:30
bui.containerwidget(
edit=self._root_widget, transition=self._transition_out
)
2021-03-29 03:24:13 +05:30
if not self._modal:
2023-08-13 17:21:49 +05:30
assert bui.app.classic is not None
bui.app.ui_v1.set_main_menu_window(
2023-12-21 15:55:50 +05:30
AdvancedSettingsWindow(transition='in_left').get_root_widget(),
from_window=self._root_widget,
)
2023-08-13 17:21:49 +05:30
plus.add_v1_account_transaction(
{
'type': 'PROMO_CODE',
'expire_time': time.time() + 5,
2023-08-13 17:21:49 +05:30
'code': bui.textwidget(query=self._text_field),
}
)
2023-08-13 17:21:49 +05:30
plus.run_v1_account_transactions()