MenuUI.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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(visibleOnStart) UiManager.ShowElement(this);
  51. else UiManager.HideElement(this);
  52. }
  53. private void OnDisable()
  54. {
  55. UiManager.UnregisterElement(this);
  56. }
  57. private void OnEnable()
  58. {
  59. UiManager.RegisterElement(elementTitle, this, this.transform, isVisible);
  60. }
  61. public virtual void OnClick(string title)
  62. {
  63. for (int i = 0; i < buttons.Count; i++)
  64. {
  65. if(buttons[i].title == title)
  66. {
  67. if(buttons[i].action != "" && handler != null)
  68. {
  69. handler.OnClick(buttons[i].action);
  70. if(debug) Debug.Log("Clicked on " + title);
  71. }
  72. }
  73. }
  74. }
  75. public void Hide()
  76. {
  77. if(uiAnimator != null) uiAnimator.Disable();
  78. else menuCanvas.gameObject.SetActive(false);
  79. isVisible = false;
  80. }
  81. public void Show()
  82. {
  83. if(uiAnimator != null) uiAnimator.Enable();
  84. else menuCanvas.gameObject.SetActive(true);
  85. isVisible = true;
  86. }
  87. public bool IsVisible() => isVisible;
  88. public virtual GameObject CreateButton(MenuButtomData data, Transform parent)
  89. {
  90. var btn = Instantiate(buttonPrefab, parent);
  91. ClickHandler clickHandler = btn.GetComponent<ClickHandler>();
  92. if(clickHandler != null)
  93. {
  94. clickHandler.receiver = this;
  95. clickHandler.title = data.title;
  96. }
  97. ButtonData buttonData = btn.GetComponent<ButtonData>();
  98. if(buttonData != null) buttonData.Setup(data.title, data.graphic);
  99. TooltipTrigger tooltip = btn.GetComponent<TooltipTrigger>();
  100. if(tooltip != null)
  101. {
  102. if(data.showTooltip)
  103. {
  104. tooltip.header = data.title;
  105. tooltip.content = data.description;
  106. tooltip.tooltipType = data.tooltipType;
  107. }
  108. else tooltip.enabled = false;
  109. }
  110. Button buttonComponent = btn.GetComponent<Button>();
  111. if(buttonComponent != null) buttonComponent.interactable = data.isInteractable;
  112. return btn;
  113. }
  114. [ButtonGroup("Buttons"), Button("Create Menu"), ShowIf("@!procedural || debug")]
  115. public virtual void CreateMenu()
  116. {
  117. for (int i = 0; i < buttons.Count; i++)
  118. {
  119. var btn = CreateButton(buttons[i], menuParent);
  120. buttons[i].button = btn;
  121. }
  122. }
  123. [ButtonGroup("Buttons"), Button("Destroy Menu"), ShowIf("@!procedural || debug")]
  124. public virtual void DestroyMenu()
  125. {
  126. for (int i = 0; i < buttons.Count; i++)
  127. {
  128. #if UNITY_EDITOR
  129. DestroyImmediate(buttons[i].button);
  130. #else
  131. Destroy(buttons[i].button);
  132. #endif
  133. }
  134. }
  135. private void UpdateButtonData()
  136. {
  137. for (int i = 0; i < buttons.Count; i++)
  138. {
  139. buttons[i].useSubMenus = useSubMenus;
  140. }
  141. }
  142. public GameObject GetButton(string action)
  143. {
  144. for (int i = 0; i < buttons.Count; i++)
  145. {
  146. if(buttons[i].action == action) return buttons[i].button;
  147. }
  148. return null;
  149. }
  150. }
  151. }