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.

342 lines
13 KiB

  1. using BSIPA_ModList.UI.ViewControllers;
  2. using CustomUI.BeatSaber;
  3. using CustomUI.MenuButton;
  4. using IPA.Loader;
  5. using IPA.Updating.BeatMods;
  6. using IPA.Utilities;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using TMPro;
  15. using UnityEngine;
  16. using UnityEngine.UI;
  17. using VRUI;
  18. namespace BSIPA_ModList.UI
  19. {
  20. internal class ModInfoViewController : VRUIViewController
  21. {
  22. internal Sprite Icon;
  23. internal string Name;
  24. internal string Version;
  25. internal string Author;
  26. internal string Description;
  27. internal PluginLoader.PluginMetadata UpdateInfo;
  28. private static RectTransform rowTransformOriginal;
  29. private ModInfoView view;
  30. private RectTransform rowTransform;
  31. private Button linkHomeButton;
  32. private Button linkSourceButton;
  33. private Button linkDonateButton;
  34. public void Init(Sprite icon, string name, string version, string author, string description, PluginLoader.PluginMetadata updateInfo, PluginManifest.LinksObject links = null)
  35. {
  36. Icon = icon;
  37. Name = name;
  38. Version = version;
  39. Author = author;
  40. Description = description;
  41. UpdateInfo = updateInfo;
  42. if (rowTransformOriginal == null)
  43. rowTransformOriginal = MenuButtonUI.Instance.GetPrivateField<RectTransform>("menuButtonsOriginal");
  44. // i also have no clue why this is necessary
  45. rectTransform.anchorMin = new Vector2(0f, 0f);
  46. rectTransform.anchorMax = new Vector2(0.5f, 1f);
  47. var go = new GameObject("Info View", typeof(RectTransform));
  48. go.SetActive(false);
  49. go.AddComponent<RectMask2D>();
  50. view = go.AddComponent<ModInfoView>();
  51. var rt = view.transform as RectTransform;
  52. rt.SetParent(transform);
  53. rt.anchorMin = new Vector2(0f, 0f);
  54. rt.anchorMax = new Vector2(1f, 1f);
  55. rt.anchoredPosition = new Vector2(0f, 0);
  56. view.Init(this);
  57. go.SetActive(true);
  58. SetupLinks(links);
  59. }
  60. private void SetupLinks(PluginManifest.LinksObject links = null, Uri moreInfoLink = null)
  61. {
  62. bool addedLink = false;
  63. if (links != null || moreInfoLink != null)
  64. {
  65. Logger.log.Debug($"Adding links");
  66. rowTransform = Instantiate(rowTransformOriginal, rectTransform);
  67. rowTransform.anchorMin = new Vector2(0f, 0f);
  68. rowTransform.anchorMax = new Vector2(1f, .15f);
  69. rowTransform.anchoredPosition = new Vector2(-3.5f, -2f);
  70. foreach (Transform child in rowTransform)
  71. {
  72. child.name = string.Empty;
  73. Destroy(child.gameObject);
  74. }
  75. if (links?.ProjectHome != null)
  76. {
  77. linkHomeButton = BeatSaberUI.CreateUIButton(rowTransform, "QuitButton", buttonText: "Home",
  78. onClick: () => Process.Start(links.ProjectHome.ToString()));
  79. linkHomeButton.GetComponentInChildren<HorizontalLayoutGroup>().padding = new RectOffset(6, 6, 0, 0);
  80. addedLink = true;
  81. }
  82. if (links?.ProjectSource != null)
  83. {
  84. linkSourceButton = BeatSaberUI.CreateUIButton(rowTransform, "QuitButton", buttonText: "Source",
  85. onClick: () => Process.Start(links.ProjectSource.ToString()));
  86. linkSourceButton.GetComponentInChildren<HorizontalLayoutGroup>().padding = new RectOffset(6, 6, 0, 0);
  87. addedLink = true;
  88. }
  89. if (links?.Donate != null)
  90. {
  91. linkDonateButton = BeatSaberUI.CreateUIButton(rowTransform, "QuitButton", buttonText: "Donate",
  92. onClick: () => Process.Start(links.Donate.ToString()));
  93. linkDonateButton.GetComponentInChildren<HorizontalLayoutGroup>().padding = new RectOffset(6, 6, 0, 0);
  94. addedLink = true;
  95. }
  96. if (moreInfoLink != null)
  97. {
  98. linkDonateButton = BeatSaberUI.CreateUIButton(rowTransform, "QuitButton", buttonText: "More Info",
  99. onClick: () => Process.Start(moreInfoLink.ToString()));
  100. linkDonateButton.GetComponentInChildren<HorizontalLayoutGroup>().padding = new RectOffset(6, 6, 0, 0);
  101. addedLink = true;
  102. }
  103. }
  104. if (UpdateInfo != null && !addedLink)
  105. StartCoroutine(GetMoreInfoLink());
  106. }
  107. private IEnumerator GetMoreInfoLink()
  108. {
  109. Logger.log.Debug($"Getting more info link");
  110. Ref<ApiEndpoint.Mod> mod = new Ref<ApiEndpoint.Mod>(null);
  111. if (UpdateInfo.Id == null) yield break;
  112. yield return Updater.GetModInfo(UpdateInfo.Id, UpdateInfo.Version.ToString(), mod);
  113. try { mod.Verify(); }
  114. catch (Exception e)
  115. {
  116. Logger.log.Warn($"Error getting more info link for mod {UpdateInfo.Id}");
  117. Logger.log.Warn(e);
  118. yield break;
  119. }
  120. SetupLinks(null, mod.Value.Link);
  121. }
  122. #if DEBUG
  123. public void Update()
  124. {
  125. #if ADJUST_INFO_BUTTON_UI_LINKS
  126. RectTransform rt = rowTransform;
  127. if (rt == null) return;
  128. var cpos = rt.anchoredPosition;
  129. if (Input.GetKey(KeyCode.LeftArrow))
  130. {
  131. rt.anchoredPosition = new Vector2(cpos.x - .1f, cpos.y);
  132. }
  133. else if (Input.GetKey(KeyCode.RightArrow))
  134. {
  135. rt.anchoredPosition = new Vector2(cpos.x + .1f, cpos.y);
  136. }
  137. else if (Input.GetKey(KeyCode.UpArrow))
  138. {
  139. rt.anchoredPosition = new Vector2(cpos.x, cpos.y + .1f);
  140. }
  141. else if (Input.GetKey(KeyCode.DownArrow))
  142. {
  143. rt.anchoredPosition = new Vector2(cpos.x, cpos.y - .1f);
  144. }
  145. else
  146. return;
  147. Logger.log.Debug($"Position now at {rt.anchoredPosition}");
  148. #endif
  149. }
  150. #endif
  151. }
  152. internal class ModInfoView : MonoBehaviour
  153. {
  154. private TextMeshProUGUI titleText;
  155. private TextMeshProUGUI authorText;
  156. private TextMeshProUGUI descText;
  157. private Image icon;
  158. public void Init(ModInfoViewController controller)
  159. {
  160. var rectTransform = transform as RectTransform;
  161. rectTransform.sizeDelta = new Vector2(60f, 10f);
  162. titleText = BeatSaberUI.CreateText(rectTransform, $"{controller.Name} <size=60%>{controller.Version}", new Vector2(11f, 27.5f));
  163. titleText.fontSize = 6f;
  164. authorText = BeatSaberUI.CreateText(rectTransform, controller.Author, new Vector2(11f, 22f));
  165. authorText.fontSize = 4.5f;
  166. /*
  167. descText = BeatSaberUI.CreateText(rectTransform, controller.Description, new Vector2(-4.5f, 12f));
  168. descText.fontSize = 3.5f;
  169. descText.enableWordWrapping = true;
  170. descText.overflowMode = TextOverflowModes.ScrollRect;*/
  171. var mdv = new GameObject("MarkDown Desc").AddComponent<MarkdownView>();
  172. mdv.rectTransform.SetParent(rectTransform);
  173. mdv.rectTransform.anchorMin = new Vector2(.22f, 0f);
  174. mdv.rectTransform.anchorMax = new Vector2(1f, .25f);
  175. mdv.rectTransform.anchoredPosition = Vector2.zero;
  176. mdv.Markdown = controller.Description;
  177. icon = new GameObject("Mod Info View Icon", typeof(RectTransform)).AddComponent<Image>();
  178. icon.gameObject.SetActive(false);
  179. icon.rectTransform.SetParent(rectTransform, false);
  180. icon.rectTransform.anchorMin = new Vector2(0.5f, 0.44f);
  181. icon.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
  182. icon.rectTransform.sizeDelta = new Vector2(60f, 10f);
  183. icon.rectTransform.anchoredPosition = new Vector2(-27.8f, 27.3f);
  184. icon.sprite = controller.Icon;
  185. icon.preserveAspect = true;
  186. icon.useSpriteMesh = true;
  187. icon.material = CustomUI.Utilities.UIUtilities.NoGlowMaterial;
  188. icon.gameObject.SetActive(true);
  189. }
  190. #if DEBUG
  191. #if ADJUST_INFO_TEXT_UI_KEYS
  192. private int currentItem = 0;
  193. #endif
  194. public void Update()
  195. {
  196. #if ADJUST_INFO_TEXT_UI_KEYS
  197. RectTransform rt;
  198. switch (currentItem)
  199. {
  200. default:
  201. currentItem = 0;
  202. goto case 0; // idk why this is needed tbh
  203. case 0:
  204. rt = titleText.rectTransform;
  205. break;
  206. case 1:
  207. rt = authorText.rectTransform;
  208. break;
  209. case 2:
  210. rt = descText.rectTransform;
  211. break;
  212. }
  213. if (Input.GetKeyDown(KeyCode.N))
  214. currentItem++;
  215. var cpos = rt.anchoredPosition;
  216. if (Input.GetKey(KeyCode.LeftArrow))
  217. {
  218. rt.anchoredPosition = new Vector2(cpos.x - .1f, cpos.y);
  219. }
  220. else if (Input.GetKey(KeyCode.RightArrow))
  221. {
  222. rt.anchoredPosition = new Vector2(cpos.x + .1f, cpos.y);
  223. }
  224. else if (Input.GetKey(KeyCode.UpArrow))
  225. {
  226. rt.anchoredPosition = new Vector2(cpos.x, cpos.y + .1f);
  227. }
  228. else if (Input.GetKey(KeyCode.DownArrow))
  229. {
  230. rt.anchoredPosition = new Vector2(cpos.x, cpos.y - .1f);
  231. }
  232. else
  233. return;
  234. Logger.log.Debug($"Position now at {rt.anchoredPosition}");
  235. #endif
  236. #if ADJUST_INFO_ICON_UI_KEYS
  237. var rt = icon.rectTransform;
  238. if (Input.GetKey(KeyCode.Z))
  239. { // adjust anchormin
  240. var cpos = rt.anchorMin;
  241. if (Input.GetKey(KeyCode.LeftArrow))
  242. {
  243. rt.anchorMin = new Vector2(cpos.x - .001f, cpos.y);
  244. }
  245. else if (Input.GetKey(KeyCode.RightArrow))
  246. {
  247. rt.anchorMin = new Vector2(cpos.x + .001f, cpos.y);
  248. }
  249. else if (Input.GetKey(KeyCode.UpArrow))
  250. {
  251. rt.anchorMin = new Vector2(cpos.x, cpos.y + .001f);
  252. }
  253. else if (Input.GetKey(KeyCode.DownArrow))
  254. {
  255. rt.anchorMin = new Vector2(cpos.x, cpos.y - .001f);
  256. }
  257. else
  258. return;
  259. Logger.log.Debug($"Anchor min now at {rt.anchorMin}");
  260. }
  261. else if(Input.GetKey(KeyCode.X))
  262. { // adjust anchorMax
  263. var cpos = rt.anchorMax;
  264. if (Input.GetKey(KeyCode.LeftArrow))
  265. {
  266. rt.anchorMax = new Vector2(cpos.x - .001f, cpos.y);
  267. }
  268. else if (Input.GetKey(KeyCode.RightArrow))
  269. {
  270. rt.anchorMax = new Vector2(cpos.x + .001f, cpos.y);
  271. }
  272. else if (Input.GetKey(KeyCode.UpArrow))
  273. {
  274. rt.anchorMax = new Vector2(cpos.x, cpos.y + .001f);
  275. }
  276. else if (Input.GetKey(KeyCode.DownArrow))
  277. {
  278. rt.anchorMax = new Vector2(cpos.x, cpos.y - .001f);
  279. }
  280. else
  281. return;
  282. Logger.log.Debug($"Anchor max now at {rt.anchorMax}");
  283. }
  284. else
  285. {
  286. var cpos = rt.anchoredPosition;
  287. if (Input.GetKey(KeyCode.LeftArrow))
  288. {
  289. rt.anchoredPosition = new Vector2(cpos.x - .1f, cpos.y);
  290. }
  291. else if (Input.GetKey(KeyCode.RightArrow))
  292. {
  293. rt.anchoredPosition = new Vector2(cpos.x + .1f, cpos.y);
  294. }
  295. else if (Input.GetKey(KeyCode.UpArrow))
  296. {
  297. rt.anchoredPosition = new Vector2(cpos.x, cpos.y + .1f);
  298. }
  299. else if (Input.GetKey(KeyCode.DownArrow))
  300. {
  301. rt.anchoredPosition = new Vector2(cpos.x, cpos.y - .1f);
  302. }
  303. else
  304. return;
  305. Logger.log.Debug($"Position now at {rt.anchoredPosition}");
  306. }
  307. #endif
  308. }
  309. #endif
  310. }
  311. }