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_extensions 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
 27                if uiscale is bui.UIScale.MEDIUM
 28                else 1.23
 29            )
 30        self._transitioning_out = False
 31        self._width = 450
 32        self._height = (
 33            300
 34            if uiscale is bui.UIScale.SMALL
 35            else 370
 36            if uiscale is bui.UIScale.MEDIUM
 37            else 450
 38        )
 39        bg_color = (0.5, 0.4, 0.6)
 40
 41        # creates our _root_widget
 42        super().__init__(
 43            position=position,
 44            size=(self._width, self._height),
 45            scale=scale,
 46            bg_color=bg_color,
 47        )
 48
 49        self._cancel_button = bui.buttonwidget(
 50            parent=self.root_widget,
 51            position=(50, self._height - 30),
 52            size=(50, 50),
 53            scale=0.5,
 54            label='',
 55            color=bg_color,
 56            on_activate_call=self._on_cancel_press,
 57            autoselect=True,
 58            icon=bui.gettexture('crossOut'),
 59            iconscale=1.2,
 60        )
 61
 62        achievements = bui.app.classic.ach.achievements
 63        num_complete = len([a for a in achievements if a.complete])
 64
 65        txt_final = bui.Lstr(
 66            resource='accountSettingsWindow.achievementProgressText',
 67            subs=[
 68                ('${COUNT}', str(num_complete)),
 69                ('${TOTAL}', str(len(achievements))),
 70            ],
 71        )
 72        self._title_text = bui.textwidget(
 73            parent=self.root_widget,
 74            position=(self._width * 0.5, self._height - 20),
 75            size=(0, 0),
 76            h_align='center',
 77            v_align='center',
 78            scale=0.6,
 79            text=txt_final,
 80            maxwidth=200,
 81            color=(1, 1, 1, 0.4),
 82        )
 83
 84        self._scrollwidget = bui.scrollwidget(
 85            parent=self.root_widget,
 86            size=(self._width - 60, self._height - 70),
 87            position=(30, 30),
 88            capture_arrows=True,
 89            simple_culling_v=10,
 90        )
 91        bui.widget(edit=self._scrollwidget, autoselect=True)
 92
 93        bui.containerwidget(
 94            edit=self.root_widget, cancel_button=self._cancel_button
 95        )
 96
 97        incr = 36
 98        sub_width = self._width - 90
 99        sub_height = 40 + len(achievements) * incr
100
101        eq_rsrc = 'coopSelectWindow.powerRankingPointsEqualsText'
102        pts_rsrc = 'coopSelectWindow.powerRankingPointsText'
103
104        self._subcontainer = bui.containerwidget(
105            parent=self._scrollwidget,
106            size=(sub_width, sub_height),
107            background=False,
108        )
109
110        total_pts = 0
111        for i, ach in enumerate(achievements):
112            complete = ach.complete
113            bui.textwidget(
114                parent=self._subcontainer,
115                position=(sub_width * 0.08 - 5, sub_height - 20 - incr * i),
116                maxwidth=20,
117                scale=0.5,
118                color=(0.6, 0.6, 0.7) if complete else (0.6, 0.6, 0.7, 0.2),
119                flatness=1.0,
120                shadow=0.0,
121                text=str(i + 1),
122                size=(0, 0),
123                h_align='right',
124                v_align='center',
125            )
126
127            bui.imagewidget(
128                parent=self._subcontainer,
129                position=(sub_width * 0.10 + 1, sub_height - 20 - incr * i - 9)
130                if complete
131                else (sub_width * 0.10 - 4, sub_height - 20 - incr * i - 14),
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=ach.description_full_complete
171                if complete
172                else ach.description_full,
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
 28                if uiscale is bui.UIScale.MEDIUM
 29                else 1.23
 30            )
 31        self._transitioning_out = False
 32        self._width = 450
 33        self._height = (
 34            300
 35            if uiscale is bui.UIScale.SMALL
 36            else 370
 37            if uiscale is bui.UIScale.MEDIUM
 38            else 450
 39        )
 40        bg_color = (0.5, 0.4, 0.6)
 41
 42        # creates our _root_widget
 43        super().__init__(
 44            position=position,
 45            size=(self._width, self._height),
 46            scale=scale,
 47            bg_color=bg_color,
 48        )
 49
 50        self._cancel_button = bui.buttonwidget(
 51            parent=self.root_widget,
 52            position=(50, self._height - 30),
 53            size=(50, 50),
 54            scale=0.5,
 55            label='',
 56            color=bg_color,
 57            on_activate_call=self._on_cancel_press,
 58            autoselect=True,
 59            icon=bui.gettexture('crossOut'),
 60            iconscale=1.2,
 61        )
 62
 63        achievements = bui.app.classic.ach.achievements
 64        num_complete = len([a for a in achievements if a.complete])
 65
 66        txt_final = bui.Lstr(
 67            resource='accountSettingsWindow.achievementProgressText',
 68            subs=[
 69                ('${COUNT}', str(num_complete)),
 70                ('${TOTAL}', str(len(achievements))),
 71            ],
 72        )
 73        self._title_text = bui.textwidget(
 74            parent=self.root_widget,
 75            position=(self._width * 0.5, self._height - 20),
 76            size=(0, 0),
 77            h_align='center',
 78            v_align='center',
 79            scale=0.6,
 80            text=txt_final,
 81            maxwidth=200,
 82            color=(1, 1, 1, 0.4),
 83        )
 84
 85        self._scrollwidget = bui.scrollwidget(
 86            parent=self.root_widget,
 87            size=(self._width - 60, self._height - 70),
 88            position=(30, 30),
 89            capture_arrows=True,
 90            simple_culling_v=10,
 91        )
 92        bui.widget(edit=self._scrollwidget, autoselect=True)
 93
 94        bui.containerwidget(
 95            edit=self.root_widget, cancel_button=self._cancel_button
 96        )
 97
 98        incr = 36
 99        sub_width = self._width - 90
100        sub_height = 40 + len(achievements) * incr
101
102        eq_rsrc = 'coopSelectWindow.powerRankingPointsEqualsText'
103        pts_rsrc = 'coopSelectWindow.powerRankingPointsText'
104
105        self._subcontainer = bui.containerwidget(
106            parent=self._scrollwidget,
107            size=(sub_width, sub_height),
108            background=False,
109        )
110
111        total_pts = 0
112        for i, ach in enumerate(achievements):
113            complete = ach.complete
114            bui.textwidget(
115                parent=self._subcontainer,
116                position=(sub_width * 0.08 - 5, sub_height - 20 - incr * i),
117                maxwidth=20,
118                scale=0.5,
119                color=(0.6, 0.6, 0.7) if complete else (0.6, 0.6, 0.7, 0.2),
120                flatness=1.0,
121                shadow=0.0,
122                text=str(i + 1),
123                size=(0, 0),
124                h_align='right',
125                v_align='center',
126            )
127
128            bui.imagewidget(
129                parent=self._subcontainer,
130                position=(sub_width * 0.10 + 1, sub_height - 20 - incr * i - 9)
131                if complete
132                else (sub_width * 0.10 - 4, sub_height - 20 - incr * i - 14),
133                size=(18, 18) if complete else (27, 27),
134                opacity=1.0 if complete else 0.3,
135                color=ach.get_icon_color(complete)[:3],
136                texture=ach.get_icon_ui_texture(complete),
137            )
138            if complete:
139                bui.imagewidget(
140                    parent=self._subcontainer,
141                    position=(
142                        sub_width * 0.10 - 4,
143                        sub_height - 25 - incr * i - 9,
144                    ),
145                    size=(28, 28),
146                    color=(2, 1.4, 0),
147                    texture=bui.gettexture('achievementOutline'),
148                )
149            bui.textwidget(
150                parent=self._subcontainer,
151                position=(sub_width * 0.19, sub_height - 19 - incr * i + 3),
152                maxwidth=sub_width * 0.62,
153                scale=0.6,
154                flatness=1.0,
155                shadow=0.0,
156                color=(1, 1, 1) if complete else (1, 1, 1, 0.2),
157                text=ach.display_name,
158                size=(0, 0),
159                h_align='left',
160                v_align='center',
161            )
162
163            bui.textwidget(
164                parent=self._subcontainer,
165                position=(sub_width * 0.19, sub_height - 19 - incr * i - 10),
166                maxwidth=sub_width * 0.62,
167                scale=0.4,
168                flatness=1.0,
169                shadow=0.0,
170                color=(0.83, 0.8, 0.85) if complete else (0.8, 0.8, 0.8, 0.2),
171                text=ach.description_full_complete
172                if complete
173                else ach.description_full,
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
 28                if uiscale is bui.UIScale.MEDIUM
 29                else 1.23
 30            )
 31        self._transitioning_out = False
 32        self._width = 450
 33        self._height = (
 34            300
 35            if uiscale is bui.UIScale.SMALL
 36            else 370
 37            if uiscale is bui.UIScale.MEDIUM
 38            else 450
 39        )
 40        bg_color = (0.5, 0.4, 0.6)
 41
 42        # creates our _root_widget
 43        super().__init__(
 44            position=position,
 45            size=(self._width, self._height),
 46            scale=scale,
 47            bg_color=bg_color,
 48        )
 49
 50        self._cancel_button = bui.buttonwidget(
 51            parent=self.root_widget,
 52            position=(50, self._height - 30),
 53            size=(50, 50),
 54            scale=0.5,
 55            label='',
 56            color=bg_color,
 57            on_activate_call=self._on_cancel_press,
 58            autoselect=True,
 59            icon=bui.gettexture('crossOut'),
 60            iconscale=1.2,
 61        )
 62
 63        achievements = bui.app.classic.ach.achievements
 64        num_complete = len([a for a in achievements if a.complete])
 65
 66        txt_final = bui.Lstr(
 67            resource='accountSettingsWindow.achievementProgressText',
 68            subs=[
 69                ('${COUNT}', str(num_complete)),
 70                ('${TOTAL}', str(len(achievements))),
 71            ],
 72        )
 73        self._title_text = bui.textwidget(
 74            parent=self.root_widget,
 75            position=(self._width * 0.5, self._height - 20),
 76            size=(0, 0),
 77            h_align='center',
 78            v_align='center',
 79            scale=0.6,
 80            text=txt_final,
 81            maxwidth=200,
 82            color=(1, 1, 1, 0.4),
 83        )
 84
 85        self._scrollwidget = bui.scrollwidget(
 86            parent=self.root_widget,
 87            size=(self._width - 60, self._height - 70),
 88            position=(30, 30),
 89            capture_arrows=True,
 90            simple_culling_v=10,
 91        )
 92        bui.widget(edit=self._scrollwidget, autoselect=True)
 93
 94        bui.containerwidget(
 95            edit=self.root_widget, cancel_button=self._cancel_button
 96        )
 97
 98        incr = 36
 99        sub_width = self._width - 90
100        sub_height = 40 + len(achievements) * incr
101
102        eq_rsrc = 'coopSelectWindow.powerRankingPointsEqualsText'
103        pts_rsrc = 'coopSelectWindow.powerRankingPointsText'
104
105        self._subcontainer = bui.containerwidget(
106            parent=self._scrollwidget,
107            size=(sub_width, sub_height),
108            background=False,
109        )
110
111        total_pts = 0
112        for i, ach in enumerate(achievements):
113            complete = ach.complete
114            bui.textwidget(
115                parent=self._subcontainer,
116                position=(sub_width * 0.08 - 5, sub_height - 20 - incr * i),
117                maxwidth=20,
118                scale=0.5,
119                color=(0.6, 0.6, 0.7) if complete else (0.6, 0.6, 0.7, 0.2),
120                flatness=1.0,
121                shadow=0.0,
122                text=str(i + 1),
123                size=(0, 0),
124                h_align='right',
125                v_align='center',
126            )
127
128            bui.imagewidget(
129                parent=self._subcontainer,
130                position=(sub_width * 0.10 + 1, sub_height - 20 - incr * i - 9)
131                if complete
132                else (sub_width * 0.10 - 4, sub_height - 20 - incr * i - 14),
133                size=(18, 18) if complete else (27, 27),
134                opacity=1.0 if complete else 0.3,
135                color=ach.get_icon_color(complete)[:3],
136                texture=ach.get_icon_ui_texture(complete),
137            )
138            if complete:
139                bui.imagewidget(
140                    parent=self._subcontainer,
141                    position=(
142                        sub_width * 0.10 - 4,
143                        sub_height - 25 - incr * i - 9,
144                    ),
145                    size=(28, 28),
146                    color=(2, 1.4, 0),
147                    texture=bui.gettexture('achievementOutline'),
148                )
149            bui.textwidget(
150                parent=self._subcontainer,
151                position=(sub_width * 0.19, sub_height - 19 - incr * i + 3),
152                maxwidth=sub_width * 0.62,
153                scale=0.6,
154                flatness=1.0,
155                shadow=0.0,
156                color=(1, 1, 1) if complete else (1, 1, 1, 0.2),
157                text=ach.display_name,
158                size=(0, 0),
159                h_align='left',
160                v_align='center',
161            )
162
163            bui.textwidget(
164                parent=self._subcontainer,
165                position=(sub_width * 0.19, sub_height - 19 - incr * i - 10),
166                maxwidth=sub_width * 0.62,
167                scale=0.4,
168                flatness=1.0,
169                shadow=0.0,
170                color=(0.83, 0.8, 0.85) if complete else (0.8, 0.8, 0.8, 0.2),
171                text=ach.description_full_complete
172                if complete
173                else ach.description_full,
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.