MenuUI.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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, isVisible);
  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)
  92. {
  93. var btn = Instantiate(buttonPrefab, parent);
  94. ClickHandler clickHandler = btn.GetComponent<ClickHandler>();
  95. if(clickHandler != null)
  96. {
  97. clickHandler.receiver = this;
  98. clickHandler.title = data.title;
  99. }
  100. ButtonData buttonData = btn.GetComponent<ButtonData>();
  101. if(buttonData != null) buttonData.Setup(data.title, data.graphic);
  102. TooltipTrigger tooltip = btn.GetComponent<TooltipTrigger>();
  103. if(tooltip != null)
  104. {
  105. if(data.showTooltip)
  106. {
  107. tooltip.header = data.title;
  108. tooltip.content = data.description;
  109. tooltip.tooltipType = data.tooltipType;
  110. }
  111. else tooltip.enabled = false;
  112. }
  113. Button buttonComponent = btn.GetComponent<Button>();
  114. if(buttonComponent != null) buttonComponent.interactable = data.isInteractable;
  115. return btn;
  116. }
  117. [ButtonGroup("Buttons"), Button("Create Menu"), ShowIf("@!procedural || debug")]
  118. public virtual void CreateMenu()
  119. {
  120. for (int i = 0; i < buttons.Count; i++)
  121. {
  122. var btn = CreateButton(buttons[i], menuParent);
  123. buttons[i].button = btn;
  124. }
  125. }
  126. [ButtonGroup("Buttons"), Button("Destroy Menu"), ShowIf("@!procedural || debug")]
  127. public virtual void DestroyMenu()
  128. {
  129. for (int i = 0; i < buttons.Count; i++)
  130. {
  131. #if UNITY_EDITOR
  132. DestroyImmediate(buttons[i].button);
  133. #else
  134. Destroy(buttons[i].button);
  135. #endif
  136. }
  137. }
  138. private void UpdateButtonData()
  139. {
  140. for (int i = 0; i < buttons.Count; i++)
  141. {
  142. buttons[i].useSubMenus = useSubMenus;
  143. }
  144. }
  145. public GameObject GetButton(string action)
  146. {
  147. for (int i = 0; i < buttons.Count; i++)
  148. {
  149. if(buttons[i].action == action) return buttons[i].button;
  150. }
  151. return null;
  152. }
  153. }
  154. }