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')