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