123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using KairoEngine.UI;
- using KairoEngine.UI.InteractionHandler;
- using KairoEngine.UI.Tooltips;
- namespace KairoEngine.GameTools.Selectables
- {
- /// <summary>
- /// A UI component that shows the actions for a given selected object or objects.
- /// </summary>
- public class SelectionActionsView : MonoBehaviour, IUiSystemElement, IClickHandler
- {
- public bool hideWhenEmpty = true;
- public SelectableObjectTool selectableObjectTool;
- public GameObject panel;
- public RectTransform container;
- public GameObject actionButtonPrefab;
- private List<SelectableObjectAction> actions = new List<SelectableObjectAction>();
- private List<GameObject> buttons = new List<GameObject>();
- 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<GameObject>();
- 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<ClickHandler>();
- if(handler != null)
- {
- handler.title = actions[i].title;
- handler.receiver = this;
- }
- ButtonData buttonData = obj.GetComponent<ButtonData>();
- if(buttonData != null) buttonData.Setup(actions[i].title, actions[i].icon);
- TooltipTrigger tooltip = obj.GetComponent<TooltipTrigger>();
- 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;
- }
- }
- }
- }
- }
|