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

Window for importing a shared playlist.

SharePlaylistImportWindow( origin_widget: _bauiv1.Widget | None = None, on_success_callback: Optional[Callable[[], Any]] = None)
23    def __init__(
24        self,
25        origin_widget: bui.Widget | None = None,
26        on_success_callback: Callable[[], Any] | None = None,
27    ):
28        PromoCodeWindow.__init__(self, modal=True, origin_widget=origin_widget)
29        self._on_success_callback = on_success_callback
Inherited Members
bauiv1._uitypes.Window
get_root_widget
class SharePlaylistResultsWindow(bauiv1._uitypes.Window):
 78class SharePlaylistResultsWindow(bui.Window):
 79    """Window for sharing playlists."""
 80
 81    def __init__(
 82        self, name: str, data: str, origin: tuple[float, float] = (0.0, 0.0)
 83    ):
 84        del origin  # unused arg
 85        self._width = 450
 86        self._height = 300
 87        assert bui.app.classic is not None
 88        uiscale = bui.app.ui_v1.uiscale
 89        super().__init__(
 90            root_widget=bui.containerwidget(
 91                size=(self._width, self._height),
 92                color=(0.45, 0.63, 0.15),
 93                transition='in_scale',
 94                scale=(
 95                    1.8
 96                    if uiscale is bui.UIScale.SMALL
 97                    else 1.35
 98                    if uiscale is bui.UIScale.MEDIUM
 99                    else 1.0
100                ),
101            )
102        )
103        bui.getsound('cashRegister').play()
104        bui.getsound('swish').play()
105
106        self._cancel_button = bui.buttonwidget(
107            parent=self._root_widget,
108            scale=0.7,
109            position=(40, self._height - 40),
110            size=(50, 50),
111            label='',
112            on_activate_call=self.close,
113            autoselect=True,
114            color=(0.45, 0.63, 0.15),
115            icon=bui.gettexture('crossOut'),
116            iconscale=1.2,
117        )
118        bui.containerwidget(
119            edit=self._root_widget, cancel_button=self._cancel_button
120        )
121
122        bui.textwidget(
123            parent=self._root_widget,
124            position=(self._width * 0.5, self._height * 0.745),
125            size=(0, 0),
126            color=bui.app.ui_v1.infotextcolor,
127            scale=1.0,
128            flatness=1.0,
129            h_align='center',
130            v_align='center',
131            text=bui.Lstr(
132                resource='exportSuccessText', subs=[('${NAME}', name)]
133            ),
134            maxwidth=self._width * 0.85,
135        )
136
137        bui.textwidget(
138            parent=self._root_widget,
139            position=(self._width * 0.5, self._height * 0.645),
140            size=(0, 0),
141            color=bui.app.ui_v1.infotextcolor,
142            scale=0.6,
143            flatness=1.0,
144            h_align='center',
145            v_align='center',
146            text=bui.Lstr(resource='importPlaylistCodeInstructionsText'),
147            maxwidth=self._width * 0.85,
148        )
149
150        bui.textwidget(
151            parent=self._root_widget,
152            position=(self._width * 0.5, self._height * 0.4),
153            size=(0, 0),
154            color=(1.0, 3.0, 1.0),
155            scale=2.3,
156            h_align='center',
157            v_align='center',
158            text=data,
159            maxwidth=self._width * 0.85,
160        )
161
162    def close(self) -> None:
163        """Close the window."""
164        bui.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))
 81    def __init__(
 82        self, name: str, data: str, origin: tuple[float, float] = (0.0, 0.0)
 83    ):
 84        del origin  # unused arg
 85        self._width = 450
 86        self._height = 300
 87        assert bui.app.classic is not None
 88        uiscale = bui.app.ui_v1.uiscale
 89        super().__init__(
 90            root_widget=bui.containerwidget(
 91                size=(self._width, self._height),
 92                color=(0.45, 0.63, 0.15),
 93                transition='in_scale',
 94                scale=(
 95                    1.8
 96                    if uiscale is bui.UIScale.SMALL
 97                    else 1.35
 98                    if uiscale is bui.UIScale.MEDIUM
 99                    else 1.0
100                ),
101            )
102        )
103        bui.getsound('cashRegister').play()
104        bui.getsound('swish').play()
105
106        self._cancel_button = bui.buttonwidget(
107            parent=self._root_widget,
108            scale=0.7,
109            position=(40, self._height - 40),
110            size=(50, 50),
111            label='',
112            on_activate_call=self.close,
113            autoselect=True,
114            color=(0.45, 0.63, 0.15),
115            icon=bui.gettexture('crossOut'),
116            iconscale=1.2,
117        )
118        bui.containerwidget(
119            edit=self._root_widget, cancel_button=self._cancel_button
120        )
121
122        bui.textwidget(
123            parent=self._root_widget,
124            position=(self._width * 0.5, self._height * 0.745),
125            size=(0, 0),
126            color=bui.app.ui_v1.infotextcolor,
127            scale=1.0,
128            flatness=1.0,
129            h_align='center',
130            v_align='center',
131            text=bui.Lstr(
132                resource='exportSuccessText', subs=[('${NAME}', name)]
133            ),
134            maxwidth=self._width * 0.85,
135        )
136
137        bui.textwidget(
138            parent=self._root_widget,
139            position=(self._width * 0.5, self._height * 0.645),
140            size=(0, 0),
141            color=bui.app.ui_v1.infotextcolor,
142            scale=0.6,
143            flatness=1.0,
144            h_align='center',
145            v_align='center',
146            text=bui.Lstr(resource='importPlaylistCodeInstructionsText'),
147            maxwidth=self._width * 0.85,
148        )
149
150        bui.textwidget(
151            parent=self._root_widget,
152            position=(self._width * 0.5, self._height * 0.4),
153            size=(0, 0),
154            color=(1.0, 3.0, 1.0),
155            scale=2.3,
156            h_align='center',
157            v_align='center',
158            text=data,
159            maxwidth=self._width * 0.85,
160        )
def close(self) -> None:
162    def close(self) -> None:
163        """Close the window."""
164        bui.containerwidget(edit=self._root_widget, transition='out_scale')

Close the window.

Inherited Members
bauiv1._uitypes.Window
get_root_widget