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
 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__(self,
 20                 callback: Callable[[Any], Any],
 21                 current_entry: Any,
 22                 selection_target_name: str,
 23                 transition: str = 'in_right'):
 24        music = ba.app.music
 25        self._r = 'editSoundtrackWindow'
 26
 27        self._callback = callback
 28        self._current_entry = copy.deepcopy(current_entry)
 29
 30        self._width = 580
 31        self._height = 220
 32        spacing = 80
 33
 34        # FIXME: Generalize this so new custom soundtrack types can add
 35        # themselves here.
 36        do_default = True
 37        do_mac_music_app_playlist = music.supports_soundtrack_entry_type(
 38            'iTunesPlaylist')
 39        do_music_file = music.supports_soundtrack_entry_type('musicFile')
 40        do_music_folder = music.supports_soundtrack_entry_type('musicFolder')
 41
 42        if do_mac_music_app_playlist:
 43            self._height += spacing
 44        if do_music_file:
 45            self._height += spacing
 46        if do_music_folder:
 47            self._height += spacing
 48
 49        uiscale = ba.app.ui.uiscale
 50
 51        # NOTE: When something is selected, we close our UI and kick off
 52        # another window which then calls us back when its done, so the
 53        # standard UI-cleanup-check complains that something is holding on
 54        # to our instance after its ui is gone. Should restructure in a
 55        # cleaner way, but just disabling that check for now.
 56        super().__init__(root_widget=ba.containerwidget(
 57            size=(self._width, self._height),
 58            transition=transition,
 59            scale=(1.7 if uiscale is ba.UIScale.SMALL else
 60                   1.4 if uiscale is ba.UIScale.MEDIUM else 1.0)),
 61                         cleanupcheck=False)
 62        btn = ba.buttonwidget(parent=self._root_widget,
 63                              position=(35, self._height - 65),
 64                              size=(160, 60),
 65                              scale=0.8,
 66                              text_scale=1.2,
 67                              label=ba.Lstr(resource='cancelText'),
 68                              on_activate_call=self._on_cancel_press)
 69        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
 70        ba.textwidget(parent=self._root_widget,
 71                      position=(self._width * 0.5, self._height - 32),
 72                      size=(0, 0),
 73                      text=ba.Lstr(resource=self._r + '.selectASourceText'),
 74                      color=ba.app.ui.title_color,
 75                      maxwidth=230,
 76                      h_align='center',
 77                      v_align='center')
 78
 79        ba.textwidget(parent=self._root_widget,
 80                      position=(self._width * 0.5, self._height - 56),
 81                      size=(0, 0),
 82                      text=selection_target_name,
 83                      color=ba.app.ui.infotextcolor,
 84                      scale=0.7,
 85                      maxwidth=230,
 86                      h_align='center',
 87                      v_align='center')
 88
 89        v = self._height - 155
 90
 91        current_entry_type = music.get_soundtrack_entry_type(current_entry)
 92
 93        if do_default:
 94            btn = ba.buttonwidget(parent=self._root_widget,
 95                                  size=(self._width - 100, 60),
 96                                  position=(50, v),
 97                                  label=ba.Lstr(resource=self._r +
 98                                                '.useDefaultGameMusicText'),
 99                                  on_activate_call=self._on_default_press)
100            if current_entry_type == 'default':
101                ba.containerwidget(edit=self._root_widget, selected_child=btn)
102            v -= spacing
103
104        if do_mac_music_app_playlist:
105            btn = ba.buttonwidget(
106                parent=self._root_widget,
107                size=(self._width - 100, 60),
108                position=(50, v),
109                label=ba.Lstr(resource=self._r + '.useITunesPlaylistText'),
110                on_activate_call=self._on_mac_music_app_playlist_press,
111                icon=None)
112            if current_entry_type == 'iTunesPlaylist':
113                ba.containerwidget(edit=self._root_widget, selected_child=btn)
114            v -= spacing
115
116        if do_music_file:
117            btn = ba.buttonwidget(parent=self._root_widget,
118                                  size=(self._width - 100, 60),
119                                  position=(50, v),
120                                  label=ba.Lstr(resource=self._r +
121                                                '.useMusicFileText'),
122                                  on_activate_call=self._on_music_file_press,
123                                  icon=ba.gettexture('file'))
124            if current_entry_type == 'musicFile':
125                ba.containerwidget(edit=self._root_widget, selected_child=btn)
126            v -= spacing
127
128        if do_music_folder:
129            btn = ba.buttonwidget(parent=self._root_widget,
130                                  size=(self._width - 100, 60),
131                                  position=(50, v),
132                                  label=ba.Lstr(resource=self._r +
133                                                '.useMusicFolderText'),
134                                  on_activate_call=self._on_music_folder_press,
135                                  icon=ba.gettexture('folder'),
136                                  icon_color=(1.1, 0.8, 0.2))
137            if current_entry_type == 'musicFolder':
138                ba.containerwidget(edit=self._root_widget, selected_child=btn)
139            v -= spacing
140
141    def _on_mac_music_app_playlist_press(self) -> None:
142        music = ba.app.music
143        from bastd.ui.soundtrack.macmusicapp import (
144            MacMusicAppPlaylistSelectWindow)
145        ba.containerwidget(edit=self._root_widget, transition='out_left')
146
147        current_playlist_entry: str | None
148        if (music.get_soundtrack_entry_type(
149                self._current_entry) == 'iTunesPlaylist'):
150            current_playlist_entry = music.get_soundtrack_entry_name(
151                self._current_entry)
152        else:
153            current_playlist_entry = None
154        ba.app.ui.set_main_menu_window(
155            MacMusicAppPlaylistSelectWindow(
156                self._callback, current_playlist_entry,
157                self._current_entry).get_root_widget())
158
159    def _on_music_file_press(self) -> None:
160        from ba.osmusic import OSMusicPlayer
161        from bastd.ui.fileselector import FileSelectorWindow
162        ba.containerwidget(edit=self._root_widget, transition='out_left')
163        base_path = _ba.android_get_external_files_dir()
164        ba.app.ui.set_main_menu_window(
165            FileSelectorWindow(
166                base_path,
167                callback=self._music_file_selector_cb,
168                show_base_path=False,
169                valid_file_extensions=(
170                    OSMusicPlayer.get_valid_music_file_extensions()),
171                allow_folders=False).get_root_widget())
172
173    def _on_music_folder_press(self) -> None:
174        from bastd.ui.fileselector import FileSelectorWindow
175        ba.containerwidget(edit=self._root_widget, transition='out_left')
176        base_path = _ba.android_get_external_files_dir()
177        ba.app.ui.set_main_menu_window(
178            FileSelectorWindow(base_path,
179                               callback=self._music_folder_selector_cb,
180                               show_base_path=False,
181                               valid_file_extensions=[],
182                               allow_folders=True).get_root_widget())
183
184    def _music_file_selector_cb(self, result: str | None) -> None:
185        if result is None:
186            self._callback(self._current_entry)
187        else:
188            self._callback({'type': 'musicFile', 'name': result})
189
190    def _music_folder_selector_cb(self, result: str | None) -> None:
191        if result is None:
192            self._callback(self._current_entry)
193        else:
194            self._callback({'type': 'musicFolder', 'name': result})
195
196    def _on_default_press(self) -> None:
197        ba.containerwidget(edit=self._root_widget, transition='out_right')
198        self._callback(None)
199
200    def _on_cancel_press(self) -> None:
201        ba.containerwidget(edit=self._root_widget, transition='out_right')
202        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__(self,
 21                 callback: Callable[[Any], Any],
 22                 current_entry: Any,
 23                 selection_target_name: str,
 24                 transition: str = 'in_right'):
 25        music = ba.app.music
 26        self._r = 'editSoundtrackWindow'
 27
 28        self._callback = callback
 29        self._current_entry = copy.deepcopy(current_entry)
 30
 31        self._width = 580
 32        self._height = 220
 33        spacing = 80
 34
 35        # FIXME: Generalize this so new custom soundtrack types can add
 36        # themselves here.
 37        do_default = True
 38        do_mac_music_app_playlist = music.supports_soundtrack_entry_type(
 39            'iTunesPlaylist')
 40        do_music_file = music.supports_soundtrack_entry_type('musicFile')
 41        do_music_folder = music.supports_soundtrack_entry_type('musicFolder')
 42
 43        if do_mac_music_app_playlist:
 44            self._height += spacing
 45        if do_music_file:
 46            self._height += spacing
 47        if do_music_folder:
 48            self._height += spacing
 49
 50        uiscale = ba.app.ui.uiscale
 51
 52        # NOTE: When something is selected, we close our UI and kick off
 53        # another window which then calls us back when its done, so the
 54        # standard UI-cleanup-check complains that something is holding on
 55        # to our instance after its ui is gone. Should restructure in a
 56        # cleaner way, but just disabling that check for now.
 57        super().__init__(root_widget=ba.containerwidget(
 58            size=(self._width, self._height),
 59            transition=transition,
 60            scale=(1.7 if uiscale is ba.UIScale.SMALL else
 61                   1.4 if uiscale is ba.UIScale.MEDIUM else 1.0)),
 62                         cleanupcheck=False)
 63        btn = ba.buttonwidget(parent=self._root_widget,
 64                              position=(35, self._height - 65),
 65                              size=(160, 60),
 66                              scale=0.8,
 67                              text_scale=1.2,
 68                              label=ba.Lstr(resource='cancelText'),
 69                              on_activate_call=self._on_cancel_press)
 70        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
 71        ba.textwidget(parent=self._root_widget,
 72                      position=(self._width * 0.5, self._height - 32),
 73                      size=(0, 0),
 74                      text=ba.Lstr(resource=self._r + '.selectASourceText'),
 75                      color=ba.app.ui.title_color,
 76                      maxwidth=230,
 77                      h_align='center',
 78                      v_align='center')
 79
 80        ba.textwidget(parent=self._root_widget,
 81                      position=(self._width * 0.5, self._height - 56),
 82                      size=(0, 0),
 83                      text=selection_target_name,
 84                      color=ba.app.ui.infotextcolor,
 85                      scale=0.7,
 86                      maxwidth=230,
 87                      h_align='center',
 88                      v_align='center')
 89
 90        v = self._height - 155
 91
 92        current_entry_type = music.get_soundtrack_entry_type(current_entry)
 93
 94        if do_default:
 95            btn = ba.buttonwidget(parent=self._root_widget,
 96                                  size=(self._width - 100, 60),
 97                                  position=(50, v),
 98                                  label=ba.Lstr(resource=self._r +
 99                                                '.useDefaultGameMusicText'),
100                                  on_activate_call=self._on_default_press)
101            if current_entry_type == 'default':
102                ba.containerwidget(edit=self._root_widget, selected_child=btn)
103            v -= spacing
104
105        if do_mac_music_app_playlist:
106            btn = ba.buttonwidget(
107                parent=self._root_widget,
108                size=(self._width - 100, 60),
109                position=(50, v),
110                label=ba.Lstr(resource=self._r + '.useITunesPlaylistText'),
111                on_activate_call=self._on_mac_music_app_playlist_press,
112                icon=None)
113            if current_entry_type == 'iTunesPlaylist':
114                ba.containerwidget(edit=self._root_widget, selected_child=btn)
115            v -= spacing
116
117        if do_music_file:
118            btn = ba.buttonwidget(parent=self._root_widget,
119                                  size=(self._width - 100, 60),
120                                  position=(50, v),
121                                  label=ba.Lstr(resource=self._r +
122                                                '.useMusicFileText'),
123                                  on_activate_call=self._on_music_file_press,
124                                  icon=ba.gettexture('file'))
125            if current_entry_type == 'musicFile':
126                ba.containerwidget(edit=self._root_widget, selected_child=btn)
127            v -= spacing
128
129        if do_music_folder:
130            btn = ba.buttonwidget(parent=self._root_widget,
131                                  size=(self._width - 100, 60),
132                                  position=(50, v),
133                                  label=ba.Lstr(resource=self._r +
134                                                '.useMusicFolderText'),
135                                  on_activate_call=self._on_music_folder_press,
136                                  icon=ba.gettexture('folder'),
137                                  icon_color=(1.1, 0.8, 0.2))
138            if current_entry_type == 'musicFolder':
139                ba.containerwidget(edit=self._root_widget, selected_child=btn)
140            v -= spacing
141
142    def _on_mac_music_app_playlist_press(self) -> None:
143        music = ba.app.music
144        from bastd.ui.soundtrack.macmusicapp import (
145            MacMusicAppPlaylistSelectWindow)
146        ba.containerwidget(edit=self._root_widget, transition='out_left')
147
148        current_playlist_entry: str | None
149        if (music.get_soundtrack_entry_type(
150                self._current_entry) == 'iTunesPlaylist'):
151            current_playlist_entry = music.get_soundtrack_entry_name(
152                self._current_entry)
153        else:
154            current_playlist_entry = None
155        ba.app.ui.set_main_menu_window(
156            MacMusicAppPlaylistSelectWindow(
157                self._callback, current_playlist_entry,
158                self._current_entry).get_root_widget())
159
160    def _on_music_file_press(self) -> None:
161        from ba.osmusic import OSMusicPlayer
162        from bastd.ui.fileselector import FileSelectorWindow
163        ba.containerwidget(edit=self._root_widget, transition='out_left')
164        base_path = _ba.android_get_external_files_dir()
165        ba.app.ui.set_main_menu_window(
166            FileSelectorWindow(
167                base_path,
168                callback=self._music_file_selector_cb,
169                show_base_path=False,
170                valid_file_extensions=(
171                    OSMusicPlayer.get_valid_music_file_extensions()),
172                allow_folders=False).get_root_widget())
173
174    def _on_music_folder_press(self) -> None:
175        from bastd.ui.fileselector import FileSelectorWindow
176        ba.containerwidget(edit=self._root_widget, transition='out_left')
177        base_path = _ba.android_get_external_files_dir()
178        ba.app.ui.set_main_menu_window(
179            FileSelectorWindow(base_path,
180                               callback=self._music_folder_selector_cb,
181                               show_base_path=False,
182                               valid_file_extensions=[],
183                               allow_folders=True).get_root_widget())
184
185    def _music_file_selector_cb(self, result: str | None) -> None:
186        if result is None:
187            self._callback(self._current_entry)
188        else:
189            self._callback({'type': 'musicFile', 'name': result})
190
191    def _music_folder_selector_cb(self, result: str | None) -> None:
192        if result is None:
193            self._callback(self._current_entry)
194        else:
195            self._callback({'type': 'musicFolder', 'name': result})
196
197    def _on_default_press(self) -> None:
198        ba.containerwidget(edit=self._root_widget, transition='out_right')
199        self._callback(None)
200
201    def _on_cancel_press(self) -> None:
202        ba.containerwidget(edit=self._root_widget, transition='out_right')
203        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__(self,
 21                 callback: Callable[[Any], Any],
 22                 current_entry: Any,
 23                 selection_target_name: str,
 24                 transition: str = 'in_right'):
 25        music = ba.app.music
 26        self._r = 'editSoundtrackWindow'
 27
 28        self._callback = callback
 29        self._current_entry = copy.deepcopy(current_entry)
 30
 31        self._width = 580
 32        self._height = 220
 33        spacing = 80
 34
 35        # FIXME: Generalize this so new custom soundtrack types can add
 36        # themselves here.
 37        do_default = True
 38        do_mac_music_app_playlist = music.supports_soundtrack_entry_type(
 39            'iTunesPlaylist')
 40        do_music_file = music.supports_soundtrack_entry_type('musicFile')
 41        do_music_folder = music.supports_soundtrack_entry_type('musicFolder')
 42
 43        if do_mac_music_app_playlist:
 44            self._height += spacing
 45        if do_music_file:
 46            self._height += spacing
 47        if do_music_folder:
 48            self._height += spacing
 49
 50        uiscale = ba.app.ui.uiscale
 51
 52        # NOTE: When something is selected, we close our UI and kick off
 53        # another window which then calls us back when its done, so the
 54        # standard UI-cleanup-check complains that something is holding on
 55        # to our instance after its ui is gone. Should restructure in a
 56        # cleaner way, but just disabling that check for now.
 57        super().__init__(root_widget=ba.containerwidget(
 58            size=(self._width, self._height),
 59            transition=transition,
 60            scale=(1.7 if uiscale is ba.UIScale.SMALL else
 61                   1.4 if uiscale is ba.UIScale.MEDIUM else 1.0)),
 62                         cleanupcheck=False)
 63        btn = ba.buttonwidget(parent=self._root_widget,
 64                              position=(35, self._height - 65),
 65                              size=(160, 60),
 66                              scale=0.8,
 67                              text_scale=1.2,
 68                              label=ba.Lstr(resource='cancelText'),
 69                              on_activate_call=self._on_cancel_press)
 70        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
 71        ba.textwidget(parent=self._root_widget,
 72                      position=(self._width * 0.5, self._height - 32),
 73                      size=(0, 0),
 74                      text=ba.Lstr(resource=self._r + '.selectASourceText'),
 75                      color=ba.app.ui.title_color,
 76                      maxwidth=230,
 77                      h_align='center',
 78                      v_align='center')
 79
 80        ba.textwidget(parent=self._root_widget,
 81                      position=(self._width * 0.5, self._height - 56),
 82                      size=(0, 0),
 83                      text=selection_target_name,
 84                      color=ba.app.ui.infotextcolor,
 85                      scale=0.7,
 86                      maxwidth=230,
 87                      h_align='center',
 88                      v_align='center')
 89
 90        v = self._height - 155
 91
 92        current_entry_type = music.get_soundtrack_entry_type(current_entry)
 93
 94        if do_default:
 95            btn = ba.buttonwidget(parent=self._root_widget,
 96                                  size=(self._width - 100, 60),
 97                                  position=(50, v),
 98                                  label=ba.Lstr(resource=self._r +
 99                                                '.useDefaultGameMusicText'),
100                                  on_activate_call=self._on_default_press)
101            if current_entry_type == 'default':
102                ba.containerwidget(edit=self._root_widget, selected_child=btn)
103            v -= spacing
104
105        if do_mac_music_app_playlist:
106            btn = ba.buttonwidget(
107                parent=self._root_widget,
108                size=(self._width - 100, 60),
109                position=(50, v),
110                label=ba.Lstr(resource=self._r + '.useITunesPlaylistText'),
111                on_activate_call=self._on_mac_music_app_playlist_press,
112                icon=None)
113            if current_entry_type == 'iTunesPlaylist':
114                ba.containerwidget(edit=self._root_widget, selected_child=btn)
115            v -= spacing
116
117        if do_music_file:
118            btn = ba.buttonwidget(parent=self._root_widget,
119                                  size=(self._width - 100, 60),
120                                  position=(50, v),
121                                  label=ba.Lstr(resource=self._r +
122                                                '.useMusicFileText'),
123                                  on_activate_call=self._on_music_file_press,
124                                  icon=ba.gettexture('file'))
125            if current_entry_type == 'musicFile':
126                ba.containerwidget(edit=self._root_widget, selected_child=btn)
127            v -= spacing
128
129        if do_music_folder:
130            btn = ba.buttonwidget(parent=self._root_widget,
131                                  size=(self._width - 100, 60),
132                                  position=(50, v),
133                                  label=ba.Lstr(resource=self._r +
134                                                '.useMusicFolderText'),
135                                  on_activate_call=self._on_music_folder_press,
136                                  icon=ba.gettexture('folder'),
137                                  icon_color=(1.1, 0.8, 0.2))
138            if current_entry_type == 'musicFolder':
139                ba.containerwidget(edit=self._root_widget, selected_child=btn)
140            v -= spacing
Inherited Members
ba.ui.Window
get_root_widget