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.

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