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

Popup window to view achievements.

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

Called when the popup is canceled.

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