PlaylistSelectionListViewController.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using HMUI;
  2. using SongBrowserPlugin.DataAccess;
  3. using SongBrowserPlugin.Internals;
  4. using SongLoaderPlugin;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using TMPro;
  10. using UnityEngine;
  11. using UnityEngine.UI;
  12. using VRUI;
  13. namespace SongBrowserPlugin.UI
  14. {
  15. class PlaylistListViewController : VRUIViewController, TableView.IDataSource
  16. {
  17. public event Action<Playlist> didSelectRow;
  18. public List<Playlist> playlistList = new List<Playlist>();
  19. public bool highlightDownloadedPlaylists = false;
  20. private Button _pageUpButton;
  21. private Button _pageDownButton;
  22. private TableView _songsTableView;
  23. private LevelListTableCell _songListTableCellInstance;
  24. private int _lastSelectedRow;
  25. protected override void DidActivate(bool firstActivation, ActivationType type)
  26. {
  27. if (firstActivation && type == ActivationType.AddedToHierarchy)
  28. {
  29. rectTransform.anchorMin = new Vector2(0.5f, 0f);
  30. rectTransform.anchorMax = new Vector2(0.5f, 1f);
  31. rectTransform.sizeDelta = new Vector2(74f, 0f);
  32. rectTransform.pivot = new Vector2(0.4f, 0.5f);
  33. _pageUpButton = Instantiate(Resources.FindObjectsOfTypeAll<Button>().Last(x => (x.name == "PageUpButton")), rectTransform, false);
  34. (_pageUpButton.transform as RectTransform).anchorMin = new Vector2(0.5f, 1f);
  35. (_pageUpButton.transform as RectTransform).anchorMax = new Vector2(0.5f, 1f);
  36. (_pageUpButton.transform as RectTransform).anchoredPosition = new Vector2(0f, -10f);
  37. (_pageUpButton.transform as RectTransform).sizeDelta = new Vector2(40f, 10f);
  38. _pageUpButton.interactable = true;
  39. _pageUpButton.onClick.AddListener(delegate ()
  40. {
  41. _songsTableView.PageScrollUp();
  42. });
  43. _pageDownButton = Instantiate(Resources.FindObjectsOfTypeAll<Button>().Last(x => (x.name == "PageDownButton")), rectTransform, false);
  44. (_pageDownButton.transform as RectTransform).anchorMin = new Vector2(0.5f, 0f);
  45. (_pageDownButton.transform as RectTransform).anchorMax = new Vector2(0.5f, 0f);
  46. (_pageDownButton.transform as RectTransform).anchoredPosition = new Vector2(0f, 10f);
  47. (_pageDownButton.transform as RectTransform).sizeDelta = new Vector2(40f, 10f);
  48. _pageDownButton.interactable = true;
  49. _pageDownButton.onClick.AddListener(delegate ()
  50. {
  51. _songsTableView.PageScrollDown();
  52. });
  53. _songListTableCellInstance = Resources.FindObjectsOfTypeAll<LevelListTableCell>().First(x => (x.name == "LevelListTableCell"));
  54. RectTransform container = new GameObject("CustomListContainer", typeof(RectTransform)).transform as RectTransform;
  55. container.SetParent(rectTransform, false);
  56. container.sizeDelta = new Vector2(60f, 0f);
  57. _songsTableView = new GameObject("CustomTableView", typeof(RectTransform)).AddComponent<TableView>();
  58. _songsTableView.gameObject.AddComponent<RectMask2D>();
  59. _songsTableView.transform.SetParent(container, false);
  60. _songsTableView.SetPrivateField("_isInitialized", false);
  61. _songsTableView.SetPrivateField("_preallocatedCells", new TableView.CellsGroup[0]);
  62. _songsTableView.Init();
  63. (_songsTableView.transform as RectTransform).anchorMin = new Vector2(0f, 0f);
  64. (_songsTableView.transform as RectTransform).anchorMax = new Vector2(1f, 1f);
  65. (_songsTableView.transform as RectTransform).sizeDelta = new Vector2(0f, 60f);
  66. (_songsTableView.transform as RectTransform).anchoredPosition = new Vector2(0f, 0f);
  67. _songsTableView.dataSource = this;
  68. _songsTableView.ScrollToCellWithIdx(0, TableView.ScrollPositionType.Beginning, false);
  69. _lastSelectedRow = -1;
  70. _songsTableView.didSelectCellWithIdxEvent += _songsTableView_DidSelectRowEvent;
  71. }
  72. else
  73. {
  74. _songsTableView.ReloadData();
  75. _songsTableView.ScrollToCellWithIdx(0, TableView.ScrollPositionType.Beginning, false);
  76. _lastSelectedRow = -1;
  77. }
  78. }
  79. internal void Refresh()
  80. {
  81. _songsTableView.RefreshTable();
  82. }
  83. protected override void DidDeactivate(DeactivationType type)
  84. {
  85. _lastSelectedRow = -1;
  86. }
  87. public void SetContent(List<Playlist> playlists)
  88. {
  89. if (playlists == null && playlistList != null)
  90. playlistList.Clear();
  91. else
  92. playlistList = new List<Playlist>(playlists);
  93. if (_songsTableView != null)
  94. {
  95. _songsTableView.ReloadData();
  96. _songsTableView.ScrollToCellWithIdx(0, TableView.ScrollPositionType.Center, false);
  97. }
  98. }
  99. private void _songsTableView_DidSelectRowEvent(TableView sender, int row)
  100. {
  101. _lastSelectedRow = row;
  102. didSelectRow?.Invoke(playlistList[row]);
  103. }
  104. public float CellSize()
  105. {
  106. return 10f;
  107. }
  108. public int NumberOfCells()
  109. {
  110. return playlistList.Count;
  111. }
  112. public TableCell CellForIdx(int row)
  113. {
  114. LevelListTableCell _tableCell = Instantiate(_songListTableCellInstance);
  115. _tableCell.reuseIdentifier = "PlaylistTableCell";
  116. _tableCell.GetPrivateField<TextMeshProUGUI>("_songNameText").text = playlistList[row].playlistTitle;
  117. _tableCell.GetPrivateField<TextMeshProUGUI>("_authorText").text = playlistList[row].playlistAuthor;
  118. _tableCell.GetPrivateField<UnityEngine.UI.Image>("_coverImage").sprite = playlistList[row].icon;
  119. _tableCell.SetPrivateField("_beatmapCharacteristicAlphas", new float[0]);
  120. _tableCell.SetPrivateField("_beatmapCharacteristicImages", new UnityEngine.UI.Image[0]);
  121. _tableCell.SetPrivateField("_bought", true);
  122. foreach (var icon in _tableCell.GetComponentsInChildren<UnityEngine.UI.Image>().Where(x => x.name.StartsWith("LevelTypeIcon")))
  123. {
  124. Destroy(icon.gameObject);
  125. }
  126. if (highlightDownloadedPlaylists)
  127. {
  128. if (PlaylistsCollection.loadedPlaylists.Any(x => x.PlaylistEqual(playlistList[row])))
  129. {
  130. foreach (UnityEngine.UI.Image img in _tableCell.GetComponentsInChildren<UnityEngine.UI.Image>())
  131. {
  132. img.color = new Color(1f, 1f, 1f, 0.2f);
  133. }
  134. foreach (TextMeshProUGUI text in _tableCell.GetComponentsInChildren<TextMeshProUGUI>())
  135. {
  136. text.faceColor = new Color(1f, 1f, 1f, 0.2f);
  137. }
  138. }
  139. }
  140. return _tableCell;
  141. }
  142. #if DEBUG
  143. private void LateUpdate()
  144. {
  145. CheckDebugUserInput();
  146. }
  147. private void CheckDebugUserInput()
  148. {
  149. bool isShiftKeyDown = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  150. if (Input.GetKeyDown(KeyCode.N) && isShiftKeyDown)
  151. {
  152. _songsTableView.PageScrollUp();
  153. }
  154. else if (Input.GetKeyDown(KeyCode.N))
  155. {
  156. _lastSelectedRow = (_lastSelectedRow - 1) % _songsTableView.dataSource.NumberOfCells();
  157. if (_lastSelectedRow < 0)
  158. {
  159. _lastSelectedRow = _songsTableView.dataSource.NumberOfCells() - 1;
  160. }
  161. _songsTableView.ScrollToCellWithIdx(_lastSelectedRow, TableView.ScrollPositionType.Beginning, false);
  162. this._songsTableView_DidSelectRowEvent(_songsTableView, _lastSelectedRow);
  163. }
  164. if (Input.GetKeyDown(KeyCode.M) && isShiftKeyDown)
  165. {
  166. _songsTableView.PageScrollDown();
  167. }
  168. else if (Input.GetKeyDown(KeyCode.M))
  169. {
  170. _lastSelectedRow = (_lastSelectedRow + 1) % _songsTableView.dataSource.NumberOfCells();
  171. _songsTableView.ScrollToCellWithIdx(_lastSelectedRow, TableView.ScrollPositionType.End, false);
  172. this._songsTableView_DidSelectRowEvent(_songsTableView, _lastSelectedRow);
  173. }
  174. }
  175. #endif
  176. }
  177. }