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.internal
 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__(
 22        self,
 23        origin_widget: ba.Widget | None = None,
 24        on_success_callback: Callable[[], Any] | None = None,
 25    ):
 26        promocode.PromoCodeWindow.__init__(
 27            self, modal=True, origin_widget=origin_widget
 28        )
 29        self._on_success_callback = on_success_callback
 30
 31    def _on_import_response(self, response: dict[str, Any] | None) -> None:
 32        if response is None:
 33            ba.screenmessage(ba.Lstr(resource='errorText'), color=(1, 0, 0))
 34            ba.playsound(ba.getsound('error'))
 35            return
 36
 37        if response['playlistType'] == 'Team Tournament':
 38            playlist_type_name = ba.Lstr(resource='playModes.teamsText')
 39        elif response['playlistType'] == 'Free-for-All':
 40            playlist_type_name = ba.Lstr(resource='playModes.freeForAllText')
 41        else:
 42            playlist_type_name = ba.Lstr(value=response['playlistType'])
 43
 44        ba.screenmessage(
 45            ba.Lstr(
 46                resource='importPlaylistSuccessText',
 47                subs=[
 48                    ('${TYPE}', playlist_type_name),
 49                    ('${NAME}', response['playlistName']),
 50                ],
 51            ),
 52            color=(0, 1, 0),
 53        )
 54        ba.playsound(ba.getsound('gunCocking'))
 55        if self._on_success_callback is not None:
 56            self._on_success_callback()
 57        ba.containerwidget(
 58            edit=self._root_widget, transition=self._transition_out
 59        )
 60
 61    def _do_enter(self) -> None:
 62        ba.internal.add_transaction(
 63            {
 64                'type': 'IMPORT_PLAYLIST',
 65                'expire_time': time.time() + 5,
 66                'code': ba.textwidget(query=self._text_field),
 67            },
 68            callback=ba.WeakCall(self._on_import_response),
 69        )
 70        ba.internal.run_transactions()
 71        ba.screenmessage(ba.Lstr(resource='importingText'))
 72
 73
 74class SharePlaylistResultsWindow(ba.Window):
 75    """Window for sharing playlists."""
 76
 77    def __init__(
 78        self, name: str, data: str, origin: tuple[float, float] = (0.0, 0.0)
 79    ):
 80        del origin  # unused arg
 81        self._width = 450
 82        self._height = 300
 83        uiscale = ba.app.ui.uiscale
 84        super().__init__(
 85            root_widget=ba.containerwidget(
 86                size=(self._width, self._height),
 87                color=(0.45, 0.63, 0.15),
 88                transition='in_scale',
 89                scale=(
 90                    1.8
 91                    if uiscale is ba.UIScale.SMALL
 92                    else 1.35
 93                    if uiscale is ba.UIScale.MEDIUM
 94                    else 1.0
 95                ),
 96            )
 97        )
 98        ba.playsound(ba.getsound('cashRegister'))
 99        ba.playsound(ba.getsound('swish'))
100
101        self._cancel_button = ba.buttonwidget(
102            parent=self._root_widget,
103            scale=0.7,
104            position=(40, self._height - 40),
105            size=(50, 50),
106            label='',
107            on_activate_call=self.close,
108            autoselect=True,
109            color=(0.45, 0.63, 0.15),
110            icon=ba.gettexture('crossOut'),
111            iconscale=1.2,
112        )
113        ba.containerwidget(
114            edit=self._root_widget, cancel_button=self._cancel_button
115        )
116
117        ba.textwidget(
118            parent=self._root_widget,
119            position=(self._width * 0.5, self._height * 0.745),
120            size=(0, 0),
121            color=ba.app.ui.infotextcolor,
122            scale=1.0,
123            flatness=1.0,
124            h_align='center',
125            v_align='center',
126            text=ba.Lstr(
127                resource='exportSuccessText', subs=[('${NAME}', name)]
128            ),
129            maxwidth=self._width * 0.85,
130        )
131
132        ba.textwidget(
133            parent=self._root_widget,
134            position=(self._width * 0.5, self._height * 0.645),
135            size=(0, 0),
136            color=ba.app.ui.infotextcolor,
137            scale=0.6,
138            flatness=1.0,
139            h_align='center',
140            v_align='center',
141            text=ba.Lstr(resource='importPlaylistCodeInstructionsText'),
142            maxwidth=self._width * 0.85,
143        )
144
145        ba.textwidget(
146            parent=self._root_widget,
147            position=(self._width * 0.5, self._height * 0.4),
148            size=(0, 0),
149            color=(1.0, 3.0, 1.0),
150            scale=2.3,
151            h_align='center',
152            v_align='center',
153            text=data,
154            maxwidth=self._width * 0.85,
155        )
156
157    def close(self) -> None:
158        """Close the window."""
159        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__(
23        self,
24        origin_widget: ba.Widget | None = None,
25        on_success_callback: Callable[[], Any] | None = None,
26    ):
27        promocode.PromoCodeWindow.__init__(
28            self, modal=True, origin_widget=origin_widget
29        )
30        self._on_success_callback = on_success_callback
31
32    def _on_import_response(self, response: dict[str, Any] | None) -> None:
33        if response is None:
34            ba.screenmessage(ba.Lstr(resource='errorText'), color=(1, 0, 0))
35            ba.playsound(ba.getsound('error'))
36            return
37
38        if response['playlistType'] == 'Team Tournament':
39            playlist_type_name = ba.Lstr(resource='playModes.teamsText')
40        elif response['playlistType'] == 'Free-for-All':
41            playlist_type_name = ba.Lstr(resource='playModes.freeForAllText')
42        else:
43            playlist_type_name = ba.Lstr(value=response['playlistType'])
44
45        ba.screenmessage(
46            ba.Lstr(
47                resource='importPlaylistSuccessText',
48                subs=[
49                    ('${TYPE}', playlist_type_name),
50                    ('${NAME}', response['playlistName']),
51                ],
52            ),
53            color=(0, 1, 0),
54        )
55        ba.playsound(ba.getsound('gunCocking'))
56        if self._on_success_callback is not None:
57            self._on_success_callback()
58        ba.containerwidget(
59            edit=self._root_widget, transition=self._transition_out
60        )
61
62    def _do_enter(self) -> None:
63        ba.internal.add_transaction(
64            {
65                'type': 'IMPORT_PLAYLIST',
66                'expire_time': time.time() + 5,
67                'code': ba.textwidget(query=self._text_field),
68            },
69            callback=ba.WeakCall(self._on_import_response),
70        )
71        ba.internal.run_transactions()
72        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__(
23        self,
24        origin_widget: ba.Widget | None = None,
25        on_success_callback: Callable[[], Any] | None = None,
26    ):
27        promocode.PromoCodeWindow.__init__(
28            self, modal=True, origin_widget=origin_widget
29        )
30        self._on_success_callback = on_success_callback
Inherited Members
ba.ui.Window
get_root_widget
class SharePlaylistResultsWindow(ba.ui.Window):
 75class SharePlaylistResultsWindow(ba.Window):
 76    """Window for sharing playlists."""
 77
 78    def __init__(
 79        self, name: str, data: str, origin: tuple[float, float] = (0.0, 0.0)
 80    ):
 81        del origin  # unused arg
 82        self._width = 450
 83        self._height = 300
 84        uiscale = ba.app.ui.uiscale
 85        super().__init__(
 86            root_widget=ba.containerwidget(
 87                size=(self._width, self._height),
 88                color=(0.45, 0.63, 0.15),
 89                transition='in_scale',
 90                scale=(
 91                    1.8
 92                    if uiscale is ba.UIScale.SMALL
 93                    else 1.35
 94                    if uiscale is ba.UIScale.MEDIUM
 95                    else 1.0
 96                ),
 97            )
 98        )
 99        ba.playsound(ba.getsound('cashRegister'))
100        ba.playsound(ba.getsound('swish'))
101
102        self._cancel_button = ba.buttonwidget(
103            parent=self._root_widget,
104            scale=0.7,
105            position=(40, self._height - 40),
106            size=(50, 50),
107            label='',
108            on_activate_call=self.close,
109            autoselect=True,
110            color=(0.45, 0.63, 0.15),
111            icon=ba.gettexture('crossOut'),
112            iconscale=1.2,
113        )
114        ba.containerwidget(
115            edit=self._root_widget, cancel_button=self._cancel_button
116        )
117
118        ba.textwidget(
119            parent=self._root_widget,
120            position=(self._width * 0.5, self._height * 0.745),
121            size=(0, 0),
122            color=ba.app.ui.infotextcolor,
123            scale=1.0,
124            flatness=1.0,
125            h_align='center',
126            v_align='center',
127            text=ba.Lstr(
128                resource='exportSuccessText', subs=[('${NAME}', name)]
129            ),
130            maxwidth=self._width * 0.85,
131        )
132
133        ba.textwidget(
134            parent=self._root_widget,
135            position=(self._width * 0.5, self._height * 0.645),
136            size=(0, 0),
137            color=ba.app.ui.infotextcolor,
138            scale=0.6,
139            flatness=1.0,
140            h_align='center',
141            v_align='center',
142            text=ba.Lstr(resource='importPlaylistCodeInstructionsText'),
143            maxwidth=self._width * 0.85,
144        )
145
146        ba.textwidget(
147            parent=self._root_widget,
148            position=(self._width * 0.5, self._height * 0.4),
149            size=(0, 0),
150            color=(1.0, 3.0, 1.0),
151            scale=2.3,
152            h_align='center',
153            v_align='center',
154            text=data,
155            maxwidth=self._width * 0.85,
156        )
157
158    def close(self) -> None:
159        """Close the window."""
160        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))
 78    def __init__(
 79        self, name: str, data: str, origin: tuple[float, float] = (0.0, 0.0)
 80    ):
 81        del origin  # unused arg
 82        self._width = 450
 83        self._height = 300
 84        uiscale = ba.app.ui.uiscale
 85        super().__init__(
 86            root_widget=ba.containerwidget(
 87                size=(self._width, self._height),
 88                color=(0.45, 0.63, 0.15),
 89                transition='in_scale',
 90                scale=(
 91                    1.8
 92                    if uiscale is ba.UIScale.SMALL
 93                    else 1.35
 94                    if uiscale is ba.UIScale.MEDIUM
 95                    else 1.0
 96                ),
 97            )
 98        )
 99        ba.playsound(ba.getsound('cashRegister'))
100        ba.playsound(ba.getsound('swish'))
101
102        self._cancel_button = ba.buttonwidget(
103            parent=self._root_widget,
104            scale=0.7,
105            position=(40, self._height - 40),
106            size=(50, 50),
107            label='',
108            on_activate_call=self.close,
109            autoselect=True,
110            color=(0.45, 0.63, 0.15),
111            icon=ba.gettexture('crossOut'),
112            iconscale=1.2,
113        )
114        ba.containerwidget(
115            edit=self._root_widget, cancel_button=self._cancel_button
116        )
117
118        ba.textwidget(
119            parent=self._root_widget,
120            position=(self._width * 0.5, self._height * 0.745),
121            size=(0, 0),
122            color=ba.app.ui.infotextcolor,
123            scale=1.0,
124            flatness=1.0,
125            h_align='center',
126            v_align='center',
127            text=ba.Lstr(
128                resource='exportSuccessText', subs=[('${NAME}', name)]
129            ),
130            maxwidth=self._width * 0.85,
131        )
132
133        ba.textwidget(
134            parent=self._root_widget,
135            position=(self._width * 0.5, self._height * 0.645),
136            size=(0, 0),
137            color=ba.app.ui.infotextcolor,
138            scale=0.6,
139            flatness=1.0,
140            h_align='center',
141            v_align='center',
142            text=ba.Lstr(resource='importPlaylistCodeInstructionsText'),
143            maxwidth=self._width * 0.85,
144        )
145
146        ba.textwidget(
147            parent=self._root_widget,
148            position=(self._width * 0.5, self._height * 0.4),
149            size=(0, 0),
150            color=(1.0, 3.0, 1.0),
151            scale=2.3,
152            h_align='center',
153            v_align='center',
154            text=data,
155            maxwidth=self._width * 0.85,
156        )
def close(self) -> None:
158    def close(self) -> None:
159        """Close the window."""
160        ba.containerwidget(edit=self._root_widget, transition='out_scale')

Close the window.

Inherited Members
ba.ui.Window
get_root_widget