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

Called when the popup is canceled.

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