MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / PlaylistAdd

Method PlaylistAdd

src/music_gui.cpp:326–358  ·  view source on GitHub ↗

* Append a song to a custom playlist. * Always adds to the currently active playlist. * @param song_index Index of song in the current music set to add */

Source from the content-addressed store, hash-verified

324 * @param song_index Index of song in the current music set to add
325 */
326void MusicSystem::PlaylistAdd(size_t song_index)
327{
328 if (!this->IsCustomPlaylist()) return;
329
330 /* Pick out song from the music set */
331 if (song_index >= this->music_set.size()) return;
332 PlaylistEntry entry = this->music_set[song_index];
333
334 /* Check for maximum length */
335 if (this->standard_playlists[this->selected_playlist].size() >= NUM_SONGS_PLAYLIST) return;
336
337 /* Add it to the appropriate playlist, and the display */
338 this->standard_playlists[this->selected_playlist].push_back(entry);
339
340 /* Add it to the active playlist, if playback is shuffled select a random position to add at */
341 if (this->active_playlist.empty()) {
342 this->active_playlist.push_back(std::move(entry));
343 if (this->IsPlaying()) this->Play();
344 } else if (this->IsShuffle()) {
345 /* Generate a random position between 0 and n (inclusive, new length) to insert at */
346 size_t maxpos = this->active_playlist.size() + 1;
347 size_t newpos = InteractiveRandom() % maxpos;
348 this->active_playlist.insert(this->active_playlist.begin() + newpos, entry);
349 /* Make sure to shift up the current playback position if the song was inserted before it */
350 if ((int)newpos <= this->playlist_position) this->playlist_position++;
351 } else {
352 this->active_playlist.push_back(std::move(entry));
353 }
354
355 this->SaveCustomPlaylist(this->selected_playlist);
356
357 InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0);
358}
359
360/**
361 * Remove a song from a custom playlist.

Callers 1

OnClickMethod · 0.80

Calls 12

IsCustomPlaylistMethod · 0.95
IsPlayingMethod · 0.95
PlayMethod · 0.95
IsShuffleMethod · 0.95
SaveCustomPlaylistMethod · 0.95
InteractiveRandomFunction · 0.85
push_backMethod · 0.80
InvalidateWindowDataFunction · 0.70
sizeMethod · 0.45
emptyMethod · 0.45
insertMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected