bastd.ui.playlist.share

UI functionality for importing shared playlists.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""UI functionality for importing shared playlists."""
  4
  5from __future__ import annotations
  6
  7import time
  8from typing import TYPE_CHECKING
  9
 10import _ba
 11import ba
 12from bastd.ui import promocode
 13
 14if TYPE_CHECKING:
 15    from typing import Any, Callable
 16
 17
 18class SharePlaylistImportWindow(promocode.PromoCodeWindow):
 19    """Window for importing a shared playlist."""
 20
 21    def __init__(self,
 22                 origin_widget: ba.Widget | None = None,
 23                 on_success_callback: Callable[[], Any] | None = None):
 24        promocode.PromoCodeWindow.__init__(self,
 25                                           modal=True,
 26                                           origin_widget=origin_widget)
 27        self._on_success_callback = on_success_callback
 28
 29    def _on_import_response(self, response: dict[str, Any] | None) -> None:
 30        if response is None:
 31            ba.screenmessage(ba.Lstr(resource='errorText'), color=(1, 0, 0))
 32            ba.playsound(ba.getsound('error'))
 33            return
 34
 35        if response['playlistType'] == 'Team Tournament':
 36            playlist_type_name = ba.Lstr(resource='playModes.teamsText')
 37        elif response['playlistType'] == 'Free-for-All':
 38            playlist_type_name = ba.Lstr(resource='playModes.freeForAllText')
 39        else:
 40            playlist_type_name = ba.Lstr(value=response['playlistType'])
 41
 42        ba.screenmessage(ba.Lstr(resource='importPlaylistSuccessText',
 43                                 subs=[('${TYPE}', playlist_type_name),
 44                                       ('${NAME}', response['playlistName'])]),
 45                         color=(0, 1, 0))
 46        ba.playsound(ba.getsound('gunCocking'))
 47        if self._on_success_callback is not None:
 48            self._on_success_callback()
 49        ba.containerwidget(edit=self._root_widget,
 50                           transition=self._transition_out)
 51
 52    def _do_enter(self) -> None:
 53        _ba.add_transaction(
 54            {
 55                'type': 'IMPORT_PLAYLIST',
 56                'expire_time': time.time() + 5,
 57                'code': ba.textwidget(query=self._text_field)
 58            },
 59            callback=ba.WeakCall(self._on_import_response))
 60        _ba.run_transactions()
 61        ba.screenmessage(ba.Lstr(resource='importingText'))
 62
 63
 64class SharePlaylistResultsWindow(ba.Window):
 65    """Window for sharing playlists."""
 66
 67    def __init__(self,
 68                 name: str,
 69                 data: str,
 70                 origin: tuple[float, float] = (0.0, 0.0)):
 71        del origin  # unused arg
 72        self._width = 450
 73        self._height = 300
 74        uiscale = ba.app.ui.uiscale
 75        super().__init__(root_widget=ba.containerwidget(
 76            size=(self._width, self._height),
 77            color=(0.45, 0.63, 0.15),
 78            transition='in_scale',
 79            scale=(1.8 if uiscale is ba.UIScale.SMALL else
 80                   1.35 if uiscale is ba.UIScale.MEDIUM else 1.0)))
 81        ba.playsound(ba.getsound('cashRegister'))
 82        ba.playsound(ba.getsound('swish'))
 83
 84        self._cancel_button = ba.buttonwidget(parent=self._root_widget,
 85                                              scale=0.7,
 86                                              position=(40, self._height - 40),
 87                                              size=(50, 50),
 88                                              label='',
 89                                              on_activate_call=self.close,
 90                                              autoselect=True,
 91                                              color=(0.45, 0.63, 0.15),
 92                                              icon=ba.gettexture('crossOut'),
 93                                              iconscale=1.2)
 94        ba.containerwidget(edit=self._root_widget,
 95                           cancel_button=self._cancel_button)
 96
 97        ba.textwidget(parent=self._root_widget,
 98                      position=(self._width * 0.5, self._height * 0.745),
 99                      size=(0, 0),
100                      color=ba.app.ui.infotextcolor,
101                      scale=1.0,
102                      flatness=1.0,
103                      h_align='center',
104                      v_align='center',
105                      text=ba.Lstr(resource='exportSuccessText',
106                                   subs=[('${NAME}', name)]),
107                      maxwidth=self._width * 0.85)
108
109        ba.textwidget(
110            parent=self._root_widget,
111            position=(self._width * 0.5, self._height * 0.645),
112            size=(0, 0),
113            color=ba.app.ui.infotextcolor,
114            scale=0.6,
115            flatness=1.0,
116            h_align='center',
117            v_align='center',
118            text=ba.Lstr(resource='importPlaylistCodeInstructionsText'),
119            maxwidth=self._width * 0.85)
120
121        ba.textwidget(parent=self._root_widget,
122                      position=(self._width * 0.5, self._height * 0.4),
123                      size=(0, 0),
124                      color=(1.0, 3.0, 1.0),
125                      scale=2.3,
126                      h_align='center',
127                      v_align='center',
128                      text=data,
129                      maxwidth=self._width * 0.85)
130
131    def close(self) -> None:
132        """Close the window."""
133        ba.containerwidget(edit=self._root_widget, transition='out_scale')
class SharePlaylistImportWindow(bastd.ui.promocode.PromoCodeWindow):
19class SharePlaylistImportWindow(promocode.PromoCodeWindow):
20    """Window for importing a shared playlist."""
21
22    def __init__(self,
23                 origin_widget: ba.Widget | None = None,
24                 on_success_callback: Callable[[], Any] | None = None):
25        promocode.PromoCodeWindow.__init__(self,
26                                           modal=True,
27                                           origin_widget=origin_widget)
28        self._on_success_callback = on_success_callback
29
30    def _on_import_response(self, response: dict[str, Any] | None) -> None:
31        if response is None:
32            ba.screenmessage(ba.Lstr(resource='errorText'), color=(1, 0, 0))
33            ba.playsound(ba.getsound('error'))
34            return
35
36        if response['playlistType'] == 'Team Tournament':
37            playlist_type_name = ba.Lstr(resource='playModes.teamsText')
38        elif response['playlistType'] == 'Free-for-All':
39            playlist_type_name = ba.Lstr(resource='playModes.freeForAllText')
40        else:
41            playlist_type_name = ba.Lstr(value=response['playlistType'])
42
43        ba.screenmessage(ba.Lstr(resource='importPlaylistSuccessText',
44                                 subs=[('${TYPE}', playlist_type_name),
45                                       ('${NAME}', response['playlistName'])]),
46                         color=(0, 1, 0))
47        ba.playsound(ba.getsound('gunCocking'))
48        if self._on_success_callback is not None:
49            self._on_success_callback()
50        ba.containerwidget(edit=self._root_widget,
51                           transition=self._transition_out)
52
53    def _do_enter(self) -> None:
54        _ba.add_transaction(
55            {
56                'type': 'IMPORT_PLAYLIST',
57                'expire_time': time.time() + 5,
58                'code': ba.textwidget(query=self._text_field)
59            },
60            callback=ba.WeakCall(self._on_import_response))
61        _ba.run_transactions()
62        ba.screenmessage(ba.Lstr(resource='importingText'))

Window for importing a shared playlist.

SharePlaylistImportWindow( origin_widget: _ba.Widget | None = None, on_success_callback: Optional[Callable[[], Any]] = None)
22    def __init__(self,
23                 origin_widget: ba.Widget | None = None,
24                 on_success_callback: Callable[[], Any] | None = None):
25        promocode.PromoCodeWindow.__init__(self,
26                                           modal=True,
27                                           origin_widget=origin_widget)
28        self._on_success_callback = on_success_callback
Inherited Members
ba.ui.Window
get_root_widget
class SharePlaylistResultsWindow(ba.ui.Window):
 65class SharePlaylistResultsWindow(ba.Window):
 66    """Window for sharing playlists."""
 67
 68    def __init__(self,
 69                 name: str,
 70                 data: str,
 71                 origin: tuple[float, float] = (0.0, 0.0)):
 72        del origin  # unused arg
 73        self._width = 450
 74        self._height = 300
 75        uiscale = ba.app.ui.uiscale
 76        super().__init__(root_widget=ba.containerwidget(
 77            size=(self._width, self._height),
 78            color=(0.45, 0.63, 0.15),
 79            transition='in_scale',
 80            scale=(1.8 if uiscale is ba.UIScale.SMALL else
 81                   1.35 if uiscale is ba.UIScale.MEDIUM else 1.0)))
 82        ba.playsound(ba.getsound('cashRegister'))
 83        ba.playsound(ba.getsound('swish'))
 84
 85        self._cancel_button = ba.buttonwidget(parent=self._root_widget,
 86                                              scale=0.7,
 87                                              position=(40, self._height - 40),
 88                                              size=(50, 50),
 89                                              label='',
 90                                              on_activate_call=self.close,
 91                                              autoselect=True,
 92                                              color=(0.45, 0.63, 0.15),
 93                                              icon=ba.gettexture('crossOut'),
 94                                              iconscale=1.2)
 95        ba.containerwidget(edit=self._root_widget,
 96                           cancel_button=self._cancel_button)
 97
 98        ba.textwidget(parent=self._root_widget,
 99                      position=(self._width * 0.5, self._height * 0.745),
100                      size=(0, 0),
101                      color=ba.app.ui.infotextcolor,
102                      scale=1.0,
103                      flatness=1.0,
104                      h_align='center',
105                      v_align='center',
106                      text=ba.Lstr(resource='exportSuccessText',
107                                   subs=[('${NAME}', name)]),
108                      maxwidth=self._width * 0.85)
109
110        ba.textwidget(
111            parent=self._root_widget,
112            position=(self._width * 0.5, self._height * 0.645),
113            size=(0, 0),
114            color=ba.app.ui.infotextcolor,
115            scale=0.6,
116            flatness=1.0,
117            h_align='center',
118            v_align='center',
119            text=ba.Lstr(resource='importPlaylistCodeInstructionsText'),
120            maxwidth=self._width * 0.85)
121
122        ba.textwidget(parent=self._root_widget,
123                      position=(self._width * 0.5, self._height * 0.4),
124                      size=(0, 0),
125                      color=(1.0, 3.0, 1.0),
126                      scale=2.3,
127                      h_align='center',
128                      v_align='center',
129                      text=data,
130                      maxwidth=self._width * 0.85)
131
132    def close(self) -> None:
133        """Close the window."""
134        ba.containerwidget(edit=self._root_widget, transition='out_scale')

Window for sharing playlists.

SharePlaylistResultsWindow(name: str, data: str, origin: tuple[float, float] = (0.0, 0.0))
 68    def __init__(self,
 69                 name: str,
 70                 data: str,
 71                 origin: tuple[float, float] = (0.0, 0.0)):
 72        del origin  # unused arg
 73        self._width = 450
 74        self._height = 300
 75        uiscale = ba.app.ui.uiscale
 76        super().__init__(root_widget=ba.containerwidget(
 77            size=(self._width, self._height),
 78            color=(0.45, 0.63, 0.15),
 79            transition='in_scale',
 80            scale=(1.8 if uiscale is ba.UIScale.SMALL else
 81                   1.35 if uiscale is ba.UIScale.MEDIUM else 1.0)))
 82        ba.playsound(ba.getsound('cashRegister'))
 83        ba.playsound(ba.getsound('swish'))
 84
 85        self._cancel_button = ba.buttonwidget(parent=self._root_widget,
 86                                              scale=0.7,
 87                                              position=(40, self._height - 40),
 88                                              size=(50, 50),
 89                                              label='',
 90                                              on_activate_call=self.close,
 91                                              autoselect=True,
 92                                              color=(0.45, 0.63, 0.15),
 93                                              icon=ba.gettexture('crossOut'),
 94                                              iconscale=1.2)
 95        ba.containerwidget(edit=self._root_widget,
 96                           cancel_button=self._cancel_button)
 97
 98        ba.textwidget(parent=self._root_widget,
 99                      position=(self._width * 0.5, self._height * 0.745),
100                      size=(0, 0),
101                      color=ba.app.ui.infotextcolor,
102                      scale=1.0,
103                      flatness=1.0,
104                      h_align='center',
105                      v_align='center',
106                      text=ba.Lstr(resource='exportSuccessText',
107                                   subs=[('${NAME}', name)]),
108                      maxwidth=self._width * 0.85)
109
110        ba.textwidget(
111            parent=self._root_widget,
112            position=(self._width * 0.5, self._height * 0.645),
113            size=(0, 0),
114            color=ba.app.ui.infotextcolor,
115            scale=0.6,
116            flatness=1.0,
117            h_align='center',
118            v_align='center',
119            text=ba.Lstr(resource='importPlaylistCodeInstructionsText'),
120            maxwidth=self._width * 0.85)
121
122        ba.textwidget(parent=self._root_widget,
123                      position=(self._width * 0.5, self._height * 0.4),
124                      size=(0, 0),
125                      color=(1.0, 3.0, 1.0),
126                      scale=2.3,
127                      h_align='center',
128                      v_align='center',
129                      text=data,
130                      maxwidth=self._width * 0.85)
def close(self) -> None:
132    def close(self) -> None:
133        """Close the window."""
134        ba.containerwidget(edit=self._root_widget, transition='out_scale')

Close the window.

Inherited Members
ba.ui.Window
get_root_widget