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 if uiscale is bui.UIScale.MEDIUM else 1.0
 67                ),
 68            ),
 69            cleanupcheck=False,
 70        )
 71        btn = bui.buttonwidget(
 72            parent=self._root_widget,
 73            position=(35, self._height - 65),
 74            size=(160, 60),
 75            scale=0.8,
 76            text_scale=1.2,
 77            label=bui.Lstr(resource='cancelText'),
 78            on_activate_call=self._on_cancel_press,
 79        )
 80        bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 81        bui.textwidget(
 82            parent=self._root_widget,
 83            position=(self._width * 0.5, self._height - 32),
 84            size=(0, 0),
 85            text=bui.Lstr(resource=self._r + '.selectASourceText'),
 86            color=bui.app.ui_v1.title_color,
 87            maxwidth=230,
 88            h_align='center',
 89            v_align='center',
 90        )
 91
 92        bui.textwidget(
 93            parent=self._root_widget,
 94            position=(self._width * 0.5, self._height - 56),
 95            size=(0, 0),
 96            text=selection_target_name,
 97            color=bui.app.ui_v1.infotextcolor,
 98            scale=0.7,
 99            maxwidth=230,
100            h_align='center',
101            v_align='center',
102        )
103
104        v = self._height - 155
105
106        current_entry_type = music.get_soundtrack_entry_type(current_entry)
107
108        if do_default:
109            btn = bui.buttonwidget(
110                parent=self._root_widget,
111                size=(self._width - 100, 60),
112                position=(50, v),
113                label=bui.Lstr(resource=self._r + '.useDefaultGameMusicText'),
114                on_activate_call=self._on_default_press,
115            )
116            if current_entry_type == 'default':
117                bui.containerwidget(edit=self._root_widget, selected_child=btn)
118            v -= spacing
119
120        if do_mac_music_app_playlist:
121            btn = bui.buttonwidget(
122                parent=self._root_widget,
123                size=(self._width - 100, 60),
124                position=(50, v),
125                label=bui.Lstr(resource=self._r + '.useITunesPlaylistText'),
126                on_activate_call=self._on_mac_music_app_playlist_press,
127                icon=None,
128            )
129            if current_entry_type == 'iTunesPlaylist':
130                bui.containerwidget(edit=self._root_widget, selected_child=btn)
131            v -= spacing
132
133        if do_music_file:
134            btn = bui.buttonwidget(
135                parent=self._root_widget,
136                size=(self._width - 100, 60),
137                position=(50, v),
138                label=bui.Lstr(resource=self._r + '.useMusicFileText'),
139                on_activate_call=self._on_music_file_press,
140                icon=bui.gettexture('file'),
141            )
142            if current_entry_type == 'musicFile':
143                bui.containerwidget(edit=self._root_widget, selected_child=btn)
144            v -= spacing
145
146        if do_music_folder:
147            btn = bui.buttonwidget(
148                parent=self._root_widget,
149                size=(self._width - 100, 60),
150                position=(50, v),
151                label=bui.Lstr(resource=self._r + '.useMusicFolderText'),
152                on_activate_call=self._on_music_folder_press,
153                icon=bui.gettexture('folder'),
154                icon_color=(1.1, 0.8, 0.2),
155            )
156            if current_entry_type == 'musicFolder':
157                bui.containerwidget(edit=self._root_widget, selected_child=btn)
158            v -= spacing
159
160    def _on_mac_music_app_playlist_press(self) -> None:
161        assert bui.app.classic is not None
162        music = bui.app.classic.music
163        from bauiv1lib.soundtrack.macmusicapp import (
164            MacMusicAppPlaylistSelectWindow,
165        )
166
167        # no-op if our underlying widget is dead or on its way out.
168        if not self._root_widget or self._root_widget.transitioning_out:
169            return
170
171        bui.containerwidget(edit=self._root_widget, transition='out_left')
172
173        current_playlist_entry: str | None
174        if (
175            music.get_soundtrack_entry_type(self._current_entry)
176            == 'iTunesPlaylist'
177        ):
178            current_playlist_entry = music.get_soundtrack_entry_name(
179                self._current_entry
180            )
181        else:
182            current_playlist_entry = None
183        bui.app.ui_v1.set_main_menu_window(
184            MacMusicAppPlaylistSelectWindow(
185                self._callback, current_playlist_entry, self._current_entry
186            ).get_root_widget(),
187            from_window=self._root_widget,
188        )
189
190    def _on_music_file_press(self) -> None:
191        from babase import android_get_external_files_dir
192        from baclassic.osmusic import OSMusicPlayer
193        from bauiv1lib.fileselector import FileSelectorWindow
194
195        # no-op if our underlying widget is dead or on its way out.
196        if not self._root_widget or self._root_widget.transitioning_out:
197            return
198
199        bui.containerwidget(edit=self._root_widget, transition='out_left')
200        base_path = android_get_external_files_dir()
201        assert bui.app.classic is not None
202        bui.app.ui_v1.set_main_menu_window(
203            FileSelectorWindow(
204                base_path,
205                callback=self._music_file_selector_cb,
206                show_base_path=False,
207                valid_file_extensions=(
208                    OSMusicPlayer.get_valid_music_file_extensions()
209                ),
210                allow_folders=False,
211            ).get_root_widget(),
212            from_window=self._root_widget,
213        )
214
215    def _on_music_folder_press(self) -> None:
216        from bauiv1lib.fileselector import FileSelectorWindow
217        from babase import android_get_external_files_dir
218
219        # no-op if our underlying widget is dead or on its way out.
220        if not self._root_widget or self._root_widget.transitioning_out:
221            return
222
223        bui.containerwidget(edit=self._root_widget, transition='out_left')
224        base_path = android_get_external_files_dir()
225        assert bui.app.classic is not None
226        bui.app.ui_v1.set_main_menu_window(
227            FileSelectorWindow(
228                base_path,
229                callback=self._music_folder_selector_cb,
230                show_base_path=False,
231                valid_file_extensions=[],
232                allow_folders=True,
233            ).get_root_widget(),
234            from_window=self._root_widget,
235        )
236
237    def _music_file_selector_cb(self, result: str | None) -> None:
238        if result is None:
239            self._callback(self._current_entry)
240        else:
241            self._callback({'type': 'musicFile', 'name': result})
242
243    def _music_folder_selector_cb(self, result: str | None) -> None:
244        if result is None:
245            self._callback(self._current_entry)
246        else:
247            self._callback({'type': 'musicFolder', 'name': result})
248
249    def _on_default_press(self) -> None:
250        bui.containerwidget(edit=self._root_widget, transition='out_right')
251        self._callback(None)
252
253    def _on_cancel_press(self) -> None:
254        bui.containerwidget(edit=self._root_widget, transition='out_right')
255        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 if uiscale is bui.UIScale.MEDIUM else 1.0
 68                ),
 69            ),
 70            cleanupcheck=False,
 71        )
 72        btn = bui.buttonwidget(
 73            parent=self._root_widget,
 74            position=(35, self._height - 65),
 75            size=(160, 60),
 76            scale=0.8,
 77            text_scale=1.2,
 78            label=bui.Lstr(resource='cancelText'),
 79            on_activate_call=self._on_cancel_press,
 80        )
 81        bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 82        bui.textwidget(
 83            parent=self._root_widget,
 84            position=(self._width * 0.5, self._height - 32),
 85            size=(0, 0),
 86            text=bui.Lstr(resource=self._r + '.selectASourceText'),
 87            color=bui.app.ui_v1.title_color,
 88            maxwidth=230,
 89            h_align='center',
 90            v_align='center',
 91        )
 92
 93        bui.textwidget(
 94            parent=self._root_widget,
 95            position=(self._width * 0.5, self._height - 56),
 96            size=(0, 0),
 97            text=selection_target_name,
 98            color=bui.app.ui_v1.infotextcolor,
 99            scale=0.7,
100            maxwidth=230,
101            h_align='center',
102            v_align='center',
103        )
104
105        v = self._height - 155
106
107        current_entry_type = music.get_soundtrack_entry_type(current_entry)
108
109        if do_default:
110            btn = bui.buttonwidget(
111                parent=self._root_widget,
112                size=(self._width - 100, 60),
113                position=(50, v),
114                label=bui.Lstr(resource=self._r + '.useDefaultGameMusicText'),
115                on_activate_call=self._on_default_press,
116            )
117            if current_entry_type == 'default':
118                bui.containerwidget(edit=self._root_widget, selected_child=btn)
119            v -= spacing
120
121        if do_mac_music_app_playlist:
122            btn = bui.buttonwidget(
123                parent=self._root_widget,
124                size=(self._width - 100, 60),
125                position=(50, v),
126                label=bui.Lstr(resource=self._r + '.useITunesPlaylistText'),
127                on_activate_call=self._on_mac_music_app_playlist_press,
128                icon=None,
129            )
130            if current_entry_type == 'iTunesPlaylist':
131                bui.containerwidget(edit=self._root_widget, selected_child=btn)
132            v -= spacing
133
134        if do_music_file:
135            btn = bui.buttonwidget(
136                parent=self._root_widget,
137                size=(self._width - 100, 60),
138                position=(50, v),
139                label=bui.Lstr(resource=self._r + '.useMusicFileText'),
140                on_activate_call=self._on_music_file_press,
141                icon=bui.gettexture('file'),
142            )
143            if current_entry_type == 'musicFile':
144                bui.containerwidget(edit=self._root_widget, selected_child=btn)
145            v -= spacing
146
147        if do_music_folder:
148            btn = bui.buttonwidget(
149                parent=self._root_widget,
150                size=(self._width - 100, 60),
151                position=(50, v),
152                label=bui.Lstr(resource=self._r + '.useMusicFolderText'),
153                on_activate_call=self._on_music_folder_press,
154                icon=bui.gettexture('folder'),
155                icon_color=(1.1, 0.8, 0.2),
156            )
157            if current_entry_type == 'musicFolder':
158                bui.containerwidget(edit=self._root_widget, selected_child=btn)
159            v -= spacing
160
161    def _on_mac_music_app_playlist_press(self) -> None:
162        assert bui.app.classic is not None
163        music = bui.app.classic.music
164        from bauiv1lib.soundtrack.macmusicapp import (
165            MacMusicAppPlaylistSelectWindow,
166        )
167
168        # no-op if our underlying widget is dead or on its way out.
169        if not self._root_widget or self._root_widget.transitioning_out:
170            return
171
172        bui.containerwidget(edit=self._root_widget, transition='out_left')
173
174        current_playlist_entry: str | None
175        if (
176            music.get_soundtrack_entry_type(self._current_entry)
177            == 'iTunesPlaylist'
178        ):
179            current_playlist_entry = music.get_soundtrack_entry_name(
180                self._current_entry
181            )
182        else:
183            current_playlist_entry = None
184        bui.app.ui_v1.set_main_menu_window(
185            MacMusicAppPlaylistSelectWindow(
186                self._callback, current_playlist_entry, self._current_entry
187            ).get_root_widget(),
188            from_window=self._root_widget,
189        )
190
191    def _on_music_file_press(self) -> None:
192        from babase import android_get_external_files_dir
193        from baclassic.osmusic import OSMusicPlayer
194        from bauiv1lib.fileselector import FileSelectorWindow
195
196        # no-op if our underlying widget is dead or on its way out.
197        if not self._root_widget or self._root_widget.transitioning_out:
198            return
199
200        bui.containerwidget(edit=self._root_widget, transition='out_left')
201        base_path = android_get_external_files_dir()
202        assert bui.app.classic is not None
203        bui.app.ui_v1.set_main_menu_window(
204            FileSelectorWindow(
205                base_path,
206                callback=self._music_file_selector_cb,
207                show_base_path=False,
208                valid_file_extensions=(
209                    OSMusicPlayer.get_valid_music_file_extensions()
210                ),
211                allow_folders=False,
212            ).get_root_widget(),
213            from_window=self._root_widget,
214        )
215
216    def _on_music_folder_press(self) -> None:
217        from bauiv1lib.fileselector import FileSelectorWindow
218        from babase import android_get_external_files_dir
219
220        # no-op if our underlying widget is dead or on its way out.
221        if not self._root_widget or self._root_widget.transitioning_out:
222            return
223
224        bui.containerwidget(edit=self._root_widget, transition='out_left')
225        base_path = android_get_external_files_dir()
226        assert bui.app.classic is not None
227        bui.app.ui_v1.set_main_menu_window(
228            FileSelectorWindow(
229                base_path,
230                callback=self._music_folder_selector_cb,
231                show_base_path=False,
232                valid_file_extensions=[],
233                allow_folders=True,
234            ).get_root_widget(),
235            from_window=self._root_widget,
236        )
237
238    def _music_file_selector_cb(self, result: str | None) -> None:
239        if result is None:
240            self._callback(self._current_entry)
241        else:
242            self._callback({'type': 'musicFile', 'name': result})
243
244    def _music_folder_selector_cb(self, result: str | None) -> None:
245        if result is None:
246            self._callback(self._current_entry)
247        else:
248            self._callback({'type': 'musicFolder', 'name': result})
249
250    def _on_default_press(self) -> None:
251        bui.containerwidget(edit=self._root_widget, transition='out_right')
252        self._callback(None)
253
254    def _on_cancel_press(self) -> None:
255        bui.containerwidget(edit=self._root_widget, transition='out_right')
256        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 if uiscale is bui.UIScale.MEDIUM else 1.0
 68                ),
 69            ),
 70            cleanupcheck=False,
 71        )
 72        btn = bui.buttonwidget(
 73            parent=self._root_widget,
 74            position=(35, self._height - 65),
 75            size=(160, 60),
 76            scale=0.8,
 77            text_scale=1.2,
 78            label=bui.Lstr(resource='cancelText'),
 79            on_activate_call=self._on_cancel_press,
 80        )
 81        bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 82        bui.textwidget(
 83            parent=self._root_widget,
 84            position=(self._width * 0.5, self._height - 32),
 85            size=(0, 0),
 86            text=bui.Lstr(resource=self._r + '.selectASourceText'),
 87            color=bui.app.ui_v1.title_color,
 88            maxwidth=230,
 89            h_align='center',
 90            v_align='center',
 91        )
 92
 93        bui.textwidget(
 94            parent=self._root_widget,
 95            position=(self._width * 0.5, self._height - 56),
 96            size=(0, 0),
 97            text=selection_target_name,
 98            color=bui.app.ui_v1.infotextcolor,
 99            scale=0.7,
100            maxwidth=230,
101            h_align='center',
102            v_align='center',
103        )
104
105        v = self._height - 155
106
107        current_entry_type = music.get_soundtrack_entry_type(current_entry)
108
109        if do_default:
110            btn = bui.buttonwidget(
111                parent=self._root_widget,
112                size=(self._width - 100, 60),
113                position=(50, v),
114                label=bui.Lstr(resource=self._r + '.useDefaultGameMusicText'),
115                on_activate_call=self._on_default_press,
116            )
117            if current_entry_type == 'default':
118                bui.containerwidget(edit=self._root_widget, selected_child=btn)
119            v -= spacing
120
121        if do_mac_music_app_playlist:
122            btn = bui.buttonwidget(
123                parent=self._root_widget,
124                size=(self._width - 100, 60),
125                position=(50, v),
126                label=bui.Lstr(resource=self._r + '.useITunesPlaylistText'),
127                on_activate_call=self._on_mac_music_app_playlist_press,
128                icon=None,
129            )
130            if current_entry_type == 'iTunesPlaylist':
131                bui.containerwidget(edit=self._root_widget, selected_child=btn)
132            v -= spacing
133
134        if do_music_file:
135            btn = bui.buttonwidget(
136                parent=self._root_widget,
137                size=(self._width - 100, 60),
138                position=(50, v),
139                label=bui.Lstr(resource=self._r + '.useMusicFileText'),
140                on_activate_call=self._on_music_file_press,
141                icon=bui.gettexture('file'),
142            )
143            if current_entry_type == 'musicFile':
144                bui.containerwidget(edit=self._root_widget, selected_child=btn)
145            v -= spacing
146
147        if do_music_folder:
148            btn = bui.buttonwidget(
149                parent=self._root_widget,
150                size=(self._width - 100, 60),
151                position=(50, v),
152                label=bui.Lstr(resource=self._r + '.useMusicFolderText'),
153                on_activate_call=self._on_music_folder_press,
154                icon=bui.gettexture('folder'),
155                icon_color=(1.1, 0.8, 0.2),
156            )
157            if current_entry_type == 'musicFolder':
158                bui.containerwidget(edit=self._root_widget, selected_child=btn)
159            v -= spacing
Inherited Members
bauiv1._uitypes.Window
get_root_widget