SelectableObjectTool.cs 4.8 KB

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