PlaylistTableView.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using HMUI;
  2. using SongBrowserPlugin.DataAccess;
  3. using SongLoaderPlugin;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. namespace SongBrowserPlugin.UI
  12. {
  13. public class PlaylistTableView : MonoBehaviour, TableView.IDataSource
  14. {
  15. public const String Name = "PlaylistTableView";
  16. private Logger _log = new Logger(Name);
  17. private StandardLevelListTableCell _cellInstanceTemplate;
  18. protected TableView _tableView;
  19. private RectTransform _rect;
  20. private PlaylistsReader _reader;
  21. private int _selectedRow;
  22. public event Action<PlaylistTableView, int> didSelectPlaylistEvent;
  23. [SerializeField]
  24. protected float _cellHeight = 12f;
  25. /// <summary>
  26. /// Constructor.
  27. /// </summary>
  28. public PlaylistTableView()
  29. {
  30. }
  31. public virtual void OnDestroy()
  32. {
  33. _log.Trace("OnDestroy()");
  34. Destroy(this._tableView);
  35. }
  36. /// <summary>
  37. /// Setup the tableview.
  38. /// </summary>
  39. /// <param name="parent"></param>
  40. public void Init(RectTransform parent, PlaylistsReader reader)
  41. {
  42. _rect = parent;
  43. _reader = reader;
  44. try
  45. {
  46. _cellInstanceTemplate = Resources.FindObjectsOfTypeAll<StandardLevelListTableCell>().First(x => (x.name == "StandardLevelListTableCell"));
  47. if (_tableView == null)
  48. {
  49. _tableView = new GameObject().AddComponent<TableView>();
  50. _tableView.Awake();
  51. _tableView.transform.SetParent(parent, false);
  52. Mask viewportMask = Instantiate(Resources.FindObjectsOfTypeAll<Mask>().First(), _tableView.transform, false);
  53. viewportMask.transform.DetachChildren();
  54. _tableView.GetComponentsInChildren<RectTransform>().First(x => x.name == "Content").transform.SetParent(viewportMask.rectTransform, false);
  55. (_tableView.transform as RectTransform).anchorMin = new Vector2(0f, 0.5f);
  56. (_tableView.transform as RectTransform).anchorMax = new Vector2(1f, 0.5f);
  57. (_tableView.transform as RectTransform).sizeDelta = new Vector2(0f, 60f);
  58. (_tableView.transform as RectTransform).position = new Vector3(0f, 0f, 2.4f);
  59. (_tableView.transform as RectTransform).anchoredPosition = new Vector3(0f, -3f);
  60. Button pageUpButton = Instantiate(Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == "PageUpButton")), parent, false);
  61. Button pageDownButton = Instantiate(Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == "PageDownButton")), parent, false);
  62. (pageUpButton.transform as RectTransform).anchorMin = new Vector2(0.5f, 1f);
  63. (pageUpButton.transform as RectTransform).anchorMax = new Vector2(0.5f, 1f);
  64. (pageUpButton.transform as RectTransform).anchoredPosition = new Vector2(0f, -14f);
  65. pageUpButton.interactable = true;
  66. pageUpButton.onClick.AddListener(delegate ()
  67. {
  68. _tableView.PageScrollUp();
  69. });
  70. (pageDownButton.transform as RectTransform).anchorMin = new Vector2(0.5f, 0f);
  71. (pageDownButton.transform as RectTransform).anchorMax = new Vector2(0.5f, 0f);
  72. (pageDownButton.transform as RectTransform).anchoredPosition = new Vector2(0f, 8f);
  73. pageDownButton.interactable = true;
  74. pageDownButton.onClick.AddListener(delegate ()
  75. {
  76. _tableView.PageScrollDown();
  77. });
  78. _tableView.SetPrivateField("_pageUpButton", pageUpButton);
  79. _tableView.SetPrivateField("_pageDownButton", pageDownButton);
  80. }
  81. this._tableView.didSelectRowEvent += this.HandleDidSelectRowEvent;
  82. this._tableView.dataSource = this;
  83. _log.Debug("Initialized PlaylistTableView");
  84. }
  85. catch (Exception e)
  86. {
  87. _log.Exception("Exception initializing playlist table view: ", e);
  88. }
  89. }
  90. /// <summary>
  91. ///
  92. /// </summary>
  93. /// <param name="tableView"></param>
  94. /// <param name="row"></param>
  95. public virtual void HandleDidSelectRowEvent(TableView tableView, int row)
  96. {
  97. _log.Debug("HandleDidSelectRowEvent - Row: {0}", row);
  98. if (this.didSelectPlaylistEvent != null)
  99. {
  100. this.didSelectPlaylistEvent(this, row);
  101. }
  102. }
  103. /// <summary>
  104. ///
  105. /// </summary>
  106. /// <returns></returns>
  107. public float RowHeight()
  108. {
  109. return this._cellHeight;
  110. }
  111. /// <summary>
  112. ///
  113. /// </summary>
  114. /// <returns></returns>
  115. public int NumberOfRows()
  116. {
  117. if (this._reader == null)
  118. {
  119. return 0;
  120. }
  121. return this._reader.Playlists.Count;
  122. }
  123. /// <summary>
  124. ///
  125. /// </summary>
  126. /// <param name="row"></param>
  127. /// <returns></returns>
  128. public TableCell CellForRow(int row)
  129. {
  130. _log.Debug("CellForRow({0})", row);
  131. try
  132. {
  133. Playlist p = _reader.Playlists[row];
  134. StandardLevelListTableCell tableCell = Instantiate(_cellInstanceTemplate, this._tableView.transform, false);
  135. tableCell.coverImage = Base64Sprites.Base64ToSprite(p.Image);
  136. tableCell.songName = p.Title;
  137. tableCell.author = p.Author;
  138. return tableCell;
  139. }
  140. catch (Exception e)
  141. {
  142. _log.Exception("Exception getting cell for row", e);
  143. }
  144. return null;
  145. }
  146. /// <summary>
  147. ///
  148. /// </summary>
  149. private void CheckDebugUserInput()
  150. {
  151. if (Input.GetKeyDown(KeyCode.N) && Input.GetKey(KeyCode.LeftShift))
  152. {
  153. _tableView.PageScrollUp();
  154. }
  155. else if (Input.GetKeyDown(KeyCode.N))
  156. {
  157. _selectedRow = (_selectedRow - 1) % this._reader.Playlists.Count;
  158. if (_selectedRow < 0)
  159. {
  160. _selectedRow = this._reader.Playlists.Count-1;
  161. }
  162. _tableView.ScrollToRow(_selectedRow, true);
  163. this.HandleDidSelectRowEvent(_tableView, _selectedRow);
  164. }
  165. if (Input.GetKeyDown(KeyCode.M) && Input.GetKey(KeyCode.LeftShift))
  166. {
  167. _tableView.PageScrollDown();
  168. }
  169. else if (Input.GetKeyDown(KeyCode.M))
  170. {
  171. _selectedRow = (_selectedRow + 1) % this._reader.Playlists.Count;
  172. _tableView.ScrollToRow(_selectedRow, true);
  173. this.HandleDidSelectRowEvent(_tableView, _selectedRow);
  174. }
  175. }
  176. /// <summary>
  177. ///
  178. /// </summary>
  179. private void Update()
  180. {
  181. CheckDebugUserInput();
  182. }
  183. }
  184. }