PlaylistSelectionListViewController.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using HMUI;
  2. using SongBrowser.DataAccess;
  3. using SongBrowser.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 SongBrowser.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(75f, 0f);
  32. rectTransform.pivot = new Vector2(0.5f, 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, -9f);
  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>().First(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, 9f);
  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.anchorMin = new Vector2(0f, 0.5f);
  57. container.anchorMax = new Vector2(1f, 0.5f);
  58. container.sizeDelta = new Vector2(0f, 0f);
  59. container.anchoredPosition = new Vector2(0f, 0f);
  60. _songsTableView = new GameObject("CustomTableView", typeof(RectTransform)).AddComponent<TableView>();
  61. _songsTableView.gameObject.AddComponent<RectMask2D>();
  62. _songsTableView.transform.SetParent(container, false);
  63. _songsTableView.SetPrivateField("_isInitialized", false);
  64. _songsTableView.SetPrivateField("_preallocatedCells", new TableView.CellsGroup[0]);
  65. _songsTableView.Init();
  66. (_songsTableView.transform as RectTransform).anchorMin = new Vector2(0f, 0f);
  67. (_songsTableView.transform as RectTransform).anchorMax = new Vector2(1f, 1f);
  68. (_songsTableView.transform as RectTransform).sizeDelta = new Vector2(0f, 60f);
  69. (_songsTableView.transform as RectTransform).anchoredPosition = new Vector2(0f, 0f);
  70. _songsTableView.dataSource = this;
  71. _songsTableView.ScrollToCellWithIdx(0, TableView.ScrollPositionType.Beginning, false);
  72. _lastSelectedRow = -1;
  73. _songsTableView.didSelectCellWithIdxEvent += _songsTableView_DidSelectRowEvent;
  74. }
  75. else
  76. {
  77. _songsTableView.ReloadData();
  78. _songsTableView.ScrollToCellWithIdx(0, TableView.ScrollPositionType.Beginning, false);
  79. _lastSelectedRow = -1;
  80. }
  81. }
  82. internal void Refresh()
  83. {
  84. _songsTableView.RefreshTable();
  85. }
  86. protected override void DidDeactivate(DeactivationType type)
  87. {
  88. _lastSelectedRow = -1;
  89. }
  90. public void SetContent(List<Playlist> playlists)
  91. {
  92. if (playlists == null && playlistList != null)
  93. playlistList.Clear();
  94. else
  95. playlistList = new List<Playlist>(playlists);
  96. if (_songsTableView != null)
  97. {
  98. _songsTableView.ReloadData();
  99. _songsTableView.ScrollToCellWithIdx(0, TableView.ScrollPositionType.Center, false);
  100. }
  101. }
  102. private void _songsTableView_DidSelectRowEvent(TableView sender, int row)
  103. {
  104. _lastSelectedRow = row;
  105. didSelectRow?.Invoke(playlistList[row]);
  106. }
  107. public float CellSize()
  108. {
  109. return 10f;
  110. }
  111. public int NumberOfCells()
  112. {
  113. return playlistList.Count;
  114. }
  115. public TableCell CellForIdx(int row)
  116. {
  117. LevelListTableCell _tableCell = Instantiate(_songListTableCellInstance);
  118. _tableCell.reuseIdentifier = "PlaylistTableCell";
  119. var songNameText = _tableCell.GetPrivateField<TextMeshProUGUI>("_songNameText");
  120. songNameText.text = playlistList[row].playlistTitle;
  121. songNameText.overflowMode = TextOverflowModes.Overflow;
  122. _tableCell.GetPrivateField<TextMeshProUGUI>("_authorText").text = playlistList[row].playlistAuthor;
  123. _tableCell.GetPrivateField<UnityEngine.UI.Image>("_coverImage").sprite = playlistList[row].icon;
  124. _tableCell.SetPrivateField("_beatmapCharacteristicAlphas", new float[0]);
  125. _tableCell.SetPrivateField("_beatmapCharacteristicImages", new UnityEngine.UI.Image[0]);
  126. _tableCell.SetPrivateField("_bought", true);
  127. foreach (var icon in _tableCell.GetComponentsInChildren<UnityEngine.UI.Image>().Where(x => x.name.StartsWith("LevelTypeIcon")))
  128. {
  129. Destroy(icon.gameObject);
  130. }
  131. if (highlightDownloadedPlaylists)
  132. {
  133. if (PlaylistsCollection.loadedPlaylists.Any(x => x.PlaylistEqual(playlistList[row])))
  134. {
  135. foreach (UnityEngine.UI.Image img in _tableCell.GetComponentsInChildren<UnityEngine.UI.Image>())
  136. {
  137. img.color = new Color(1f, 1f, 1f, 0.2f);
  138. }
  139. foreach (TextMeshProUGUI text in _tableCell.GetComponentsInChildren<TextMeshProUGUI>())
  140. {
  141. text.faceColor = new Color(1f, 1f, 1f, 0.2f);
  142. }
  143. }
  144. }
  145. return _tableCell;
  146. }
  147. #if DEBUG
  148. private void LateUpdate()
  149. {
  150. CheckDebugUserInput();
  151. }
  152. private void CheckDebugUserInput()
  153. {
  154. bool isShiftKeyDown = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  155. if (Input.GetKeyDown(KeyCode.N) && isShiftKeyDown)
  156. {
  157. _songsTableView.PageScrollUp();
  158. }
  159. else if (Input.GetKeyDown(KeyCode.N))
  160. {
  161. _lastSelectedRow = (_lastSelectedRow - 1) % _songsTableView.dataSource.NumberOfCells();
  162. if (_lastSelectedRow < 0)
  163. {
  164. _lastSelectedRow = _songsTableView.dataSource.NumberOfCells() - 1;
  165. }
  166. _songsTableView.ScrollToCellWithIdx(_lastSelectedRow, TableView.ScrollPositionType.Beginning, false);
  167. this._songsTableView_DidSelectRowEvent(_songsTableView, _lastSelectedRow);
  168. }
  169. if (Input.GetKeyDown(KeyCode.M) && isShiftKeyDown)
  170. {
  171. _songsTableView.PageScrollDown();
  172. }
  173. else if (Input.GetKeyDown(KeyCode.M))
  174. {
  175. _lastSelectedRow = (_lastSelectedRow + 1) % _songsTableView.dataSource.NumberOfCells();
  176. _songsTableView.ScrollToCellWithIdx(_lastSelectedRow, TableView.ScrollPositionType.End, false);
  177. this._songsTableView_DidSelectRowEvent(_songsTableView, _lastSelectedRow);
  178. }
  179. }
  180. #endif
  181. }
  182. }