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.

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