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

Called when the popup is canceled.

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