bastd.ui.achievements

Provides a popup window to view achievements.

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

Popup window to view achievements.

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

Called when the popup is canceled.

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