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        # pylint: disable=too-many-positional-arguments
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()
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        # pylint: disable=too-many-positional-arguments
139        from bascenev1 import get_trophy_string
140
141        total_pts = 0
142        for i, trophy_type in enumerate(trophy_types):
143            t_count = self._data['t' + trophy_type[0]]
144            t_mult = self._data['t' + trophy_type[0] + 'm']
145            bui.textwidget(
146                parent=self._subcontainer,
147                position=(sub_width * 0.15, sub_height - 20 - incr * i),
148                scale=0.7,
149                flatness=1.0,
150                shadow=0.7,
151                color=(1, 1, 1),
152                text=get_trophy_string(trophy_type[0]),
153                size=(0, 0),
154                h_align='center',
155                v_align='center',
156            )
157
158            bui.textwidget(
159                parent=self._subcontainer,
160                position=(sub_width * 0.31, sub_height - 20 - incr * i),
161                maxwidth=sub_width * 0.2,
162                scale=0.8,
163                flatness=1.0,
164                shadow=0.0,
165                color=(0, 1, 0) if (t_count > 0) else (0.6, 0.6, 0.6, 0.5),
166                text=str(t_count),
167                size=(0, 0),
168                h_align='center',
169                v_align='center',
170            )
171
172            txt = multi_txt.replace('${NUMBER}', str(t_mult))
173            bui.textwidget(
174                parent=self._subcontainer,
175                position=(sub_width * 0.57, sub_height - 20 - incr * i),
176                maxwidth=sub_width * 0.3,
177                scale=0.4,
178                flatness=1.0,
179                shadow=0.0,
180                color=(
181                    (0.63, 0.6, 0.75) if (t_count > 0) else (0.6, 0.6, 0.6, 0.4)
182                ),
183                text=txt,
184                size=(0, 0),
185                h_align='center',
186                v_align='center',
187            )
188
189            this_pts = t_count * t_mult
190            bui.textwidget(
191                parent=self._subcontainer,
192                position=(sub_width * 0.88, sub_height - 20 - incr * i),
193                maxwidth=sub_width * 0.3,
194                color=(
195                    (0.7, 0.8, 1.0) if (t_count > 0) else (0.9, 0.9, 1.0, 0.3)
196                ),
197                flatness=1.0,
198                shadow=0.0,
199                scale=0.5,
200                text=eq_text.replace('${NUMBER}', str(this_pts)),
201                size=(0, 0),
202                h_align='center',
203                v_align='center',
204            )
205            total_pts += this_pts
206        return total_pts
207
208    def _on_cancel_press(self) -> None:
209        self._transition_out()
210
211    def _transition_out(self) -> None:
212        if not self._transitioning_out:
213            self._transitioning_out = True
214            bui.containerwidget(edit=self.root_widget, transition='out_scale')
215
216    @override
217    def on_popup_cancel(self) -> None:
218        bui.getsound('swish').play()
219        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:
216    @override
217    def on_popup_cancel(self) -> None:
218        bui.getsound('swish').play()
219        self._transition_out()

Called when the popup is canceled.

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