From 19f5a6cbb07a3d9edcfee083f6b211b6023f1292 Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Tue, 20 Mar 2018 17:06:39 +0100 Subject: [PATCH] Feature #6090: Change music set during gameplay --- src/lang/english.txt | 3 ++ src/music_gui.cpp | 58 +++++++++++++++++++++++++++++++++++++- src/settings_gui.cpp | 17 +++++++---- src/settings_gui.h | 6 ++++ src/widgets/music_widget.h | 2 ++ 5 files changed, 79 insertions(+), 7 deletions(-) diff --git a/src/lang/english.txt b/src/lang/english.txt index 1b025997f4..83b19381e4 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -674,11 +674,14 @@ STR_ERROR_NO_SONGS :{WHITE}A music # Playlist window STR_PLAYLIST_MUSIC_PROGRAM_SELECTION :{WHITE}Music Programme Selection +STR_PLAYLIST_MUSIC_SELECTION_SETNAME :{WHITE}Music Programme - '{RAW_STRING}' STR_PLAYLIST_TRACK_NAME :{TINY_FONT}{LTBLUE}{ZEROFILL_NUM} "{RAW_STRING}" STR_PLAYLIST_TRACK_INDEX :{TINY_FONT}{BLACK}Track Index STR_PLAYLIST_PROGRAM :{TINY_FONT}{BLACK}Programme - '{STRING}' STR_PLAYLIST_CLEAR :{TINY_FONT}{BLACK}Clear +STR_PLAYLIST_CHANGE_SET :{BLACK}Change set STR_PLAYLIST_TOOLTIP_CLEAR_CURRENT_PROGRAM_CUSTOM1 :{BLACK}Clear current programme (Custom1 or Custom2 only) +STR_PLAYLIST_TOOLTIP_CHANGE_SET :{BLACK}Change music selection to another installed set STR_PLAYLIST_TOOLTIP_CLICK_TO_ADD_TRACK :{BLACK}Click on music track to add to current programme (Custom1 or Custom2 only) STR_PLAYLIST_TOOLTIP_CLICK_TO_REMOVE_TRACK :{BLACK}Click on music track to remove it from current programme (Custom1 or Custom2 only) diff --git a/src/music_gui.cpp b/src/music_gui.cpp index fb01cf505e..25bb413146 100644 --- a/src/music_gui.cpp +++ b/src/music_gui.cpp @@ -24,6 +24,9 @@ #include "core/geometry_func.hpp" #include "string_func.h" #include "settings_type.h" +#include "settings_gui.h" +#include "widgets/dropdown_func.h" +#include "widgets/dropdown_type.h" #include "widgets/music_widget.h" @@ -303,6 +306,36 @@ static void SelectPlaylist(byte list) InvalidateWindowData(WC_MUSIC_WINDOW, 0); } +/** + * Change the configured music set and reset playback + * @param index Index of music set to switch to + */ +void ChangeMusicSet(int index) +{ + if (BaseMusic::GetIndexOfUsedSet() == index) return; + + /* Resume playback after switching? + * Always if music is already playing, and also if the user is switching + * away from an empty music set. + * If the user switches away from an empty set, assume it's because they + * want to hear music now. */ + bool shouldplay = _song_is_active || (BaseMusic::GetUsedSet()->num_available == 0); + StopMusic(); + + const char *name = BaseMusic::GetSet(index)->name; + BaseMusic::SetSet(name); + free(BaseMusic::ini_set); + BaseMusic::ini_set = stredup(name); + + InitializeMusic(); + ResetPlaylist(); + _settings_client.music.playing = shouldplay; + + InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0); + InvalidateWindowData(WC_MUSIC_WINDOW, 0); + InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_OPTIONS, 0, true); +} + struct MusicTrackSelectionWindow : public Window { MusicTrackSelectionWindow(WindowDesc *desc, WindowNumber number) : Window(desc) { @@ -319,6 +352,9 @@ struct MusicTrackSelectionWindow : public Window { case WID_MTS_PLAYLIST: SetDParam(0, STR_MUSIC_PLAYLIST_ALL + _settings_client.music.playlist); break; + case WID_MTS_CAPTION: + SetDParamStr(0, BaseMusic::GetUsedSet()->name); + break; } } @@ -456,6 +492,13 @@ struct MusicTrackSelectionWindow : public Window { break; } + case WID_MTS_MUSICSET: { + int selected = 0; + DropDownList *dropdown = BuildMusicSetDropDownList(&selected); + ShowDropDownList(this, dropdown, selected, widget, 0, true, false); + break; + } + case WID_MTS_CLEAR: // clear for (uint i = 0; _playlists[_settings_client.music.playlist][i] != 0; i++) _playlists[_settings_client.music.playlist][i] = 0; this->SetDirty(); @@ -471,12 +514,25 @@ struct MusicTrackSelectionWindow : public Window { break; } } + + virtual void OnDropdownSelect(int widget, int index) + { + switch (widget) { + case WID_MTS_MUSICSET: + ChangeMusicSet(index); + break; + default: + NOT_REACHED(); + break; + } + } }; static const NWidgetPart _nested_music_track_selection_widgets[] = { NWidget(NWID_HORIZONTAL), NWidget(WWT_CLOSEBOX, COLOUR_GREY), - NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_PLAYLIST_MUSIC_PROGRAM_SELECTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), + NWidget(WWT_CAPTION, COLOUR_GREY, WID_MTS_CAPTION), SetDataTip(STR_PLAYLIST_MUSIC_SELECTION_SETNAME, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), + NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_MTS_MUSICSET), SetDataTip(STR_PLAYLIST_CHANGE_SET, STR_PLAYLIST_TOOLTIP_CHANGE_SET), EndContainer(), NWidget(WWT_PANEL, COLOUR_GREY), NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2), diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 79bb71fb83..5ebac02807 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -114,19 +114,24 @@ static int GetCurRes() static void ShowCustCurrency(); template -static DropDownList *BuiltSetDropDownList(int *selected_index) +static DropDownList *BuildSetDropDownList(int *selected_index, bool allow_selection) { int n = T::GetNumSets(); *selected_index = T::GetIndexOfUsedSet(); DropDownList *list = new DropDownList(); for (int i = 0; i < n; i++) { - *list->Append() = new DropDownListCharStringItem(T::GetSet(i)->name, i, (_game_mode == GM_MENU) ? false : (*selected_index != i)); + *list->Append() = new DropDownListCharStringItem(T::GetSet(i)->name, i, !allow_selection && (*selected_index != i)); } return list; } +DropDownList *BuildMusicSetDropDownList(int *selected_index) +{ + return BuildSetDropDownList(selected_index, true); +} + /** Window for displaying the textfile of a BaseSet. */ template struct BaseSetTextfileWindow : public TextfileWindow { @@ -297,15 +302,15 @@ struct GameOptionsWindow : Window { } case WID_GO_BASE_GRF_DROPDOWN: - list = BuiltSetDropDownList(selected_index); + list = BuildSetDropDownList(selected_index, (_game_mode == GM_MENU)); break; case WID_GO_BASE_SFX_DROPDOWN: - list = BuiltSetDropDownList(selected_index); + list = BuildSetDropDownList(selected_index, (_game_mode == GM_MENU)); break; case WID_GO_BASE_MUSIC_DROPDOWN: - list = BuiltSetDropDownList(selected_index); + list = BuildMusicSetDropDownList(selected_index); break; default: @@ -544,7 +549,7 @@ struct GameOptionsWindow : Window { break; case WID_GO_BASE_MUSIC_DROPDOWN: - this->SetMediaSet(index); + ChangeMusicSet(index); break; } } diff --git a/src/settings_gui.h b/src/settings_gui.h index 05955aa557..23a343219f 100644 --- a/src/settings_gui.h +++ b/src/settings_gui.h @@ -13,6 +13,7 @@ #define SETTING_GUI_H #include "gfx_type.h" +#include "widgets/dropdown_type.h" /** Width of setting buttons */ #define SETTING_BUTTON_WIDTH ((int)NWidgetScrollbar::GetHorizontalDimension().width * 2) @@ -23,5 +24,10 @@ void DrawArrowButtons(int x, int y, Colours button_colour, byte state, bool clic void DrawDropDownButton(int x, int y, Colours button_colour, bool state, bool clickable); void DrawBoolButton(int x, int y, bool state, bool clickable); +DropDownList *BuildMusicSetDropDownList(int *selected_index); + +/* Actually implemented in music_gui.cpp */ +void ChangeMusicSet(int index); + #endif /* SETTING_GUI_H */ diff --git a/src/widgets/music_widget.h b/src/widgets/music_widget.h index 3a99bc6150..c8e6a98186 100644 --- a/src/widgets/music_widget.h +++ b/src/widgets/music_widget.h @@ -14,9 +14,11 @@ /** Widgets of the #MusicTrackSelectionWindow class. */ enum MusicTrackSelectionWidgets { + WID_MTS_CAPTION, ///< Window caption. WID_MTS_LIST_LEFT, ///< Left button. WID_MTS_PLAYLIST, ///< Playlist. WID_MTS_LIST_RIGHT, ///< Right button. + WID_MTS_MUSICSET, ///< Music set selection. WID_MTS_ALL, ///< All button. WID_MTS_OLD, ///< Old button. WID_MTS_NEW, ///< New button.