PlaylistSelectionListViewController.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using HMUI;
  2. using SongBrowser.DataAccess;
  3. using SongBrowser.Internals;
  4. using SongCore.Utilities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using TMPro;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using VRUI;
  12. using Logger = SongBrowser.Logging.Logger;
  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.GetPrivateField<TableViewScroller>("_scroller").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.GetPrivateField<TableViewScroller>("_scroller").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. var gameObject = new GameObject("CustomTableView", typeof(RectTransform));
  61. gameObject.SetActive(false);
  62. _songsTableView = gameObject.AddComponent<TableView>();
  63. _songsTableView.gameObject.AddComponent<RectMask2D>();
  64. _songsTableView.transform.SetParent(container, false);
  65. _songsTableView.SetPrivateField("_isInitialized", false);
  66. _songsTableView.SetPrivateField("_preallocatedCells", new TableView.CellsGroup[0]);
  67. _songsTableView.Init();
  68. gameObject.SetActive(true);
  69. (_songsTableView.transform as RectTransform).anchorMin = new Vector2(0f, 0f);
  70. (_songsTableView.transform as RectTransform).anchorMax = new Vector2(1f, 1f);
  71. (_songsTableView.transform as RectTransform).sizeDelta = new Vector2(0f, 60f);
  72. (_songsTableView.transform as RectTransform).anchoredPosition = new Vector2(0f, 0f);
  73. _songsTableView.dataSource = this;
  74. _songsTableView.ScrollToCellWithIdx(0, TableViewScroller.ScrollPositionType.Beginning, false);
  75. _lastSelectedRow = -1;
  76. _songsTableView.didSelectCellWithIdxEvent += _songsTableView_DidSelectRowEvent;
  77. }
  78. else
  79. {
  80. _songsTableView.ReloadData();
  81. _songsTableView.ScrollToCellWithIdx(0, TableViewScroller.ScrollPositionType.Beginning, false);
  82. _lastSelectedRow = -1;
  83. }
  84. }
  85. internal void Refresh()
  86. {
  87. _songsTableView.RefreshTable();
  88. }
  89. protected override void DidDeactivate(DeactivationType type)
  90. {
  91. _lastSelectedRow = -1;
  92. }
  93. public void SetContent(List<Playlist> playlists)
  94. {
  95. if (playlists == null && playlistList != null)
  96. playlistList.Clear();
  97. else
  98. playlistList = new List<Playlist>(playlists);
  99. if (_songsTableView != null)
  100. {
  101. _songsTableView.ReloadData();
  102. _songsTableView.ScrollToCellWithIdx(0, TableViewScroller.ScrollPositionType.Center, false);
  103. }
  104. }
  105. private void _songsTableView_DidSelectRowEvent(TableView sender, int row)
  106. {
  107. _lastSelectedRow = row;
  108. didSelectRow?.Invoke(playlistList[row]);
  109. }
  110. public float CellSize()
  111. {
  112. return 10f;
  113. }
  114. public int NumberOfCells()
  115. {
  116. return playlistList.Count;
  117. }
  118. public TableCell CellForIdx(TableView view, int row)
  119. {
  120. LevelListTableCell _tableCell = Instantiate(_songListTableCellInstance);
  121. _tableCell.reuseIdentifier = "PlaylistTableCell";
  122. var songNameText = _tableCell.GetPrivateField<TextMeshProUGUI>("_songNameText");
  123. songNameText.text = playlistList[row].playlistTitle;
  124. songNameText.overflowMode = TextOverflowModes.Overflow;
  125. _tableCell.GetPrivateField<TextMeshProUGUI>("_authorText").text = playlistList[row].playlistAuthor;
  126. if (playlistList[row].icon != null)
  127. {
  128. _tableCell.GetPrivateField<RawImage>("_coverRawImage").texture = playlistList[row].icon.texture;
  129. }
  130. _tableCell.SetPrivateField("_beatmapCharacteristicAlphas", new float[0]);
  131. _tableCell.SetPrivateField("_beatmapCharacteristicImages", new UnityEngine.UI.Image[0]);
  132. _tableCell.SetPrivateField("_bought", true);
  133. foreach (var icon in _tableCell.GetComponentsInChildren<UnityEngine.UI.Image>().Where(x => x.name.StartsWith("LevelTypeIcon")))
  134. {
  135. Destroy(icon.gameObject);
  136. }
  137. if (highlightDownloadedPlaylists)
  138. {
  139. if (PlaylistsCollection.loadedPlaylists.Any(x => x.PlaylistEqual(playlistList[row])))
  140. {
  141. foreach (UnityEngine.UI.Image img in _tableCell.GetComponentsInChildren<UnityEngine.UI.Image>())
  142. {
  143. img.color = new Color(1f, 1f, 1f, 0.2f);
  144. }
  145. foreach (TextMeshProUGUI text in _tableCell.GetComponentsInChildren<TextMeshProUGUI>())
  146. {
  147. text.faceColor = new Color(1f, 1f, 1f, 0.2f);
  148. }
  149. }
  150. }
  151. return _tableCell;
  152. }
  153. }
  154. }