bastd.ui.promocode

UI functionality for entering promo codes.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""UI functionality for entering promo codes."""
  4
  5from __future__ import annotations
  6
  7import time
  8from typing import TYPE_CHECKING
  9
 10import ba
 11import ba.internal
 12
 13if TYPE_CHECKING:
 14    pass
 15
 16
 17class PromoCodeWindow(ba.Window):
 18    """Window for entering promo codes."""
 19
 20    def __init__(
 21        self, modal: bool = False, origin_widget: ba.Widget | None = None
 22    ):
 23
 24        scale_origin: tuple[float, float] | None
 25        if origin_widget is not None:
 26            self._transition_out = 'out_scale'
 27            scale_origin = origin_widget.get_screen_space_center()
 28            transition = 'in_scale'
 29        else:
 30            self._transition_out = 'out_right'
 31            scale_origin = None
 32            transition = 'in_right'
 33
 34        width = 450
 35        height = 230
 36
 37        self._modal = modal
 38        self._r = 'promoCodeWindow'
 39
 40        uiscale = ba.app.ui.uiscale
 41        super().__init__(
 42            root_widget=ba.containerwidget(
 43                size=(width, height),
 44                transition=transition,
 45                toolbar_visibility='menu_minimal_no_back',
 46                scale_origin_stack_offset=scale_origin,
 47                scale=(
 48                    2.0
 49                    if uiscale is ba.UIScale.SMALL
 50                    else 1.5
 51                    if uiscale is ba.UIScale.MEDIUM
 52                    else 1.0
 53                ),
 54            )
 55        )
 56
 57        btn = ba.buttonwidget(
 58            parent=self._root_widget,
 59            scale=0.5,
 60            position=(40, height - 40),
 61            size=(60, 60),
 62            label='',
 63            on_activate_call=self._do_back,
 64            autoselect=True,
 65            color=(0.55, 0.5, 0.6),
 66            icon=ba.gettexture('crossOut'),
 67            iconscale=1.2,
 68        )
 69
 70        ba.textwidget(
 71            parent=self._root_widget,
 72            text=ba.Lstr(resource=self._r + '.codeText'),
 73            position=(22, height - 113),
 74            color=(0.8, 0.8, 0.8, 1.0),
 75            size=(90, 30),
 76            h_align='right',
 77        )
 78        self._text_field = ba.textwidget(
 79            parent=self._root_widget,
 80            position=(125, height - 121),
 81            size=(280, 46),
 82            text='',
 83            h_align='left',
 84            v_align='center',
 85            max_chars=64,
 86            color=(0.9, 0.9, 0.9, 1.0),
 87            description=ba.Lstr(resource=self._r + '.codeText'),
 88            editable=True,
 89            padding=4,
 90            on_return_press_call=self._activate_enter_button,
 91        )
 92        ba.widget(edit=btn, down_widget=self._text_field)
 93
 94        b_width = 200
 95        self._enter_button = btn2 = ba.buttonwidget(
 96            parent=self._root_widget,
 97            position=(width * 0.5 - b_width * 0.5, height - 200),
 98            size=(b_width, 60),
 99            scale=1.0,
100            label=ba.Lstr(
101                resource='submitText', fallback_resource=self._r + '.enterText'
102            ),
103            on_activate_call=self._do_enter,
104        )
105        ba.containerwidget(
106            edit=self._root_widget,
107            cancel_button=btn,
108            start_button=btn2,
109            selected_child=self._text_field,
110        )
111
112    def _do_back(self) -> None:
113        # pylint: disable=cyclic-import
114        from bastd.ui.settings.advanced import AdvancedSettingsWindow
115
116        ba.containerwidget(
117            edit=self._root_widget, transition=self._transition_out
118        )
119        if not self._modal:
120            ba.app.ui.set_main_menu_window(
121                AdvancedSettingsWindow(transition='in_left').get_root_widget()
122            )
123
124    def _activate_enter_button(self) -> None:
125        self._enter_button.activate()
126
127    def _do_enter(self) -> None:
128        # pylint: disable=cyclic-import
129        from bastd.ui.settings.advanced import AdvancedSettingsWindow
130
131        ba.containerwidget(
132            edit=self._root_widget, transition=self._transition_out
133        )
134        if not self._modal:
135            ba.app.ui.set_main_menu_window(
136                AdvancedSettingsWindow(transition='in_left').get_root_widget()
137            )
138        ba.internal.add_transaction(
139            {
140                'type': 'PROMO_CODE',
141                'expire_time': time.time() + 5,
142                'code': ba.textwidget(query=self._text_field),
143            }
144        )
145        ba.internal.run_transactions()
class PromoCodeWindow(ba.ui.Window):
 18class PromoCodeWindow(ba.Window):
 19    """Window for entering promo codes."""
 20
 21    def __init__(
 22        self, modal: bool = False, origin_widget: ba.Widget | None = None
 23    ):
 24
 25        scale_origin: tuple[float, float] | None
 26        if origin_widget is not None:
 27            self._transition_out = 'out_scale'
 28            scale_origin = origin_widget.get_screen_space_center()
 29            transition = 'in_scale'
 30        else:
 31            self._transition_out = 'out_right'
 32            scale_origin = None
 33            transition = 'in_right'
 34
 35        width = 450
 36        height = 230
 37
 38        self._modal = modal
 39        self._r = 'promoCodeWindow'
 40
 41        uiscale = ba.app.ui.uiscale
 42        super().__init__(
 43            root_widget=ba.containerwidget(
 44                size=(width, height),
 45                transition=transition,
 46                toolbar_visibility='menu_minimal_no_back',
 47                scale_origin_stack_offset=scale_origin,
 48                scale=(
 49                    2.0
 50                    if uiscale is ba.UIScale.SMALL
 51                    else 1.5
 52                    if uiscale is ba.UIScale.MEDIUM
 53                    else 1.0
 54                ),
 55            )
 56        )
 57
 58        btn = ba.buttonwidget(
 59            parent=self._root_widget,
 60            scale=0.5,
 61            position=(40, height - 40),
 62            size=(60, 60),
 63            label='',
 64            on_activate_call=self._do_back,
 65            autoselect=True,
 66            color=(0.55, 0.5, 0.6),
 67            icon=ba.gettexture('crossOut'),
 68            iconscale=1.2,
 69        )
 70
 71        ba.textwidget(
 72            parent=self._root_widget,
 73            text=ba.Lstr(resource=self._r + '.codeText'),
 74            position=(22, height - 113),
 75            color=(0.8, 0.8, 0.8, 1.0),
 76            size=(90, 30),
 77            h_align='right',
 78        )
 79        self._text_field = ba.textwidget(
 80            parent=self._root_widget,
 81            position=(125, height - 121),
 82            size=(280, 46),
 83            text='',
 84            h_align='left',
 85            v_align='center',
 86            max_chars=64,
 87            color=(0.9, 0.9, 0.9, 1.0),
 88            description=ba.Lstr(resource=self._r + '.codeText'),
 89            editable=True,
 90            padding=4,
 91            on_return_press_call=self._activate_enter_button,
 92        )
 93        ba.widget(edit=btn, down_widget=self._text_field)
 94
 95        b_width = 200
 96        self._enter_button = btn2 = ba.buttonwidget(
 97            parent=self._root_widget,
 98            position=(width * 0.5 - b_width * 0.5, height - 200),
 99            size=(b_width, 60),
100            scale=1.0,
101            label=ba.Lstr(
102                resource='submitText', fallback_resource=self._r + '.enterText'
103            ),
104            on_activate_call=self._do_enter,
105        )
106        ba.containerwidget(
107            edit=self._root_widget,
108            cancel_button=btn,
109            start_button=btn2,
110            selected_child=self._text_field,
111        )
112
113    def _do_back(self) -> None:
114        # pylint: disable=cyclic-import
115        from bastd.ui.settings.advanced import AdvancedSettingsWindow
116
117        ba.containerwidget(
118            edit=self._root_widget, transition=self._transition_out
119        )
120        if not self._modal:
121            ba.app.ui.set_main_menu_window(
122                AdvancedSettingsWindow(transition='in_left').get_root_widget()
123            )
124
125    def _activate_enter_button(self) -> None:
126        self._enter_button.activate()
127
128    def _do_enter(self) -> None:
129        # pylint: disable=cyclic-import
130        from bastd.ui.settings.advanced import AdvancedSettingsWindow
131
132        ba.containerwidget(
133            edit=self._root_widget, transition=self._transition_out
134        )
135        if not self._modal:
136            ba.app.ui.set_main_menu_window(
137                AdvancedSettingsWindow(transition='in_left').get_root_widget()
138            )
139        ba.internal.add_transaction(
140            {
141                'type': 'PROMO_CODE',
142                'expire_time': time.time() + 5,
143                'code': ba.textwidget(query=self._text_field),
144            }
145        )
146        ba.internal.run_transactions()

Window for entering promo codes.

PromoCodeWindow(modal: bool = False, origin_widget: _ba.Widget | None = None)
 21    def __init__(
 22        self, modal: bool = False, origin_widget: ba.Widget | None = None
 23    ):
 24
 25        scale_origin: tuple[float, float] | None
 26        if origin_widget is not None:
 27            self._transition_out = 'out_scale'
 28            scale_origin = origin_widget.get_screen_space_center()
 29            transition = 'in_scale'
 30        else:
 31            self._transition_out = 'out_right'
 32            scale_origin = None
 33            transition = 'in_right'
 34
 35        width = 450
 36        height = 230
 37
 38        self._modal = modal
 39        self._r = 'promoCodeWindow'
 40
 41        uiscale = ba.app.ui.uiscale
 42        super().__init__(
 43            root_widget=ba.containerwidget(
 44                size=(width, height),
 45                transition=transition,
 46                toolbar_visibility='menu_minimal_no_back',
 47                scale_origin_stack_offset=scale_origin,
 48                scale=(
 49                    2.0
 50                    if uiscale is ba.UIScale.SMALL
 51                    else 1.5
 52                    if uiscale is ba.UIScale.MEDIUM
 53                    else 1.0
 54                ),
 55            )
 56        )
 57
 58        btn = ba.buttonwidget(
 59            parent=self._root_widget,
 60            scale=0.5,
 61            position=(40, height - 40),
 62            size=(60, 60),
 63            label='',
 64            on_activate_call=self._do_back,
 65            autoselect=True,
 66            color=(0.55, 0.5, 0.6),
 67            icon=ba.gettexture('crossOut'),
 68            iconscale=1.2,
 69        )
 70
 71        ba.textwidget(
 72            parent=self._root_widget,
 73            text=ba.Lstr(resource=self._r + '.codeText'),
 74            position=(22, height - 113),
 75            color=(0.8, 0.8, 0.8, 1.0),
 76            size=(90, 30),
 77            h_align='right',
 78        )
 79        self._text_field = ba.textwidget(
 80            parent=self._root_widget,
 81            position=(125, height - 121),
 82            size=(280, 46),
 83            text='',
 84            h_align='left',
 85            v_align='center',
 86            max_chars=64,
 87            color=(0.9, 0.9, 0.9, 1.0),
 88            description=ba.Lstr(resource=self._r + '.codeText'),
 89            editable=True,
 90            padding=4,
 91            on_return_press_call=self._activate_enter_button,
 92        )
 93        ba.widget(edit=btn, down_widget=self._text_field)
 94
 95        b_width = 200
 96        self._enter_button = btn2 = ba.buttonwidget(
 97            parent=self._root_widget,
 98            position=(width * 0.5 - b_width * 0.5, height - 200),
 99            size=(b_width, 60),
100            scale=1.0,
101            label=ba.Lstr(
102                resource='submitText', fallback_resource=self._r + '.enterText'
103            ),
104            on_activate_call=self._do_enter,
105        )
106        ba.containerwidget(
107            edit=self._root_widget,
108            cancel_button=btn,
109            start_button=btn2,
110            selected_child=self._text_field,
111        )
Inherited Members
ba.ui.Window
get_root_widget