mirror of https://github.com/OpenTTD/OpenTTD.git
Change: Don't restart playback when toggling playlist shuffle.
Instead update the selected playlist entry for the current song.
This commit is contained in:
parent
6c91c4b99d
commit
ab1a4c6c80
|
@ -87,6 +87,7 @@ struct MusicSystem {
|
||||||
void PlaylistClear();
|
void PlaylistClear();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void SetPositionBySetIndex(uint set_index);
|
||||||
void ChangePlaylistPosition(int ofs);
|
void ChangePlaylistPosition(int ofs);
|
||||||
int playlist_position;
|
int playlist_position;
|
||||||
|
|
||||||
|
@ -184,30 +185,45 @@ void MusicSystem::ChangeMusicSet(const std::string &set_name)
|
||||||
InvalidateWindowData(WC_MUSIC_WINDOW, 0, 1, true);
|
InvalidateWindowData(WC_MUSIC_WINDOW, 0, 1, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Enable shuffle mode and restart playback */
|
/**
|
||||||
|
* Set playlist position by set index.
|
||||||
|
* @param set_index Set index to select.
|
||||||
|
*/
|
||||||
|
void MusicSystem::SetPositionBySetIndex(uint set_index)
|
||||||
|
{
|
||||||
|
auto it = std::find_if(std::begin(this->active_playlist), std::end(this->active_playlist), [&set_index](const PlaylistEntry &ple) { return ple.set_index == set_index; });
|
||||||
|
if (it != std::end(this->active_playlist)) this->playlist_position = std::distance(std::begin(this->active_playlist), it);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable shuffle mode.
|
||||||
|
*/
|
||||||
void MusicSystem::Shuffle()
|
void MusicSystem::Shuffle()
|
||||||
{
|
{
|
||||||
_settings_client.music.shuffle = true;
|
_settings_client.music.shuffle = true;
|
||||||
|
|
||||||
|
uint set_index = this->active_playlist[this->playlist_position].set_index;
|
||||||
this->active_playlist = this->displayed_playlist;
|
this->active_playlist = this->displayed_playlist;
|
||||||
for (size_t i = 0; i < this->active_playlist.size(); i++) {
|
for (size_t i = 0; i < this->active_playlist.size(); i++) {
|
||||||
size_t shuffle_index = InteractiveRandom() % (this->active_playlist.size() - i);
|
size_t shuffle_index = InteractiveRandom() % (this->active_playlist.size() - i);
|
||||||
std::swap(this->active_playlist[i], this->active_playlist[i + shuffle_index]);
|
std::swap(this->active_playlist[i], this->active_playlist[i + shuffle_index]);
|
||||||
}
|
}
|
||||||
|
this->SetPositionBySetIndex(set_index);
|
||||||
if (_settings_client.music.playing) this->Play();
|
|
||||||
|
|
||||||
InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0);
|
InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0);
|
||||||
InvalidateWindowData(WC_MUSIC_WINDOW, 0);
|
InvalidateWindowData(WC_MUSIC_WINDOW, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Disable shuffle and restart playback */
|
/**
|
||||||
|
* Disable shuffle mode.
|
||||||
|
*/
|
||||||
void MusicSystem::Unshuffle()
|
void MusicSystem::Unshuffle()
|
||||||
{
|
{
|
||||||
_settings_client.music.shuffle = false;
|
_settings_client.music.shuffle = false;
|
||||||
this->active_playlist = this->displayed_playlist;
|
|
||||||
|
|
||||||
if (_settings_client.music.playing) this->Play();
|
uint set_index = this->active_playlist[this->playlist_position].set_index;
|
||||||
|
this->active_playlist = this->displayed_playlist;
|
||||||
|
this->SetPositionBySetIndex(set_index);
|
||||||
|
|
||||||
InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0);
|
InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0);
|
||||||
InvalidateWindowData(WC_MUSIC_WINDOW, 0);
|
InvalidateWindowData(WC_MUSIC_WINDOW, 0);
|
||||||
|
|
Loading…
Reference in New Issue