You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.2 KiB

  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using CustomUI.BeatSaber;
  5. using CustomUI.Utilities;
  6. using HMUI;
  7. using IPA.Loader;
  8. using IPA.Old;
  9. using UnityEngine;
  10. using VRUI;
  11. using IPA.Loader.Features;
  12. using TMPro;
  13. namespace BSIPA_ModList.UI
  14. {
  15. internal class ModListController : CustomListViewController
  16. {
  17. private interface IClickableCell
  18. {
  19. void OnSelect(ModListController cntrl);
  20. }
  21. private class BSIPAModCell : CustomCellInfo, IClickableCell
  22. {
  23. private static Sprite _defaultIcon;
  24. public static Sprite DefaultIcon
  25. {
  26. get
  27. {
  28. if (_defaultIcon == null)
  29. _defaultIcon = UIUtilities.LoadSpriteFromResources("BSIPA_ModList.Icons.mod_bsipa.png");
  30. return _defaultIcon;
  31. }
  32. }
  33. internal PluginLoader.PluginInfo Plugin;
  34. private ModListController list;
  35. public BSIPAModCell(ModListController list, PluginLoader.PluginInfo plugin)
  36. : base($"{plugin.Metadata.Name} <size=60%>v{plugin.Metadata.Version}", plugin.Metadata.Manifest.Author, null)
  37. {
  38. Plugin = plugin;
  39. this.list = list;
  40. if (plugin.Metadata.Manifest.IconPath != null)
  41. icon = UIUtilities.LoadSpriteRaw(UIUtilities.GetResource(plugin.Metadata.Assembly, plugin.Metadata.Manifest.IconPath));
  42. else
  43. icon = DefaultIcon;
  44. Logger.log.Debug($"BSIPAModCell {plugin.Metadata.Name} {plugin.Metadata.Version}");
  45. }
  46. private ModInfoViewController infoView;
  47. public void OnSelect(ModListController cntrl)
  48. {
  49. Logger.log.Debug($"Selected BSIPAModCell {Plugin.Metadata.Name} {Plugin.Metadata.Version}");
  50. if (infoView == null)
  51. {
  52. infoView = BeatSaberUI.CreateViewController<ModInfoViewController>();
  53. infoView.Init(icon, Plugin.Metadata.Name, "v" + Plugin.Metadata.Version.ToString(), Plugin.Metadata.Manifest.Author,
  54. Plugin.Metadata.Manifest.Description, Plugin.Metadata.Features.FirstOrDefault(f => f is NoUpdateFeature) == null);
  55. }
  56. list.flow.SetSelected(infoView, immediate: list.flow.HasSelected);
  57. }
  58. }
  59. private class BSIPAIgnoredModCell : CustomCellInfo, IClickableCell
  60. {
  61. internal PluginLoader.PluginMetadata Plugin;
  62. private ModListController list;
  63. public BSIPAIgnoredModCell(ModListController list, PluginLoader.PluginMetadata plugin)
  64. : base($"<color=#878787>{plugin.Name} <size=60%>v{plugin.Version}", $"{plugin.Manifest.Author} <color=#BFBFBF>- <i>Not loaded</i>", BSIPAModCell.DefaultIcon)
  65. {
  66. Plugin = plugin;
  67. this.list = list;
  68. Logger.log.Debug($"BSIPAIgnoredModCell {plugin.Name} {plugin.Version}");
  69. }
  70. public void OnSelect(ModListController cntrl)
  71. {
  72. Logger.log.Debug($"Selected BSIPAIgnoredModCell {Plugin.Name} {Plugin.Version}");
  73. list.flow.ClearSelected();
  74. }
  75. }
  76. #pragma warning disable CS0618
  77. private class IPAModCell : CustomCellInfo, IClickableCell
  78. {
  79. private static Sprite _defaultIcon;
  80. public static Sprite DefaultIcon
  81. {
  82. get
  83. {
  84. if (_defaultIcon == null)
  85. _defaultIcon = UIUtilities.LoadSpriteFromResources("BSIPA_ModList.Icons.mod_ipa.png");
  86. return _defaultIcon;
  87. }
  88. }
  89. internal IPlugin Plugin;
  90. private ModListController list;
  91. public IPAModCell(ModListController list, IPlugin plugin)
  92. : base($"{plugin.Name} <size=60%>{plugin.Version}", "<color=#BFBFBF><i>Legacy</i>", DefaultIcon)
  93. {
  94. Plugin = plugin;
  95. this.list = list;
  96. Logger.log.Debug($"IPAModCell {plugin.Name} {plugin.Version}");
  97. }
  98. public void OnSelect(ModListController cntrl)
  99. {
  100. Logger.log.Debug($"Selected IPAModCell {Plugin.Name} {Plugin.Version}");
  101. list.flow.ClearSelected();
  102. }
  103. }
  104. #pragma warning restore
  105. public override TableCell CellForIdx(int idx)
  106. {
  107. var cell = base.CellForIdx(idx) as LevelListTableCell;
  108. var nameText = cell.GetPrivateField<TextMeshProUGUI>("_songNameText");
  109. nameText.overflowMode = TextOverflowModes.Overflow;
  110. return cell;
  111. }
  112. private ModListFlowCoordinator flow;
  113. #pragma warning disable CS0618
  114. public void Init(ModListFlowCoordinator flow, IEnumerable<PluginLoader.PluginInfo> bsipaPlugins, IEnumerable<PluginLoader.PluginMetadata> ignoredPlugins, IEnumerable<IPlugin> ipaPlugins)
  115. {
  116. Logger.log.Debug("List Controller Init");
  117. DidActivateEvent = DidActivate;
  118. DidSelectRowEvent = DidSelectRow;
  119. rectTransform.anchorMin = new Vector2(0f, 0f);
  120. rectTransform.anchorMax = new Vector2(.4f, 1f);
  121. includePageButtons = true;
  122. this.flow = flow;
  123. reuseIdentifier = "BSIPAModListTableCell";
  124. foreach (var plugin in bsipaPlugins)
  125. Data.Add(new BSIPAModCell(this, plugin));
  126. foreach (var plugin in ignoredPlugins)
  127. Data.Add(new BSIPAIgnoredModCell(this, plugin));
  128. foreach (var plugin in ipaPlugins)
  129. Data.Add(new IPAModCell(this, plugin));
  130. }
  131. #pragma warning restore
  132. private void DidSelectRow(TableView view, int index)
  133. {
  134. Debug.Assert(ReferenceEquals(view.dataSource, this));
  135. (Data[index] as IClickableCell)?.OnSelect(this);
  136. }
  137. private new void DidActivate(bool first, ActivationType type)
  138. {
  139. var rt = _customListTableView.transform as RectTransform;
  140. rt.anchorMin = new Vector2(.1f, 0f);
  141. rt.anchorMax = new Vector2(.9f, 1f);
  142. }
  143. }
  144. }