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 = 310
 36        self._height = 310
 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            color=bui.app.ui_v1.title_color,
 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            border_opacity=0.4,
 78        )
 79        bui.widget(edit=self._scrollwidget, autoselect=True)
 80
 81        bui.containerwidget(
 82            edit=self.root_widget, cancel_button=self._cancel_button
 83        )
 84
 85        incr = 31
 86        sub_width = self._width - 90
 87
 88        trophy_types = [['0a'], ['0b'], ['1'], ['2'], ['3'], ['4']]
 89        sub_height = 40 + len(trophy_types) * incr
 90
 91        eq_text = bui.Lstr(
 92            resource='coopSelectWindow.powerRankingPointsEqualsText'
 93        ).evaluate()
 94
 95        self._subcontainer = bui.containerwidget(
 96            parent=self._scrollwidget,
 97            size=(sub_width, sub_height),
 98            background=False,
 99        )
100
101        total_pts = 0
102
103        multi_txt = bui.Lstr(
104            resource='coopSelectWindow.powerRankingPointsMultText'
105        ).evaluate()
106
107        total_pts += self._create_trophy_type_widgets(
108            eq_text, incr, multi_txt, sub_height, sub_width, trophy_types
109        )
110
111        bui.textwidget(
112            parent=self._subcontainer,
113            position=(
114                sub_width * 1.0,
115                sub_height - 20 - incr * len(trophy_types),
116            ),
117            maxwidth=sub_width * 0.5,
118            scale=0.7,
119            color=(0.7, 0.8, 1.0),
120            flatness=1.0,
121            shadow=0.0,
122            text=bui.Lstr(resource='coopSelectWindow.totalText').evaluate()
123            + ' '
124            + eq_text.replace('${NUMBER}', str(total_pts)),
125            size=(0, 0),
126            h_align='right',
127            v_align='center',
128        )
129
130    def _create_trophy_type_widgets(
131        self,
132        eq_text: str,
133        incr: int,
134        multi_txt: str,
135        sub_height: int,
136        sub_width: int,
137        trophy_types: list[list[str]],
138    ) -> int:
139        # pylint: disable=too-many-positional-arguments
140        from bascenev1 import get_trophy_string
141
142        total_pts = 0
143        for i, trophy_type in enumerate(trophy_types):
144            t_count = self._data['t' + trophy_type[0]]
145            t_mult = self._data['t' + trophy_type[0] + 'm']
146            bui.textwidget(
147                parent=self._subcontainer,
148                position=(sub_width * 0.15, sub_height - 20 - incr * i),
149                scale=0.7,
150                flatness=1.0,
151                shadow=0.7,
152                color=(1, 1, 1),
153                text=get_trophy_string(trophy_type[0]),
154                size=(0, 0),
155                h_align='center',
156                v_align='center',
157            )
158
159            bui.textwidget(
160                parent=self._subcontainer,
161                position=(sub_width * 0.31, sub_height - 20 - incr * i),
162                maxwidth=sub_width * 0.2,
163                scale=0.8,
164                flatness=1.0,
165                shadow=0.0,
166                color=(0, 1, 0) if (t_count > 0) else (0.6, 0.6, 0.6, 0.5),
167                text=str(t_count),
168                size=(0, 0),
169                h_align='center',
170                v_align='center',
171            )
172
173            txt = multi_txt.replace('${NUMBER}', str(t_mult))
174            bui.textwidget(
175                parent=self._subcontainer,
176                position=(sub_width * 0.57, sub_height - 20 - incr * i),
177                maxwidth=sub_width * 0.3,
178                scale=0.4,
179                flatness=1.0,
180                shadow=0.0,
181                color=(
182                    (0.63, 0.6, 0.75) if (t_count > 0) else (0.6, 0.6, 0.6, 0.4)
183                ),
184                text=txt,
185                size=(0, 0),
186                h_align='center',
187                v_align='center',
188            )
189
190            this_pts = t_count * t_mult
191            bui.textwidget(
192                parent=self._subcontainer,
193                position=(sub_width * 0.88, sub_height - 20 - incr * i),
194                maxwidth=sub_width * 0.3,
195                color=(
196                    (0.7, 0.8, 1.0) if (t_count > 0) else (0.9, 0.9, 1.0, 0.3)
197                ),
198                flatness=1.0,
199                shadow=0.0,
200                scale=0.5,
201                text=eq_text.replace('${NUMBER}', str(this_pts)),
202                size=(0, 0),
203                h_align='center',
204                v_align='center',
205            )
206            total_pts += this_pts
207        return total_pts
208
209    def _on_cancel_press(self) -> None:
210        self._transition_out()
211
212    def _transition_out(self) -> None:
213        if not self._transitioning_out:
214            self._transitioning_out = True
215            bui.containerwidget(edit=self.root_widget, transition='out_scale')
216
217    @override
218    def on_popup_cancel(self) -> None:
219        bui.getsound('swish').play()
220        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 = 310
 37        self._height = 310
 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            color=bui.app.ui_v1.title_color,
 71        )
 72
 73        self._scrollwidget = bui.scrollwidget(
 74            parent=self.root_widget,
 75            size=(self._width - 60, self._height - 70),
 76            position=(30, 30),
 77            capture_arrows=True,
 78            border_opacity=0.4,
 79        )
 80        bui.widget(edit=self._scrollwidget, autoselect=True)
 81
 82        bui.containerwidget(
 83            edit=self.root_widget, cancel_button=self._cancel_button
 84        )
 85
 86        incr = 31
 87        sub_width = self._width - 90
 88
 89        trophy_types = [['0a'], ['0b'], ['1'], ['2'], ['3'], ['4']]
 90        sub_height = 40 + len(trophy_types) * incr
 91
 92        eq_text = bui.Lstr(
 93            resource='coopSelectWindow.powerRankingPointsEqualsText'
 94        ).evaluate()
 95
 96        self._subcontainer = bui.containerwidget(
 97            parent=self._scrollwidget,
 98            size=(sub_width, sub_height),
 99            background=False,
100        )
101
102        total_pts = 0
103
104        multi_txt = bui.Lstr(
105            resource='coopSelectWindow.powerRankingPointsMultText'
106        ).evaluate()
107
108        total_pts += self._create_trophy_type_widgets(
109            eq_text, incr, multi_txt, sub_height, sub_width, trophy_types
110        )
111
112        bui.textwidget(
113            parent=self._subcontainer,
114            position=(
115                sub_width * 1.0,
116                sub_height - 20 - incr * len(trophy_types),
117            ),
118            maxwidth=sub_width * 0.5,
119            scale=0.7,
120            color=(0.7, 0.8, 1.0),
121            flatness=1.0,
122            shadow=0.0,
123            text=bui.Lstr(resource='coopSelectWindow.totalText').evaluate()
124            + ' '
125            + eq_text.replace('${NUMBER}', str(total_pts)),
126            size=(0, 0),
127            h_align='right',
128            v_align='center',
129        )
130
131    def _create_trophy_type_widgets(
132        self,
133        eq_text: str,
134        incr: int,
135        multi_txt: str,
136        sub_height: int,
137        sub_width: int,
138        trophy_types: list[list[str]],
139    ) -> int:
140        # pylint: disable=too-many-positional-arguments
141        from bascenev1 import get_trophy_string
142
143        total_pts = 0
144        for i, trophy_type in enumerate(trophy_types):
145            t_count = self._data['t' + trophy_type[0]]
146            t_mult = self._data['t' + trophy_type[0] + 'm']
147            bui.textwidget(
148                parent=self._subcontainer,
149                position=(sub_width * 0.15, sub_height - 20 - incr * i),
150                scale=0.7,
151                flatness=1.0,
152                shadow=0.7,
153                color=(1, 1, 1),
154                text=get_trophy_string(trophy_type[0]),
155                size=(0, 0),
156                h_align='center',
157                v_align='center',
158            )
159
160            bui.textwidget(
161                parent=self._subcontainer,
162                position=(sub_width * 0.31, sub_height - 20 - incr * i),
163                maxwidth=sub_width * 0.2,
164                scale=0.8,
165                flatness=1.0,
166                shadow=0.0,
167                color=(0, 1, 0) if (t_count > 0) else (0.6, 0.6, 0.6, 0.5),
168                text=str(t_count),
169                size=(0, 0),
170                h_align='center',
171                v_align='center',
172            )
173
174            txt = multi_txt.replace('${NUMBER}', str(t_mult))
175            bui.textwidget(
176                parent=self._subcontainer,
177                position=(sub_width * 0.57, sub_height - 20 - incr * i),
178                maxwidth=sub_width * 0.3,
179                scale=0.4,
180                flatness=1.0,
181                shadow=0.0,
182                color=(
183                    (0.63, 0.6, 0.75) if (t_count > 0) else (0.6, 0.6, 0.6, 0.4)
184                ),
185                text=txt,
186                size=(0, 0),
187                h_align='center',
188                v_align='center',
189            )
190
191            this_pts = t_count * t_mult
192            bui.textwidget(
193                parent=self._subcontainer,
194                position=(sub_width * 0.88, sub_height - 20 - incr * i),
195                maxwidth=sub_width * 0.3,
196                color=(
197                    (0.7, 0.8, 1.0) if (t_count > 0) else (0.9, 0.9, 1.0, 0.3)
198                ),
199                flatness=1.0,
200                shadow=0.0,
201                scale=0.5,
202                text=eq_text.replace('${NUMBER}', str(this_pts)),
203                size=(0, 0),
204                h_align='center',
205                v_align='center',
206            )
207            total_pts += this_pts
208        return total_pts
209
210    def _on_cancel_press(self) -> None:
211        self._transition_out()
212
213    def _transition_out(self) -> None:
214        if not self._transitioning_out:
215            self._transitioning_out = True
216            bui.containerwidget(edit=self.root_widget, transition='out_scale')
217
218    @override
219    def on_popup_cancel(self) -> None:
220        bui.getsound('swish').play()
221        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 = 310
 37        self._height = 310
 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            color=bui.app.ui_v1.title_color,
 71        )
 72
 73        self._scrollwidget = bui.scrollwidget(
 74            parent=self.root_widget,
 75            size=(self._width - 60, self._height - 70),
 76            position=(30, 30),
 77            capture_arrows=True,
 78            border_opacity=0.4,
 79        )
 80        bui.widget(edit=self._scrollwidget, autoselect=True)
 81
 82        bui.containerwidget(
 83            edit=self.root_widget, cancel_button=self._cancel_button
 84        )
 85
 86        incr = 31
 87        sub_width = self._width - 90
 88
 89        trophy_types = [['0a'], ['0b'], ['1'], ['2'], ['3'], ['4']]
 90        sub_height = 40 + len(trophy_types) * incr
 91
 92        eq_text = bui.Lstr(
 93            resource='coopSelectWindow.powerRankingPointsEqualsText'
 94        ).evaluate()
 95
 96        self._subcontainer = bui.containerwidget(
 97            parent=self._scrollwidget,
 98            size=(sub_width, sub_height),
 99            background=False,
100        )
101
102        total_pts = 0
103
104        multi_txt = bui.Lstr(
105            resource='coopSelectWindow.powerRankingPointsMultText'
106        ).evaluate()
107
108        total_pts += self._create_trophy_type_widgets(
109            eq_text, incr, multi_txt, sub_height, sub_width, trophy_types
110        )
111
112        bui.textwidget(
113            parent=self._subcontainer,
114            position=(
115                sub_width * 1.0,
116                sub_height - 20 - incr * len(trophy_types),
117            ),
118            maxwidth=sub_width * 0.5,
119            scale=0.7,
120            color=(0.7, 0.8, 1.0),
121            flatness=1.0,
122            shadow=0.0,
123            text=bui.Lstr(resource='coopSelectWindow.totalText').evaluate()
124            + ' '
125            + eq_text.replace('${NUMBER}', str(total_pts)),
126            size=(0, 0),
127            h_align='right',
128            v_align='center',
129        )
@override
def on_popup_cancel(self) -> None:
218    @override
219    def on_popup_cancel(self) -> None:
220        bui.getsound('swish').play()
221        self._transition_out()

Called when the popup is canceled.

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