using System.Collections; using System.Collections.Generic; using UnityEngine; using KairoEngine.GameTools.InteractionTools; using KairoEngine.UI; using Sirenix.OdinInspector; namespace KairoEngine.GameTools.Selectables { /// /// The selectableObject tools controls input for selecting objects in the game and visualizing its properties and actions. /// public class SelectableObjectTool : MonoBehaviour, IInteractionTool, IHighlightableObject { public bool allowMultiSelection = true; public KeyCode multiSelectionKey = KeyCode.LeftControl; public LayerMask layerMask; public bool selectableObjectToolEnabled = true; public Sprite icon; public Color selectionColor = Color.white; [ShowInInspector, ReadOnly] public List selectedObjects = new List(); private void Update() { SelectObjects(); } public void DisableTool() { selectableObjectToolEnabled = false; } public void EnableTool() { selectableObjectToolEnabled = true; } public Sprite GetIcon() => icon; public Color GetHighlightColor() => selectionColor; public bool ShowOutline() => true; private void SelectObjects() { actionListChanged = false; if(!selectableObjectToolEnabled) return; if(Input.GetMouseButtonDown(0) && !MouseInputUIBlocker.BlockedByUI) { if(Camera.main == null) return; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray, out RaycastHit raycastHit, 999f, layerMask)) { var selectableObjectTrigger = raycastHit.collider.gameObject.GetComponent(); if(selectableObjectTrigger != null) { if(allowMultiSelection && Input.GetKey(multiSelectionKey)) AddToSelection(selectableObjectTrigger); else { ClearSelection(); AddToSelection(selectableObjectTrigger); } } } } } public void AddToSelection(SelectableObjectTrigger selectableObjectTrigger) { bool alreadySelected = false; for (int i = 0; i < selectedObjects.Count; i++) { if(selectedObjects[i] == selectableObjectTrigger) alreadySelected = true; } if(alreadySelected == false) { selectableObjectTrigger.Select(); selectedObjects.Add(selectableObjectTrigger); } else { selectableObjectTrigger.Deselect(); selectedObjects.Remove(selectableObjectTrigger); } actionListChanged = true; } public void ClearSelection() { for (int i = 0; i < selectedObjects.Count; i++) { if(selectedObjects[i] != null) selectedObjects[i].Deselect(); } selectedObjects.Clear(); actionListChanged = true; } public bool ActionListChanged() => actionListChanged; private bool actionListChanged = false; public List GetActions() { List actions = new List(); for (int i = 0; i < selectedObjects.Count; i++) { if(selectedObjects[i].selectableObject == null) continue; if(selectedObjects[i].selectableObject.actions == null) continue; for (int a = 0; a < selectedObjects[i].selectableObject.actions.Count; a++) { bool duplicate = false; for (int v = 0; v < actions.Count; v++) { if(actions[v].title == selectedObjects[i].selectableObject.actions[a].title) duplicate = true; } if(!duplicate) actions.Add(selectedObjects[i].selectableObject.actions[a]); } } return actions; } public void ExecuteAction(SelectableObjectAction action) { for (int i = 0; i < selectedObjects.Count; i++) { if(selectedObjects[i].selectableObject == null) continue; if(selectedObjects[i].selectableObject.actions == null) continue; for (int a = 0; a < selectedObjects[i].selectableObject.actions.Count; a++) { if(selectedObjects[i].selectableObject.actions[a].title == action.title) { selectedObjects[i].selectableObject.ExecuteAction(action); } } } } } }