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.

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