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
{
///
/// 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.
///
[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 buttons = new List();
private bool isVisible = false;
[HideInInspector] public IClickHandler handler;
[HideInInspector] public bool useSubMenus = false;
public virtual void Start()
{
if(handler == null)
{
var handlers = gameObject.GetComponents();
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();
if(clickHandler != null)
{
clickHandler.receiver = this;
clickHandler.title = data.title;
}
ButtonData buttonData = btn.GetComponent();
if(buttonData != null) buttonData.Setup(data.title, data.graphic);
TooltipTrigger tooltip = btn.GetComponent();
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