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.

301 lines
11 KiB

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