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 12 13if TYPE_CHECKING: 14 pass 15 16 17class PromoCodeWindow(ba.Window): 18 """Window for entering promo codes.""" 19 20 def __init__(self, 21 modal: bool = False, 22 origin_widget: ba.Widget | None = None): 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__(root_widget=ba.containerwidget( 42 size=(width, height), 43 transition=transition, 44 toolbar_visibility='menu_minimal_no_back', 45 scale_origin_stack_offset=scale_origin, 46 scale=(2.0 if uiscale is ba.UIScale.SMALL else 47 1.5 if uiscale is ba.UIScale.MEDIUM else 1.0))) 48 49 btn = ba.buttonwidget(parent=self._root_widget, 50 scale=0.5, 51 position=(40, height - 40), 52 size=(60, 60), 53 label='', 54 on_activate_call=self._do_back, 55 autoselect=True, 56 color=(0.55, 0.5, 0.6), 57 icon=ba.gettexture('crossOut'), 58 iconscale=1.2) 59 60 ba.textwidget(parent=self._root_widget, 61 text=ba.Lstr(resource=self._r + '.codeText'), 62 position=(22, height - 113), 63 color=(0.8, 0.8, 0.8, 1.0), 64 size=(90, 30), 65 h_align='right') 66 self._text_field = ba.textwidget( 67 parent=self._root_widget, 68 position=(125, height - 121), 69 size=(280, 46), 70 text='', 71 h_align='left', 72 v_align='center', 73 max_chars=64, 74 color=(0.9, 0.9, 0.9, 1.0), 75 description=ba.Lstr(resource=self._r + '.codeText'), 76 editable=True, 77 padding=4, 78 on_return_press_call=self._activate_enter_button) 79 ba.widget(edit=btn, down_widget=self._text_field) 80 81 b_width = 200 82 self._enter_button = btn2 = ba.buttonwidget( 83 parent=self._root_widget, 84 position=(width * 0.5 - b_width * 0.5, height - 200), 85 size=(b_width, 60), 86 scale=1.0, 87 label=ba.Lstr(resource='submitText', 88 fallback_resource=self._r + '.enterText'), 89 on_activate_call=self._do_enter) 90 ba.containerwidget(edit=self._root_widget, 91 cancel_button=btn, 92 start_button=btn2, 93 selected_child=self._text_field) 94 95 def _do_back(self) -> None: 96 # pylint: disable=cyclic-import 97 from bastd.ui.settings.advanced import AdvancedSettingsWindow 98 ba.containerwidget(edit=self._root_widget, 99 transition=self._transition_out) 100 if not self._modal: 101 ba.app.ui.set_main_menu_window( 102 AdvancedSettingsWindow(transition='in_left').get_root_widget()) 103 104 def _activate_enter_button(self) -> None: 105 self._enter_button.activate() 106 107 def _do_enter(self) -> None: 108 # pylint: disable=cyclic-import 109 from bastd.ui.settings.advanced import AdvancedSettingsWindow 110 ba.containerwidget(edit=self._root_widget, 111 transition=self._transition_out) 112 if not self._modal: 113 ba.app.ui.set_main_menu_window( 114 AdvancedSettingsWindow(transition='in_left').get_root_widget()) 115 _ba.add_transaction({ 116 'type': 'PROMO_CODE', 117 'expire_time': time.time() + 5, 118 'code': ba.textwidget(query=self._text_field) 119 }) 120 _ba.run_transactions()
class
PromoCodeWindow(ba.ui.Window):
18class PromoCodeWindow(ba.Window): 19 """Window for entering promo codes.""" 20 21 def __init__(self, 22 modal: bool = False, 23 origin_widget: ba.Widget | None = None): 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__(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=(2.0 if uiscale is ba.UIScale.SMALL else 48 1.5 if uiscale is ba.UIScale.MEDIUM else 1.0))) 49 50 btn = ba.buttonwidget(parent=self._root_widget, 51 scale=0.5, 52 position=(40, height - 40), 53 size=(60, 60), 54 label='', 55 on_activate_call=self._do_back, 56 autoselect=True, 57 color=(0.55, 0.5, 0.6), 58 icon=ba.gettexture('crossOut'), 59 iconscale=1.2) 60 61 ba.textwidget(parent=self._root_widget, 62 text=ba.Lstr(resource=self._r + '.codeText'), 63 position=(22, height - 113), 64 color=(0.8, 0.8, 0.8, 1.0), 65 size=(90, 30), 66 h_align='right') 67 self._text_field = ba.textwidget( 68 parent=self._root_widget, 69 position=(125, height - 121), 70 size=(280, 46), 71 text='', 72 h_align='left', 73 v_align='center', 74 max_chars=64, 75 color=(0.9, 0.9, 0.9, 1.0), 76 description=ba.Lstr(resource=self._r + '.codeText'), 77 editable=True, 78 padding=4, 79 on_return_press_call=self._activate_enter_button) 80 ba.widget(edit=btn, down_widget=self._text_field) 81 82 b_width = 200 83 self._enter_button = btn2 = ba.buttonwidget( 84 parent=self._root_widget, 85 position=(width * 0.5 - b_width * 0.5, height - 200), 86 size=(b_width, 60), 87 scale=1.0, 88 label=ba.Lstr(resource='submitText', 89 fallback_resource=self._r + '.enterText'), 90 on_activate_call=self._do_enter) 91 ba.containerwidget(edit=self._root_widget, 92 cancel_button=btn, 93 start_button=btn2, 94 selected_child=self._text_field) 95 96 def _do_back(self) -> None: 97 # pylint: disable=cyclic-import 98 from bastd.ui.settings.advanced import AdvancedSettingsWindow 99 ba.containerwidget(edit=self._root_widget, 100 transition=self._transition_out) 101 if not self._modal: 102 ba.app.ui.set_main_menu_window( 103 AdvancedSettingsWindow(transition='in_left').get_root_widget()) 104 105 def _activate_enter_button(self) -> None: 106 self._enter_button.activate() 107 108 def _do_enter(self) -> None: 109 # pylint: disable=cyclic-import 110 from bastd.ui.settings.advanced import AdvancedSettingsWindow 111 ba.containerwidget(edit=self._root_widget, 112 transition=self._transition_out) 113 if not self._modal: 114 ba.app.ui.set_main_menu_window( 115 AdvancedSettingsWindow(transition='in_left').get_root_widget()) 116 _ba.add_transaction({ 117 'type': 'PROMO_CODE', 118 'expire_time': time.time() + 5, 119 'code': ba.textwidget(query=self._text_field) 120 }) 121 _ba.run_transactions()
Window for entering promo codes.
PromoCodeWindow(modal: bool = False, origin_widget: _ba.Widget | None = None)
21 def __init__(self, 22 modal: bool = False, 23 origin_widget: ba.Widget | None = None): 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__(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=(2.0 if uiscale is ba.UIScale.SMALL else 48 1.5 if uiscale is ba.UIScale.MEDIUM else 1.0))) 49 50 btn = ba.buttonwidget(parent=self._root_widget, 51 scale=0.5, 52 position=(40, height - 40), 53 size=(60, 60), 54 label='', 55 on_activate_call=self._do_back, 56 autoselect=True, 57 color=(0.55, 0.5, 0.6), 58 icon=ba.gettexture('crossOut'), 59 iconscale=1.2) 60 61 ba.textwidget(parent=self._root_widget, 62 text=ba.Lstr(resource=self._r + '.codeText'), 63 position=(22, height - 113), 64 color=(0.8, 0.8, 0.8, 1.0), 65 size=(90, 30), 66 h_align='right') 67 self._text_field = ba.textwidget( 68 parent=self._root_widget, 69 position=(125, height - 121), 70 size=(280, 46), 71 text='', 72 h_align='left', 73 v_align='center', 74 max_chars=64, 75 color=(0.9, 0.9, 0.9, 1.0), 76 description=ba.Lstr(resource=self._r + '.codeText'), 77 editable=True, 78 padding=4, 79 on_return_press_call=self._activate_enter_button) 80 ba.widget(edit=btn, down_widget=self._text_field) 81 82 b_width = 200 83 self._enter_button = btn2 = ba.buttonwidget( 84 parent=self._root_widget, 85 position=(width * 0.5 - b_width * 0.5, height - 200), 86 size=(b_width, 60), 87 scale=1.0, 88 label=ba.Lstr(resource='submitText', 89 fallback_resource=self._r + '.enterText'), 90 on_activate_call=self._do_enter) 91 ba.containerwidget(edit=self._root_widget, 92 cancel_button=btn, 93 start_button=btn2, 94 selected_child=self._text_field)
Inherited Members
- ba.ui.Window
- get_root_widget