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

373 lines
13 KiB
Python
Raw Permalink 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 view achievements."""
from __future__ import annotations
2024-05-19 18:25:43 +05:30
from typing import override
2024-01-27 21:25:16 +05:30
2025-10-21 19:08:49 +05:30
from bauiv1lib.utils import scroll_fade_bottom, scroll_fade_top
2023-08-13 17:21:49 +05:30
import bauiv1 as bui
2021-03-29 03:24:13 +05:30
2025-02-09 00:17:58 +05:30
class AchievementsWindow(bui.MainWindow):
2021-03-29 03:24:13 +05:30
"""Popup window to view achievements."""
def __init__(
2025-02-09 00:17:58 +05:30
self,
transition: str | None = 'in_right',
origin_widget: bui.Widget | None = None,
2025-09-07 18:34:55 +05:30
auxiliary_style: bool = True,
):
2021-03-29 03:24:13 +05:30
# pylint: disable=too-many-locals
2025-02-09 00:17:58 +05:30
# pylint: disable=too-many-statements
# pylint: disable=cyclic-import
from baclassic import (
CHEST_APPEARANCE_DISPLAY_INFOS,
CHEST_APPEARANCE_DISPLAY_INFO_DEFAULT,
)
2023-08-13 17:21:49 +05:30
assert bui.app.classic is not None
uiscale = bui.app.ui_v1.uiscale
2025-02-09 00:17:58 +05:30
2026-02-15 14:00:00 +05:30
self._uiopenstate = bui.UIOpenState('classicachievements')
2025-09-07 18:34:55 +05:30
self._width = 800 if uiscale is bui.UIScale.SMALL else 550
self._height = (
2025-02-09 00:17:58 +05:30
450
2023-08-13 17:21:49 +05:30
if uiscale is bui.UIScale.SMALL
2024-05-19 18:25:43 +05:30
else 370 if uiscale is bui.UIScale.MEDIUM else 450
)
2021-03-29 03:24:13 +05:30
2025-02-09 00:17:58 +05:30
# Do some fancy math to fill all available screen area up to the
# size of our backing container. This lets us fit to the exact
# screen shape at small ui scale.
screensize = bui.get_virtual_screen_size()
scale = (
2025-09-07 18:34:55 +05:30
2.4
2025-02-09 00:17:58 +05:30
if uiscale is bui.UIScale.SMALL
else 1.5 if uiscale is bui.UIScale.MEDIUM else 1.2
)
2025-02-09 00:17:58 +05:30
# Calc screen size in our local container space and clamp to a
# bit smaller than our container size.
target_width = min(self._width - 60, screensize[0] / scale)
target_height = min(self._height - 70, screensize[1] / scale)
# To get top/left coords, go to the center of our window and
# offset by half the width/height of our target area.
yoffs = 0.5 * self._height + 0.5 * target_height + 30.0
scroll_width = target_width
scroll_height = target_height - 25
scroll_bottom = yoffs - 54 - scroll_height
2021-03-29 03:24:13 +05:30
2025-10-21 19:08:49 +05:30
# Go with full-screen scrollable area in small ui.
if uiscale is bui.UIScale.SMALL:
scroll_height += 30
scroll_bottom -= 3
2025-02-09 00:17:58 +05:30
super().__init__(
root_widget=bui.containerwidget(
size=(self._width, self._height),
2025-10-21 19:08:49 +05:30
toolbar_visibility='menu_full',
2025-09-07 18:34:55 +05:30
toolbar_cancel_button_style=(
'close' if auxiliary_style else 'back'
2025-02-09 00:17:58 +05:30
),
scale=scale,
),
transition=transition,
origin_widget=origin_widget,
# We're affected by screen size only at small ui-scale.
refresh_on_screen_size_changes=uiscale is bui.UIScale.SMALL,
)
2021-03-29 03:24:13 +05:30
2025-02-09 00:17:58 +05:30
if uiscale is bui.UIScale.SMALL:
bui.containerwidget(
edit=self._root_widget, on_cancel_call=self.main_window_back
)
self._back_button = None
else:
self._back_button = bui.buttonwidget(
parent=self._root_widget,
2025-10-21 19:08:49 +05:30
id=f'{self.main_window_id_prefix}|back',
2025-02-09 00:17:58 +05:30
autoselect=True,
position=(50, yoffs - 48),
size=(60, 60),
scale=0.6,
2025-09-07 18:34:55 +05:30
label=bui.charstr(
bui.SpecialChar.CLOSE
if auxiliary_style
else bui.SpecialChar.BACK
),
button_type=None if auxiliary_style else 'backSmall',
2025-02-09 00:17:58 +05:30
on_activate_call=self.main_window_back,
)
bui.containerwidget(
edit=self._root_widget, cancel_button=self._back_button
)
2023-08-13 17:21:49 +05:30
achievements = bui.app.classic.ach.achievements
2021-03-29 03:24:13 +05:30
num_complete = len([a for a in achievements if a.complete])
2025-10-21 19:08:49 +05:30
self._scrollwidget = bui.scrollwidget(
parent=self._root_widget,
id=f'{self.main_window_id_prefix}|scroll',
size=(scroll_width, scroll_height),
position=(self._width * 0.5 - scroll_width * 0.5, scroll_bottom),
capture_arrows=True,
simple_culling_v=10,
border_opacity=0.4,
)
bui.containerwidget(
edit=self._root_widget, selected_child=self._scrollwidget
)
bui.widget(edit=self._scrollwidget, autoselect=True)
if uiscale is bui.UIScale.SMALL:
bui.widget(
edit=self._scrollwidget,
left_widget=bui.get_special_widget('back_button'),
)
# With full-screen scrolling, fade content as it approaches
# toolbars.
if uiscale is bui.UIScale.SMALL:
scroll_fade_top(
self._root_widget,
self._width * 0.5 - scroll_width * 0.5,
scroll_bottom,
scroll_width,
scroll_height,
)
scroll_fade_bottom(
self._root_widget,
self._width * 0.5 - scroll_width * 0.5,
scroll_bottom,
scroll_width,
scroll_height,
)
2025-09-07 18:34:55 +05:30
# In small UI mode when the screen is narrow enough we need to
# go with a smaller title to avoid it overlapping with toolbar
# bits.
if uiscale is bui.UIScale.SMALL and target_width < 630:
self._title_text = bui.textwidget(
parent=self._root_widget,
position=(self._width * 0.5, yoffs - 42),
size=(0, 0),
h_align='center',
v_align='center',
scale=0.6,
text=bui.Lstr(
value='${A}: ${C}/${T}',
subs=[
('${A}', bui.Lstr(resource='achievementsText')),
('${C}', str(num_complete)),
('${T}', str(len(achievements))),
],
),
maxwidth=86,
color=bui.app.ui_v1.title_color,
)
else:
self._title_text = bui.textwidget(
parent=self._root_widget,
position=(
self._width * 0.5,
yoffs - (42 if uiscale is bui.UIScale.SMALL else 30),
),
size=(0, 0),
h_align='center',
v_align='center',
scale=0.6,
text=bui.Lstr(
resource='accountSettingsWindow.achievementProgressText',
subs=[
('${COUNT}', str(num_complete)),
('${TOTAL}', str(len(achievements))),
],
),
maxwidth=180,
color=bui.app.ui_v1.title_color,
)
2023-08-13 17:21:49 +05:30
bui.containerwidget(
2025-02-09 00:17:58 +05:30
edit=self._root_widget, cancel_button=self._back_button
)
2021-03-29 03:24:13 +05:30
incr = 36
2025-02-09 00:17:58 +05:30
sub_width = scroll_width - 25
2025-09-07 18:34:55 +05:30
sub_height = 85 + len(achievements) * incr
2021-03-29 03:24:13 +05:30
2025-10-21 19:08:49 +05:30
# For fullscreen scrollable, account for toolbar.
if uiscale is bui.UIScale.SMALL:
sub_height += 30
2021-03-29 03:24:13 +05:30
eq_rsrc = 'coopSelectWindow.powerRankingPointsEqualsText'
pts_rsrc = 'coopSelectWindow.powerRankingPointsText'
2023-08-13 17:21:49 +05:30
self._subcontainer = bui.containerwidget(
parent=self._scrollwidget,
2025-10-21 19:08:49 +05:30
id=f'{self.main_window_id_prefix}|subcontainer',
size=(sub_width, sub_height),
background=False,
)
2021-03-29 03:24:13 +05:30
2025-10-21 19:08:49 +05:30
basey = sub_height
# For fullscreen scrollable, account for toolbar.
if uiscale is bui.UIScale.SMALL:
basey -= 30
2021-03-29 03:24:13 +05:30
total_pts = 0
for i, ach in enumerate(achievements):
complete = ach.complete
2023-08-13 17:21:49 +05:30
bui.textwidget(
parent=self._subcontainer,
2025-10-21 19:08:49 +05:30
position=(sub_width * 0.08 - 5, basey - 20 - incr * i),
maxwidth=20,
scale=0.5,
color=(0.6, 0.6, 0.7) if complete else (0.6, 0.6, 0.7, 0.2),
flatness=1.0,
shadow=0.0,
text=str(i + 1),
size=(0, 0),
h_align='right',
v_align='center',
)
2023-08-13 17:21:49 +05:30
bui.imagewidget(
parent=self._subcontainer,
2024-05-19 18:25:43 +05:30
position=(
2025-10-21 19:08:49 +05:30
(sub_width * 0.10 + 1, basey - 20 - incr * i - 9)
2024-05-19 18:25:43 +05:30
if complete
2025-10-21 19:08:49 +05:30
else (sub_width * 0.10 - 4, basey - 20 - incr * i - 14)
2024-05-19 18:25:43 +05:30
),
size=(18, 18) if complete else (27, 27),
opacity=1.0 if complete else 0.3,
color=ach.get_icon_color(complete)[:3],
2023-08-13 17:21:49 +05:30
texture=ach.get_icon_ui_texture(complete),
)
2021-03-29 03:24:13 +05:30
if complete:
2023-08-13 17:21:49 +05:30
bui.imagewidget(
parent=self._subcontainer,
position=(
sub_width * 0.10 - 4,
2025-10-21 19:08:49 +05:30
basey - 25 - incr * i - 9,
),
size=(28, 28),
color=(2, 1.4, 0),
2023-08-13 17:21:49 +05:30
texture=bui.gettexture('achievementOutline'),
)
2023-08-13 17:21:49 +05:30
bui.textwidget(
parent=self._subcontainer,
2025-10-21 19:08:49 +05:30
position=(sub_width * 0.19, basey - 19 - incr * i + 3),
maxwidth=sub_width * 0.62,
scale=0.6,
flatness=1.0,
shadow=0.0,
color=(1, 1, 1) if complete else (1, 1, 1, 0.2),
text=ach.display_name,
size=(0, 0),
h_align='left',
v_align='center',
)
2023-08-13 17:21:49 +05:30
bui.textwidget(
parent=self._subcontainer,
2025-10-21 19:08:49 +05:30
position=(sub_width * 0.19, basey - 19 - incr * i - 10),
maxwidth=sub_width * 0.62,
scale=0.4,
flatness=1.0,
shadow=0.0,
color=(0.83, 0.8, 0.85) if complete else (0.8, 0.8, 0.8, 0.2),
2024-05-19 18:25:43 +05:30
text=(
ach.description_full_complete
if complete
else ach.description_full
),
size=(0, 0),
h_align='left',
v_align='center',
)
2025-02-09 00:17:58 +05:30
chest_type = ach.get_award_chest_type()
chestdisplayinfo = CHEST_APPEARANCE_DISPLAY_INFOS.get(
chest_type, CHEST_APPEARANCE_DISPLAY_INFO_DEFAULT
)
chestsize = 24.0
bui.imagewidget(
parent=self._subcontainer,
opacity=0.0 if complete else 1.0,
position=(
sub_width * 0.92 - 40.0 - chestsize * 0.5,
2025-10-21 19:08:49 +05:30
basey - 20 - incr * i - chestsize * 0.5,
2025-02-09 00:17:58 +05:30
),
size=(chestsize, chestsize),
color=chestdisplayinfo.color,
texture=bui.gettexture(chestdisplayinfo.texclosed),
tint_texture=bui.gettexture(chestdisplayinfo.texclosedtint),
tint_color=chestdisplayinfo.tint,
tint2_color=chestdisplayinfo.tint2,
)
2021-03-29 03:24:13 +05:30
pts = ach.power_ranking_value
2023-08-13 17:21:49 +05:30
bui.textwidget(
parent=self._subcontainer,
2025-10-21 19:08:49 +05:30
position=(sub_width * 0.92, basey - 20 - incr * i),
maxwidth=sub_width * 0.15,
color=(0.7, 0.8, 1.0) if complete else (0.9, 0.9, 1.0, 0.3),
flatness=1.0,
shadow=0.0,
scale=0.6,
2023-08-13 17:21:49 +05:30
text=bui.Lstr(
resource=pts_rsrc, subs=[('${NUMBER}', str(pts))]
),
size=(0, 0),
h_align='center',
v_align='center',
)
2021-03-29 03:24:13 +05:30
if complete:
total_pts += pts
2023-08-13 17:21:49 +05:30
bui.textwidget(
parent=self._subcontainer,
position=(
sub_width * 1.0,
2025-10-21 19:08:49 +05:30
basey - 20 - incr * len(achievements),
),
maxwidth=sub_width * 0.5,
scale=0.7,
color=(0.7, 0.8, 1.0),
flatness=1.0,
shadow=0.0,
2023-08-13 17:21:49 +05:30
text=bui.Lstr(
value='${A} ${B}',
subs=[
2023-08-13 17:21:49 +05:30
('${A}', bui.Lstr(resource='coopSelectWindow.totalText')),
(
'${B}',
2023-08-13 17:21:49 +05:30
bui.Lstr(
resource=eq_rsrc,
subs=[('${NUMBER}', str(total_pts))],
),
),
],
),
size=(0, 0),
h_align='right',
v_align='center',
)
2021-03-29 03:24:13 +05:30
2024-01-27 21:25:16 +05:30
@override
2025-02-09 00:17:58 +05:30
def get_main_window_state(self) -> bui.MainWindowState:
# Support recreating our window for back/refresh purposes.
cls = type(self)
return bui.BasicMainWindowState(
create_call=lambda transition, origin_widget: cls(
transition=transition, origin_widget=origin_widget
)
)
2025-10-21 19:08:49 +05:30
@override
def main_window_should_preserve_selection(self) -> bool:
return True