UIBuilder.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using CustomUI.BeatSaber;
  2. using SongBrowser.DataAccess;
  3. using System;
  4. using System.Linq;
  5. using TMPro;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using VRUI;
  9. using Image = UnityEngine.UI.Image;
  10. using Logger = SongBrowser.Logging.Logger;
  11. namespace SongBrowser.UI
  12. {
  13. public static class UIBuilder
  14. {
  15. /// <summary>
  16. /// Create an empty BeatSaber VRUI view controller.
  17. /// </summary>
  18. /// <typeparam name="T"></typeparam>
  19. /// <param name="name"></param>
  20. /// <returns></returns>
  21. public static T CreateViewController<T>(string name) where T : VRUIViewController
  22. {
  23. T vc = new GameObject(name).AddComponent<T>();
  24. vc.rectTransform.anchorMin = new Vector2(0f, 0f);
  25. vc.rectTransform.anchorMax = new Vector2(1f, 1f);
  26. vc.rectTransform.sizeDelta = new Vector2(0f, 0f);
  27. vc.rectTransform.anchoredPosition = new Vector2(0f, 0f);
  28. return vc;
  29. }
  30. /// <summary>
  31. /// Create empty FlowCoordinator
  32. /// </summary>
  33. /// <typeparam name="T"></typeparam>
  34. /// <param name="name"></param>
  35. /// <returns></returns>
  36. public static T CreateFlowCoordinator<T>(string name) where T : FlowCoordinator
  37. {
  38. T vc = new GameObject(name).AddComponent<T>();
  39. return vc;
  40. }
  41. /// <summary>
  42. /// Clone a Unity Button into a Button we control.
  43. /// </summary>
  44. /// <param name="parent"></param>
  45. /// <param name="buttonTemplate"></param>
  46. /// <param name="buttonInstance"></param>
  47. /// <returns></returns>
  48. static public Button CreateUIButton(RectTransform parent, Button buttonTemplate)
  49. {
  50. Button btn = UnityEngine.Object.Instantiate(buttonTemplate, parent, false);
  51. UnityEngine.Object.DestroyImmediate(btn.GetComponent<SignalOnUIButtonClick>());
  52. btn.onClick = new Button.ButtonClickedEvent();
  53. btn.name = "CustomUIButton";
  54. return btn;
  55. }
  56. /// <summary>
  57. /// Create an icon button, simple.
  58. /// </summary>
  59. /// <param name="parent"></param>
  60. /// <param name="buttonTemplate"></param>
  61. /// <param name="iconSprite"></param>
  62. /// <returns></returns>
  63. public static Button CreateIconButton(RectTransform parent, Button buttonTemplate, Sprite iconSprite)
  64. {
  65. Button newButton = UIBuilder.CreateUIButton(parent, buttonTemplate);
  66. newButton.interactable = true;
  67. RectTransform textRect = newButton.GetComponentsInChildren<RectTransform>(true).FirstOrDefault(c => c.name == "Text");
  68. if (textRect != null)
  69. {
  70. UnityEngine.Object.Destroy(textRect.gameObject);
  71. }
  72. newButton.SetButtonIcon(iconSprite);
  73. newButton.onClick.RemoveAllListeners();
  74. return newButton;
  75. }
  76. /// <summary>
  77. /// Adjust button text size.
  78. /// </summary>
  79. /// <param name="button"></param>
  80. /// <param name="fontSize"></param>
  81. static public void SetButtonTextColor(Button button, Color color)
  82. {
  83. TextMeshProUGUI txt = button.GetComponentsInChildren<TextMeshProUGUI>().FirstOrDefault(x => x.name == "Text");
  84. if (txt != null)
  85. {
  86. txt.color = color;
  87. }
  88. }
  89. /// <summary>
  90. /// Adjust button border.
  91. /// </summary>
  92. /// <param name="button"></param>
  93. /// <param name="color"></param>
  94. static public void SetButtonBorder(Button button, Color color)
  95. {
  96. Image img = button.GetComponentsInChildren<Image>().FirstOrDefault(x => x.name == "Stroke");
  97. if (img != null)
  98. {
  99. img.color = color;
  100. }
  101. }
  102. /// <summary>
  103. /// Adjust button border.
  104. /// </summary>
  105. /// <param name="button"></param>
  106. /// <param name="color"></param>
  107. static public void SetButtonBorderActive(Button button, bool active)
  108. {
  109. Image img = button.GetComponentsInChildren<Image>().FirstOrDefault(x => x.name == "Stroke");
  110. if (img != null)
  111. {
  112. img.gameObject.SetActive(active);
  113. }
  114. }
  115. /// <summary>
  116. /// Find and adjust a stat panel item text fields.
  117. /// </summary>
  118. /// <param name="rect"></param>
  119. /// <param name="text"></param>
  120. static public void SetStatButtonText(RectTransform rect, String text)
  121. {
  122. TextMeshProUGUI txt = rect.GetComponentsInChildren<TextMeshProUGUI>().FirstOrDefault(x => x.name == "ValueText");
  123. if (txt != null)
  124. {
  125. txt.text = text;
  126. }
  127. }
  128. /// <summary>
  129. /// Find and adjust a stat panel item icon.
  130. /// </summary>
  131. /// <param name="rect"></param>
  132. /// <param name="icon"></param>
  133. static public void SetStatButtonIcon(RectTransform rect, Sprite icon)
  134. {
  135. Image img = rect.GetComponentsInChildren<Image>().FirstOrDefault(x => x.name == "Icon");
  136. if (img != null)
  137. {
  138. img.sprite = icon;
  139. img.color = Color.white;
  140. }
  141. }
  142. }
  143. }