bastd.ui.account.link

UI functionality for linking accounts.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""UI functionality for linking accounts."""
  4
  5from __future__ import annotations
  6
  7import copy
  8import time
  9from typing import TYPE_CHECKING
 10
 11import _ba
 12import ba
 13
 14if TYPE_CHECKING:
 15    from typing import Any
 16
 17
 18class AccountLinkWindow(ba.Window):
 19    """Window for linking accounts."""
 20
 21    def __init__(self, origin_widget: ba.Widget | None = None):
 22        scale_origin: tuple[float, float] | None
 23        if origin_widget is not None:
 24            self._transition_out = 'out_scale'
 25            scale_origin = origin_widget.get_screen_space_center()
 26            transition = 'in_scale'
 27        else:
 28            self._transition_out = 'out_right'
 29            scale_origin = None
 30            transition = 'in_right'
 31        bg_color = (0.4, 0.4, 0.5)
 32        self._width = 560
 33        self._height = 420
 34        uiscale = ba.app.ui.uiscale
 35        base_scale = (1.65 if uiscale is ba.UIScale.SMALL else
 36                      1.5 if uiscale is ba.UIScale.MEDIUM else 1.1)
 37        super().__init__(root_widget=ba.containerwidget(
 38            size=(self._width, self._height),
 39            transition=transition,
 40            scale=base_scale,
 41            scale_origin_stack_offset=scale_origin,
 42            stack_offset=(0, -10) if uiscale is ba.UIScale.SMALL else (0, 0)))
 43        self._cancel_button = ba.buttonwidget(parent=self._root_widget,
 44                                              position=(40, self._height - 45),
 45                                              size=(50, 50),
 46                                              scale=0.7,
 47                                              label='',
 48                                              color=bg_color,
 49                                              on_activate_call=self._cancel,
 50                                              autoselect=True,
 51                                              icon=ba.gettexture('crossOut'),
 52                                              iconscale=1.2)
 53        maxlinks = _ba.get_v1_account_misc_read_val('maxLinkAccounts', 5)
 54        ba.textwidget(
 55            parent=self._root_widget,
 56            position=(self._width * 0.5, self._height * 0.56),
 57            size=(0, 0),
 58            text=ba.Lstr(resource=(
 59                'accountSettingsWindow.linkAccountsInstructionsNewText'),
 60                         subs=[('${COUNT}', str(maxlinks))]),
 61            maxwidth=self._width * 0.9,
 62            color=ba.app.ui.infotextcolor,
 63            max_height=self._height * 0.6,
 64            h_align='center',
 65            v_align='center')
 66        ba.containerwidget(edit=self._root_widget,
 67                           cancel_button=self._cancel_button)
 68        ba.buttonwidget(
 69            parent=self._root_widget,
 70            position=(40, 30),
 71            size=(200, 60),
 72            label=ba.Lstr(
 73                resource='accountSettingsWindow.linkAccountsGenerateCodeText'),
 74            autoselect=True,
 75            on_activate_call=self._generate_press)
 76        self._enter_code_button = ba.buttonwidget(
 77            parent=self._root_widget,
 78            position=(self._width - 240, 30),
 79            size=(200, 60),
 80            label=ba.Lstr(
 81                resource='accountSettingsWindow.linkAccountsEnterCodeText'),
 82            autoselect=True,
 83            on_activate_call=self._enter_code_press)
 84
 85    def _generate_press(self) -> None:
 86        from bastd.ui import account
 87        if _ba.get_v1_account_state() != 'signed_in':
 88            account.show_sign_in_prompt()
 89            return
 90        ba.screenmessage(
 91            ba.Lstr(resource='gatherWindow.requestingAPromoCodeText'),
 92            color=(0, 1, 0))
 93        _ba.add_transaction({
 94            'type': 'ACCOUNT_LINK_CODE_REQUEST',
 95            'expire_time': time.time() + 5
 96        })
 97        _ba.run_transactions()
 98
 99    def _enter_code_press(self) -> None:
100        from bastd.ui import promocode
101        promocode.PromoCodeWindow(modal=True,
102                                  origin_widget=self._enter_code_button)
103        ba.containerwidget(edit=self._root_widget,
104                           transition=self._transition_out)
105
106    def _cancel(self) -> None:
107        ba.containerwidget(edit=self._root_widget,
108                           transition=self._transition_out)
109
110
111class AccountLinkCodeWindow(ba.Window):
112    """Window showing code for account-linking."""
113
114    def __init__(self, data: dict[str, Any]):
115        self._width = 350
116        self._height = 200
117        uiscale = ba.app.ui.uiscale
118        super().__init__(root_widget=ba.containerwidget(
119            size=(self._width, self._height),
120            color=(0.45, 0.63, 0.15),
121            transition='in_scale',
122            scale=(1.8 if uiscale is ba.UIScale.SMALL else
123                   1.35 if uiscale is ba.UIScale.MEDIUM else 1.0)))
124        self._data = copy.deepcopy(data)
125        ba.playsound(ba.getsound('cashRegister'))
126        ba.playsound(ba.getsound('swish'))
127        self._cancel_button = ba.buttonwidget(parent=self._root_widget,
128                                              scale=0.5,
129                                              position=(40, self._height - 40),
130                                              size=(50, 50),
131                                              label='',
132                                              on_activate_call=self.close,
133                                              autoselect=True,
134                                              color=(0.45, 0.63, 0.15),
135                                              icon=ba.gettexture('crossOut'),
136                                              iconscale=1.2)
137        ba.containerwidget(edit=self._root_widget,
138                           cancel_button=self._cancel_button)
139        ba.textwidget(parent=self._root_widget,
140                      position=(self._width * 0.5, self._height * 0.5),
141                      size=(0, 0),
142                      color=(1.0, 3.0, 1.0),
143                      scale=2.0,
144                      h_align='center',
145                      v_align='center',
146                      text=data['code'],
147                      maxwidth=self._width * 0.85)
148
149    def close(self) -> None:
150        """close the window"""
151        ba.containerwidget(edit=self._root_widget, transition='out_scale')
class AccountLinkWindow(ba.ui.Window):
 19class AccountLinkWindow(ba.Window):
 20    """Window for linking accounts."""
 21
 22    def __init__(self, origin_widget: ba.Widget | None = None):
 23        scale_origin: tuple[float, float] | None
 24        if origin_widget is not None:
 25            self._transition_out = 'out_scale'
 26            scale_origin = origin_widget.get_screen_space_center()
 27            transition = 'in_scale'
 28        else:
 29            self._transition_out = 'out_right'
 30            scale_origin = None
 31            transition = 'in_right'
 32        bg_color = (0.4, 0.4, 0.5)
 33        self._width = 560
 34        self._height = 420
 35        uiscale = ba.app.ui.uiscale
 36        base_scale = (1.65 if uiscale is ba.UIScale.SMALL else
 37                      1.5 if uiscale is ba.UIScale.MEDIUM else 1.1)
 38        super().__init__(root_widget=ba.containerwidget(
 39            size=(self._width, self._height),
 40            transition=transition,
 41            scale=base_scale,
 42            scale_origin_stack_offset=scale_origin,
 43            stack_offset=(0, -10) if uiscale is ba.UIScale.SMALL else (0, 0)))
 44        self._cancel_button = ba.buttonwidget(parent=self._root_widget,
 45                                              position=(40, self._height - 45),
 46                                              size=(50, 50),
 47                                              scale=0.7,
 48                                              label='',
 49                                              color=bg_color,
 50                                              on_activate_call=self._cancel,
 51                                              autoselect=True,
 52                                              icon=ba.gettexture('crossOut'),
 53                                              iconscale=1.2)
 54        maxlinks = _ba.get_v1_account_misc_read_val('maxLinkAccounts', 5)
 55        ba.textwidget(
 56            parent=self._root_widget,
 57            position=(self._width * 0.5, self._height * 0.56),
 58            size=(0, 0),
 59            text=ba.Lstr(resource=(
 60                'accountSettingsWindow.linkAccountsInstructionsNewText'),
 61                         subs=[('${COUNT}', str(maxlinks))]),
 62            maxwidth=self._width * 0.9,
 63            color=ba.app.ui.infotextcolor,
 64            max_height=self._height * 0.6,
 65            h_align='center',
 66            v_align='center')
 67        ba.containerwidget(edit=self._root_widget,
 68                           cancel_button=self._cancel_button)
 69        ba.buttonwidget(
 70            parent=self._root_widget,
 71            position=(40, 30),
 72            size=(200, 60),
 73            label=ba.Lstr(
 74                resource='accountSettingsWindow.linkAccountsGenerateCodeText'),
 75            autoselect=True,
 76            on_activate_call=self._generate_press)
 77        self._enter_code_button = ba.buttonwidget(
 78            parent=self._root_widget,
 79            position=(self._width - 240, 30),
 80            size=(200, 60),
 81            label=ba.Lstr(
 82                resource='accountSettingsWindow.linkAccountsEnterCodeText'),
 83            autoselect=True,
 84            on_activate_call=self._enter_code_press)
 85
 86    def _generate_press(self) -> None:
 87        from bastd.ui import account
 88        if _ba.get_v1_account_state() != 'signed_in':
 89            account.show_sign_in_prompt()
 90            return
 91        ba.screenmessage(
 92            ba.Lstr(resource='gatherWindow.requestingAPromoCodeText'),
 93            color=(0, 1, 0))
 94        _ba.add_transaction({
 95            'type': 'ACCOUNT_LINK_CODE_REQUEST',
 96            'expire_time': time.time() + 5
 97        })
 98        _ba.run_transactions()
 99
100    def _enter_code_press(self) -> None:
101        from bastd.ui import promocode
102        promocode.PromoCodeWindow(modal=True,
103                                  origin_widget=self._enter_code_button)
104        ba.containerwidget(edit=self._root_widget,
105                           transition=self._transition_out)
106
107    def _cancel(self) -> None:
108        ba.containerwidget(edit=self._root_widget,
109                           transition=self._transition_out)

Window for linking accounts.

AccountLinkWindow(origin_widget: _ba.Widget | None = None)
22    def __init__(self, origin_widget: ba.Widget | None = None):
23        scale_origin: tuple[float, float] | None
24        if origin_widget is not None:
25            self._transition_out = 'out_scale'
26            scale_origin = origin_widget.get_screen_space_center()
27            transition = 'in_scale'
28        else:
29            self._transition_out = 'out_right'
30            scale_origin = None
31            transition = 'in_right'
32        bg_color = (0.4, 0.4, 0.5)
33        self._width = 560
34        self._height = 420
35        uiscale = ba.app.ui.uiscale
36        base_scale = (1.65 if uiscale is ba.UIScale.SMALL else
37                      1.5 if uiscale is ba.UIScale.MEDIUM else 1.1)
38        super().__init__(root_widget=ba.containerwidget(
39            size=(self._width, self._height),
40            transition=transition,
41            scale=base_scale,
42            scale_origin_stack_offset=scale_origin,
43            stack_offset=(0, -10) if uiscale is ba.UIScale.SMALL else (0, 0)))
44        self._cancel_button = ba.buttonwidget(parent=self._root_widget,
45                                              position=(40, self._height - 45),
46                                              size=(50, 50),
47                                              scale=0.7,
48                                              label='',
49                                              color=bg_color,
50                                              on_activate_call=self._cancel,
51                                              autoselect=True,
52                                              icon=ba.gettexture('crossOut'),
53                                              iconscale=1.2)
54        maxlinks = _ba.get_v1_account_misc_read_val('maxLinkAccounts', 5)
55        ba.textwidget(
56            parent=self._root_widget,
57            position=(self._width * 0.5, self._height * 0.56),
58            size=(0, 0),
59            text=ba.Lstr(resource=(
60                'accountSettingsWindow.linkAccountsInstructionsNewText'),
61                         subs=[('${COUNT}', str(maxlinks))]),
62            maxwidth=self._width * 0.9,
63            color=ba.app.ui.infotextcolor,
64            max_height=self._height * 0.6,
65            h_align='center',
66            v_align='center')
67        ba.containerwidget(edit=self._root_widget,
68                           cancel_button=self._cancel_button)
69        ba.buttonwidget(
70            parent=self._root_widget,
71            position=(40, 30),
72            size=(200, 60),
73            label=ba.Lstr(
74                resource='accountSettingsWindow.linkAccountsGenerateCodeText'),
75            autoselect=True,
76            on_activate_call=self._generate_press)
77        self._enter_code_button = ba.buttonwidget(
78            parent=self._root_widget,
79            position=(self._width - 240, 30),
80            size=(200, 60),
81            label=ba.Lstr(
82                resource='accountSettingsWindow.linkAccountsEnterCodeText'),
83            autoselect=True,
84            on_activate_call=self._enter_code_press)
Inherited Members
ba.ui.Window
get_root_widget
class AccountLinkCodeWindow(ba.ui.Window):
112class AccountLinkCodeWindow(ba.Window):
113    """Window showing code for account-linking."""
114
115    def __init__(self, data: dict[str, Any]):
116        self._width = 350
117        self._height = 200
118        uiscale = ba.app.ui.uiscale
119        super().__init__(root_widget=ba.containerwidget(
120            size=(self._width, self._height),
121            color=(0.45, 0.63, 0.15),
122            transition='in_scale',
123            scale=(1.8 if uiscale is ba.UIScale.SMALL else
124                   1.35 if uiscale is ba.UIScale.MEDIUM else 1.0)))
125        self._data = copy.deepcopy(data)
126        ba.playsound(ba.getsound('cashRegister'))
127        ba.playsound(ba.getsound('swish'))
128        self._cancel_button = ba.buttonwidget(parent=self._root_widget,
129                                              scale=0.5,
130                                              position=(40, self._height - 40),
131                                              size=(50, 50),
132                                              label='',
133                                              on_activate_call=self.close,
134                                              autoselect=True,
135                                              color=(0.45, 0.63, 0.15),
136                                              icon=ba.gettexture('crossOut'),
137                                              iconscale=1.2)
138        ba.containerwidget(edit=self._root_widget,
139                           cancel_button=self._cancel_button)
140        ba.textwidget(parent=self._root_widget,
141                      position=(self._width * 0.5, self._height * 0.5),
142                      size=(0, 0),
143                      color=(1.0, 3.0, 1.0),
144                      scale=2.0,
145                      h_align='center',
146                      v_align='center',
147                      text=data['code'],
148                      maxwidth=self._width * 0.85)
149
150    def close(self) -> None:
151        """close the window"""
152        ba.containerwidget(edit=self._root_widget, transition='out_scale')

Window showing code for account-linking.

AccountLinkCodeWindow(data: dict[str, typing.Any])
115    def __init__(self, data: dict[str, Any]):
116        self._width = 350
117        self._height = 200
118        uiscale = ba.app.ui.uiscale
119        super().__init__(root_widget=ba.containerwidget(
120            size=(self._width, self._height),
121            color=(0.45, 0.63, 0.15),
122            transition='in_scale',
123            scale=(1.8 if uiscale is ba.UIScale.SMALL else
124                   1.35 if uiscale is ba.UIScale.MEDIUM else 1.0)))
125        self._data = copy.deepcopy(data)
126        ba.playsound(ba.getsound('cashRegister'))
127        ba.playsound(ba.getsound('swish'))
128        self._cancel_button = ba.buttonwidget(parent=self._root_widget,
129                                              scale=0.5,
130                                              position=(40, self._height - 40),
131                                              size=(50, 50),
132                                              label='',
133                                              on_activate_call=self.close,
134                                              autoselect=True,
135                                              color=(0.45, 0.63, 0.15),
136                                              icon=ba.gettexture('crossOut'),
137                                              iconscale=1.2)
138        ba.containerwidget(edit=self._root_widget,
139                           cancel_button=self._cancel_button)
140        ba.textwidget(parent=self._root_widget,
141                      position=(self._width * 0.5, self._height * 0.5),
142                      size=(0, 0),
143                      color=(1.0, 3.0, 1.0),
144                      scale=2.0,
145                      h_align='center',
146                      v_align='center',
147                      text=data['code'],
148                      maxwidth=self._width * 0.85)
def close(self) -> None:
150    def close(self) -> None:
151        """close the window"""
152        ba.containerwidget(edit=self._root_widget, transition='out_scale')

close the window

Inherited Members
ba.ui.Window
get_root_widget