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

Close the window.

Inherited Members
bauiv1._uitypes.Window
get_root_widget