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.

346 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 mdvgo = new GameObject("MarkDown Desc");
  172. mdvgo.SetActive(false);
  173. var mdv = mdvgo.AddComponent<MarkdownView>();
  174. mdv.rectTransform.SetParent(rectTransform);
  175. mdv.rectTransform.anchorMin = new Vector2(.5f, .5f);
  176. mdv.rectTransform.anchorMax = new Vector2(.5f, .5f);
  177. mdv.rectTransform.anchoredPosition = new Vector2(-4f, -3.5f);
  178. mdv.rectTransform.sizeDelta = new Vector2(65f, 40f);
  179. mdvgo.SetActive(true);
  180. mdv.Markdown = controller.Description;
  181. icon = new GameObject("Mod Info View Icon", typeof(RectTransform)).AddComponent<Image>();
  182. icon.gameObject.SetActive(false);
  183. icon.rectTransform.SetParent(rectTransform, false);
  184. icon.rectTransform.anchorMin = new Vector2(0.5f, 0.44f);
  185. icon.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
  186. icon.rectTransform.sizeDelta = new Vector2(60f, 10f);
  187. icon.rectTransform.anchoredPosition = new Vector2(-27.8f, 27.3f);
  188. icon.sprite = controller.Icon;
  189. icon.preserveAspect = true;
  190. icon.useSpriteMesh = true;
  191. icon.material = CustomUI.Utilities.UIUtilities.NoGlowMaterial;
  192. icon.gameObject.SetActive(true);
  193. }
  194. #if DEBUG
  195. #if ADJUST_INFO_TEXT_UI_KEYS
  196. private int currentItem = 0;
  197. #endif
  198. public void Update()
  199. {
  200. #if ADJUST_INFO_TEXT_UI_KEYS
  201. RectTransform rt;
  202. switch (currentItem)
  203. {
  204. default:
  205. currentItem = 0;
  206. goto case 0; // idk why this is needed tbh
  207. case 0:
  208. rt = titleText.rectTransform;
  209. break;
  210. case 1:
  211. rt = authorText.rectTransform;
  212. break;
  213. case 2:
  214. rt = descText.rectTransform;
  215. break;
  216. }
  217. if (Input.GetKeyDown(KeyCode.N))
  218. currentItem++;
  219. var cpos = rt.anchoredPosition;
  220. if (Input.GetKey(KeyCode.LeftArrow))
  221. {
  222. rt.anchoredPosition = new Vector2(cpos.x - .1f, cpos.y);
  223. }
  224. else if (Input.GetKey(KeyCode.RightArrow))
  225. {
  226. rt.anchoredPosition = new Vector2(cpos.x + .1f, cpos.y);
  227. }
  228. else if (Input.GetKey(KeyCode.UpArrow))
  229. {
  230. rt.anchoredPosition = new Vector2(cpos.x, cpos.y + .1f);
  231. }
  232. else if (Input.GetKey(KeyCode.DownArrow))
  233. {
  234. rt.anchoredPosition = new Vector2(cpos.x, cpos.y - .1f);
  235. }
  236. else
  237. return;
  238. Logger.log.Debug($"Position now at {rt.anchoredPosition}");
  239. #endif
  240. #if ADJUST_INFO_ICON_UI_KEYS
  241. var rt = icon.rectTransform;
  242. if (Input.GetKey(KeyCode.Z))
  243. { // adjust anchormin
  244. var cpos = rt.anchorMin;
  245. if (Input.GetKey(KeyCode.LeftArrow))
  246. {
  247. rt.anchorMin = new Vector2(cpos.x - .001f, cpos.y);
  248. }
  249. else if (Input.GetKey(KeyCode.RightArrow))
  250. {
  251. rt.anchorMin = new Vector2(cpos.x + .001f, cpos.y);
  252. }
  253. else if (Input.GetKey(KeyCode.UpArrow))
  254. {
  255. rt.anchorMin = new Vector2(cpos.x, cpos.y + .001f);
  256. }
  257. else if (Input.GetKey(KeyCode.DownArrow))
  258. {
  259. rt.anchorMin = new Vector2(cpos.x, cpos.y - .001f);
  260. }
  261. else
  262. return;
  263. Logger.log.Debug($"Anchor min now at {rt.anchorMin}");
  264. }
  265. else if(Input.GetKey(KeyCode.X))
  266. { // adjust anchorMax
  267. var cpos = rt.anchorMax;
  268. if (Input.GetKey(KeyCode.LeftArrow))
  269. {
  270. rt.anchorMax = new Vector2(cpos.x - .001f, cpos.y);
  271. }
  272. else if (Input.GetKey(KeyCode.RightArrow))
  273. {
  274. rt.anchorMax = new Vector2(cpos.x + .001f, cpos.y);
  275. }
  276. else if (Input.GetKey(KeyCode.UpArrow))
  277. {
  278. rt.anchorMax = new Vector2(cpos.x, cpos.y + .001f);
  279. }
  280. else if (Input.GetKey(KeyCode.DownArrow))
  281. {
  282. rt.anchorMax = new Vector2(cpos.x, cpos.y - .001f);
  283. }
  284. else
  285. return;
  286. Logger.log.Debug($"Anchor max now at {rt.anchorMax}");
  287. }
  288. else
  289. {
  290. var cpos = rt.anchoredPosition;
  291. if (Input.GetKey(KeyCode.LeftArrow))
  292. {
  293. rt.anchoredPosition = new Vector2(cpos.x - .1f, cpos.y);
  294. }
  295. else if (Input.GetKey(KeyCode.RightArrow))
  296. {
  297. rt.anchoredPosition = new Vector2(cpos.x + .1f, cpos.y);
  298. }
  299. else if (Input.GetKey(KeyCode.UpArrow))
  300. {
  301. rt.anchoredPosition = new Vector2(cpos.x, cpos.y + .1f);
  302. }
  303. else if (Input.GetKey(KeyCode.DownArrow))
  304. {
  305. rt.anchoredPosition = new Vector2(cpos.x, cpos.y - .1f);
  306. }
  307. else
  308. return;
  309. Logger.log.Debug($"Position now at {rt.anchoredPosition}");
  310. }
  311. #endif
  312. }
  313. #endif
  314. }
  315. }