|
OpenTTD Source
14.0-beta3
|
Go to the documentation of this file.
29 #include "widgets/slider_func.h"
34 #include "table/strings.h"
46 bool IsValid()
const {
return !this->
songname.empty(); }
48 typedef std::vector<PlaylistEntry> Playlist;
50 enum PlaylistChoices {
65 PlaylistChoices selected_playlist;
93 int playlist_position;
97 Playlist standard_playlists[PLCH_MAX];
109 for (
size_t i = 0; i <
lengthof(this->standard_playlists); ++i) this->standard_playlists[i].clear();
115 if (!entry.IsValid())
continue;
120 if (i == 0) this->standard_playlists[PLCH_THEMEONLY].push_back(entry);
124 this->standard_playlists[PLCH_ALLMUSIC].push_back(entry);
126 this->standard_playlists[PLCH_OLDSTYLE + theme].push_back(entry);
135 if (entry.IsValid()) this->standard_playlists[PLCH_CUSTOM1].push_back(entry);
139 if (entry.IsValid()) this->standard_playlists[PLCH_CUSTOM2].push_back(entry);
150 assert(pl < PLCH_MAX && pl >= PLCH_ALLMUSIC);
154 if (_game_mode != GM_MENU || pl == PLCH_THEMEONLY) {
157 this->selected_playlist = pl;
158 this->playlist_position = 0;
201 return static_cast<size_t>(this->playlist_position) < this->
active_playlist.size()
216 size_t shuffle_index = InteractiveRandom() % (this->
active_playlist.size() - i);
253 if (_game_mode == GM_MENU && this->selected_playlist == PLCH_THEMEONLY) song.
loop =
true;
289 if ((_game_mode == GM_MENU) != (this->selected_playlist == PLCH_THEMEONLY)) {
320 return (this->selected_playlist == PLCH_CUSTOM1) || (this->selected_playlist == PLCH_CUSTOM2);
333 if (song_index >= this->
music_set.size())
return;
337 if (this->standard_playlists[this->selected_playlist].size() >=
NUM_SONGS_PLAYLIST)
return;
340 this->standard_playlists[this->selected_playlist].push_back(entry);
350 size_t newpos = InteractiveRandom() % maxpos;
353 if ((
int)newpos <= this->playlist_position) this->playlist_position++;
371 Playlist &pl = this->standard_playlists[this->selected_playlist];
372 if (song_index >= pl.size())
return;
376 pl.erase(pl.begin() + song_index);
385 if ((
int)i == this->playlist_position && this->
IsPlaying()) this->
Play();
403 this->standard_playlists[this->selected_playlist].clear();
417 this->playlist_position = 0;
419 this->playlist_position += ofs;
421 while (this->playlist_position < 0) this->playlist_position += (int)this->
active_playlist.size();
432 if (pl == PLCH_CUSTOM1) {
434 }
else if (pl == PLCH_CUSTOM2) {
443 for (
const auto &song : this->standard_playlists[pl]) {
445 settings_pl[num++] = (byte)song.set_index + 1;
489 void SetStringParameters(
WidgetID widget)
const override
506 void OnInvalidateData([[maybe_unused]]
int data = 0, [[maybe_unused]]
bool gui_scope =
true)
override
508 if (!gui_scope)
return;
509 for (
int i = 0; i < 6; i++) {
527 for (
int i = 0; i < 6; i++) {
528 SetDParam(0, STR_MUSIC_PLAYLIST_ALL + i);
531 d.width += padding.width;
532 d.height += padding.height;
540 for (
const auto &song :
_music.music_set) {
545 d.width = std::max(d.width, d2.width);
546 d.height += d2.height;
548 d.width += padding.width;
549 d.height += padding.height;
556 void DrawWidget(
const Rect &r,
WidgetID widget)
const override
563 for (
const auto &song :
_music.music_set) {
577 for (
const auto &song :
_music.active_playlist) {
589 void OnClick([[maybe_unused]]
Point pt,
WidgetID widget, [[maybe_unused]]
int click_count)
override
606 ShowDropDownList(
this, BuildSetDropDownList<BaseMusic>(&selected), selected, widget);
621 void OnDropdownSelect(
WidgetID widget,
int index)
override
633 static constexpr
NWidgetPart _nested_music_track_selection_widgets[] = {
644 NWidget(
WWT_PANEL, COLOUR_GREY,
WID_MTS_LIST_LEFT),
SetFill(1, 1),
SetMinimalSize(180, 194),
SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_ADD_TRACK),
EndContainer(),
663 NWidget(
WWT_PANEL, COLOUR_GREY,
WID_MTS_LIST_RIGHT),
SetFill(1, 1),
SetMinimalSize(180, 194),
SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_REMOVE_TRACK),
EndContainer(),
670 static WindowDesc _music_track_selection_desc(__FILE__, __LINE__,
674 std::begin(_nested_music_track_selection_widgets), std::end(_nested_music_track_selection_widgets)
677 static void ShowMusicTrackSelection()
679 AllocateWindowDescFront<MusicTrackSelectionWindow>(&_music_track_selection_desc, 0);
689 UpdateDisabledButtons();
692 void UpdateDisabledButtons()
711 d.width += padding.width;
712 d.height += padding.height;
719 d.width += padding.width;
720 d.height += padding.height;
727 for (
const auto &song :
_music.music_set) {
731 d.width += padding.width;
732 d.height += padding.height;
745 void DrawWidget(
const Rect &r,
WidgetID widget)
const override
753 StringID str = STR_MUSIC_TRACK_NONE;
757 str = STR_MUSIC_TRACK_DIGIT;
765 StringID str = STR_MUSIC_TITLE_NONE;
768 str = STR_MUSIC_TITLE_NOMUSIC;
769 }
else if (
_music.IsPlaying()) {
770 str = STR_MUSIC_TITLE_NAME;
792 void OnInvalidateData([[maybe_unused]]
int data = 0, [[maybe_unused]]
bool gui_scope =
true)
override
794 if (!gui_scope)
return;
795 for (
int i = 0; i < 6; i++) {
799 UpdateDisabledButtons();
808 void OnClick([[maybe_unused]]
Point pt,
WidgetID widget, [[maybe_unused]]
int click_count)
override
829 if (
ClickSliderWidget(this->GetWidget<NWidgetBase>(widget)->GetCurrentRect(), pt, 0, INT8_MAX, vol)) {
833 SetEffectVolume(vol);
854 ShowMusicTrackSelection();
859 _music.ChangePlaylist((MusicSystem::PlaylistChoices)(widget -
WID_M_ALL));
865 static constexpr
NWidgetPart _nested_music_window_widgets[] = {
888 NWidget(
WWT_EMPTY, COLOUR_GREY,
WID_M_MUSIC_VOL),
SetMinimalSize(67, 0),
SetPadding(2),
SetMinimalTextLines(1, 0),
SetFill(1, 0),
SetDataTip(0x0, STR_MUSIC_TOOLTIP_DRAG_SLIDERS_TO_SET_MUSIC),
892 NWidget(
WWT_EMPTY, COLOUR_GREY,
WID_M_EFFECT_VOL),
SetMinimalSize(67, 0),
SetPadding(2),
SetMinimalTextLines(1, 0),
SetFill(1, 0),
SetDataTip(0x0, STR_MUSIC_TOOLTIP_DRAG_SLIDERS_TO_SET_MUSIC),
929 static WindowDesc _music_window_desc(__FILE__, __LINE__,
933 std::begin(_nested_music_window_widgets), std::end(_nested_music_window_widgets)
936 void ShowMusicWindow()
938 AllocateWindowDescFront<MusicWindow>(&_music_window_desc, 0);
void Shuffle()
Enable shuffle mode.
void ChangePlaylist(PlaylistChoices pl)
Switch to another playlist, or reload the current one.
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
void PlaylistClear()
Remove all songs from the current custom playlist.
Dimensions (a width and height) of a rectangle in 2D.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
virtual void PlaySong(const MusicSongInfo &song)=0
Play a particular song.
void SetWidgetDirty(WidgetID widget_index) const
Invalidate a widget, i.e.
virtual void SetVolume(byte vol)=0
Set the volume, if possible.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
bool IsShuffle() const
Is shuffle mode enabled?
bool IsCustomPlaylist() const
Is one of the custom playlists selected?
bool shuffle
Whether to shuffle the music.
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
void PlaylistAdd(size_t song_index)
Append a song to a custom playlist.
bool loop
song should play in a tight loop if possible, never ending
ClientSettings _settings_client
The current settings for this game.
void Play()
Start/restart playback at current song.
@ WC_MUSIC_WINDOW
Music window; Window numbers:
void MusicLoop()
Check music playback status and start/stop/song-finished.
void Prev()
Skip to previous track.
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
uint GetSetIndex()
Get set index from current playlist position.
Playlist displayed_playlist
current playlist as displayed in GUI, never in shuffled order
bool playing
Whether music is playing.
void Next()
Skip to next track.
@ WN_GAME_OPTIONS_GAME_OPTIONS
Game options.
High level window description.
@ WDP_AUTO
Find a place automatically.
std::string filename
file on disk containing song (when used in MusicSet class)
ResizeInfo resize
Resize information.
int32_t WindowNumber
Number to differentiate different windows of the same class.
static MusicDriver * GetInstance()
Get the currently active instance of the music driver.
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
void CheckStatus()
Check that music is playing if it should, and that appropriate playlist is active for game/main menu.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
@ FS_SMALL
Index of the small font in the font tables.
void ChangeMusicSet(const std::string &set_name)
Change to named music set, and reset playback.
void BuildPlaylists()
Rebuild all playlists for the current music set.
bool IsPlaying() const
Is the player getting music right now?
Metadata about a music track.
void Unshuffle()
Disable shuffle mode.
void ReInit(int rx=0, int ry=0, bool reposition=false)
Re-initialize a window, and optionally change its size.
byte custom_2[33]
The order of the second custom playlist.
void DrawSliderWidget(Rect r, int min_value, int max_value, int value, const std::map< int, StringID > &labels)
Draw a slider widget with knob at given value.
void LowerWidget(WidgetID widget_index)
Marks a widget as lowered.
byte playlist
The playlist (number) to play.
void OnInvalidateData([[maybe_unused]] int data=0, [[maybe_unused]] bool gui_scope=true) override
Some data on this window has become invalid.
Coordinates of a point in 2D.
std::string songname
name of song displayed in UI
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
static std::string ini_set
The set as saved in the config file.
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
@ SA_HOR_CENTER
Horizontally center the text.
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
@ WC_GAME_OPTIONS
Game options window; Window numbers:
void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width, bool instant_close)
Show a drop down list.
void InitializeMusic()
Prepare the music system for use.
static IDirectMusic * _music
The direct music object manages buffers and ports.
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
void SaveCustomPlaylist(PlaylistChoices pl)
Save a custom playlist to settings after modification.
bool ClickSliderWidget(Rect r, Point pt, int min_value, int max_value, int &value)
Handle click on a slider widget to change the value.
WidgetID mouse_capture_widget
ID of current mouse capture widget (e.g. dragged scrollbar). -1 if no widget has mouse capture.
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
static const uint8_t PC_BLACK
Black palette colour.
void OnInvalidateData([[maybe_unused]] int data=0, [[maybe_unused]] bool gui_scope=true) override
Some data on this window has become invalid.
PlaylistEntry GetCurrentSong() const
Return the current song, or a dummy if none.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
#define lengthof(x)
Return the length of an fixed size array.
byte effect_vol
The requested effects volume.
void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
int GetRowFromWidget(int clickpos, WidgetID widget, int padding, int line_height=-1) const
Compute the row of a widget that a user clicked in.
void SetPositionBySetIndex(uint set_index)
Set playlist position by set index.
const MusicSet * set
music set the song comes from
uint set_index
index of song in set
void Stop()
Stop playback and set flag that we don't intend to play music.
Data structure for an opened window.
void ChangePlaylistPosition(int ofs)
Change playlist position pointer by the given offset, making sure to keep it within valid range.
Playlist active_playlist
current play order of songs, including any shuffle
Specification of a rectangle with absolute coordinates of all edges.
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
void SetWidgetsDisabledState(bool disab_stat, Args... widgets)
Sets the enabled/disabled status of a list of widgets.
MusicSettings music
settings related to music/sound
void ChangeMusicSet(int index)
Change the configured music set and reset playback.
byte music_vol
The requested music volume.
Playlist music_set
all songs in current music set, in set order
@ TD_RTL
Text is written right-to-left by default.
TextDirection _current_text_dir
Text direction of the currently selected language.
int cat_index
entry index in CAT file, for filetype==MTT_MPSMIDI
byte custom_1[33]
The order of the first custom playlist.
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
@ WC_MUSIC_TRACK_SELECTION
Music track selection; Window numbers:
void PlaylistRemove(size_t song_index)
Remove a song from a custom playlist.
virtual void StopSong()=0
Stop playing the current song.