SelectableObjectTool.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.GameTools.InteractionTools;
  5. using KairoEngine.UI;
  6. using Sirenix.OdinInspector;
  7. namespace KairoEngine.GameTools.Selectables
  8. {
  9. /// <summary>
  10. /// The selectableObject tools controls input for selecting objects in the game and visualizing its properties and actions.
  11. /// </summary>
  12. public class SelectableObjectTool : MonoBehaviour, IInteractionTool, IHighlightableObject
  13. {
  14. public bool allowMultiSelection = true;
  15. public KeyCode multiSelectionKey = KeyCode.LeftControl;
  16. public LayerMask layerMask;
  17. public bool selectableObjectToolEnabled = true;
  18. public Sprite icon;
  19. public Color selectionColor = Color.white;
  20. [ShowInInspector, ReadOnly] public List<SelectableObjectTrigger> selectedObjects = new List<SelectableObjectTrigger>();
  21. private void Update()
  22. {
  23. SelectObjects();
  24. }
  25. public void DisableTool()
  26. {
  27. selectableObjectToolEnabled = false;
  28. }
  29. public void EnableTool()
  30. {
  31. selectableObjectToolEnabled = true;
  32. }
  33. public Sprite GetIcon() => icon;
  34. public Color GetHighlightColor() => selectionColor;
  35. public bool ShowOutline() => true;
  36. private void SelectObjects()
  37. {
  38. actionListChanged = false;
  39. if(!selectableObjectToolEnabled) return;
  40. if(Input.GetMouseButtonDown(0) && !MouseInputUIBlocker.BlockedByUI)
  41. {
  42. if(Camera.main == null) return;
  43. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  44. if(Physics.Raycast(ray, out RaycastHit raycastHit, 999f, layerMask))
  45. {
  46. var selectableObjectTrigger = raycastHit.collider.gameObject.GetComponent<SelectableObjectTrigger>();
  47. if(selectableObjectTrigger != null)
  48. {
  49. if(allowMultiSelection && Input.GetKey(multiSelectionKey)) AddToSelection(selectableObjectTrigger);
  50. else
  51. {
  52. ClearSelection();
  53. AddToSelection(selectableObjectTrigger);
  54. }
  55. }
  56. }
  57. }
  58. }
  59. public void AddToSelection(SelectableObjectTrigger selectableObjectTrigger)
  60. {
  61. bool alreadySelected = false;
  62. for (int i = 0; i < selectedObjects.Count; i++)
  63. {
  64. if(selectedObjects[i] == selectableObjectTrigger) alreadySelected = true;
  65. }
  66. if(alreadySelected == false)
  67. {
  68. selectableObjectTrigger.Select();
  69. selectedObjects.Add(selectableObjectTrigger);
  70. }
  71. else
  72. {
  73. selectableObjectTrigger.Deselect();
  74. selectedObjects.Remove(selectableObjectTrigger);
  75. }
  76. actionListChanged = true;
  77. }
  78. public void ClearSelection()
  79. {
  80. for (int i = 0; i < selectedObjects.Count; i++)
  81. {
  82. if(selectedObjects[i] != null) selectedObjects[i].Deselect();
  83. }
  84. selectedObjects.Clear();
  85. actionListChanged = true;
  86. }
  87. public bool ActionListChanged() => actionListChanged;
  88. private bool actionListChanged = false;
  89. public List<SelectableObjectAction> GetActions()
  90. {
  91. List<SelectableObjectAction> actions = new List<SelectableObjectAction>();
  92. for (int i = 0; i < selectedObjects.Count; i++)
  93. {
  94. if(selectedObjects[i].selectableObject == null) continue;
  95. if(selectedObjects[i].selectableObject.actions == null) continue;
  96. for (int a = 0; a < selectedObjects[i].selectableObject.actions.Count; a++)
  97. {
  98. bool duplicate = false;
  99. for (int v = 0; v < actions.Count; v++)
  100. {
  101. if(actions[v].title == selectedObjects[i].selectableObject.actions[a].title) duplicate = true;
  102. }
  103. if(!duplicate) actions.Add(selectedObjects[i].selectableObject.actions[a]);
  104. }
  105. }
  106. return actions;
  107. }
  108. public void ExecuteAction(SelectableObjectAction action)
  109. {
  110. for (int i = 0; i < selectedObjects.Count; i++)
  111. {
  112. if(selectedObjects[i].selectableObject == null) continue;
  113. if(selectedObjects[i].selectableObject.actions == null) continue;
  114. for (int a = 0; a < selectedObjects[i].selectableObject.actions.Count; a++)
  115. {
  116. if(selectedObjects[i].selectableObject.actions[a].title == action.title)
  117. {
  118. selectedObjects[i].selectableObject.ExecuteAction(action);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }