* 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 */
| 324 | * @param song_index Index of song in the current music set to add |
| 325 | */ |
| 326 | void 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. |
no test coverage detected