BeatSaberExtensions.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Linq;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. using UnityEngine.UI;
  5. using Image = UnityEngine.UI.Image;
  6. namespace SongBrowser.Internals
  7. {
  8. public static class BeatSaberUIExtensions
  9. {
  10. #region Button Extensions
  11. public static void SetButtonText(this Button _button, string _text)
  12. {
  13. HMUI.CurvedTextMeshPro textMesh = _button.GetComponentInChildren<HMUI.CurvedTextMeshPro>();
  14. if (textMesh != null)
  15. {
  16. textMesh.SetText(_text);
  17. }
  18. }
  19. public static void SetButtonTextSize(this Button _button, float _fontSize)
  20. {
  21. var txtMesh = _button.GetComponentInChildren<HMUI.CurvedTextMeshPro>();
  22. if (txtMesh != null)
  23. {
  24. txtMesh.fontSize = _fontSize;
  25. }
  26. }
  27. public static void ToggleWordWrapping(this Button _button, bool enableWordWrapping)
  28. {
  29. var txtMesh = _button.GetComponentInChildren<HMUI.CurvedTextMeshPro>();
  30. if (txtMesh != null)
  31. {
  32. txtMesh.enableWordWrapping = enableWordWrapping;
  33. }
  34. }
  35. public static void SetButtonBackgroundActive(this Button parent, bool active)
  36. {
  37. HMUI.ImageView img = parent.GetComponentsInChildren<HMUI.ImageView>().Last(x => x.name == "BG");
  38. if (img != null)
  39. {
  40. img.gameObject.SetActive(active);
  41. }
  42. }
  43. public static void SetButtonUnderlineColor(this Button parent, Color color)
  44. {
  45. HMUI.ImageView img = parent.GetComponentsInChildren<HMUI.ImageView>().FirstOrDefault(x => x.name == "Underline");
  46. if (img != null)
  47. {
  48. img.color = color;
  49. }
  50. }
  51. public static void SetButtonBorder(this Button button, Color color)
  52. {
  53. HMUI.ImageView img = button.GetComponentsInChildren<HMUI.ImageView>().FirstOrDefault(x => x.name == "Border");
  54. if (img != null)
  55. {
  56. img.color0 = color;
  57. img.color1 = color;
  58. img.color = color;
  59. img.fillMethod = Image.FillMethod.Horizontal;
  60. img.SetAllDirty();
  61. }
  62. }
  63. #endregion
  64. #region ViewController Extensions
  65. public static Button CreateUIButton(this HMUI.ViewController parent, string name, string buttonTemplate, Vector2 anchoredPosition, Vector2 sizeDelta, UnityAction onClick = null, string buttonText = "BUTTON")
  66. {
  67. Button btn = BeatSaberUI.CreateUIButton(name, parent.rectTransform, buttonTemplate, anchoredPosition, sizeDelta, onClick, buttonText);
  68. return btn;
  69. }
  70. public static Button CreateIconButton(this HMUI.ViewController parent, string name, string buttonTemplate, Vector2 anchoredPosition, Vector2 sizeDelta, UnityAction onClick, Sprite icon)
  71. {
  72. Button btn = BeatSaberUI.CreateIconButton(name, parent.rectTransform, buttonTemplate, anchoredPosition, sizeDelta, onClick, icon);
  73. return btn;
  74. }
  75. #endregion
  76. }
  77. }