SelectionActionsView.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.UI;
  5. using KairoEngine.UI.InteractionHandler;
  6. using KairoEngine.UI.Tooltips;
  7. namespace KairoEngine.GameTools.Selectables
  8. {
  9. /// <summary>
  10. /// A UI component that shows the actions for a given selected object or objects.
  11. /// </summary>
  12. public class SelectionActionsView : MonoBehaviour, IUiSystemElement, IClickHandler
  13. {
  14. public bool hideWhenEmpty = true;
  15. public SelectableObjectTool selectableObjectTool;
  16. public GameObject panel;
  17. public RectTransform container;
  18. public GameObject actionButtonPrefab;
  19. private List<SelectableObjectAction> actions = new List<SelectableObjectAction>();
  20. private List<GameObject> buttons = new List<GameObject>();
  21. private bool hidden = false;
  22. private void Start()
  23. {
  24. if(actionButtonPrefab == null) Debug.LogError("Missing Action Button Prefab in SelectionActionsView", this.gameObject);
  25. if(container == null) Debug.LogError("Missing container in SelectionActionsView", this.gameObject);
  26. UiManager.RegisterElement("SelectionActions", this, this.transform, true);
  27. if(hideWhenEmpty) panel.SetActive(false);
  28. }
  29. private void Update()
  30. {
  31. if(selectableObjectTool.ActionListChanged())
  32. {
  33. actions = selectableObjectTool.GetActions();
  34. UpdateActionButtons();
  35. }
  36. if(!hidden)
  37. {
  38. if(actions.Count == 0 && hideWhenEmpty) panel.SetActive(false);
  39. else panel.SetActive(true);
  40. }
  41. else panel.SetActive(false);
  42. }
  43. public void Hide()
  44. {
  45. hidden = true;
  46. panel.SetActive(false);
  47. }
  48. public bool IsVisible()
  49. {
  50. return panel.activeSelf;
  51. }
  52. public void Show()
  53. {
  54. hidden = false;
  55. panel.SetActive(true);
  56. }
  57. private void UpdateActionButtons()
  58. {
  59. for (int i = 0; i < buttons.Count; i++)
  60. {
  61. Destroy(buttons[i].gameObject);
  62. }
  63. buttons = new List<GameObject>();
  64. if(actionButtonPrefab == null || container == null) return;
  65. for (int i = 0; i < actions.Count; i++)
  66. {
  67. var obj = Instantiate(actionButtonPrefab, container);
  68. buttons.Add(obj);
  69. ClickHandler handler = obj.GetComponent<ClickHandler>();
  70. if(handler != null)
  71. {
  72. handler.title = actions[i].title;
  73. handler.receiver = this;
  74. }
  75. ButtonData buttonData = obj.GetComponent<ButtonData>();
  76. if(buttonData != null) buttonData.Setup(actions[i].title, actions[i].icon);
  77. TooltipTrigger tooltip = obj.GetComponent<TooltipTrigger>();
  78. if(tooltip != null)
  79. {
  80. tooltip.header = actions[i].title;
  81. tooltip.content = actions[i].description;
  82. }
  83. }
  84. }
  85. public void OnClick(string title)
  86. {
  87. for (int i = 0; i < actions.Count; i++)
  88. {
  89. if(actions[i].title == title)
  90. {
  91. selectableObjectTool.ExecuteAction(actions[i]);
  92. return;
  93. }
  94. }
  95. }
  96. }
  97. }