bastd.ui.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
  8
  9import ba
 10from bastd.ui import popup
 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        uiscale = ba.app.ui.uiscale
 27        if scale is None:
 28            scale = (
 29                2.3
 30                if uiscale is ba.UIScale.SMALL
 31                else 1.65
 32                if uiscale is ba.UIScale.MEDIUM
 33                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        popup.PopupWindow.__init__(
 41            self,
 42            position=position,
 43            size=(self._width, self._height),
 44            scale=scale,
 45            bg_color=bg_color,
 46        )
 47
 48        self._cancel_button = ba.buttonwidget(
 49            parent=self.root_widget,
 50            position=(50, self._height - 30),
 51            size=(50, 50),
 52            scale=0.5,
 53            label='',
 54            color=bg_color,
 55            on_activate_call=self._on_cancel_press,
 56            autoselect=True,
 57            icon=ba.gettexture('crossOut'),
 58            iconscale=1.2,
 59        )
 60
 61        self._title_text = ba.textwidget(
 62            parent=self.root_widget,
 63            position=(self._width * 0.5, self._height - 20),
 64            size=(0, 0),
 65            h_align='center',
 66            v_align='center',
 67            scale=0.6,
 68            text=ba.Lstr(resource='trophiesText'),
 69            maxwidth=200,
 70            color=(1, 1, 1, 0.4),
 71        )
 72
 73        self._scrollwidget = ba.scrollwidget(
 74            parent=self.root_widget,
 75            size=(self._width - 60, self._height - 70),
 76            position=(30, 30),
 77            capture_arrows=True,
 78        )
 79        ba.widget(edit=self._scrollwidget, autoselect=True)
 80
 81        ba.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 = ba.Lstr(
 92            resource='coopSelectWindow.powerRankingPointsEqualsText'
 93        ).evaluate()
 94
 95        self._subcontainer = ba.containerwidget(
 96            parent=self._scrollwidget,
 97            size=(sub_width, sub_height),
 98            background=False,
 99        )
100
101        total_pts = 0
102
103        multi_txt = ba.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        ba.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=ba.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        from ba.internal 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            ba.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            ba.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            ba.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=(0.63, 0.6, 0.75)
181                if (t_count > 0)
182                else (0.6, 0.6, 0.6, 0.4),
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            ba.textwidget(
191                parent=self._subcontainer,
192                position=(sub_width * 0.88, sub_height - 20 - incr * i),
193                maxwidth=sub_width * 0.3,
194                color=(0.7, 0.8, 1.0)
195                if (t_count > 0)
196                else (0.9, 0.9, 1.0, 0.3),
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            ba.containerwidget(edit=self.root_widget, transition='out_scale')
215
216    def on_popup_cancel(self) -> None:
217        ba.playsound(ba.getsound('swish'))
218        self._transition_out()
class TrophiesWindow(bastd.ui.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        uiscale = ba.app.ui.uiscale
 28        if scale is None:
 29            scale = (
 30                2.3
 31                if uiscale is ba.UIScale.SMALL
 32                else 1.65
 33                if uiscale is ba.UIScale.MEDIUM
 34                else 1.23
 35            )
 36        self._transitioning_out = False
 37        self._width = 300
 38        self._height = 300
 39        bg_color = (0.5, 0.4, 0.6)
 40
 41        popup.PopupWindow.__init__(
 42            self,
 43            position=position,
 44            size=(self._width, self._height),
 45            scale=scale,
 46            bg_color=bg_color,
 47        )
 48
 49        self._cancel_button = ba.buttonwidget(
 50            parent=self.root_widget,
 51            position=(50, self._height - 30),
 52            size=(50, 50),
 53            scale=0.5,
 54            label='',
 55            color=bg_color,
 56            on_activate_call=self._on_cancel_press,
 57            autoselect=True,
 58            icon=ba.gettexture('crossOut'),
 59            iconscale=1.2,
 60        )
 61
 62        self._title_text = ba.textwidget(
 63            parent=self.root_widget,
 64            position=(self._width * 0.5, self._height - 20),
 65            size=(0, 0),
 66            h_align='center',
 67            v_align='center',
 68            scale=0.6,
 69            text=ba.Lstr(resource='trophiesText'),
 70            maxwidth=200,
 71            color=(1, 1, 1, 0.4),
 72        )
 73
 74        self._scrollwidget = ba.scrollwidget(
 75            parent=self.root_widget,
 76            size=(self._width - 60, self._height - 70),
 77            position=(30, 30),
 78            capture_arrows=True,
 79        )
 80        ba.widget(edit=self._scrollwidget, autoselect=True)
 81
 82        ba.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 = ba.Lstr(
 93            resource='coopSelectWindow.powerRankingPointsEqualsText'
 94        ).evaluate()
 95
 96        self._subcontainer = ba.containerwidget(
 97            parent=self._scrollwidget,
 98            size=(sub_width, sub_height),
 99            background=False,
100        )
101
102        total_pts = 0
103
104        multi_txt = ba.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        ba.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=ba.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        from ba.internal 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            ba.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            ba.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            ba.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=(0.63, 0.6, 0.75)
182                if (t_count > 0)
183                else (0.6, 0.6, 0.6, 0.4),
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            ba.textwidget(
192                parent=self._subcontainer,
193                position=(sub_width * 0.88, sub_height - 20 - incr * i),
194                maxwidth=sub_width * 0.3,
195                color=(0.7, 0.8, 1.0)
196                if (t_count > 0)
197                else (0.9, 0.9, 1.0, 0.3),
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            ba.containerwidget(edit=self.root_widget, transition='out_scale')
216
217    def on_popup_cancel(self) -> None:
218        ba.playsound(ba.getsound('swish'))
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        uiscale = ba.app.ui.uiscale
 28        if scale is None:
 29            scale = (
 30                2.3
 31                if uiscale is ba.UIScale.SMALL
 32                else 1.65
 33                if uiscale is ba.UIScale.MEDIUM
 34                else 1.23
 35            )
 36        self._transitioning_out = False
 37        self._width = 300
 38        self._height = 300
 39        bg_color = (0.5, 0.4, 0.6)
 40
 41        popup.PopupWindow.__init__(
 42            self,
 43            position=position,
 44            size=(self._width, self._height),
 45            scale=scale,
 46            bg_color=bg_color,
 47        )
 48
 49        self._cancel_button = ba.buttonwidget(
 50            parent=self.root_widget,
 51            position=(50, self._height - 30),
 52            size=(50, 50),
 53            scale=0.5,
 54            label='',
 55            color=bg_color,
 56            on_activate_call=self._on_cancel_press,
 57            autoselect=True,
 58            icon=ba.gettexture('crossOut'),
 59            iconscale=1.2,
 60        )
 61
 62        self._title_text = ba.textwidget(
 63            parent=self.root_widget,
 64            position=(self._width * 0.5, self._height - 20),
 65            size=(0, 0),
 66            h_align='center',
 67            v_align='center',
 68            scale=0.6,
 69            text=ba.Lstr(resource='trophiesText'),
 70            maxwidth=200,
 71            color=(1, 1, 1, 0.4),
 72        )
 73
 74        self._scrollwidget = ba.scrollwidget(
 75            parent=self.root_widget,
 76            size=(self._width - 60, self._height - 70),
 77            position=(30, 30),
 78            capture_arrows=True,
 79        )
 80        ba.widget(edit=self._scrollwidget, autoselect=True)
 81
 82        ba.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 = ba.Lstr(
 93            resource='coopSelectWindow.powerRankingPointsEqualsText'
 94        ).evaluate()
 95
 96        self._subcontainer = ba.containerwidget(
 97            parent=self._scrollwidget,
 98            size=(sub_width, sub_height),
 99            background=False,
100        )
101
102        total_pts = 0
103
104        multi_txt = ba.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        ba.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=ba.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        )
def on_popup_cancel(self) -> None:
217    def on_popup_cancel(self) -> None:
218        ba.playsound(ba.getsound('swish'))
219        self._transition_out()

Called when the popup is canceled.

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