bastd.ui.achievements

Provides a popup window to view achievements.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""Provides a popup window to view achievements."""
  4
  5from __future__ import annotations
  6
  7from typing import TYPE_CHECKING
  8
  9import ba
 10from bastd.ui import popup
 11
 12if TYPE_CHECKING:
 13    pass
 14
 15
 16class AchievementsWindow(popup.PopupWindow):
 17    """Popup window to view achievements."""
 18
 19    def __init__(
 20        self, position: tuple[float, float], scale: float | None = None
 21    ):
 22        # pylint: disable=too-many-locals
 23        uiscale = ba.app.ui.uiscale
 24        if scale is None:
 25            scale = (
 26                2.3
 27                if uiscale is ba.UIScale.SMALL
 28                else 1.65
 29                if uiscale is ba.UIScale.MEDIUM
 30                else 1.23
 31            )
 32        self._transitioning_out = False
 33        self._width = 450
 34        self._height = (
 35            300
 36            if uiscale is ba.UIScale.SMALL
 37            else 370
 38            if uiscale is ba.UIScale.MEDIUM
 39            else 450
 40        )
 41        bg_color = (0.5, 0.4, 0.6)
 42
 43        # creates our _root_widget
 44        popup.PopupWindow.__init__(
 45            self,
 46            position=position,
 47            size=(self._width, self._height),
 48            scale=scale,
 49            bg_color=bg_color,
 50        )
 51
 52        self._cancel_button = ba.buttonwidget(
 53            parent=self.root_widget,
 54            position=(50, self._height - 30),
 55            size=(50, 50),
 56            scale=0.5,
 57            label='',
 58            color=bg_color,
 59            on_activate_call=self._on_cancel_press,
 60            autoselect=True,
 61            icon=ba.gettexture('crossOut'),
 62            iconscale=1.2,
 63        )
 64
 65        achievements = ba.app.ach.achievements
 66        num_complete = len([a for a in achievements if a.complete])
 67
 68        txt_final = ba.Lstr(
 69            resource='accountSettingsWindow.achievementProgressText',
 70            subs=[
 71                ('${COUNT}', str(num_complete)),
 72                ('${TOTAL}', str(len(achievements))),
 73            ],
 74        )
 75        self._title_text = ba.textwidget(
 76            parent=self.root_widget,
 77            position=(self._width * 0.5, self._height - 20),
 78            size=(0, 0),
 79            h_align='center',
 80            v_align='center',
 81            scale=0.6,
 82            text=txt_final,
 83            maxwidth=200,
 84            color=(1, 1, 1, 0.4),
 85        )
 86
 87        self._scrollwidget = ba.scrollwidget(
 88            parent=self.root_widget,
 89            size=(self._width - 60, self._height - 70),
 90            position=(30, 30),
 91            capture_arrows=True,
 92            simple_culling_v=10,
 93        )
 94        ba.widget(edit=self._scrollwidget, autoselect=True)
 95
 96        ba.containerwidget(
 97            edit=self.root_widget, cancel_button=self._cancel_button
 98        )
 99
100        incr = 36
101        sub_width = self._width - 90
102        sub_height = 40 + len(achievements) * incr
103
104        eq_rsrc = 'coopSelectWindow.powerRankingPointsEqualsText'
105        pts_rsrc = 'coopSelectWindow.powerRankingPointsText'
106
107        self._subcontainer = ba.containerwidget(
108            parent=self._scrollwidget,
109            size=(sub_width, sub_height),
110            background=False,
111        )
112
113        total_pts = 0
114        for i, ach in enumerate(achievements):
115            complete = ach.complete
116            ba.textwidget(
117                parent=self._subcontainer,
118                position=(sub_width * 0.08 - 5, sub_height - 20 - incr * i),
119                maxwidth=20,
120                scale=0.5,
121                color=(0.6, 0.6, 0.7) if complete else (0.6, 0.6, 0.7, 0.2),
122                flatness=1.0,
123                shadow=0.0,
124                text=str(i + 1),
125                size=(0, 0),
126                h_align='right',
127                v_align='center',
128            )
129
130            ba.imagewidget(
131                parent=self._subcontainer,
132                position=(sub_width * 0.10 + 1, sub_height - 20 - incr * i - 9)
133                if complete
134                else (sub_width * 0.10 - 4, sub_height - 20 - incr * i - 14),
135                size=(18, 18) if complete else (27, 27),
136                opacity=1.0 if complete else 0.3,
137                color=ach.get_icon_color(complete)[:3],
138                texture=ach.get_icon_texture(complete),
139            )
140            if complete:
141                ba.imagewidget(
142                    parent=self._subcontainer,
143                    position=(
144                        sub_width * 0.10 - 4,
145                        sub_height - 25 - incr * i - 9,
146                    ),
147                    size=(28, 28),
148                    color=(2, 1.4, 0),
149                    texture=ba.gettexture('achievementOutline'),
150                )
151            ba.textwidget(
152                parent=self._subcontainer,
153                position=(sub_width * 0.19, sub_height - 19 - incr * i + 3),
154                maxwidth=sub_width * 0.62,
155                scale=0.6,
156                flatness=1.0,
157                shadow=0.0,
158                color=(1, 1, 1) if complete else (1, 1, 1, 0.2),
159                text=ach.display_name,
160                size=(0, 0),
161                h_align='left',
162                v_align='center',
163            )
164
165            ba.textwidget(
166                parent=self._subcontainer,
167                position=(sub_width * 0.19, sub_height - 19 - incr * i - 10),
168                maxwidth=sub_width * 0.62,
169                scale=0.4,
170                flatness=1.0,
171                shadow=0.0,
172                color=(0.83, 0.8, 0.85) if complete else (0.8, 0.8, 0.8, 0.2),
173                text=ach.description_full_complete
174                if complete
175                else ach.description_full,
176                size=(0, 0),
177                h_align='left',
178                v_align='center',
179            )
180
181            pts = ach.power_ranking_value
182            ba.textwidget(
183                parent=self._subcontainer,
184                position=(sub_width * 0.92, sub_height - 20 - incr * i),
185                maxwidth=sub_width * 0.15,
186                color=(0.7, 0.8, 1.0) if complete else (0.9, 0.9, 1.0, 0.3),
187                flatness=1.0,
188                shadow=0.0,
189                scale=0.6,
190                text=ba.Lstr(resource=pts_rsrc, subs=[('${NUMBER}', str(pts))]),
191                size=(0, 0),
192                h_align='center',
193                v_align='center',
194            )
195            if complete:
196                total_pts += pts
197
198        ba.textwidget(
199            parent=self._subcontainer,
200            position=(
201                sub_width * 1.0,
202                sub_height - 20 - incr * len(achievements),
203            ),
204            maxwidth=sub_width * 0.5,
205            scale=0.7,
206            color=(0.7, 0.8, 1.0),
207            flatness=1.0,
208            shadow=0.0,
209            text=ba.Lstr(
210                value='${A} ${B}',
211                subs=[
212                    ('${A}', ba.Lstr(resource='coopSelectWindow.totalText')),
213                    (
214                        '${B}',
215                        ba.Lstr(
216                            resource=eq_rsrc,
217                            subs=[('${NUMBER}', str(total_pts))],
218                        ),
219                    ),
220                ],
221            ),
222            size=(0, 0),
223            h_align='right',
224            v_align='center',
225        )
226
227    def _on_cancel_press(self) -> None:
228        self._transition_out()
229
230    def _transition_out(self) -> None:
231        if not self._transitioning_out:
232            self._transitioning_out = True
233            ba.containerwidget(edit=self.root_widget, transition='out_scale')
234
235    def on_popup_cancel(self) -> None:
236        ba.playsound(ba.getsound('swish'))
237        self._transition_out()
class AchievementsWindow(bastd.ui.popup.PopupWindow):
 17class AchievementsWindow(popup.PopupWindow):
 18    """Popup window to view achievements."""
 19
 20    def __init__(
 21        self, position: tuple[float, float], scale: float | None = None
 22    ):
 23        # pylint: disable=too-many-locals
 24        uiscale = ba.app.ui.uiscale
 25        if scale is None:
 26            scale = (
 27                2.3
 28                if uiscale is ba.UIScale.SMALL
 29                else 1.65
 30                if uiscale is ba.UIScale.MEDIUM
 31                else 1.23
 32            )
 33        self._transitioning_out = False
 34        self._width = 450
 35        self._height = (
 36            300
 37            if uiscale is ba.UIScale.SMALL
 38            else 370
 39            if uiscale is ba.UIScale.MEDIUM
 40            else 450
 41        )
 42        bg_color = (0.5, 0.4, 0.6)
 43
 44        # creates our _root_widget
 45        popup.PopupWindow.__init__(
 46            self,
 47            position=position,
 48            size=(self._width, self._height),
 49            scale=scale,
 50            bg_color=bg_color,
 51        )
 52
 53        self._cancel_button = ba.buttonwidget(
 54            parent=self.root_widget,
 55            position=(50, self._height - 30),
 56            size=(50, 50),
 57            scale=0.5,
 58            label='',
 59            color=bg_color,
 60            on_activate_call=self._on_cancel_press,
 61            autoselect=True,
 62            icon=ba.gettexture('crossOut'),
 63            iconscale=1.2,
 64        )
 65
 66        achievements = ba.app.ach.achievements
 67        num_complete = len([a for a in achievements if a.complete])
 68
 69        txt_final = ba.Lstr(
 70            resource='accountSettingsWindow.achievementProgressText',
 71            subs=[
 72                ('${COUNT}', str(num_complete)),
 73                ('${TOTAL}', str(len(achievements))),
 74            ],
 75        )
 76        self._title_text = ba.textwidget(
 77            parent=self.root_widget,
 78            position=(self._width * 0.5, self._height - 20),
 79            size=(0, 0),
 80            h_align='center',
 81            v_align='center',
 82            scale=0.6,
 83            text=txt_final,
 84            maxwidth=200,
 85            color=(1, 1, 1, 0.4),
 86        )
 87
 88        self._scrollwidget = ba.scrollwidget(
 89            parent=self.root_widget,
 90            size=(self._width - 60, self._height - 70),
 91            position=(30, 30),
 92            capture_arrows=True,
 93            simple_culling_v=10,
 94        )
 95        ba.widget(edit=self._scrollwidget, autoselect=True)
 96
 97        ba.containerwidget(
 98            edit=self.root_widget, cancel_button=self._cancel_button
 99        )
100
101        incr = 36
102        sub_width = self._width - 90
103        sub_height = 40 + len(achievements) * incr
104
105        eq_rsrc = 'coopSelectWindow.powerRankingPointsEqualsText'
106        pts_rsrc = 'coopSelectWindow.powerRankingPointsText'
107
108        self._subcontainer = ba.containerwidget(
109            parent=self._scrollwidget,
110            size=(sub_width, sub_height),
111            background=False,
112        )
113
114        total_pts = 0
115        for i, ach in enumerate(achievements):
116            complete = ach.complete
117            ba.textwidget(
118                parent=self._subcontainer,
119                position=(sub_width * 0.08 - 5, sub_height - 20 - incr * i),
120                maxwidth=20,
121                scale=0.5,
122                color=(0.6, 0.6, 0.7) if complete else (0.6, 0.6, 0.7, 0.2),
123                flatness=1.0,
124                shadow=0.0,
125                text=str(i + 1),
126                size=(0, 0),
127                h_align='right',
128                v_align='center',
129            )
130
131            ba.imagewidget(
132                parent=self._subcontainer,
133                position=(sub_width * 0.10 + 1, sub_height - 20 - incr * i - 9)
134                if complete
135                else (sub_width * 0.10 - 4, sub_height - 20 - incr * i - 14),
136                size=(18, 18) if complete else (27, 27),
137                opacity=1.0 if complete else 0.3,
138                color=ach.get_icon_color(complete)[:3],
139                texture=ach.get_icon_texture(complete),
140            )
141            if complete:
142                ba.imagewidget(
143                    parent=self._subcontainer,
144                    position=(
145                        sub_width * 0.10 - 4,
146                        sub_height - 25 - incr * i - 9,
147                    ),
148                    size=(28, 28),
149                    color=(2, 1.4, 0),
150                    texture=ba.gettexture('achievementOutline'),
151                )
152            ba.textwidget(
153                parent=self._subcontainer,
154                position=(sub_width * 0.19, sub_height - 19 - incr * i + 3),
155                maxwidth=sub_width * 0.62,
156                scale=0.6,
157                flatness=1.0,
158                shadow=0.0,
159                color=(1, 1, 1) if complete else (1, 1, 1, 0.2),
160                text=ach.display_name,
161                size=(0, 0),
162                h_align='left',
163                v_align='center',
164            )
165
166            ba.textwidget(
167                parent=self._subcontainer,
168                position=(sub_width * 0.19, sub_height - 19 - incr * i - 10),
169                maxwidth=sub_width * 0.62,
170                scale=0.4,
171                flatness=1.0,
172                shadow=0.0,
173                color=(0.83, 0.8, 0.85) if complete else (0.8, 0.8, 0.8, 0.2),
174                text=ach.description_full_complete
175                if complete
176                else ach.description_full,
177                size=(0, 0),
178                h_align='left',
179                v_align='center',
180            )
181
182            pts = ach.power_ranking_value
183            ba.textwidget(
184                parent=self._subcontainer,
185                position=(sub_width * 0.92, sub_height - 20 - incr * i),
186                maxwidth=sub_width * 0.15,
187                color=(0.7, 0.8, 1.0) if complete else (0.9, 0.9, 1.0, 0.3),
188                flatness=1.0,
189                shadow=0.0,
190                scale=0.6,
191                text=ba.Lstr(resource=pts_rsrc, subs=[('${NUMBER}', str(pts))]),
192                size=(0, 0),
193                h_align='center',
194                v_align='center',
195            )
196            if complete:
197                total_pts += pts
198
199        ba.textwidget(
200            parent=self._subcontainer,
201            position=(
202                sub_width * 1.0,
203                sub_height - 20 - incr * len(achievements),
204            ),
205            maxwidth=sub_width * 0.5,
206            scale=0.7,
207            color=(0.7, 0.8, 1.0),
208            flatness=1.0,
209            shadow=0.0,
210            text=ba.Lstr(
211                value='${A} ${B}',
212                subs=[
213                    ('${A}', ba.Lstr(resource='coopSelectWindow.totalText')),
214                    (
215                        '${B}',
216                        ba.Lstr(
217                            resource=eq_rsrc,
218                            subs=[('${NUMBER}', str(total_pts))],
219                        ),
220                    ),
221                ],
222            ),
223            size=(0, 0),
224            h_align='right',
225            v_align='center',
226        )
227
228    def _on_cancel_press(self) -> None:
229        self._transition_out()
230
231    def _transition_out(self) -> None:
232        if not self._transitioning_out:
233            self._transitioning_out = True
234            ba.containerwidget(edit=self.root_widget, transition='out_scale')
235
236    def on_popup_cancel(self) -> None:
237        ba.playsound(ba.getsound('swish'))
238        self._transition_out()

Popup window to view achievements.

AchievementsWindow(position: tuple[float, float], scale: float | None = None)
 20    def __init__(
 21        self, position: tuple[float, float], scale: float | None = None
 22    ):
 23        # pylint: disable=too-many-locals
 24        uiscale = ba.app.ui.uiscale
 25        if scale is None:
 26            scale = (
 27                2.3
 28                if uiscale is ba.UIScale.SMALL
 29                else 1.65
 30                if uiscale is ba.UIScale.MEDIUM
 31                else 1.23
 32            )
 33        self._transitioning_out = False
 34        self._width = 450
 35        self._height = (
 36            300
 37            if uiscale is ba.UIScale.SMALL
 38            else 370
 39            if uiscale is ba.UIScale.MEDIUM
 40            else 450
 41        )
 42        bg_color = (0.5, 0.4, 0.6)
 43
 44        # creates our _root_widget
 45        popup.PopupWindow.__init__(
 46            self,
 47            position=position,
 48            size=(self._width, self._height),
 49            scale=scale,
 50            bg_color=bg_color,
 51        )
 52
 53        self._cancel_button = ba.buttonwidget(
 54            parent=self.root_widget,
 55            position=(50, self._height - 30),
 56            size=(50, 50),
 57            scale=0.5,
 58            label='',
 59            color=bg_color,
 60            on_activate_call=self._on_cancel_press,
 61            autoselect=True,
 62            icon=ba.gettexture('crossOut'),
 63            iconscale=1.2,
 64        )
 65
 66        achievements = ba.app.ach.achievements
 67        num_complete = len([a for a in achievements if a.complete])
 68
 69        txt_final = ba.Lstr(
 70            resource='accountSettingsWindow.achievementProgressText',
 71            subs=[
 72                ('${COUNT}', str(num_complete)),
 73                ('${TOTAL}', str(len(achievements))),
 74            ],
 75        )
 76        self._title_text = ba.textwidget(
 77            parent=self.root_widget,
 78            position=(self._width * 0.5, self._height - 20),
 79            size=(0, 0),
 80            h_align='center',
 81            v_align='center',
 82            scale=0.6,
 83            text=txt_final,
 84            maxwidth=200,
 85            color=(1, 1, 1, 0.4),
 86        )
 87
 88        self._scrollwidget = ba.scrollwidget(
 89            parent=self.root_widget,
 90            size=(self._width - 60, self._height - 70),
 91            position=(30, 30),
 92            capture_arrows=True,
 93            simple_culling_v=10,
 94        )
 95        ba.widget(edit=self._scrollwidget, autoselect=True)
 96
 97        ba.containerwidget(
 98            edit=self.root_widget, cancel_button=self._cancel_button
 99        )
100
101        incr = 36
102        sub_width = self._width - 90
103        sub_height = 40 + len(achievements) * incr
104
105        eq_rsrc = 'coopSelectWindow.powerRankingPointsEqualsText'
106        pts_rsrc = 'coopSelectWindow.powerRankingPointsText'
107
108        self._subcontainer = ba.containerwidget(
109            parent=self._scrollwidget,
110            size=(sub_width, sub_height),
111            background=False,
112        )
113
114        total_pts = 0
115        for i, ach in enumerate(achievements):
116            complete = ach.complete
117            ba.textwidget(
118                parent=self._subcontainer,
119                position=(sub_width * 0.08 - 5, sub_height - 20 - incr * i),
120                maxwidth=20,
121                scale=0.5,
122                color=(0.6, 0.6, 0.7) if complete else (0.6, 0.6, 0.7, 0.2),
123                flatness=1.0,
124                shadow=0.0,
125                text=str(i + 1),
126                size=(0, 0),
127                h_align='right',
128                v_align='center',
129            )
130
131            ba.imagewidget(
132                parent=self._subcontainer,
133                position=(sub_width * 0.10 + 1, sub_height - 20 - incr * i - 9)
134                if complete
135                else (sub_width * 0.10 - 4, sub_height - 20 - incr * i - 14),
136                size=(18, 18) if complete else (27, 27),
137                opacity=1.0 if complete else 0.3,
138                color=ach.get_icon_color(complete)[:3],
139                texture=ach.get_icon_texture(complete),
140            )
141            if complete:
142                ba.imagewidget(
143                    parent=self._subcontainer,
144                    position=(
145                        sub_width * 0.10 - 4,
146                        sub_height - 25 - incr * i - 9,
147                    ),
148                    size=(28, 28),
149                    color=(2, 1.4, 0),
150                    texture=ba.gettexture('achievementOutline'),
151                )
152            ba.textwidget(
153                parent=self._subcontainer,
154                position=(sub_width * 0.19, sub_height - 19 - incr * i + 3),
155                maxwidth=sub_width * 0.62,
156                scale=0.6,
157                flatness=1.0,
158                shadow=0.0,
159                color=(1, 1, 1) if complete else (1, 1, 1, 0.2),
160                text=ach.display_name,
161                size=(0, 0),
162                h_align='left',
163                v_align='center',
164            )
165
166            ba.textwidget(
167                parent=self._subcontainer,
168                position=(sub_width * 0.19, sub_height - 19 - incr * i - 10),
169                maxwidth=sub_width * 0.62,
170                scale=0.4,
171                flatness=1.0,
172                shadow=0.0,
173                color=(0.83, 0.8, 0.85) if complete else (0.8, 0.8, 0.8, 0.2),
174                text=ach.description_full_complete
175                if complete
176                else ach.description_full,
177                size=(0, 0),
178                h_align='left',
179                v_align='center',
180            )
181
182            pts = ach.power_ranking_value
183            ba.textwidget(
184                parent=self._subcontainer,
185                position=(sub_width * 0.92, sub_height - 20 - incr * i),
186                maxwidth=sub_width * 0.15,
187                color=(0.7, 0.8, 1.0) if complete else (0.9, 0.9, 1.0, 0.3),
188                flatness=1.0,
189                shadow=0.0,
190                scale=0.6,
191                text=ba.Lstr(resource=pts_rsrc, subs=[('${NUMBER}', str(pts))]),
192                size=(0, 0),
193                h_align='center',
194                v_align='center',
195            )
196            if complete:
197                total_pts += pts
198
199        ba.textwidget(
200            parent=self._subcontainer,
201            position=(
202                sub_width * 1.0,
203                sub_height - 20 - incr * len(achievements),
204            ),
205            maxwidth=sub_width * 0.5,
206            scale=0.7,
207            color=(0.7, 0.8, 1.0),
208            flatness=1.0,
209            shadow=0.0,
210            text=ba.Lstr(
211                value='${A} ${B}',
212                subs=[
213                    ('${A}', ba.Lstr(resource='coopSelectWindow.totalText')),
214                    (
215                        '${B}',
216                        ba.Lstr(
217                            resource=eq_rsrc,
218                            subs=[('${NUMBER}', str(total_pts))],
219                        ),
220                    ),
221                ],
222            ),
223            size=(0, 0),
224            h_align='right',
225            v_align='center',
226        )
def on_popup_cancel(self) -> None:
236    def on_popup_cancel(self) -> None:
237        ba.playsound(ba.getsound('swish'))
238        self._transition_out()

Called when the popup is canceled.

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