Feature #6090: Change music set during gameplay

save_ext
Niels Martin Hansen 6 years ago committed by frosch
parent 430e20f7e4
commit 19f5a6cbb0

@ -674,11 +674,14 @@ STR_ERROR_NO_SONGS :{WHITE}A music
# Playlist window # Playlist window
STR_PLAYLIST_MUSIC_PROGRAM_SELECTION :{WHITE}Music Programme Selection 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_NAME :{TINY_FONT}{LTBLUE}{ZEROFILL_NUM} "{RAW_STRING}"
STR_PLAYLIST_TRACK_INDEX :{TINY_FONT}{BLACK}Track Index STR_PLAYLIST_TRACK_INDEX :{TINY_FONT}{BLACK}Track Index
STR_PLAYLIST_PROGRAM :{TINY_FONT}{BLACK}Programme - '{STRING}' STR_PLAYLIST_PROGRAM :{TINY_FONT}{BLACK}Programme - '{STRING}'
STR_PLAYLIST_CLEAR :{TINY_FONT}{BLACK}Clear 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_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_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) STR_PLAYLIST_TOOLTIP_CLICK_TO_REMOVE_TRACK :{BLACK}Click on music track to remove it from current programme (Custom1 or Custom2 only)

@ -24,6 +24,9 @@
#include "core/geometry_func.hpp" #include "core/geometry_func.hpp"
#include "string_func.h" #include "string_func.h"
#include "settings_type.h" #include "settings_type.h"
#include "settings_gui.h"
#include "widgets/dropdown_func.h"
#include "widgets/dropdown_type.h"
#include "widgets/music_widget.h" #include "widgets/music_widget.h"
@ -303,6 +306,36 @@ static void SelectPlaylist(byte list)
InvalidateWindowData(WC_MUSIC_WINDOW, 0); 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 { struct MusicTrackSelectionWindow : public Window {
MusicTrackSelectionWindow(WindowDesc *desc, WindowNumber number) : Window(desc) MusicTrackSelectionWindow(WindowDesc *desc, WindowNumber number) : Window(desc)
{ {
@ -319,6 +352,9 @@ struct MusicTrackSelectionWindow : public Window {
case WID_MTS_PLAYLIST: case WID_MTS_PLAYLIST:
SetDParam(0, STR_MUSIC_PLAYLIST_ALL + _settings_client.music.playlist); SetDParam(0, STR_MUSIC_PLAYLIST_ALL + _settings_client.music.playlist);
break; break;
case WID_MTS_CAPTION:
SetDParamStr(0, BaseMusic::GetUsedSet()->name);
break;
} }
} }
@ -456,6 +492,13 @@ struct MusicTrackSelectionWindow : public Window {
break; 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 case WID_MTS_CLEAR: // clear
for (uint i = 0; _playlists[_settings_client.music.playlist][i] != 0; i++) _playlists[_settings_client.music.playlist][i] = 0; for (uint i = 0; _playlists[_settings_client.music.playlist][i] != 0; i++) _playlists[_settings_client.music.playlist][i] = 0;
this->SetDirty(); this->SetDirty();
@ -471,12 +514,25 @@ struct MusicTrackSelectionWindow : public Window {
break; 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[] = { static const NWidgetPart _nested_music_track_selection_widgets[] = {
NWidget(NWID_HORIZONTAL), NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_GREY), 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(), EndContainer(),
NWidget(WWT_PANEL, COLOUR_GREY), NWidget(WWT_PANEL, COLOUR_GREY),
NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2), NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),

@ -114,19 +114,24 @@ static int GetCurRes()
static void ShowCustCurrency(); static void ShowCustCurrency();
template <class T> template <class T>
static DropDownList *BuiltSetDropDownList(int *selected_index) static DropDownList *BuildSetDropDownList(int *selected_index, bool allow_selection)
{ {
int n = T::GetNumSets(); int n = T::GetNumSets();
*selected_index = T::GetIndexOfUsedSet(); *selected_index = T::GetIndexOfUsedSet();
DropDownList *list = new DropDownList(); DropDownList *list = new DropDownList();
for (int i = 0; i < n; i++) { 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; return list;
} }
DropDownList *BuildMusicSetDropDownList(int *selected_index)
{
return BuildSetDropDownList<BaseMusic>(selected_index, true);
}
/** Window for displaying the textfile of a BaseSet. */ /** Window for displaying the textfile of a BaseSet. */
template <class TBaseSet> template <class TBaseSet>
struct BaseSetTextfileWindow : public TextfileWindow { struct BaseSetTextfileWindow : public TextfileWindow {
@ -297,15 +302,15 @@ struct GameOptionsWindow : Window {
} }
case WID_GO_BASE_GRF_DROPDOWN: case WID_GO_BASE_GRF_DROPDOWN:
list = BuiltSetDropDownList<BaseGraphics>(selected_index); list = BuildSetDropDownList<BaseGraphics>(selected_index, (_game_mode == GM_MENU));
break; break;
case WID_GO_BASE_SFX_DROPDOWN: case WID_GO_BASE_SFX_DROPDOWN:
list = BuiltSetDropDownList<BaseSounds>(selected_index); list = BuildSetDropDownList<BaseSounds>(selected_index, (_game_mode == GM_MENU));
break; break;
case WID_GO_BASE_MUSIC_DROPDOWN: case WID_GO_BASE_MUSIC_DROPDOWN:
list = BuiltSetDropDownList<BaseMusic>(selected_index); list = BuildMusicSetDropDownList(selected_index);
break; break;
default: default:
@ -544,7 +549,7 @@ struct GameOptionsWindow : Window {
break; break;
case WID_GO_BASE_MUSIC_DROPDOWN: case WID_GO_BASE_MUSIC_DROPDOWN:
this->SetMediaSet<BaseMusic>(index); ChangeMusicSet(index);
break; break;
} }
} }

@ -13,6 +13,7 @@
#define SETTING_GUI_H #define SETTING_GUI_H
#include "gfx_type.h" #include "gfx_type.h"
#include "widgets/dropdown_type.h"
/** Width of setting buttons */ /** Width of setting buttons */
#define SETTING_BUTTON_WIDTH ((int)NWidgetScrollbar::GetHorizontalDimension().width * 2) #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 DrawDropDownButton(int x, int y, Colours button_colour, bool state, bool clickable);
void DrawBoolButton(int x, int y, 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 */ #endif /* SETTING_GUI_H */

@ -14,9 +14,11 @@
/** Widgets of the #MusicTrackSelectionWindow class. */ /** Widgets of the #MusicTrackSelectionWindow class. */
enum MusicTrackSelectionWidgets { enum MusicTrackSelectionWidgets {
WID_MTS_CAPTION, ///< Window caption.
WID_MTS_LIST_LEFT, ///< Left button. WID_MTS_LIST_LEFT, ///< Left button.
WID_MTS_PLAYLIST, ///< Playlist. WID_MTS_PLAYLIST, ///< Playlist.
WID_MTS_LIST_RIGHT, ///< Right button. WID_MTS_LIST_RIGHT, ///< Right button.
WID_MTS_MUSICSET, ///< Music set selection.
WID_MTS_ALL, ///< All button. WID_MTS_ALL, ///< All button.
WID_MTS_OLD, ///< Old button. WID_MTS_OLD, ///< Old button.
WID_MTS_NEW, ///< New button. WID_MTS_NEW, ///< New button.

Loading…
Cancel
Save