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

Popup window to view achievements.

AchievementsWindow(position: tuple[float, float], scale: float | None = None)
 15    def __init__(
 16        self, position: tuple[float, float], scale: float | None = None
 17    ):
 18        # pylint: disable=too-many-locals
 19        assert bui.app.classic is not None
 20        uiscale = bui.app.ui_v1.uiscale
 21        if scale is None:
 22            scale = (
 23                2.3
 24                if uiscale is bui.UIScale.SMALL
 25                else 1.65
 26                if uiscale is bui.UIScale.MEDIUM
 27                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
 35            if uiscale is bui.UIScale.MEDIUM
 36            else 450
 37        )
 38        bg_color = (0.5, 0.4, 0.6)
 39
 40        # creates our _root_widget
 41        super().__init__(
 42            position=position,
 43            size=(self._width, self._height),
 44            scale=scale,
 45            bg_color=bg_color,
 46        )
 47
 48        self._cancel_button = bui.buttonwidget(
 49            parent=self.root_widget,
 50            position=(50, self._height - 30),
 51            size=(50, 50),
 52            scale=0.5,
 53            label='',
 54            color=bg_color,
 55            on_activate_call=self._on_cancel_press,
 56            autoselect=True,
 57            icon=bui.gettexture('crossOut'),
 58            iconscale=1.2,
 59        )
 60
 61        achievements = bui.app.classic.ach.achievements
 62        num_complete = len([a for a in achievements if a.complete])
 63
 64        txt_final = bui.Lstr(
 65            resource='accountSettingsWindow.achievementProgressText',
 66            subs=[
 67                ('${COUNT}', str(num_complete)),
 68                ('${TOTAL}', str(len(achievements))),
 69            ],
 70        )
 71        self._title_text = bui.textwidget(
 72            parent=self.root_widget,
 73            position=(self._width * 0.5, self._height - 20),
 74            size=(0, 0),
 75            h_align='center',
 76            v_align='center',
 77            scale=0.6,
 78            text=txt_final,
 79            maxwidth=200,
 80            color=(1, 1, 1, 0.4),
 81        )
 82
 83        self._scrollwidget = bui.scrollwidget(
 84            parent=self.root_widget,
 85            size=(self._width - 60, self._height - 70),
 86            position=(30, 30),
 87            capture_arrows=True,
 88            simple_culling_v=10,
 89        )
 90        bui.widget(edit=self._scrollwidget, autoselect=True)
 91
 92        bui.containerwidget(
 93            edit=self.root_widget, cancel_button=self._cancel_button
 94        )
 95
 96        incr = 36
 97        sub_width = self._width - 90
 98        sub_height = 40 + len(achievements) * incr
 99
100        eq_rsrc = 'coopSelectWindow.powerRankingPointsEqualsText'
101        pts_rsrc = 'coopSelectWindow.powerRankingPointsText'
102
103        self._subcontainer = bui.containerwidget(
104            parent=self._scrollwidget,
105            size=(sub_width, sub_height),
106            background=False,
107        )
108
109        total_pts = 0
110        for i, ach in enumerate(achievements):
111            complete = ach.complete
112            bui.textwidget(
113                parent=self._subcontainer,
114                position=(sub_width * 0.08 - 5, sub_height - 20 - incr * i),
115                maxwidth=20,
116                scale=0.5,
117                color=(0.6, 0.6, 0.7) if complete else (0.6, 0.6, 0.7, 0.2),
118                flatness=1.0,
119                shadow=0.0,
120                text=str(i + 1),
121                size=(0, 0),
122                h_align='right',
123                v_align='center',
124            )
125
126            bui.imagewidget(
127                parent=self._subcontainer,
128                position=(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                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=ach.description_full_complete
170                if complete
171                else ach.description_full,
172                size=(0, 0),
173                h_align='left',
174                v_align='center',
175            )
176
177            pts = ach.power_ranking_value
178            bui.textwidget(
179                parent=self._subcontainer,
180                position=(sub_width * 0.92, sub_height - 20 - incr * i),
181                maxwidth=sub_width * 0.15,
182                color=(0.7, 0.8, 1.0) if complete else (0.9, 0.9, 1.0, 0.3),
183                flatness=1.0,
184                shadow=0.0,
185                scale=0.6,
186                text=bui.Lstr(
187                    resource=pts_rsrc, subs=[('${NUMBER}', str(pts))]
188                ),
189                size=(0, 0),
190                h_align='center',
191                v_align='center',
192            )
193            if complete:
194                total_pts += pts
195
196        bui.textwidget(
197            parent=self._subcontainer,
198            position=(
199                sub_width * 1.0,
200                sub_height - 20 - incr * len(achievements),
201            ),
202            maxwidth=sub_width * 0.5,
203            scale=0.7,
204            color=(0.7, 0.8, 1.0),
205            flatness=1.0,
206            shadow=0.0,
207            text=bui.Lstr(
208                value='${A} ${B}',
209                subs=[
210                    ('${A}', bui.Lstr(resource='coopSelectWindow.totalText')),
211                    (
212                        '${B}',
213                        bui.Lstr(
214                            resource=eq_rsrc,
215                            subs=[('${NUMBER}', str(total_pts))],
216                        ),
217                    ),
218                ],
219            ),
220            size=(0, 0),
221            h_align='right',
222            v_align='center',
223        )
def on_popup_cancel(self) -> None:
233    def on_popup_cancel(self) -> None:
234        bui.getsound('swish').play()
235        self._transition_out()

Called when the popup is canceled.

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