bastd.ui.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 ba
 10import ba.internal
 11
 12if TYPE_CHECKING:
 13    from typing import Any, Callable
 14
 15
 16class SoundtrackEntryTypeSelectWindow(ba.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        music = ba.app.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 = ba.app.ui.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=ba.containerwidget(
 61                size=(self._width, self._height),
 62                transition=transition,
 63                scale=(
 64                    1.7
 65                    if uiscale is ba.UIScale.SMALL
 66                    else 1.4
 67                    if uiscale is ba.UIScale.MEDIUM
 68                    else 1.0
 69                ),
 70            ),
 71            cleanupcheck=False,
 72        )
 73        btn = ba.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=ba.Lstr(resource='cancelText'),
 80            on_activate_call=self._on_cancel_press,
 81        )
 82        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
 83        ba.textwidget(
 84            parent=self._root_widget,
 85            position=(self._width * 0.5, self._height - 32),
 86            size=(0, 0),
 87            text=ba.Lstr(resource=self._r + '.selectASourceText'),
 88            color=ba.app.ui.title_color,
 89            maxwidth=230,
 90            h_align='center',
 91            v_align='center',
 92        )
 93
 94        ba.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=ba.app.ui.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 = ba.buttonwidget(
112                parent=self._root_widget,
113                size=(self._width - 100, 60),
114                position=(50, v),
115                label=ba.Lstr(resource=self._r + '.useDefaultGameMusicText'),
116                on_activate_call=self._on_default_press,
117            )
118            if current_entry_type == 'default':
119                ba.containerwidget(edit=self._root_widget, selected_child=btn)
120            v -= spacing
121
122        if do_mac_music_app_playlist:
123            btn = ba.buttonwidget(
124                parent=self._root_widget,
125                size=(self._width - 100, 60),
126                position=(50, v),
127                label=ba.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                ba.containerwidget(edit=self._root_widget, selected_child=btn)
133            v -= spacing
134
135        if do_music_file:
136            btn = ba.buttonwidget(
137                parent=self._root_widget,
138                size=(self._width - 100, 60),
139                position=(50, v),
140                label=ba.Lstr(resource=self._r + '.useMusicFileText'),
141                on_activate_call=self._on_music_file_press,
142                icon=ba.gettexture('file'),
143            )
144            if current_entry_type == 'musicFile':
145                ba.containerwidget(edit=self._root_widget, selected_child=btn)
146            v -= spacing
147
148        if do_music_folder:
149            btn = ba.buttonwidget(
150                parent=self._root_widget,
151                size=(self._width - 100, 60),
152                position=(50, v),
153                label=ba.Lstr(resource=self._r + '.useMusicFolderText'),
154                on_activate_call=self._on_music_folder_press,
155                icon=ba.gettexture('folder'),
156                icon_color=(1.1, 0.8, 0.2),
157            )
158            if current_entry_type == 'musicFolder':
159                ba.containerwidget(edit=self._root_widget, selected_child=btn)
160            v -= spacing
161
162    def _on_mac_music_app_playlist_press(self) -> None:
163        music = ba.app.music
164        from bastd.ui.soundtrack.macmusicapp import (
165            MacMusicAppPlaylistSelectWindow,
166        )
167
168        ba.containerwidget(edit=self._root_widget, transition='out_left')
169
170        current_playlist_entry: str | None
171        if (
172            music.get_soundtrack_entry_type(self._current_entry)
173            == 'iTunesPlaylist'
174        ):
175            current_playlist_entry = music.get_soundtrack_entry_name(
176                self._current_entry
177            )
178        else:
179            current_playlist_entry = None
180        ba.app.ui.set_main_menu_window(
181            MacMusicAppPlaylistSelectWindow(
182                self._callback, current_playlist_entry, self._current_entry
183            ).get_root_widget()
184        )
185
186    def _on_music_file_press(self) -> None:
187        from ba.osmusic import OSMusicPlayer
188        from bastd.ui.fileselector import FileSelectorWindow
189
190        ba.containerwidget(edit=self._root_widget, transition='out_left')
191        base_path = ba.internal.android_get_external_files_dir()
192        ba.app.ui.set_main_menu_window(
193            FileSelectorWindow(
194                base_path,
195                callback=self._music_file_selector_cb,
196                show_base_path=False,
197                valid_file_extensions=(
198                    OSMusicPlayer.get_valid_music_file_extensions()
199                ),
200                allow_folders=False,
201            ).get_root_widget()
202        )
203
204    def _on_music_folder_press(self) -> None:
205        from bastd.ui.fileselector import FileSelectorWindow
206
207        ba.containerwidget(edit=self._root_widget, transition='out_left')
208        base_path = ba.internal.android_get_external_files_dir()
209        ba.app.ui.set_main_menu_window(
210            FileSelectorWindow(
211                base_path,
212                callback=self._music_folder_selector_cb,
213                show_base_path=False,
214                valid_file_extensions=[],
215                allow_folders=True,
216            ).get_root_widget()
217        )
218
219    def _music_file_selector_cb(self, result: str | None) -> None:
220        if result is None:
221            self._callback(self._current_entry)
222        else:
223            self._callback({'type': 'musicFile', 'name': result})
224
225    def _music_folder_selector_cb(self, result: str | None) -> None:
226        if result is None:
227            self._callback(self._current_entry)
228        else:
229            self._callback({'type': 'musicFolder', 'name': result})
230
231    def _on_default_press(self) -> None:
232        ba.containerwidget(edit=self._root_widget, transition='out_right')
233        self._callback(None)
234
235    def _on_cancel_press(self) -> None:
236        ba.containerwidget(edit=self._root_widget, transition='out_right')
237        self._callback(self._current_entry)
class SoundtrackEntryTypeSelectWindow(ba.ui.Window):
 17class SoundtrackEntryTypeSelectWindow(ba.Window):
 18    """Window for selecting a soundtrack entry type."""
 19
 20    def __init__(
 21        self,
 22        callback: Callable[[Any], Any],
 23        current_entry: Any,
 24        selection_target_name: str,
 25        transition: str = 'in_right',
 26    ):
 27        music = ba.app.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 = ba.app.ui.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=ba.containerwidget(
 62                size=(self._width, self._height),
 63                transition=transition,
 64                scale=(
 65                    1.7
 66                    if uiscale is ba.UIScale.SMALL
 67                    else 1.4
 68                    if uiscale is ba.UIScale.MEDIUM
 69                    else 1.0
 70                ),
 71            ),
 72            cleanupcheck=False,
 73        )
 74        btn = ba.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=ba.Lstr(resource='cancelText'),
 81            on_activate_call=self._on_cancel_press,
 82        )
 83        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
 84        ba.textwidget(
 85            parent=self._root_widget,
 86            position=(self._width * 0.5, self._height - 32),
 87            size=(0, 0),
 88            text=ba.Lstr(resource=self._r + '.selectASourceText'),
 89            color=ba.app.ui.title_color,
 90            maxwidth=230,
 91            h_align='center',
 92            v_align='center',
 93        )
 94
 95        ba.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=ba.app.ui.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 = ba.buttonwidget(
113                parent=self._root_widget,
114                size=(self._width - 100, 60),
115                position=(50, v),
116                label=ba.Lstr(resource=self._r + '.useDefaultGameMusicText'),
117                on_activate_call=self._on_default_press,
118            )
119            if current_entry_type == 'default':
120                ba.containerwidget(edit=self._root_widget, selected_child=btn)
121            v -= spacing
122
123        if do_mac_music_app_playlist:
124            btn = ba.buttonwidget(
125                parent=self._root_widget,
126                size=(self._width - 100, 60),
127                position=(50, v),
128                label=ba.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                ba.containerwidget(edit=self._root_widget, selected_child=btn)
134            v -= spacing
135
136        if do_music_file:
137            btn = ba.buttonwidget(
138                parent=self._root_widget,
139                size=(self._width - 100, 60),
140                position=(50, v),
141                label=ba.Lstr(resource=self._r + '.useMusicFileText'),
142                on_activate_call=self._on_music_file_press,
143                icon=ba.gettexture('file'),
144            )
145            if current_entry_type == 'musicFile':
146                ba.containerwidget(edit=self._root_widget, selected_child=btn)
147            v -= spacing
148
149        if do_music_folder:
150            btn = ba.buttonwidget(
151                parent=self._root_widget,
152                size=(self._width - 100, 60),
153                position=(50, v),
154                label=ba.Lstr(resource=self._r + '.useMusicFolderText'),
155                on_activate_call=self._on_music_folder_press,
156                icon=ba.gettexture('folder'),
157                icon_color=(1.1, 0.8, 0.2),
158            )
159            if current_entry_type == 'musicFolder':
160                ba.containerwidget(edit=self._root_widget, selected_child=btn)
161            v -= spacing
162
163    def _on_mac_music_app_playlist_press(self) -> None:
164        music = ba.app.music
165        from bastd.ui.soundtrack.macmusicapp import (
166            MacMusicAppPlaylistSelectWindow,
167        )
168
169        ba.containerwidget(edit=self._root_widget, transition='out_left')
170
171        current_playlist_entry: str | None
172        if (
173            music.get_soundtrack_entry_type(self._current_entry)
174            == 'iTunesPlaylist'
175        ):
176            current_playlist_entry = music.get_soundtrack_entry_name(
177                self._current_entry
178            )
179        else:
180            current_playlist_entry = None
181        ba.app.ui.set_main_menu_window(
182            MacMusicAppPlaylistSelectWindow(
183                self._callback, current_playlist_entry, self._current_entry
184            ).get_root_widget()
185        )
186
187    def _on_music_file_press(self) -> None:
188        from ba.osmusic import OSMusicPlayer
189        from bastd.ui.fileselector import FileSelectorWindow
190
191        ba.containerwidget(edit=self._root_widget, transition='out_left')
192        base_path = ba.internal.android_get_external_files_dir()
193        ba.app.ui.set_main_menu_window(
194            FileSelectorWindow(
195                base_path,
196                callback=self._music_file_selector_cb,
197                show_base_path=False,
198                valid_file_extensions=(
199                    OSMusicPlayer.get_valid_music_file_extensions()
200                ),
201                allow_folders=False,
202            ).get_root_widget()
203        )
204
205    def _on_music_folder_press(self) -> None:
206        from bastd.ui.fileselector import FileSelectorWindow
207
208        ba.containerwidget(edit=self._root_widget, transition='out_left')
209        base_path = ba.internal.android_get_external_files_dir()
210        ba.app.ui.set_main_menu_window(
211            FileSelectorWindow(
212                base_path,
213                callback=self._music_folder_selector_cb,
214                show_base_path=False,
215                valid_file_extensions=[],
216                allow_folders=True,
217            ).get_root_widget()
218        )
219
220    def _music_file_selector_cb(self, result: str | None) -> None:
221        if result is None:
222            self._callback(self._current_entry)
223        else:
224            self._callback({'type': 'musicFile', 'name': result})
225
226    def _music_folder_selector_cb(self, result: str | None) -> None:
227        if result is None:
228            self._callback(self._current_entry)
229        else:
230            self._callback({'type': 'musicFolder', 'name': result})
231
232    def _on_default_press(self) -> None:
233        ba.containerwidget(edit=self._root_widget, transition='out_right')
234        self._callback(None)
235
236    def _on_cancel_press(self) -> None:
237        ba.containerwidget(edit=self._root_widget, transition='out_right')
238        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')
 20    def __init__(
 21        self,
 22        callback: Callable[[Any], Any],
 23        current_entry: Any,
 24        selection_target_name: str,
 25        transition: str = 'in_right',
 26    ):
 27        music = ba.app.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 = ba.app.ui.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=ba.containerwidget(
 62                size=(self._width, self._height),
 63                transition=transition,
 64                scale=(
 65                    1.7
 66                    if uiscale is ba.UIScale.SMALL
 67                    else 1.4
 68                    if uiscale is ba.UIScale.MEDIUM
 69                    else 1.0
 70                ),
 71            ),
 72            cleanupcheck=False,
 73        )
 74        btn = ba.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=ba.Lstr(resource='cancelText'),
 81            on_activate_call=self._on_cancel_press,
 82        )
 83        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
 84        ba.textwidget(
 85            parent=self._root_widget,
 86            position=(self._width * 0.5, self._height - 32),
 87            size=(0, 0),
 88            text=ba.Lstr(resource=self._r + '.selectASourceText'),
 89            color=ba.app.ui.title_color,
 90            maxwidth=230,
 91            h_align='center',
 92            v_align='center',
 93        )
 94
 95        ba.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=ba.app.ui.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 = ba.buttonwidget(
113                parent=self._root_widget,
114                size=(self._width - 100, 60),
115                position=(50, v),
116                label=ba.Lstr(resource=self._r + '.useDefaultGameMusicText'),
117                on_activate_call=self._on_default_press,
118            )
119            if current_entry_type == 'default':
120                ba.containerwidget(edit=self._root_widget, selected_child=btn)
121            v -= spacing
122
123        if do_mac_music_app_playlist:
124            btn = ba.buttonwidget(
125                parent=self._root_widget,
126                size=(self._width - 100, 60),
127                position=(50, v),
128                label=ba.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                ba.containerwidget(edit=self._root_widget, selected_child=btn)
134            v -= spacing
135
136        if do_music_file:
137            btn = ba.buttonwidget(
138                parent=self._root_widget,
139                size=(self._width - 100, 60),
140                position=(50, v),
141                label=ba.Lstr(resource=self._r + '.useMusicFileText'),
142                on_activate_call=self._on_music_file_press,
143                icon=ba.gettexture('file'),
144            )
145            if current_entry_type == 'musicFile':
146                ba.containerwidget(edit=self._root_widget, selected_child=btn)
147            v -= spacing
148
149        if do_music_folder:
150            btn = ba.buttonwidget(
151                parent=self._root_widget,
152                size=(self._width - 100, 60),
153                position=(50, v),
154                label=ba.Lstr(resource=self._r + '.useMusicFolderText'),
155                on_activate_call=self._on_music_folder_press,
156                icon=ba.gettexture('folder'),
157                icon_color=(1.1, 0.8, 0.2),
158            )
159            if current_entry_type == 'musicFolder':
160                ba.containerwidget(edit=self._root_widget, selected_child=btn)
161            v -= spacing
Inherited Members
ba.ui.Window
get_root_widget