You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
251 lines
8.6 KiB
251 lines
8.6 KiB
using System.Collections.Generic; |
|
using System.Linq; |
|
using UnityEngine; |
|
using UnityEngine.EventSystems; |
|
using HighlightPlus; |
|
|
|
public class PowerSelectionManager : MonoBehaviour |
|
{ |
|
/* |
|
public static PowerSelectionManager Instance; |
|
public static List<GameObject> Sets = new List<GameObject>(); |
|
public bool canMakeSelections = true; |
|
//public bool selectByLayer = true; |
|
//public int selectedLayer; |
|
public KeyCode keyMultiselect = KeyCode.LeftControl; |
|
public KeyCode keyDeselect = KeyCode.LeftAlt; |
|
public KeyCode DracRectKey = KeyCode.LeftShift; |
|
|
|
private static List<SelectableObject> selectedGameObjects = new List<SelectableObject>(); |
|
private bool isSelecting = false; |
|
private Vector3 mousePosition; |
|
|
|
private void Awake() |
|
{ |
|
Instance = this; |
|
} |
|
|
|
private struct SelectableObject |
|
{ |
|
public GameObject gameObject; |
|
public SelectableUnit selectable; |
|
public bool isActive; |
|
} |
|
|
|
private void Start() |
|
{ |
|
if (!Camera.main) |
|
Debug.Log("The SelectionManager requires a camera with the tag 'MainCamera'"); |
|
ClearSelection(); |
|
} |
|
|
|
void Update() |
|
{ |
|
if (!Camera.main) |
|
return; |
|
if (Sets.Count > 0) |
|
{ |
|
selectedGameObjects = selectedGameObjects.Where(i => i.gameObject != null).ToList(); |
|
if (canMakeSelections) |
|
{ |
|
if (Input.GetMouseButtonDown(0)) |
|
{ |
|
isSelecting = true; |
|
mousePosition = Input.mousePosition; |
|
} |
|
} |
|
if (Input.GetMouseButtonUp(0)) |
|
{ |
|
if (!EventSystem.current.IsPointerOverGameObject()) |
|
{ |
|
if (isSelecting) |
|
{ |
|
if (!(Input.GetKey(keyMultiselect)) && !(Input.GetKey(keyDeselect))) |
|
{ |
|
ClearSelection(); |
|
} |
|
List<GameObject> newSelectedObjects = new List<GameObject>(); |
|
bool isFiltered = false; |
|
//if (selectByLayer) |
|
//{ |
|
// isFiltered = true; |
|
// newSelectedObjects.AddRange(Utils.FindGameObjectsWithLayer(selectedLayer, Sets)); |
|
//} |
|
if (!isFiltered && newSelectedObjects.Count == 0) |
|
{ |
|
newSelectedObjects.AddRange(Utils.FindGameObjectsInList(Sets)); |
|
} |
|
if (Input.GetKey(DracRectKey)) |
|
{ |
|
foreach (GameObject selectedObject in newSelectedObjects) |
|
{ |
|
if (IsWithinSelectionBounds(selectedObject)) |
|
{ |
|
parseGameObjectForSelection(selectedObject); |
|
} |
|
} |
|
} |
|
} |
|
isSelecting = false; |
|
RaycastHit selectionHit; |
|
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out selectionHit)) |
|
{ |
|
parseGameObjectForSelection(selectionHit.transform.gameObject); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
#region Private Methods |
|
void detachSelection(SelectableObject selectedObject) |
|
{ |
|
if (selectedObject.gameObject != null) |
|
{ |
|
|
|
selectedObject.gameObject.GetComponent<HighlightEffect>().highlighted = false; |
|
PowerManager.Instance.PowerModel = null; |
|
if (selectedObject.selectable) |
|
{ |
|
selectedObject.selectable.OnEndSelection(); |
|
} |
|
} |
|
|
|
} |
|
void parseGameObjectForSelection(GameObject sender) |
|
{ |
|
bool validObject = true; |
|
|
|
//if (selectByLayer && sender.layer != selectedLayer) |
|
//{ |
|
// validObject = false; |
|
//} |
|
if (Sets.Contains(sender) == false) |
|
validObject = false; |
|
|
|
if (validObject) |
|
{ |
|
bool containsObject = false; |
|
List<SelectableObject> objectsToRemove = new List<SelectableObject>(); |
|
for (int x = 0; x < selectedGameObjects.Count; x++) |
|
{ |
|
SelectableObject selectedObject = selectedGameObjects[x]; |
|
|
|
if (selectedObject.gameObject == sender) |
|
{ |
|
containsObject = true; |
|
selectedObject.isActive = true; |
|
} |
|
|
|
selectedGameObjects[x] = selectedObject; |
|
|
|
if (Input.GetKey(keyDeselect) && selectedObject.gameObject == sender) |
|
{ |
|
detachSelection(selectedObject); |
|
objectsToRemove.Add(selectedObject); |
|
} |
|
} |
|
selectedGameObjects = selectedGameObjects.Except(objectsToRemove).ToList(); |
|
|
|
if (!containsObject && !Input.GetKey(keyDeselect)) |
|
{ |
|
var selectable = sender.GetComponent<SelectableUnit>(); |
|
if (selectable && selectable != selectable.playerOwned) |
|
{ |
|
return; |
|
} |
|
|
|
|
|
if (sender.GetComponent<PowerController>().PowerMode == CreationMode.Single) |
|
{ |
|
SelectableObject selectedGameObject = new SelectableObject(); |
|
selectedGameObject.gameObject = sender; |
|
selectedGameObject.isActive = true; |
|
if (selectable) |
|
{ |
|
selectedGameObject.selectable = selectable; |
|
selectedGameObject.selectable.OnBeginSelection(); |
|
} |
|
selectedGameObjects.Add(selectedGameObject); |
|
PowerManager.Instance.PowerModel = selectedGameObject.gameObject; |
|
} |
|
else |
|
{ |
|
foreach (Transform go in sender.transform.parent) |
|
{ |
|
SelectableObject selectedGameObject = new SelectableObject(); |
|
selectedGameObject.gameObject = go.gameObject; |
|
selectedGameObject.isActive = true; |
|
if (selectable) |
|
{ |
|
selectedGameObject.selectable = selectable; |
|
selectedGameObject.selectable.OnBeginSelection(); |
|
} |
|
selectedGameObjects.Add(selectedGameObject); |
|
PowerManager.Instance.PowerModel = selectedGameObject.gameObject; |
|
} |
|
} |
|
|
|
|
|
|
|
for (int i = 0; i < selectedGameObjects.Count; i++) |
|
{ |
|
selectedGameObjects[i].gameObject.GetComponent<HighlightEffect>().highlighted = true; |
|
} |
|
} |
|
} |
|
} |
|
#endregion |
|
void OnGUI() |
|
{ |
|
if (isSelecting && Input.GetKey(DracRectKey)) |
|
{ |
|
// Create a rect from both mouse positions |
|
var rect = Utils.GetScreenRect(mousePosition, Input.mousePosition); |
|
Utils.DrawScreenRect(rect, new Color(0.2f, 0.8f, 0.2f, 0.25f)); |
|
Utils.DrawScreenRectBorder(rect, 1, new Color(0.2f, 0.8f, 0.2f)); |
|
} |
|
} |
|
bool IsWithinSelectionBounds(GameObject gameObject) |
|
{ |
|
if (!isSelecting) |
|
return false; |
|
|
|
var camera = Camera.main; |
|
var viewportBounds = Utils.GetViewportBounds(camera, mousePosition, Input.mousePosition); |
|
return viewportBounds.Contains(camera.WorldToViewportPoint(gameObject.transform.position)); |
|
} |
|
|
|
#region Public Methods |
|
//returns a list of currently selected objects |
|
public static GameObject[] GetSelectedObjects() |
|
{ |
|
return selectedGameObjects.Select(i => i.gameObject).ToArray(); |
|
} |
|
public void AddGameObjectToSelection(GameObject newObject) |
|
{ |
|
parseGameObjectForSelection(newObject); |
|
|
|
} |
|
public void RemoveGameObjectFromSelection(GameObject removedObject) |
|
{ |
|
foreach (SelectableObject selectedObject in selectedGameObjects) |
|
{ |
|
if (selectedObject.gameObject == removedObject) |
|
{ |
|
detachSelection(selectedObject); |
|
selectedGameObjects.Remove(selectedObject); |
|
} |
|
} |
|
} |
|
public void ClearSelection() |
|
{ |
|
foreach (SelectableObject selectedObject in selectedGameObjects) |
|
{ |
|
detachSelection(selectedObject); |
|
} |
|
selectedGameObjects.Clear(); |
|
} |
|
#endregion |
|
*/ |
|
}
|
|
|