using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using KairoEngine.UI;
using KairoEngine.UI.InteractionHandler;
using KairoEngine.UI.Tooltips;
namespace KairoEngine.GameTools.Selectables
{
///
/// A UI component that shows the actions for a given selected object or objects.
///
public class SelectionActionsView : MonoBehaviour, IUiSystemElement, IClickHandler
{
public bool hideWhenEmpty = true;
public SelectableObjectTool selectableObjectTool;
public GameObject panel;
public RectTransform container;
public GameObject actionButtonPrefab;
private List actions = new List();
private List buttons = new List();
private bool hidden = false;
private void Start()
{
if(actionButtonPrefab == null) Debug.LogError("Missing Action Button Prefab in SelectionActionsView", this.gameObject);
if(container == null) Debug.LogError("Missing container in SelectionActionsView", this.gameObject);
UiManager.RegisterElement("SelectionActions", this, this.transform, true);
if(hideWhenEmpty) panel.SetActive(false);
}
private void Update()
{
if(selectableObjectTool.ActionListChanged())
{
actions = selectableObjectTool.GetActions();
UpdateActionButtons();
}
if(!hidden)
{
if(actions.Count == 0 && hideWhenEmpty) panel.SetActive(false);
else panel.SetActive(true);
}
else panel.SetActive(false);
}
public void Hide()
{
hidden = true;
panel.SetActive(false);
}
public bool IsVisible()
{
return panel.activeSelf;
}
public void Show()
{
hidden = false;
panel.SetActive(true);
}
private void UpdateActionButtons()
{
for (int i = 0; i < buttons.Count; i++)
{
Destroy(buttons[i].gameObject);
}
buttons = new List();
if(actionButtonPrefab == null || container == null) return;
for (int i = 0; i < actions.Count; i++)
{
var obj = Instantiate(actionButtonPrefab, container);
buttons.Add(obj);
ClickHandler handler = obj.GetComponent();
if(handler != null)
{
handler.title = actions[i].title;
handler.receiver = this;
}
ButtonData buttonData = obj.GetComponent();
if(buttonData != null) buttonData.Setup(actions[i].title, actions[i].icon);
TooltipTrigger tooltip = obj.GetComponent();
if(tooltip != null)
{
tooltip.header = actions[i].title;
tooltip.content = actions[i].description;
}
}
}
public void OnClick(string title)
{
for (int i = 0; i < actions.Count; i++)
{
if(actions[i].title == title)
{
selectableObjectTool.ExecuteAction(actions[i]);
return;
}
}
}
}
}