123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- using Sirenix.OdinInspector;
- using UnityEngine.UI;
- using KairoEngine.Core;
- using KairoEngine.UI.Tooltips;
- using KairoEngine.UI.InteractionHandler;
- namespace KairoEngine.UI
- {
- /// <summary>
- /// Create and control a menu o buttons. Buttons can be pre-generated or can be created in runtime. When a button is clicked,
- /// the MenuUI component will execute a function with the IClickHandler interface.
- /// </summary>
- [HideMonoScript]
- public class MenuUI : MonoBehaviour, IClickHandler, IUiSystemElement
- {
- [BoxGroup("Settings")] public string elementTitle = "New UI Element";
- [BoxGroup("Settings")] public bool procedural = false;
- [BoxGroup("Settings"), HideIf("@procedural")] public bool createMenuOnStart = true;
- [BoxGroup("Settings")] public bool visibleOnStart = true;
- [BoxGroup("Settings")] public bool debug = false;
- [BoxGroup("Settings")] public GameObject buttonPrefab;
- [BoxGroup("Settings")] public RectTransform menuParent;
- [BoxGroup("Settings")] public Canvas menuCanvas;
- [BoxGroup("Settings")] public UiAnimator uiAnimator;
- [PropertySpace(4,4), OnCollectionChanged("UpdateButtonData"), ShowIf("@!procedural || debug")]
- public List<MenuButtomData> buttons = new List<MenuButtomData>();
- private bool isVisible = false;
- [HideInInspector] public IClickHandler handler;
- [HideInInspector] public bool useSubMenus = false;
- public virtual void Start()
- {
- if(handler == null)
- {
- var handlers = gameObject.GetComponents<IClickHandler>();
- if(handlers.Length > 0) handler = handlers[handlers.Length - 1];
- }
- if(handler != null)
- {
- if(debug) Debug.Log("Menu handler set to " + handler, this.gameObject);
- }
- else Debug.LogError("Could not find IClickHandler for Menu", this.gameObject);
- if(createMenuOnStart)
- {
- DestroyMenu();
- CreateMenu();
- }
- if(menuCanvas != null)
- {
- if(visibleOnStart) UiManager.ShowElement(this);
- else UiManager.HideElement(this);
- }
- }
- private void OnDisable()
- {
- if(menuCanvas != null) UiManager.UnregisterElement(this);
- }
- private void OnEnable()
- {
- if(menuCanvas != null) UiManager.RegisterElement(elementTitle, this, this.transform, visibleOnStart);
- }
- public virtual void OnClick(string title)
- {
- for (int i = 0; i < buttons.Count; i++)
- {
- if(buttons[i].title == title)
- {
- if(buttons[i].action != "" && handler != null)
- {
- handler.OnClick(buttons[i].action);
- if(debug) Debug.Log("Clicked on " + title);
- }
- }
- }
- }
- public void Hide()
- {
-
- if(uiAnimator != null) uiAnimator.Disable();
- else if(menuCanvas != null) menuCanvas.gameObject.SetActive(false);
- isVisible = false;
- }
- public void Show()
- {
- if(uiAnimator != null) uiAnimator.Enable();
- else if(menuCanvas != null) menuCanvas.gameObject.SetActive(true);
- isVisible = true;
- }
- public bool IsVisible() => isVisible;
- public virtual GameObject CreateButton(MenuButtomData data, Transform parent, GameObject prefab = null)
- {
- if(prefab == null) prefab = buttonPrefab;
- var btn = Instantiate(prefab, parent);
- ClickHandler clickHandler = btn.GetComponent<ClickHandler>();
- if(clickHandler != null)
- {
- clickHandler.receiver = this;
- clickHandler.title = data.title;
- }
- ButtonData buttonData = btn.GetComponent<ButtonData>();
- if(buttonData != null) buttonData.Setup(data.title, data.graphic);
- TooltipTrigger tooltip = btn.GetComponent<TooltipTrigger>();
- if(tooltip != null)
- {
- if(data.showTooltip)
- {
- tooltip.header = data.title;
- tooltip.content = data.description;
- tooltip.tooltipType = data.tooltipType;
- }
- else tooltip.enabled = false;
- }
- Button buttonComponent = btn.GetComponent<Button>();
- if(buttonComponent != null) buttonComponent.interactable = data.isInteractable;
- return btn;
- }
- [ButtonGroup("Buttons"), Button("Create Menu"), ShowIf("@!procedural || debug")]
- public virtual void CreateMenu()
- {
- for (int i = 0; i < buttons.Count; i++)
- {
- var btn = CreateButton(buttons[i], menuParent);
- buttons[i].button = btn;
- }
- }
- [ButtonGroup("Buttons"), Button("Destroy Menu"), ShowIf("@!procedural || debug")]
- public virtual void DestroyMenu()
- {
- for (int i = 0; i < buttons.Count; i++)
- {
- #if UNITY_EDITOR
- DestroyImmediate(buttons[i].button);
- #else
- Destroy(buttons[i].button);
- #endif
- }
- }
- private void UpdateButtonData()
- {
- for (int i = 0; i < buttons.Count; i++)
- {
- buttons[i].useSubMenus = useSubMenus;
- }
- }
- public GameObject GetButton(string action)
- {
- for (int i = 0; i < buttons.Count; i++)
- {
- if(buttons[i].action == action) return buttons[i].button;
- }
- return null;
- }
- }
- }
|