/// <summary> /// When a song is selected /// </summary> /// <param name="p_TableView">Source table</param> /// <param name="p_Row">Selected row</param> private void OnSongSelected(TableView p_TableView, int p_Row) { /// Fetch song entry BeatSaverCustomSongCellInfo l_SongRow = m_SongList.data[p_Row] as BeatSaverCustomSongCellInfo; /// Get song entry var l_SongEntry = m_SongsProvider.Where(x => x.BeatMap.Hash == l_SongRow.SongHash).FirstOrDefault(); /// Hide if invalid song if (l_SongEntry == null) { /// Hide song info panel UnselectSong(); return; } /// Show UIs m_SongInfoPanel.SetActive(true); /// Update UIs m_SongInfo_Detail.FromBeatSaver(l_SongEntry.BeatMap, l_SongEntry.Cover); /// Set selected song m_SelectedSong = l_SongEntry; /// Launch preview music if local map var l_LocalSong = SongCore.Loader.GetLevelByHash(m_SelectedSong.BeatMap.Hash); if (l_LocalSong != null) { m_SongInfo_Detail.SetPlayButtonText("Play"); /// Load the song clip to get duration if (Config.ChatRequest.PlayPreviewMusic) { l_LocalSong.GetPreviewAudioClipAsync(CancellationToken.None).ContinueWith(x => { if (x.IsCompleted && x.Status == TaskStatus.RanToCompletion) { HMMainThreadDispatcher.instance.Enqueue(() => { /// Start preview song m_SongPreviewPlayer.CrossfadeTo(x.Result, l_LocalSong.previewStartTime, l_LocalSong.previewDuration, 1f); }); } }); } } /// Stop preview music if any else { m_SongInfo_Detail.SetPlayButtonText("Download"); m_SongPreviewPlayer.CrossfadeToDefault(); } }
/// <summary> /// Rebuild song list /// </summary> /// <param name="p_OnActivation">Is on activation</param> /// <returns></returns> internal void RebuildSongList(bool p_OnActivation = false) { /// Clear selection and items, then refresh the list m_SongList.tableView.ClearSelection(); m_SongList.data.Clear(); lock (m_SongsProvider) { /// Append all songs if (m_SongsProvider.Count > 0) { /// Handle page overflow if (((m_CurrentPage - 1) * 7) > m_SongsProvider.Count) { m_CurrentPage = (m_SongsProvider.Count / 7) + 1; } for (int l_I = (m_CurrentPage - 1) * 7; l_I < (m_CurrentPage * 7); ++l_I) { if (l_I >= m_SongsProvider.Count) { break; } var l_Current = m_SongsProvider[l_I]; var l_Entry = new BeatSaverCustomSongCellInfo(l_Current.BeatMap.Hash, "", ""); var l_LocalSong = SongCore.Loader.GetLevelByHash(l_Current.BeatMap.Hash); if (!l_Current.BeatMap.Partial) { l_Entry.text = (l_LocalSong != null ? "<#7F7F7F>" : "") + l_Current.BeatMap.Name; l_Entry.subtext = l_Current.BeatMap.Metadata.SongAuthorName + " [" + l_Current.BeatMap.Metadata.LevelAuthorName + "]"; if (l_Current.Cover != null) { l_Entry.icon = l_Current.Cover; } else { /// Fetch cover var l_CoverTask = l_Current.BeatMap.FetchCoverImage(); _ = l_CoverTask.ContinueWith(p_CoverTaskResult => { HMMainThreadDispatcher.instance.Enqueue(() => { var l_Texture = new Texture2D(2, 2); if (l_Texture.LoadImage(p_CoverTaskResult.Result)) { l_Entry.icon = Sprite.Create(l_Texture, new Rect(0, 0, l_Texture.width, l_Texture.height), new Vector2(0.5f, 0.5f), 100); l_Current.Cover = l_Entry.icon; } else { l_Entry.icon = null; } /// Refresh cells m_SongList.tableView.RefreshCellsContent(); }); }); } } else { l_Entry.text = "Loading from beatsaver..."; l_Entry.subtext = ""; } m_SongList.data.Add(l_Entry); if (m_SelectedSong != null && m_SelectedSong.BeatMap.Hash == l_Current.BeatMap.Hash) { m_SongList.tableView.SelectCellWithIdx(m_SongList.data.Count - 1); OnSongSelected(m_SongList.tableView, m_SongList.data.Count - 1); } } if (m_SelectedSong != null && m_SongsProvider.Where(x => x.BeatMap.Hash == m_SelectedSong.BeatMap.Hash).Count() == 0) { UnselectSong(); m_SelectedSong = null; } } else { m_CurrentPage = 1; UnselectSong(); } /// Refresh the list m_SongList.tableView.ReloadData(); /// Update UI m_SongUpButton.interactable = m_CurrentPage != 1; m_SongDownButton.interactable = m_SongsProvider.Count > (m_CurrentPage * 7); } }