bauiv1lib.soundtrack.entrytypeselect

Provides UI for selecting soundtrack entry types.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""Provides UI for selecting soundtrack entry types."""
  4from __future__ import annotations
  5
  6import copy
  7from typing import TYPE_CHECKING
  8
  9import bauiv1 as bui
 10
 11if TYPE_CHECKING:
 12    from typing import Any, Callable
 13
 14
 15class SoundtrackEntryTypeSelectWindow(bui.Window):
 16    """Window for selecting a soundtrack entry type."""
 17
 18    def __init__(
 19        self,
 20        callback: Callable[[Any], Any],
 21        current_entry: Any,
 22        selection_target_name: str,
 23        transition: str = 'in_right',
 24    ):
 25        assert bui.app.classic is not None
 26        music = bui.app.classic.music
 27        self._r = 'editSoundtrackWindow'
 28
 29        self._callback = callback
 30        self._current_entry = copy.deepcopy(current_entry)
 31
 32        self._width = 580
 33        self._height = 220
 34        spacing = 80
 35
 36        # FIXME: Generalize this so new custom soundtrack types can add
 37        # themselves here.
 38        do_default = True
 39        do_mac_music_app_playlist = music.supports_soundtrack_entry_type(
 40            'iTunesPlaylist'
 41        )
 42        do_music_file = music.supports_soundtrack_entry_type('musicFile')
 43        do_music_folder = music.supports_soundtrack_entry_type('musicFolder')
 44
 45        if do_mac_music_app_playlist:
 46            self._height += spacing
 47        if do_music_file:
 48            self._height += spacing
 49        if do_music_folder:
 50            self._height += spacing
 51
 52        uiscale = bui.app.ui_v1.uiscale
 53
 54        # NOTE: When something is selected, we close our UI and kick off
 55        # another window which then calls us back when its done, so the
 56        # standard UI-cleanup-check complains that something is holding on
 57        # to our instance after its ui is gone. Should restructure in a
 58        # cleaner way, but just disabling that check for now.
 59        super().__init__(
 60            root_widget=bui.containerwidget(
 61                size=(self._width, self._height),
 62                transition=transition,
 63                scale=(
 64                    1.7
 65                    if uiscale is bui.UIScale.SMALL
 66                    else 1.4
 67                    if uiscale is bui.UIScale.MEDIUM
 68                    else 1.0
 69                ),
 70            ),
 71            cleanupcheck=False,
 72        )
 73        btn = bui.buttonwidget(
 74            parent=self._root_widget,
 75            position=(35, self._height - 65),
 76            size=(160, 60),
 77            scale=0.8,
 78            text_scale=1.2,
 79            label=bui.Lstr(resource='cancelText'),
 80            on_activate_call=self._on_cancel_press,
 81        )
 82        bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 83        bui.textwidget(
 84            parent=self._root_widget,
 85            position=(self._width * 0.5, self._height - 32),
 86            size=(0, 0),
 87            text=bui.Lstr(resource=self._r + '.selectASourceText'),
 88            color=bui.app.ui_v1.title_color,
 89            maxwidth=230,
 90            h_align='center',
 91            v_align='center',
 92        )
 93
 94        bui.textwidget(
 95            parent=self._root_widget,
 96            position=(self._width * 0.5, self._height - 56),
 97            size=(0, 0),
 98            text=selection_target_name,
 99            color=bui.app.ui_v1.infotextcolor,
100            scale=0.7,
101            maxwidth=230,
102            h_align='center',
103            v_align='center',
104        )
105
106        v = self._height - 155
107
108        current_entry_type = music.get_soundtrack_entry_type(current_entry)
109
110        if do_default:
111            btn = bui.buttonwidget(
112                parent=self._root_widget,
113                size=(self._width - 100, 60),
114                position=(50, v),
115                label=bui.Lstr(resource=self._r + '.useDefaultGameMusicText'),
116                on_activate_call=self._on_default_press,
117            )
118            if current_entry_type == 'default':
119                bui.containerwidget(edit=self._root_widget, selected_child=btn)
120            v -= spacing
121
122        if do_mac_music_app_playlist:
123            btn = bui.buttonwidget(
124                parent=self._root_widget,
125                size=(self._width - 100, 60),
126                position=(50, v),
127                label=bui.Lstr(resource=self._r + '.useITunesPlaylistText'),
128                on_activate_call=self._on_mac_music_app_playlist_press,
129                icon=None,
130            )
131            if current_entry_type == 'iTunesPlaylist':
132                bui.containerwidget(edit=self._root_widget, selected_child=btn)
133            v -= spacing
134
135        if do_music_file:
136            btn = bui.buttonwidget(
137                parent=self._root_widget,
138                size=(self._width - 100, 60),
139                position=(50, v),
140                label=bui.Lstr(resource=self._r + '.useMusicFileText'),
141                on_activate_call=self._on_music_file_press,
142                icon=bui.gettexture('file'),
143            )
144            if current_entry_type == 'musicFile':
145                bui.containerwidget(edit=self._root_widget, selected_child=btn)
146            v -= spacing
147
148        if do_music_folder:
149            btn = bui.buttonwidget(
150                parent=self._root_widget,
151                size=(self._width - 100, 60),
152                position=(50, v),
153                label=bui.Lstr(resource=self._r + '.useMusicFolderText'),
154                on_activate_call=self._on_music_folder_press,
155                icon=bui.gettexture('folder'),
156                icon_color=(1.1, 0.8, 0.2),
157            )
158            if current_entry_type == 'musicFolder':
159                bui.containerwidget(edit=self._root_widget, selected_child=btn)
160            v -= spacing
161
162    def _on_mac_music_app_playlist_press(self) -> None:
163        assert bui.app.classic is not None
164        music = bui.app.classic.music
165        from bauiv1lib.soundtrack.macmusicapp import (
166            MacMusicAppPlaylistSelectWindow,
167        )
168
169        # no-op if our underlying widget is dead or on its way out.
170        if not self._root_widget or self._root_widget.transitioning_out:
171            return
172
173        bui.containerwidget(edit=self._root_widget, transition='out_left')
174
175        current_playlist_entry: str | None
176        if (
177            music.get_soundtrack_entry_type(self._current_entry)
178            == 'iTunesPlaylist'
179        ):
180            current_playlist_entry = music.get_soundtrack_entry_name(
181                self._current_entry
182            )
183        else:
184            current_playlist_entry = None
185        bui.app.ui_v1.set_main_menu_window(
186            MacMusicAppPlaylistSelectWindow(
187                self._callback, current_playlist_entry, self._current_entry
188            ).get_root_widget(),
189            from_window=self._root_widget,
190        )
191
192    def _on_music_file_press(self) -> None:
193        from babase import android_get_external_files_dir
194        from baclassic.osmusic import OSMusicPlayer
195        from bauiv1lib.fileselector import FileSelectorWindow
196
197        # no-op if our underlying widget is dead or on its way out.
198        if not self._root_widget or self._root_widget.transitioning_out:
199            return
200
201        bui.containerwidget(edit=self._root_widget, transition='out_left')
202        base_path = android_get_external_files_dir()
203        assert bui.app.classic is not None
204        bui.app.ui_v1.set_main_menu_window(
205            FileSelectorWindow(
206                base_path,
207                callback=self._music_file_selector_cb,
208                show_base_path=False,
209                valid_file_extensions=(
210                    OSMusicPlayer.get_valid_music_file_extensions()
211                ),
212                allow_folders=False,
213            ).get_root_widget(),
214            from_window=self._root_widget,
215        )
216
217    def _on_music_folder_press(self) -> None:
218        from bauiv1lib.fileselector import FileSelectorWindow
219        from babase import android_get_external_files_dir
220
221        # no-op if our underlying widget is dead or on its way out.
222        if not self._root_widget or self._root_widget.transitioning_out:
223            return
224
225        bui.containerwidget(edit=self._root_widget, transition='out_left')
226        base_path = android_get_external_files_dir()
227        assert bui.app.classic is not None
228        bui.app.ui_v1.set_main_menu_window(
229            FileSelectorWindow(
230                base_path,
231                callback=self._music_folder_selector_cb,
232                show_base_path=False,
233                valid_file_extensions=[],
234                allow_folders=True,
235            ).get_root_widget(),
236            from_window=self._root_widget,
237        )
238
239    def _music_file_selector_cb(self, result: str | None) -> None:
240        if result is None:
241            self._callback(self._current_entry)
242        else:
243            self._callback({'type': 'musicFile', 'name': result})
244
245    def _music_folder_selector_cb(self, result: str | None) -> None:
246        if result is None:
247            self._callback(self._current_entry)
248        else:
249            self._callback({'type': 'musicFolder', 'name': result})
250
251    def _on_default_press(self) -> None:
252        bui.containerwidget(edit=self._root_widget, transition='out_right')
253        self._callback(None)
254
255    def _on_cancel_press(self) -> None:
256        bui.containerwidget(edit=self._root_widget, transition='out_right')
257        self._callback(self._current_entry)
class SoundtrackEntryTypeSelectWindow(bauiv1._uitypes.Window):
 16class SoundtrackEntryTypeSelectWindow(bui.Window):
 17    """Window for selecting a soundtrack entry type."""
 18
 19    def __init__(
 20        self,
 21        callback: Callable[[Any], Any],
 22        current_entry: Any,
 23        selection_target_name: str,
 24        transition: str = 'in_right',
 25    ):
 26        assert bui.app.classic is not None
 27        music = bui.app.classic.music
 28        self._r = 'editSoundtrackWindow'
 29
 30        self._callback = callback
 31        self._current_entry = copy.deepcopy(current_entry)
 32
 33        self._width = 580
 34        self._height = 220
 35        spacing = 80
 36
 37        # FIXME: Generalize this so new custom soundtrack types can add
 38        # themselves here.
 39        do_default = True
 40        do_mac_music_app_playlist = music.supports_soundtrack_entry_type(
 41            'iTunesPlaylist'
 42        )
 43        do_music_file = music.supports_soundtrack_entry_type('musicFile')
 44        do_music_folder = music.supports_soundtrack_entry_type('musicFolder')
 45
 46        if do_mac_music_app_playlist:
 47            self._height += spacing
 48        if do_music_file:
 49            self._height += spacing
 50        if do_music_folder:
 51            self._height += spacing
 52
 53        uiscale = bui.app.ui_v1.uiscale
 54
 55        # NOTE: When something is selected, we close our UI and kick off
 56        # another window which then calls us back when its done, so the
 57        # standard UI-cleanup-check complains that something is holding on
 58        # to our instance after its ui is gone. Should restructure in a
 59        # cleaner way, but just disabling that check for now.
 60        super().__init__(
 61            root_widget=bui.containerwidget(
 62                size=(self._width, self._height),
 63                transition=transition,
 64                scale=(
 65                    1.7
 66                    if uiscale is bui.UIScale.SMALL
 67                    else 1.4
 68                    if uiscale is bui.UIScale.MEDIUM
 69                    else 1.0
 70                ),
 71            ),
 72            cleanupcheck=False,
 73        )
 74        btn = bui.buttonwidget(
 75            parent=self._root_widget,
 76            position=(35, self._height - 65),
 77            size=(160, 60),
 78            scale=0.8,
 79            text_scale=1.2,
 80            label=bui.Lstr(resource='cancelText'),
 81            on_activate_call=self._on_cancel_press,
 82        )
 83        bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 84        bui.textwidget(
 85            parent=self._root_widget,
 86            position=(self._width * 0.5, self._height - 32),
 87            size=(0, 0),
 88            text=bui.Lstr(resource=self._r + '.selectASourceText'),
 89            color=bui.app.ui_v1.title_color,
 90            maxwidth=230,
 91            h_align='center',
 92            v_align='center',
 93        )
 94
 95        bui.textwidget(
 96            parent=self._root_widget,
 97            position=(self._width * 0.5, self._height - 56),
 98            size=(0, 0),
 99            text=selection_target_name,
100            color=bui.app.ui_v1.infotextcolor,
101            scale=0.7,
102            maxwidth=230,
103            h_align='center',
104            v_align='center',
105        )
106
107        v = self._height - 155
108
109        current_entry_type = music.get_soundtrack_entry_type(current_entry)
110
111        if do_default:
112            btn = bui.buttonwidget(
113                parent=self._root_widget,
114                size=(self._width - 100, 60),
115                position=(50, v),
116                label=bui.Lstr(resource=self._r + '.useDefaultGameMusicText'),
117                on_activate_call=self._on_default_press,
118            )
119            if current_entry_type == 'default':
120                bui.containerwidget(edit=self._root_widget, selected_child=btn)
121            v -= spacing
122
123        if do_mac_music_app_playlist:
124            btn = bui.buttonwidget(
125                parent=self._root_widget,
126                size=(self._width - 100, 60),
127                position=(50, v),
128                label=bui.Lstr(resource=self._r + '.useITunesPlaylistText'),
129                on_activate_call=self._on_mac_music_app_playlist_press,
130                icon=None,
131            )
132            if current_entry_type == 'iTunesPlaylist':
133                bui.containerwidget(edit=self._root_widget, selected_child=btn)
134            v -= spacing
135
136        if do_music_file:
137            btn = bui.buttonwidget(
138                parent=self._root_widget,
139                size=(self._width - 100, 60),
140                position=(50, v),
141                label=bui.Lstr(resource=self._r + '.useMusicFileText'),
142                on_activate_call=self._on_music_file_press,
143                icon=bui.gettexture('file'),
144            )
145            if current_entry_type == 'musicFile':
146                bui.containerwidget(edit=self._root_widget, selected_child=btn)
147            v -= spacing
148
149        if do_music_folder:
150            btn = bui.buttonwidget(
151                parent=self._root_widget,
152                size=(self._width - 100, 60),
153                position=(50, v),
154                label=bui.Lstr(resource=self._r + '.useMusicFolderText'),
155                on_activate_call=self._on_music_folder_press,
156                icon=bui.gettexture('folder'),
157                icon_color=(1.1, 0.8, 0.2),
158            )
159            if current_entry_type == 'musicFolder':
160                bui.containerwidget(edit=self._root_widget, selected_child=btn)
161            v -= spacing
162
163    def _on_mac_music_app_playlist_press(self) -> None:
164        assert bui.app.classic is not None
165        music = bui.app.classic.music
166        from bauiv1lib.soundtrack.macmusicapp import (
167            MacMusicAppPlaylistSelectWindow,
168        )
169
170        # no-op if our underlying widget is dead or on its way out.
171        if not self._root_widget or self._root_widget.transitioning_out:
172            return
173
174        bui.containerwidget(edit=self._root_widget, transition='out_left')
175
176        current_playlist_entry: str | None
177        if (
178            music.get_soundtrack_entry_type(self._current_entry)
179            == 'iTunesPlaylist'
180        ):
181            current_playlist_entry = music.get_soundtrack_entry_name(
182                self._current_entry
183            )
184        else:
185            current_playlist_entry = None
186        bui.app.ui_v1.set_main_menu_window(
187            MacMusicAppPlaylistSelectWindow(
188                self._callback, current_playlist_entry, self._current_entry
189            ).get_root_widget(),
190            from_window=self._root_widget,
191        )
192
193    def _on_music_file_press(self) -> None:
194        from babase import android_get_external_files_dir
195        from baclassic.osmusic import OSMusicPlayer
196        from bauiv1lib.fileselector import FileSelectorWindow
197
198        # no-op if our underlying widget is dead or on its way out.
199        if not self._root_widget or self._root_widget.transitioning_out:
200            return
201
202        bui.containerwidget(edit=self._root_widget, transition='out_left')
203        base_path = android_get_external_files_dir()
204        assert bui.app.classic is not None
205        bui.app.ui_v1.set_main_menu_window(
206            FileSelectorWindow(
207                base_path,
208                callback=self._music_file_selector_cb,
209                show_base_path=False,
210                valid_file_extensions=(
211                    OSMusicPlayer.get_valid_music_file_extensions()
212                ),
213                allow_folders=False,
214            ).get_root_widget(),
215            from_window=self._root_widget,
216        )
217
218    def _on_music_folder_press(self) -> None:
219        from bauiv1lib.fileselector import FileSelectorWindow
220        from babase import android_get_external_files_dir
221
222        # no-op if our underlying widget is dead or on its way out.
223        if not self._root_widget or self._root_widget.transitioning_out:
224            return
225
226        bui.containerwidget(edit=self._root_widget, transition='out_left')
227        base_path = android_get_external_files_dir()
228        assert bui.app.classic is not None
229        bui.app.ui_v1.set_main_menu_window(
230            FileSelectorWindow(
231                base_path,
232                callback=self._music_folder_selector_cb,
233                show_base_path=False,
234                valid_file_extensions=[],
235                allow_folders=True,
236            ).get_root_widget(),
237            from_window=self._root_widget,
238        )
239
240    def _music_file_selector_cb(self, result: str | None) -> None:
241        if result is None:
242            self._callback(self._current_entry)
243        else:
244            self._callback({'type': 'musicFile', 'name': result})
245
246    def _music_folder_selector_cb(self, result: str | None) -> None:
247        if result is None:
248            self._callback(self._current_entry)
249        else:
250            self._callback({'type': 'musicFolder', 'name': result})
251
252    def _on_default_press(self) -> None:
253        bui.containerwidget(edit=self._root_widget, transition='out_right')
254        self._callback(None)
255
256    def _on_cancel_press(self) -> None:
257        bui.containerwidget(edit=self._root_widget, transition='out_right')
258        self._callback(self._current_entry)

Window for selecting a soundtrack entry type.

SoundtrackEntryTypeSelectWindow( callback: Callable[[Any], Any], current_entry: Any, selection_target_name: str, transition: str = 'in_right')
 19    def __init__(
 20        self,
 21        callback: Callable[[Any], Any],
 22        current_entry: Any,
 23        selection_target_name: str,
 24        transition: str = 'in_right',
 25    ):
 26        assert bui.app.classic is not None
 27        music = bui.app.classic.music
 28        self._r = 'editSoundtrackWindow'
 29
 30        self._callback = callback
 31        self._current_entry = copy.deepcopy(current_entry)
 32
 33        self._width = 580
 34        self._height = 220
 35        spacing = 80
 36
 37        # FIXME: Generalize this so new custom soundtrack types can add
 38        # themselves here.
 39        do_default = True
 40        do_mac_music_app_playlist = music.supports_soundtrack_entry_type(
 41            'iTunesPlaylist'
 42        )
 43        do_music_file = music.supports_soundtrack_entry_type('musicFile')
 44        do_music_folder = music.supports_soundtrack_entry_type('musicFolder')
 45
 46        if do_mac_music_app_playlist:
 47            self._height += spacing
 48        if do_music_file:
 49            self._height += spacing
 50        if do_music_folder:
 51            self._height += spacing
 52
 53        uiscale = bui.app.ui_v1.uiscale
 54
 55        # NOTE: When something is selected, we close our UI and kick off
 56        # another window which then calls us back when its done, so the
 57        # standard UI-cleanup-check complains that something is holding on
 58        # to our instance after its ui is gone. Should restructure in a
 59        # cleaner way, but just disabling that check for now.
 60        super().__init__(
 61            root_widget=bui.containerwidget(
 62                size=(self._width, self._height),
 63                transition=transition,
 64                scale=(
 65                    1.7
 66                    if uiscale is bui.UIScale.SMALL
 67                    else 1.4
 68                    if uiscale is bui.UIScale.MEDIUM
 69                    else 1.0
 70                ),
 71            ),
 72            cleanupcheck=False,
 73        )
 74        btn = bui.buttonwidget(
 75            parent=self._root_widget,
 76            position=(35, self._height - 65),
 77            size=(160, 60),
 78            scale=0.8,
 79            text_scale=1.2,
 80            label=bui.Lstr(resource='cancelText'),
 81            on_activate_call=self._on_cancel_press,
 82        )
 83        bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 84        bui.textwidget(
 85            parent=self._root_widget,
 86            position=(self._width * 0.5, self._height - 32),
 87            size=(0, 0),
 88            text=bui.Lstr(resource=self._r + '.selectASourceText'),
 89            color=bui.app.ui_v1.title_color,
 90            maxwidth=230,
 91            h_align='center',
 92            v_align='center',
 93        )
 94
 95        bui.textwidget(
 96            parent=self._root_widget,
 97            position=(self._width * 0.5, self._height - 56),
 98            size=(0, 0),
 99            text=selection_target_name,
100            color=bui.app.ui_v1.infotextcolor,
101            scale=0.7,
102            maxwidth=230,
103            h_align='center',
104            v_align='center',
105        )
106
107        v = self._height - 155
108
109        current_entry_type = music.get_soundtrack_entry_type(current_entry)
110
111        if do_default:
112            btn = bui.buttonwidget(
113                parent=self._root_widget,
114                size=(self._width - 100, 60),
115                position=(50, v),
116                label=bui.Lstr(resource=self._r + '.useDefaultGameMusicText'),
117                on_activate_call=self._on_default_press,
118            )
119            if current_entry_type == 'default':
120                bui.containerwidget(edit=self._root_widget, selected_child=btn)
121            v -= spacing
122
123        if do_mac_music_app_playlist:
124            btn = bui.buttonwidget(
125                parent=self._root_widget,
126                size=(self._width - 100, 60),
127                position=(50, v),
128                label=bui.Lstr(resource=self._r + '.useITunesPlaylistText'),
129                on_activate_call=self._on_mac_music_app_playlist_press,
130                icon=None,
131            )
132            if current_entry_type == 'iTunesPlaylist':
133                bui.containerwidget(edit=self._root_widget, selected_child=btn)
134            v -= spacing
135
136        if do_music_file:
137            btn = bui.buttonwidget(
138                parent=self._root_widget,
139                size=(self._width - 100, 60),
140                position=(50, v),
141                label=bui.Lstr(resource=self._r + '.useMusicFileText'),
142                on_activate_call=self._on_music_file_press,
143                icon=bui.gettexture('file'),
144            )
145            if current_entry_type == 'musicFile':
146                bui.containerwidget(edit=self._root_widget, selected_child=btn)
147            v -= spacing
148
149        if do_music_folder:
150            btn = bui.buttonwidget(
151                parent=self._root_widget,
152                size=(self._width - 100, 60),
153                position=(50, v),
154                label=bui.Lstr(resource=self._r + '.useMusicFolderText'),
155                on_activate_call=self._on_music_folder_press,
156                icon=bui.gettexture('folder'),
157                icon_color=(1.1, 0.8, 0.2),
158            )
159            if current_entry_type == 'musicFolder':
160                bui.containerwidget(edit=self._root_widget, selected_child=btn)
161            v -= spacing
Inherited Members
bauiv1._uitypes.Window
get_root_widget