bauiv1lib.trophies

Provides a popup window for viewing trophies.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""Provides a popup window for viewing trophies."""
  4
  5from __future__ import annotations
  6
  7from typing import TYPE_CHECKING, override
  8
  9from bauiv1lib import popup
 10import bauiv1 as bui
 11
 12if TYPE_CHECKING:
 13    from typing import Any
 14
 15
 16class TrophiesWindow(popup.PopupWindow):
 17    """Popup window for viewing trophies."""
 18
 19    def __init__(
 20        self,
 21        position: tuple[float, float],
 22        data: dict[str, Any],
 23        scale: float | None = None,
 24    ):
 25        self._data = data
 26        assert bui.app.classic is not None
 27        uiscale = bui.app.ui_v1.uiscale
 28        if scale is None:
 29            scale = (
 30                2.3
 31                if uiscale is bui.UIScale.SMALL
 32                else 1.65 if uiscale is bui.UIScale.MEDIUM else 1.23
 33            )
 34        self._transitioning_out = False
 35        self._width = 300
 36        self._height = 300
 37        bg_color = (0.5, 0.4, 0.6)
 38
 39        super().__init__(
 40            position=position,
 41            size=(self._width, self._height),
 42            scale=scale,
 43            bg_color=bg_color,
 44        )
 45
 46        self._cancel_button = bui.buttonwidget(
 47            parent=self.root_widget,
 48            position=(50, self._height - 30),
 49            size=(50, 50),
 50            scale=0.5,
 51            label='',
 52            color=bg_color,
 53            on_activate_call=self._on_cancel_press,
 54            autoselect=True,
 55            icon=bui.gettexture('crossOut'),
 56            iconscale=1.2,
 57        )
 58
 59        self._title_text = bui.textwidget(
 60            parent=self.root_widget,
 61            position=(self._width * 0.5, self._height - 20),
 62            size=(0, 0),
 63            h_align='center',
 64            v_align='center',
 65            scale=0.6,
 66            text=bui.Lstr(resource='trophiesText'),
 67            maxwidth=200,
 68            color=(1, 1, 1, 0.4),
 69        )
 70
 71        self._scrollwidget = bui.scrollwidget(
 72            parent=self.root_widget,
 73            size=(self._width - 60, self._height - 70),
 74            position=(30, 30),
 75            capture_arrows=True,
 76        )
 77        bui.widget(edit=self._scrollwidget, autoselect=True)
 78
 79        bui.containerwidget(
 80            edit=self.root_widget, cancel_button=self._cancel_button
 81        )
 82
 83        incr = 31
 84        sub_width = self._width - 90
 85
 86        trophy_types = [['0a'], ['0b'], ['1'], ['2'], ['3'], ['4']]
 87        sub_height = 40 + len(trophy_types) * incr
 88
 89        eq_text = bui.Lstr(
 90            resource='coopSelectWindow.powerRankingPointsEqualsText'
 91        ).evaluate()
 92
 93        self._subcontainer = bui.containerwidget(
 94            parent=self._scrollwidget,
 95            size=(sub_width, sub_height),
 96            background=False,
 97        )
 98
 99        total_pts = 0
100
101        multi_txt = bui.Lstr(
102            resource='coopSelectWindow.powerRankingPointsMultText'
103        ).evaluate()
104
105        total_pts += self._create_trophy_type_widgets(
106            eq_text, incr, multi_txt, sub_height, sub_width, trophy_types
107        )
108
109        bui.textwidget(
110            parent=self._subcontainer,
111            position=(
112                sub_width * 1.0,
113                sub_height - 20 - incr * len(trophy_types),
114            ),
115            maxwidth=sub_width * 0.5,
116            scale=0.7,
117            color=(0.7, 0.8, 1.0),
118            flatness=1.0,
119            shadow=0.0,
120            text=bui.Lstr(resource='coopSelectWindow.totalText').evaluate()
121            + ' '
122            + eq_text.replace('${NUMBER}', str(total_pts)),
123            size=(0, 0),
124            h_align='right',
125            v_align='center',
126        )
127
128    def _create_trophy_type_widgets(
129        self,
130        eq_text: str,
131        incr: int,
132        multi_txt: str,
133        sub_height: int,
134        sub_width: int,
135        trophy_types: list[list[str]],
136    ) -> int:
137        from bascenev1 import get_trophy_string
138
139        total_pts = 0
140        for i, trophy_type in enumerate(trophy_types):
141            t_count = self._data['t' + trophy_type[0]]
142            t_mult = self._data['t' + trophy_type[0] + 'm']
143            bui.textwidget(
144                parent=self._subcontainer,
145                position=(sub_width * 0.15, sub_height - 20 - incr * i),
146                scale=0.7,
147                flatness=1.0,
148                shadow=0.7,
149                color=(1, 1, 1),
150                text=get_trophy_string(trophy_type[0]),
151                size=(0, 0),
152                h_align='center',
153                v_align='center',
154            )
155
156            bui.textwidget(
157                parent=self._subcontainer,
158                position=(sub_width * 0.31, sub_height - 20 - incr * i),
159                maxwidth=sub_width * 0.2,
160                scale=0.8,
161                flatness=1.0,
162                shadow=0.0,
163                color=(0, 1, 0) if (t_count > 0) else (0.6, 0.6, 0.6, 0.5),
164                text=str(t_count),
165                size=(0, 0),
166                h_align='center',
167                v_align='center',
168            )
169
170            txt = multi_txt.replace('${NUMBER}', str(t_mult))
171            bui.textwidget(
172                parent=self._subcontainer,
173                position=(sub_width * 0.57, sub_height - 20 - incr * i),
174                maxwidth=sub_width * 0.3,
175                scale=0.4,
176                flatness=1.0,
177                shadow=0.0,
178                color=(
179                    (0.63, 0.6, 0.75) if (t_count > 0) else (0.6, 0.6, 0.6, 0.4)
180                ),
181                text=txt,
182                size=(0, 0),
183                h_align='center',
184                v_align='center',
185            )
186
187            this_pts = t_count * t_mult
188            bui.textwidget(
189                parent=self._subcontainer,
190                position=(sub_width * 0.88, sub_height - 20 - incr * i),
191                maxwidth=sub_width * 0.3,
192                color=(
193                    (0.7, 0.8, 1.0) if (t_count > 0) else (0.9, 0.9, 1.0, 0.3)
194                ),
195                flatness=1.0,
196                shadow=0.0,
197                scale=0.5,
198                text=eq_text.replace('${NUMBER}', str(this_pts)),
199                size=(0, 0),
200                h_align='center',
201                v_align='center',
202            )
203            total_pts += this_pts
204        return total_pts
205
206    def _on_cancel_press(self) -> None:
207        self._transition_out()
208
209    def _transition_out(self) -> None:
210        if not self._transitioning_out:
211            self._transitioning_out = True
212            bui.containerwidget(edit=self.root_widget, transition='out_scale')
213
214    @override
215    def on_popup_cancel(self) -> None:
216        bui.getsound('swish').play()
217        self._transition_out()
class TrophiesWindow(bauiv1lib.popup.PopupWindow):
 17class TrophiesWindow(popup.PopupWindow):
 18    """Popup window for viewing trophies."""
 19
 20    def __init__(
 21        self,
 22        position: tuple[float, float],
 23        data: dict[str, Any],
 24        scale: float | None = None,
 25    ):
 26        self._data = data
 27        assert bui.app.classic is not None
 28        uiscale = bui.app.ui_v1.uiscale
 29        if scale is None:
 30            scale = (
 31                2.3
 32                if uiscale is bui.UIScale.SMALL
 33                else 1.65 if uiscale is bui.UIScale.MEDIUM else 1.23
 34            )
 35        self._transitioning_out = False
 36        self._width = 300
 37        self._height = 300
 38        bg_color = (0.5, 0.4, 0.6)
 39
 40        super().__init__(
 41            position=position,
 42            size=(self._width, self._height),
 43            scale=scale,
 44            bg_color=bg_color,
 45        )
 46
 47        self._cancel_button = bui.buttonwidget(
 48            parent=self.root_widget,
 49            position=(50, self._height - 30),
 50            size=(50, 50),
 51            scale=0.5,
 52            label='',
 53            color=bg_color,
 54            on_activate_call=self._on_cancel_press,
 55            autoselect=True,
 56            icon=bui.gettexture('crossOut'),
 57            iconscale=1.2,
 58        )
 59
 60        self._title_text = bui.textwidget(
 61            parent=self.root_widget,
 62            position=(self._width * 0.5, self._height - 20),
 63            size=(0, 0),
 64            h_align='center',
 65            v_align='center',
 66            scale=0.6,
 67            text=bui.Lstr(resource='trophiesText'),
 68            maxwidth=200,
 69            color=(1, 1, 1, 0.4),
 70        )
 71
 72        self._scrollwidget = bui.scrollwidget(
 73            parent=self.root_widget,
 74            size=(self._width - 60, self._height - 70),
 75            position=(30, 30),
 76            capture_arrows=True,
 77        )
 78        bui.widget(edit=self._scrollwidget, autoselect=True)
 79
 80        bui.containerwidget(
 81            edit=self.root_widget, cancel_button=self._cancel_button
 82        )
 83
 84        incr = 31
 85        sub_width = self._width - 90
 86
 87        trophy_types = [['0a'], ['0b'], ['1'], ['2'], ['3'], ['4']]
 88        sub_height = 40 + len(trophy_types) * incr
 89
 90        eq_text = bui.Lstr(
 91            resource='coopSelectWindow.powerRankingPointsEqualsText'
 92        ).evaluate()
 93
 94        self._subcontainer = bui.containerwidget(
 95            parent=self._scrollwidget,
 96            size=(sub_width, sub_height),
 97            background=False,
 98        )
 99
100        total_pts = 0
101
102        multi_txt = bui.Lstr(
103            resource='coopSelectWindow.powerRankingPointsMultText'
104        ).evaluate()
105
106        total_pts += self._create_trophy_type_widgets(
107            eq_text, incr, multi_txt, sub_height, sub_width, trophy_types
108        )
109
110        bui.textwidget(
111            parent=self._subcontainer,
112            position=(
113                sub_width * 1.0,
114                sub_height - 20 - incr * len(trophy_types),
115            ),
116            maxwidth=sub_width * 0.5,
117            scale=0.7,
118            color=(0.7, 0.8, 1.0),
119            flatness=1.0,
120            shadow=0.0,
121            text=bui.Lstr(resource='coopSelectWindow.totalText').evaluate()
122            + ' '
123            + eq_text.replace('${NUMBER}', str(total_pts)),
124            size=(0, 0),
125            h_align='right',
126            v_align='center',
127        )
128
129    def _create_trophy_type_widgets(
130        self,
131        eq_text: str,
132        incr: int,
133        multi_txt: str,
134        sub_height: int,
135        sub_width: int,
136        trophy_types: list[list[str]],
137    ) -> int:
138        from bascenev1 import get_trophy_string
139
140        total_pts = 0
141        for i, trophy_type in enumerate(trophy_types):
142            t_count = self._data['t' + trophy_type[0]]
143            t_mult = self._data['t' + trophy_type[0] + 'm']
144            bui.textwidget(
145                parent=self._subcontainer,
146                position=(sub_width * 0.15, sub_height - 20 - incr * i),
147                scale=0.7,
148                flatness=1.0,
149                shadow=0.7,
150                color=(1, 1, 1),
151                text=get_trophy_string(trophy_type[0]),
152                size=(0, 0),
153                h_align='center',
154                v_align='center',
155            )
156
157            bui.textwidget(
158                parent=self._subcontainer,
159                position=(sub_width * 0.31, sub_height - 20 - incr * i),
160                maxwidth=sub_width * 0.2,
161                scale=0.8,
162                flatness=1.0,
163                shadow=0.0,
164                color=(0, 1, 0) if (t_count > 0) else (0.6, 0.6, 0.6, 0.5),
165                text=str(t_count),
166                size=(0, 0),
167                h_align='center',
168                v_align='center',
169            )
170
171            txt = multi_txt.replace('${NUMBER}', str(t_mult))
172            bui.textwidget(
173                parent=self._subcontainer,
174                position=(sub_width * 0.57, sub_height - 20 - incr * i),
175                maxwidth=sub_width * 0.3,
176                scale=0.4,
177                flatness=1.0,
178                shadow=0.0,
179                color=(
180                    (0.63, 0.6, 0.75) if (t_count > 0) else (0.6, 0.6, 0.6, 0.4)
181                ),
182                text=txt,
183                size=(0, 0),
184                h_align='center',
185                v_align='center',
186            )
187
188            this_pts = t_count * t_mult
189            bui.textwidget(
190                parent=self._subcontainer,
191                position=(sub_width * 0.88, sub_height - 20 - incr * i),
192                maxwidth=sub_width * 0.3,
193                color=(
194                    (0.7, 0.8, 1.0) if (t_count > 0) else (0.9, 0.9, 1.0, 0.3)
195                ),
196                flatness=1.0,
197                shadow=0.0,
198                scale=0.5,
199                text=eq_text.replace('${NUMBER}', str(this_pts)),
200                size=(0, 0),
201                h_align='center',
202                v_align='center',
203            )
204            total_pts += this_pts
205        return total_pts
206
207    def _on_cancel_press(self) -> None:
208        self._transition_out()
209
210    def _transition_out(self) -> None:
211        if not self._transitioning_out:
212            self._transitioning_out = True
213            bui.containerwidget(edit=self.root_widget, transition='out_scale')
214
215    @override
216    def on_popup_cancel(self) -> None:
217        bui.getsound('swish').play()
218        self._transition_out()

Popup window for viewing trophies.

TrophiesWindow( position: tuple[float, float], data: dict[str, typing.Any], scale: float | None = None)
 20    def __init__(
 21        self,
 22        position: tuple[float, float],
 23        data: dict[str, Any],
 24        scale: float | None = None,
 25    ):
 26        self._data = data
 27        assert bui.app.classic is not None
 28        uiscale = bui.app.ui_v1.uiscale
 29        if scale is None:
 30            scale = (
 31                2.3
 32                if uiscale is bui.UIScale.SMALL
 33                else 1.65 if uiscale is bui.UIScale.MEDIUM else 1.23
 34            )
 35        self._transitioning_out = False
 36        self._width = 300
 37        self._height = 300
 38        bg_color = (0.5, 0.4, 0.6)
 39
 40        super().__init__(
 41            position=position,
 42            size=(self._width, self._height),
 43            scale=scale,
 44            bg_color=bg_color,
 45        )
 46
 47        self._cancel_button = bui.buttonwidget(
 48            parent=self.root_widget,
 49            position=(50, self._height - 30),
 50            size=(50, 50),
 51            scale=0.5,
 52            label='',
 53            color=bg_color,
 54            on_activate_call=self._on_cancel_press,
 55            autoselect=True,
 56            icon=bui.gettexture('crossOut'),
 57            iconscale=1.2,
 58        )
 59
 60        self._title_text = bui.textwidget(
 61            parent=self.root_widget,
 62            position=(self._width * 0.5, self._height - 20),
 63            size=(0, 0),
 64            h_align='center',
 65            v_align='center',
 66            scale=0.6,
 67            text=bui.Lstr(resource='trophiesText'),
 68            maxwidth=200,
 69            color=(1, 1, 1, 0.4),
 70        )
 71
 72        self._scrollwidget = bui.scrollwidget(
 73            parent=self.root_widget,
 74            size=(self._width - 60, self._height - 70),
 75            position=(30, 30),
 76            capture_arrows=True,
 77        )
 78        bui.widget(edit=self._scrollwidget, autoselect=True)
 79
 80        bui.containerwidget(
 81            edit=self.root_widget, cancel_button=self._cancel_button
 82        )
 83
 84        incr = 31
 85        sub_width = self._width - 90
 86
 87        trophy_types = [['0a'], ['0b'], ['1'], ['2'], ['3'], ['4']]
 88        sub_height = 40 + len(trophy_types) * incr
 89
 90        eq_text = bui.Lstr(
 91            resource='coopSelectWindow.powerRankingPointsEqualsText'
 92        ).evaluate()
 93
 94        self._subcontainer = bui.containerwidget(
 95            parent=self._scrollwidget,
 96            size=(sub_width, sub_height),
 97            background=False,
 98        )
 99
100        total_pts = 0
101
102        multi_txt = bui.Lstr(
103            resource='coopSelectWindow.powerRankingPointsMultText'
104        ).evaluate()
105
106        total_pts += self._create_trophy_type_widgets(
107            eq_text, incr, multi_txt, sub_height, sub_width, trophy_types
108        )
109
110        bui.textwidget(
111            parent=self._subcontainer,
112            position=(
113                sub_width * 1.0,
114                sub_height - 20 - incr * len(trophy_types),
115            ),
116            maxwidth=sub_width * 0.5,
117            scale=0.7,
118            color=(0.7, 0.8, 1.0),
119            flatness=1.0,
120            shadow=0.0,
121            text=bui.Lstr(resource='coopSelectWindow.totalText').evaluate()
122            + ' '
123            + eq_text.replace('${NUMBER}', str(total_pts)),
124            size=(0, 0),
125            h_align='right',
126            v_align='center',
127        )
@override
def on_popup_cancel(self) -> None:
215    @override
216    def on_popup_cancel(self) -> None:
217        bui.getsound('swish').play()
218        self._transition_out()

Called when the popup is canceled.

Cancels can occur due to clicking outside the window, hitting escape, etc.