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

257 lines
8.2 KiB
Python
Raw Normal View History

2021-03-29 03:24:13 +05:30
# Released under the MIT License. See LICENSE for details.
#
"""Provides a popup window to continue a game."""
from __future__ import annotations
import weakref
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
2023-08-13 17:21:49 +05:30
import bascenev1 as bs
2021-03-29 03:24:13 +05:30
2023-08-13 17:21:49 +05:30
class ContinuesWindow(bui.Window):
2021-03-29 03:24:13 +05:30
"""A window to continue a game."""
def __init__(
self,
2023-08-13 17:21:49 +05:30
activity: bs.Activity,
cost: int,
continue_call: Callable[[], Any],
cancel_call: Callable[[], Any],
):
2023-08-13 17:21:49 +05:30
assert bui.app.classic is not None
2021-03-29 03:24:13 +05:30
self._activity = weakref.ref(activity)
self._cost = cost
self._continue_call = continue_call
self._cancel_call = cancel_call
self._start_count = self._count = 20
self._width = 300
self._height = 200
self._transitioning_out = False
super().__init__(
2023-08-13 17:21:49 +05:30
bui.containerwidget(
size=(self._width, self._height),
background=False,
toolbar_visibility='menu_currency',
transition='in_scale',
scale=1.5,
)
)
txt = (
2023-08-13 17:21:49 +05:30
bui.Lstr(resource='continuePurchaseText')
.evaluate()
.split('${PRICE}')
)
2021-03-29 03:24:13 +05:30
t_left = txt[0]
2023-08-13 17:21:49 +05:30
t_left_width = bui.get_string_width(t_left, suppress_warning=True)
t_price = bui.charstr(bui.SpecialChar.TICKET) + str(self._cost)
t_price_width = bui.get_string_width(t_price, suppress_warning=True)
2021-03-29 03:24:13 +05:30
t_right = txt[-1]
2023-08-13 17:21:49 +05:30
t_right_width = bui.get_string_width(t_right, suppress_warning=True)
2021-03-29 03:24:13 +05:30
width_total_half = (t_left_width + t_price_width + t_right_width) * 0.5
2023-08-13 17:21:49 +05:30
bui.textwidget(
parent=self._root_widget,
text=t_left,
flatness=1.0,
shadow=1.0,
size=(0, 0),
h_align='left',
v_align='center',
position=(self._width * 0.5 - width_total_half, self._height - 30),
)
2023-08-13 17:21:49 +05:30
bui.textwidget(
parent=self._root_widget,
text=t_price,
flatness=1.0,
shadow=1.0,
color=(0.2, 1.0, 0.2),
size=(0, 0),
position=(
self._width * 0.5 - width_total_half + t_left_width,
self._height - 30,
),
h_align='left',
v_align='center',
)
2023-08-13 17:21:49 +05:30
bui.textwidget(
parent=self._root_widget,
text=t_right,
flatness=1.0,
shadow=1.0,
size=(0, 0),
h_align='left',
v_align='center',
position=(
self._width * 0.5
- width_total_half
+ t_left_width
+ t_price_width
+ 5,
self._height - 30,
),
)
2021-03-29 03:24:13 +05:30
2022-06-30 00:31:52 +05:30
self._tickets_text_base: str | None
2023-08-13 17:21:49 +05:30
self._tickets_text: bui.Widget | None
if not bui.app.ui_v1.use_toolbars:
self._tickets_text_base = bui.Lstr(
2021-03-29 03:24:13 +05:30
resource='getTicketsWindow.youHaveShortText',
fallback_resource='getTicketsWindow.youHaveText',
).evaluate()
2023-08-13 17:21:49 +05:30
self._tickets_text = bui.textwidget(
2021-03-29 03:24:13 +05:30
parent=self._root_widget,
text='',
flatness=1.0,
color=(0.2, 1.0, 0.2),
shadow=1.0,
position=(
self._width * 0.5 + width_total_half,
self._height - 50,
),
2021-03-29 03:24:13 +05:30
size=(0, 0),
scale=0.35,
h_align='right',
v_align='center',
)
2021-03-29 03:24:13 +05:30
else:
self._tickets_text_base = None
self._tickets_text = None
2023-08-13 17:21:49 +05:30
self._counter_text = bui.textwidget(
parent=self._root_widget,
text=str(self._count),
color=(0.7, 0.7, 0.7),
scale=1.2,
size=(0, 0),
big=True,
position=(self._width * 0.5, self._height - 80),
flatness=1.0,
shadow=1.0,
h_align='center',
v_align='center',
)
2023-08-13 17:21:49 +05:30
self._cancel_button = bui.buttonwidget(
2021-03-29 03:24:13 +05:30
parent=self._root_widget,
position=(30, 30),
size=(120, 50),
2023-08-13 17:21:49 +05:30
label=bui.Lstr(resource='endText', fallback_resource='cancelText'),
2021-03-29 03:24:13 +05:30
autoselect=True,
enable_sound=False,
on_activate_call=self._on_cancel_press,
)
2023-08-13 17:21:49 +05:30
self._continue_button = bui.buttonwidget(
2021-03-29 03:24:13 +05:30
parent=self._root_widget,
2023-08-13 17:21:49 +05:30
label=bui.Lstr(resource='continueText'),
2021-03-29 03:24:13 +05:30
autoselect=True,
position=(self._width - 130, 30),
size=(120, 50),
on_activate_call=self._on_continue_press,
)
2023-08-13 17:21:49 +05:30
bui.containerwidget(
edit=self._root_widget,
cancel_button=self._cancel_button,
start_button=self._continue_button,
selected_child=self._cancel_button,
)
2021-03-29 03:24:13 +05:30
self._counting_down = True
2023-08-13 17:21:49 +05:30
self._countdown_timer = bui.AppTimer(
1.0, bui.WeakCall(self._tick), repeat=True
)
2022-10-01 14:51:35 +05:30
# If there is foreground activity, suspend it.
2023-08-13 17:21:49 +05:30
bui.app.classic.pause()
2021-03-29 03:24:13 +05:30
self._tick()
2022-10-01 14:51:35 +05:30
def __del__(self) -> None:
# If there is suspended foreground activity, resume it.
2023-08-13 17:21:49 +05:30
assert bui.app.classic is not None
bui.app.classic.resume()
2022-10-01 14:51:35 +05:30
2021-03-29 03:24:13 +05:30
def _tick(self) -> None:
2023-08-13 17:21:49 +05:30
plus = bui.app.plus
assert plus is not None
2021-03-29 03:24:13 +05:30
# if our target activity is gone or has ended, go away
activity = self._activity()
if activity is None or activity.has_ended():
self._on_cancel()
return
2023-08-13 17:21:49 +05:30
if plus.get_v1_account_state() == 'signed_in':
sval = bui.charstr(bui.SpecialChar.TICKET) + str(
plus.get_v1_account_ticket_count()
)
2021-03-29 03:24:13 +05:30
else:
sval = '?'
if self._tickets_text is not None:
assert self._tickets_text_base is not None
2023-08-13 17:21:49 +05:30
bui.textwidget(
edit=self._tickets_text,
text=self._tickets_text_base.replace('${COUNT}', sval),
)
2021-03-29 03:24:13 +05:30
if self._counting_down:
self._count -= 1
2023-08-13 17:21:49 +05:30
bui.getsound('tick').play()
2021-03-29 03:24:13 +05:30
if self._count <= 0:
self._on_cancel()
else:
2023-08-13 17:21:49 +05:30
bui.textwidget(edit=self._counter_text, text=str(self._count))
2021-03-29 03:24:13 +05:30
def _on_cancel_press(self) -> None:
# disallow for first second
if self._start_count - self._count < 2:
2023-08-13 17:21:49 +05:30
bui.getsound('error').play()
2021-03-29 03:24:13 +05:30
else:
self._on_cancel()
def _on_continue_press(self) -> None:
2023-08-13 17:21:49 +05:30
from bauiv1lib import getcurrency
plus = bui.app.plus
assert plus is not None
2021-03-29 03:24:13 +05:30
# Disallow for first second.
if self._start_count - self._count < 2:
2023-08-13 17:21:49 +05:30
bui.getsound('error').play()
2021-03-29 03:24:13 +05:30
else:
# If somehow we got signed out...
2023-08-13 17:21:49 +05:30
if plus.get_v1_account_state() != 'signed_in':
bui.screenmessage(
bui.Lstr(resource='notSignedInText'), color=(1, 0, 0)
)
2023-08-13 17:21:49 +05:30
bui.getsound('error').play()
2021-03-29 03:24:13 +05:30
return
# If it appears we don't have enough tickets, offer to buy more.
2023-08-13 17:21:49 +05:30
tickets = plus.get_v1_account_ticket_count()
2021-03-29 03:24:13 +05:30
if tickets < self._cost:
# FIXME: Should we start the timer back up again after?
self._counting_down = False
2023-08-13 17:21:49 +05:30
bui.textwidget(edit=self._counter_text, text='')
bui.getsound('error').play()
2021-03-29 03:24:13 +05:30
getcurrency.show_get_tickets_prompt()
return
if not self._transitioning_out:
2023-08-13 17:21:49 +05:30
bui.getsound('swish').play()
2021-03-29 03:24:13 +05:30
self._transitioning_out = True
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
self._continue_call()
def _on_cancel(self) -> None:
if not self._transitioning_out:
2023-08-13 17:21:49 +05:30
bui.getsound('swish').play()
2021-03-29 03:24:13 +05:30
self._transitioning_out = True
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
self._cancel_call()