|
@@ -4,22 +4,31 @@ 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 createMenuOnStart = true;
|
|
|
+ [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")] public List<MenuButtomData> buttons = new List<MenuButtomData>();
|
|
|
+ [PropertySpace(4,4), OnCollectionChanged("UpdateButtonData"), ShowIf("@!procedural || debug")]
|
|
|
+ public List<MenuButtomData> buttons = new List<MenuButtomData>();
|
|
|
|
|
|
private bool isVisible = false;
|
|
|
[HideInInspector] public IClickHandler handler;
|
|
@@ -27,7 +36,16 @@ namespace KairoEngine.UI
|
|
|
|
|
|
public virtual void Start()
|
|
|
{
|
|
|
- if(handler == null) handler = gameObject.GetComponent<IClickHandler>();
|
|
|
+ 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();
|
|
@@ -56,6 +74,7 @@ namespace KairoEngine.UI
|
|
|
if(buttons[i].action != "" && handler != null)
|
|
|
{
|
|
|
handler.OnClick(buttons[i].action);
|
|
|
+ if(debug) Debug.Log("Clicked on " + title);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -63,14 +82,17 @@ namespace KairoEngine.UI
|
|
|
|
|
|
public void Hide()
|
|
|
{
|
|
|
- menuCanvas.gameObject.SetActive(false);
|
|
|
+ if(uiAnimator != null) uiAnimator.Disable();
|
|
|
+ else menuCanvas.gameObject.SetActive(false);
|
|
|
isVisible = false;
|
|
|
}
|
|
|
|
|
|
public void Show()
|
|
|
{
|
|
|
- menuCanvas.gameObject.SetActive(true);
|
|
|
+ if(uiAnimator != null) uiAnimator.Enable();
|
|
|
+ else menuCanvas.gameObject.SetActive(true);
|
|
|
isVisible = true;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public bool IsVisible() => isVisible;
|
|
@@ -102,7 +124,7 @@ namespace KairoEngine.UI
|
|
|
return btn;
|
|
|
}
|
|
|
|
|
|
- [ButtonGroup("Buttons"), Button("Create Menu")]
|
|
|
+ [ButtonGroup("Buttons"), Button("Create Menu"), ShowIf("@!procedural || debug")]
|
|
|
public virtual void CreateMenu()
|
|
|
{
|
|
|
for (int i = 0; i < buttons.Count; i++)
|
|
@@ -112,7 +134,7 @@ namespace KairoEngine.UI
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- [ButtonGroup("Buttons"), Button("Destroy Menu")]
|
|
|
+ [ButtonGroup("Buttons"), Button("Destroy Menu"), ShowIf("@!procedural || debug")]
|
|
|
public virtual void DestroyMenu()
|
|
|
{
|
|
|
for (int i = 0; i < buttons.Count; i++)
|
|
@@ -132,6 +154,15 @@ namespace KairoEngine.UI
|
|
|
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;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|