MenuUI.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using Sirenix.OdinInspector;
  6. using UnityEngine.UI;
  7. using KairoEngine.Core;
  8. using KairoEngine.UI.Tooltips;
  9. using KairoEngine.UI.InteractionHandler;
  10. namespace KairoEngine.UI
  11. {
  12. /// <summary>
  13. /// Create and control a menu o buttons. Buttons can be pre-generated or can be created in runtime. When a button is clicked,
  14. /// the MenuUI component will execute a function with the IClickHandler interface.
  15. /// </summary>
  16. [HideMonoScript]
  17. public class MenuUI : MonoBehaviour, IClickHandler, IUiSystemElement
  18. {
  19. [BoxGroup("Settings")] public string elementTitle = "New UI Element";
  20. [BoxGroup("Settings")] public bool procedural = false;
  21. [BoxGroup("Settings"), HideIf("@procedural")] public bool createMenuOnStart = true;
  22. [BoxGroup("Settings")] public bool visibleOnStart = true;
  23. [BoxGroup("Settings")] public bool debug = false;
  24. [BoxGroup("Settings")] public GameObject buttonPrefab;
  25. [BoxGroup("Settings")] public RectTransform menuParent;
  26. [BoxGroup("Settings")] public Canvas menuCanvas;
  27. [BoxGroup("Settings")] public UiAnimator uiAnimator;
  28. [PropertySpace(4,4), OnCollectionChanged("UpdateButtonData"), ShowIf("@!procedural || debug")]
  29. public List<MenuButtomData> buttons = new List<MenuButtomData>();
  30. private bool isVisible = false;
  31. [HideInInspector] public IClickHandler handler;
  32. [HideInInspector] public bool useSubMenus = false;
  33. public virtual void Start()
  34. {
  35. if(handler == null)
  36. {
  37. var handlers = gameObject.GetComponents<IClickHandler>();
  38. if(handlers.Length > 0) handler = handlers[handlers.Length - 1];
  39. }
  40. if(handler != null)
  41. {
  42. if(debug) Debug.Log("Menu handler set to " + handler, this.gameObject);
  43. }
  44. else Debug.LogError("Could not find IClickHandler for Menu", this.gameObject);
  45. if(createMenuOnStart)
  46. {
  47. DestroyMenu();
  48. CreateMenu();
  49. }
  50. if(menuCanvas != null)
  51. {
  52. if(visibleOnStart) UiManager.ShowElement(this);
  53. else UiManager.HideElement(this);
  54. }
  55. }
  56. private void OnDisable()
  57. {
  58. if(menuCanvas != null) UiManager.UnregisterElement(this);
  59. }
  60. private void OnEnable()
  61. {
  62. if(menuCanvas != null) UiManager.RegisterElement(elementTitle, this, this.transform, visibleOnStart);
  63. }
  64. public virtual void OnClick(string title)
  65. {
  66. for (int i = 0; i < buttons.Count; i++)
  67. {
  68. if(buttons[i].title == title)
  69. {
  70. if(buttons[i].action != "" && handler != null)
  71. {
  72. handler.OnClick(buttons[i].action);
  73. if(debug) Debug.Log("Clicked on " + title);
  74. }
  75. }
  76. }
  77. }
  78. public void Hide()
  79. {
  80. if(uiAnimator != null) uiAnimator.Disable();
  81. else if(menuCanvas != null) menuCanvas.gameObject.SetActive(false);
  82. isVisible = false;
  83. }
  84. public void Show()
  85. {
  86. if(uiAnimator != null) uiAnimator.Enable();
  87. else if(menuCanvas != null) menuCanvas.gameObject.SetActive(true);
  88. isVisible = true;
  89. }
  90. public bool IsVisible() => isVisible;
  91. public virtual GameObject CreateButton(MenuButtomData data, Transform parent, GameObject prefab = null)
  92. {
  93. if(prefab == null) prefab = buttonPrefab;
  94. var btn = Instantiate(prefab, parent);
  95. ClickHandler clickHandler = btn.GetComponent<ClickHandler>();
  96. if(clickHandler != null)
  97. {
  98. clickHandler.receiver = this;
  99. clickHandler.title = data.title;
  100. }
  101. ButtonData buttonData = btn.GetComponent<ButtonData>();
  102. if(buttonData != null) buttonData.Setup(data.title, data.graphic);
  103. TooltipTrigger tooltip = btn.GetComponent<TooltipTrigger>();
  104. if(tooltip != null)
  105. {
  106. if(data.showTooltip)
  107. {
  108. tooltip.header = data.title;
  109. tooltip.content = data.description;
  110. tooltip.tooltipType = data.tooltipType;
  111. }
  112. else tooltip.enabled = false;
  113. }
  114. Button buttonComponent = btn.GetComponent<Button>();
  115. if(buttonComponent != null) buttonComponent.interactable = data.isInteractable;
  116. return btn;
  117. }
  118. [ButtonGroup("Buttons"), Button("Create Menu"), ShowIf("@!procedural || debug")]
  119. public virtual void CreateMenu()
  120. {
  121. for (int i = 0; i < buttons.Count; i++)
  122. {
  123. var btn = CreateButton(buttons[i], menuParent);
  124. buttons[i].button = btn;
  125. }
  126. }
  127. [ButtonGroup("Buttons"), Button("Destroy Menu"), ShowIf("@!procedural || debug")]
  128. public virtual void DestroyMenu()
  129. {
  130. for (int i = 0; i < buttons.Count; i++)
  131. {
  132. #if UNITY_EDITOR
  133. DestroyImmediate(buttons[i].button);
  134. #else
  135. Destroy(buttons[i].button);
  136. #endif
  137. }
  138. }
  139. private void UpdateButtonData()
  140. {
  141. for (int i = 0; i < buttons.Count; i++)
  142. {
  143. buttons[i].useSubMenus = useSubMenus;
  144. }
  145. }
  146. public GameObject GetButton(string action)
  147. {
  148. for (int i = 0; i < buttons.Count; i++)
  149. {
  150. if(buttons[i].action == action) return buttons[i].button;
  151. }
  152. return null;
  153. }
  154. }
  155. }