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.

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