123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using KairoEngine.GameTools.InteractionTools;
- using KairoEngine.UI;
- using Sirenix.OdinInspector;
- namespace KairoEngine.GameTools.Selectables
- {
- /// <summary>
- /// The selectableObject tools controls input for selecting objects in the game and visualizing its properties and actions.
- /// </summary>
- 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<SelectableObjectTrigger> selectedObjects = new List<SelectableObjectTrigger>();
- 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<SelectableObjectTrigger>();
- 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<SelectableObjectAction> GetActions()
- {
- List<SelectableObjectAction> actions = new List<SelectableObjectAction>();
- 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);
- }
- }
- }
- }
- }
- }
|