PlaylistSelectionListViewController.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using HMUI;
  2. using SongBrowserPlugin.DataAccess;
  3. using SongLoaderPlugin;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using VRUI;
  11. namespace SongBrowserPlugin.UI
  12. {
  13. class PlaylistListViewController : VRUIViewController, TableView.IDataSource
  14. {
  15. public event Action<Playlist> didSelectRow;
  16. public List<Playlist> playlistList = new List<Playlist>();
  17. private Button _pageUpButton;
  18. private Button _pageDownButton;
  19. private TableView _songsTableView;
  20. private LevelListTableCell _songListTableCellInstance;
  21. private int _lastSelectedRow;
  22. protected override void DidActivate(bool firstActivation, ActivationType type)
  23. {
  24. if (firstActivation && type == ActivationType.AddedToHierarchy)
  25. {
  26. rectTransform.anchorMin = new Vector2(0.3f, 0f);
  27. rectTransform.anchorMax = new Vector2(0.7f, 1f);
  28. _pageUpButton = Instantiate(Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == "PageUpButton")), rectTransform, false);
  29. (_pageUpButton.transform as RectTransform).anchorMin = new Vector2(0.5f, 1f);
  30. (_pageUpButton.transform as RectTransform).anchorMax = new Vector2(0.5f, 1f);
  31. (_pageUpButton.transform as RectTransform).anchoredPosition = new Vector2(0f, -14f);
  32. (_pageUpButton.transform as RectTransform).sizeDelta = new Vector2(40f, 10f);
  33. _pageUpButton.interactable = true;
  34. _pageUpButton.onClick.AddListener(delegate ()
  35. {
  36. _songsTableView.PageScrollUp();
  37. });
  38. _pageDownButton = Instantiate(Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == "PageDownButton")), rectTransform, false);
  39. (_pageDownButton.transform as RectTransform).anchorMin = new Vector2(0.5f, 0f);
  40. (_pageDownButton.transform as RectTransform).anchorMax = new Vector2(0.5f, 0f);
  41. (_pageDownButton.transform as RectTransform).anchoredPosition = new Vector2(0f, 11f);
  42. (_pageDownButton.transform as RectTransform).sizeDelta = new Vector2(40f, 10f);
  43. _pageDownButton.interactable = true;
  44. _pageDownButton.onClick.AddListener(delegate ()
  45. {
  46. _songsTableView.PageScrollDown();
  47. });
  48. _songListTableCellInstance = Resources.FindObjectsOfTypeAll<LevelListTableCell>().First(x => (x.name == "LevelListTableCell"));
  49. _songsTableView = new GameObject().AddComponent<TableView>();
  50. _songsTableView.transform.SetParent(rectTransform, false);
  51. _songsTableView.SetPrivateField("_isInitialized", false);
  52. _songsTableView.SetPrivateField("_preallocatedCells", new TableView.CellsGroup[0]);
  53. _songsTableView.Init();
  54. RectMask2D viewportMask = Instantiate(Resources.FindObjectsOfTypeAll<RectMask2D>().First(), _songsTableView.transform, false);
  55. viewportMask.transform.DetachChildren();
  56. _songsTableView.GetComponentsInChildren<RectTransform>().First(x => x.name == "Content").transform.SetParent(viewportMask.rectTransform, false);
  57. (_songsTableView.transform as RectTransform).anchorMin = new Vector2(0f, 0.5f);
  58. (_songsTableView.transform as RectTransform).anchorMax = new Vector2(1f, 0.5f);
  59. (_songsTableView.transform as RectTransform).sizeDelta = new Vector2(0f, 60f);
  60. (_songsTableView.transform as RectTransform).position = new Vector3(0f, 0f, 2.4f);
  61. (_songsTableView.transform as RectTransform).anchoredPosition = new Vector3(0f, -3f);
  62. _songsTableView.SetPrivateField("_pageUpButton", _pageUpButton);
  63. _songsTableView.SetPrivateField("_pageDownButton", _pageDownButton);
  64. _songsTableView.dataSource = this;
  65. _songsTableView.ScrollToRow(0, false);
  66. _lastSelectedRow = -1;
  67. _songsTableView.didSelectRowEvent += _songsTableView_DidSelectRowEvent;
  68. }
  69. else
  70. {
  71. _songsTableView.ReloadData();
  72. _songsTableView.ScrollToRow(0, false);
  73. _lastSelectedRow = -1;
  74. }
  75. }
  76. internal void Refresh()
  77. {
  78. _songsTableView.ReloadData();
  79. if (_lastSelectedRow > -1)
  80. _songsTableView.SelectRow(_lastSelectedRow);
  81. }
  82. protected override void DidDeactivate(DeactivationType type)
  83. {
  84. _lastSelectedRow = -1;
  85. }
  86. public void SetContent(List<Playlist> playlists)
  87. {
  88. if (playlists == null && playlistList != null)
  89. playlistList.Clear();
  90. else
  91. playlistList = new List<Playlist>(playlists);
  92. if (_songsTableView != null)
  93. {
  94. _songsTableView.ReloadData();
  95. _songsTableView.ScrollToRow(0, false);
  96. _songsTableView.SelectRow(0, true);
  97. }
  98. }
  99. private void _songsTableView_DidSelectRowEvent(TableView sender, int row)
  100. {
  101. _lastSelectedRow = row;
  102. didSelectRow?.Invoke(playlistList[row]);
  103. }
  104. public float RowHeight()
  105. {
  106. return 10f;
  107. }
  108. public int NumberOfRows()
  109. {
  110. return playlistList.Count;
  111. }
  112. public TableCell CellForRow(int row)
  113. {
  114. LevelListTableCell _tableCell = Instantiate(_songListTableCellInstance);
  115. _tableCell.reuseIdentifier = "PlaylistTableCell";
  116. _tableCell.songName = playlistList[row].Title;
  117. _tableCell.author = playlistList[row].Author;
  118. if (playlistList[row].Image != null)
  119. _tableCell.coverImage = Base64Sprites.Base64ToSprite(playlistList[row].Image);
  120. return _tableCell;
  121. }
  122. #if DEBUG
  123. private void LateUpdate()
  124. {
  125. CheckDebugUserInput();
  126. }
  127. private void CheckDebugUserInput()
  128. {
  129. bool isShiftKeyDown = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  130. if (Input.GetKeyDown(KeyCode.N) && isShiftKeyDown)
  131. {
  132. _songsTableView.PageScrollUp();
  133. }
  134. else if (Input.GetKeyDown(KeyCode.N))
  135. {
  136. _lastSelectedRow = (_lastSelectedRow - 1) % _songsTableView.dataSource.NumberOfRows();
  137. if (_lastSelectedRow < 0)
  138. {
  139. _lastSelectedRow = _songsTableView.dataSource.NumberOfRows() - 1;
  140. }
  141. _songsTableView.ScrollToRow(_lastSelectedRow, true);
  142. this._songsTableView_DidSelectRowEvent(_songsTableView, _lastSelectedRow);
  143. }
  144. if (Input.GetKeyDown(KeyCode.M) && isShiftKeyDown)
  145. {
  146. _songsTableView.PageScrollDown();
  147. }
  148. else if (Input.GetKeyDown(KeyCode.M))
  149. {
  150. _lastSelectedRow = (_lastSelectedRow + 1) % _songsTableView.dataSource.NumberOfRows();
  151. _songsTableView.ScrollToRow(_lastSelectedRow, true);
  152. this._songsTableView_DidSelectRowEvent(_songsTableView, _lastSelectedRow);
  153. }
  154. }
  155. #endif
  156. }
  157. }