using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class MoveHandles : MonoBehaviour { public static MoveHandles Instance; [Header("Asset References")] [SerializeField] Sprite _defaultMove; [SerializeField] Sprite _movingGrabbed; [SerializeField] Sprite _arrow; [SerializeField] Sprite _elevateDefault; [SerializeField] Color _normalBackgroundColor; [SerializeField] Color _interactingBackgroundColor; [Header("Scene References")] [SerializeField] GameObject _handleGroup; public GameObject HandleGroup { get { return _handleGroup; } } [SerializeField] GameObject _moveHandle; public GameObject MoveHandle { get { return _moveHandle; } } [SerializeField] GameObject _rotationHandle; //[SerializeField] GameObject _rotationHandle2; public GameObject RotationHandle { get { return _rotationHandle; } } //public GameObject RotationHandle2 { get { return _rotationHandle2; } } [SerializeField] GameObject _elevateHandle; //[SerializeField] GameObject _elevateHandle2; public GameObject ElevateHandle { get { return _elevateHandle; } } //public GameObject ElevateHandle2 { get { return _elevateHandle2; } } [SerializeField] GameObject _hiderControl; public GameObject HiderControl { get { return _hiderControl; } } [SerializeField] GameObject _torus; public GameObject Torus { get { return _torus; } } public GameObject _lastClicked = null; public float Radius { get { float radius = _torus.transform.localScale.x; return radius; } } public static Vector3 _elevateHandleLocalPosition; public static Vector3 _editHandleLocalPosition; public static Vector3 _rotationHandleLocalPosition; public void Initialize() { if (Instance == null) { Instance = this; } else { Destroy(this.gameObject); } _torus.SetActive(false); _elevateHandleLocalPosition = _elevateHandle.transform.localPosition; _rotationHandleLocalPosition = _rotationHandle.transform.localPosition; //if (_editHandle) { _editHandleLocalPosition = _editHandle.transform.localPosition; } if (_hiderControl == null) { _hiderControl = GameObject.Find("HIDER").transform.Find("CONTROL").gameObject; } } public void OnMoveEnter() { _rotationHandle.SetActive(false); //_rotationHandle2.SetActive(false); _elevateHandle.SetActive(false); //_elevateHandle2.SetActive(false); _moveHandle.GetComponent().color = _interactingBackgroundColor; } public void OnElevateEnter() { SetHandleGroupActive(true); _moveHandle.SetActive(false); _rotationHandle.SetActive(false); //_rotationHandle2.SetActive(false); Image handleToModifiy = null; if (_lastClicked == _elevateHandle) { _elevateHandle.SetActive(true); //_elevateHandle2.SetActive(false); handleToModifiy = _elevateHandle.GetComponent(); } //else //{ // _elevateHandle.SetActive(false); // // _elevateHandle2.SetActive(true); // handleToModifiy = _elevateHandle2.GetComponent(); //} handleToModifiy.color = _interactingBackgroundColor; } public void OnRotationEnter() { SetHandleGroupActive(true); _moveHandle.SetActive(false); _elevateHandle.SetActive(false); // _elevateHandle2.SetActive(false); Image handleToModifiy = null; if (_lastClicked == _rotationHandle) { _rotationHandle.SetActive(true); //_rotationHandle2.SetActive(false); handleToModifiy = _rotationHandle.GetComponent(); } //else //{ // _rotationHandle.SetActive(false); // // _rotationHandle2.SetActive(true); // handleToModifiy = _rotationHandle2.GetComponent(); //} handleToModifiy.color = _interactingBackgroundColor; } public void SetHandleGroupActive(bool active) { _handleGroup.SetActive(active); if (active) { SetToDefaultValue(); } else { HideAllHandles(); } } public void SetToDefaultValue() { GameObject obj = SceneModeManager.Selected; if (obj == null) { Debug.Log("sceneModeManager instance is null"); return; } bool isRotationActive = true; bool isElevateActive = true; if(obj.TryGetComponent(out MoveObject move)) { isRotationActive = move._allowRotation; isElevateActive = move._allowElevating; } _moveHandle.SetActive(true); _moveHandle.GetComponent().color = _normalBackgroundColor; _rotationHandle.SetActive(isRotationActive); _rotationHandle.GetComponent().color = _normalBackgroundColor; _elevateHandle.SetActive(isElevateActive); _elevateHandle.GetComponent().color = _normalBackgroundColor; } public void HideAllHandles() { _handleGroup.SetActive(false); return; } public void UpdateHandlesVisual(Transform objectMoving) { if (_handleGroup == null || !_handleGroup.activeInHierarchy) return; if (objectMoving == null) return; Vector3 position = objectMoving.position; int nO = Get.GetObjectIndex(objectMoving.name); float height = DOIT.ConvertStringToNumber(_G.OBJs[nO][7]) * 0.5f; position.y -= height; // Convert the 3D world position to screen coordinates Vector3 screenPosition = Camera.main.WorldToScreenPoint(position); // Set the position of the handle group on the screen _handleGroup.transform.position = screenPosition; } public void ActivateTorus(bool active ,Transform obj) { if (active) { _torus.SetActive(active); DrawRadiusAroundTurret line = _torus.GetComponent(); Vector3 position = obj.position; int nO = Get.GetObjectIndex(obj.name); float Lenght = DOIT.ConvertStringToNumber(_G.OBJs[nO][6]); float depth = DOIT.ConvertStringToNumber(_G.OBJs[nO][8]); float height = DOIT.ConvertStringToNumber(_G.OBJs[nO][7]) * 0.5f; position.y -= height * 0.75f; float maxDistance = Mathf.Max(Lenght, depth); _torus.transform.position = position; _torus.transform.localScale = Vector3.one * maxDistance; _torus.transform.rotation = obj.transform.rotation; } else { _torus.SetActive(active); } } }