240 lines
8.4 KiB
C#
240 lines
8.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class TransformManager : MonoBehaviour
|
|
{
|
|
[Header("-- Toggles --")]
|
|
[SerializeField] private Toggle FreeMove;
|
|
[SerializeField] private Toggle AxialMove;
|
|
[SerializeField] private Toggle AxialRotate;
|
|
private SelectionManager selectionManager;
|
|
private Camera mianCamera;
|
|
private bool FMoveEn = false;
|
|
|
|
[Header("-- Gizmos --")]
|
|
[SerializeField] private GameObject gizmosMovePrefab;
|
|
[SerializeField] private GameObject gizmosRotatePrefab;
|
|
[SerializeField] private float scaleFactor = 1;
|
|
private GameObject currentGizmosMove;
|
|
private GameObject currentGizmosRotate;
|
|
private IInteractable CurrentSelection;
|
|
private GhostStrategyHandler GhostSH;
|
|
private GizmoMoveElement MElement;
|
|
private GizmoRotateElement RElement;
|
|
private int currentRotationPivotIndex = -1;
|
|
private GameObject lastSelectionForRot = null;
|
|
private RayInteractor RayInteractor;
|
|
private Transform[] GizmosArray;
|
|
private bool IsSelected = false;
|
|
|
|
[SerializeField] private Material UnSelectedMaterial;
|
|
|
|
void Start()
|
|
{
|
|
selectionManager = SelectionManager.getInstance();
|
|
|
|
FreeMove.onValueChanged.AddListener(FMove);
|
|
AxialMove.onValueChanged.AddListener(AMove);
|
|
AxialRotate.onValueChanged.AddListener(ARot);
|
|
mianCamera = Camera.main;
|
|
RayInteractor = RayInteractor.getInstance();
|
|
selectionManager.SelectionEvent.AddListener(SetCurrent);
|
|
RayInteractor.OnWorldClick.AddListener(GizmoDeselect);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (currentGizmosMove != null)
|
|
{
|
|
// update scale
|
|
currentGizmosMove.transform.localScale = Vector3.Distance(currentGizmosMove.transform.position, mianCamera.transform.position)
|
|
* scaleFactor * Vector3.one / currentGizmosMove.transform.parent.localScale.x;
|
|
|
|
if (Input.GetKeyDown(KeyCode.Escape))
|
|
GizmoDeselect();
|
|
|
|
if (Input.GetMouseButtonUp(0) && MElement != null)
|
|
{
|
|
IsSelected = false;
|
|
MElement.onSelectExit();
|
|
foreach (var g in GizmosArray)
|
|
{
|
|
g.GetComponent<GizmoMoveElement>().ResetMat();
|
|
}
|
|
MElement = null;
|
|
}
|
|
if (RayInteractor.GetCurrentGizmo() && !IsSelected)
|
|
SelectionToMGizmo(RayInteractor.GetCurrentGizmo().GetComponent<GizmoMoveElement>());
|
|
if (Input.GetMouseButtonDown(0) && MElement != null)
|
|
{
|
|
IsSelected = true;
|
|
MElement.onSelectEnter();
|
|
foreach (var g in GizmosArray)
|
|
{
|
|
if (g == MElement.transform)
|
|
continue;
|
|
g.GetComponent<MeshRenderer>().material = UnSelectedMaterial;
|
|
}
|
|
Debug.Log($"The element selected is {MElement.name}");
|
|
}
|
|
}
|
|
if (currentGizmosRotate != null)
|
|
{
|
|
// update scale
|
|
currentGizmosRotate.transform.localScale = Vector3.Distance(currentGizmosRotate.transform.position, mianCamera.transform.position)
|
|
* scaleFactor * Vector3.one / currentGizmosRotate.transform.parent.localScale.x;
|
|
|
|
if (Input.GetKeyDown(KeyCode.Escape))
|
|
GizmoDeselect();
|
|
|
|
if (Input.GetKeyDown(KeyCode.C))
|
|
CycleRotationPivot();
|
|
|
|
if (Input.GetMouseButtonUp(0) && RElement != null)
|
|
{
|
|
IsSelected = false;
|
|
RElement.onSelectExit();
|
|
foreach (var g in GizmosArray)
|
|
{
|
|
g.GetComponent<GizmoRotateElement>().ResetMat();
|
|
}
|
|
RElement = null;
|
|
}
|
|
if (RayInteractor.GetCurrentGizmo() && !IsSelected)
|
|
SelectionToRGizmo(RayInteractor.GetCurrentGizmo().GetComponent<GizmoRotateElement>());
|
|
if (Input.GetMouseButtonDown(0) && RElement != null)
|
|
{
|
|
IsSelected = true;
|
|
RElement.onSelectEnter();
|
|
foreach (var g in GizmosArray)
|
|
{
|
|
if (g == RElement.transform)
|
|
continue;
|
|
g.GetComponent<MeshRenderer>().material = UnSelectedMaterial;
|
|
}
|
|
Debug.Log($"The element selected is {RElement.name}");
|
|
}
|
|
}
|
|
}
|
|
|
|
void SelectionToMGizmo(GizmoMoveElement element)
|
|
{
|
|
MElement = element;
|
|
}
|
|
|
|
void SelectionToRGizmo(GizmoRotateElement element)
|
|
{
|
|
RElement = element;
|
|
}
|
|
|
|
void GizmoDeselect()
|
|
{
|
|
if (currentGizmosMove != null)
|
|
{
|
|
Destroy(currentGizmosMove);
|
|
currentGizmosMove = null;
|
|
}
|
|
if (currentGizmosRotate != null)
|
|
{
|
|
Destroy(currentGizmosRotate);
|
|
currentGizmosRotate = null;
|
|
}
|
|
|
|
if (AxialMove != null) AxialMove.SetIsOnWithoutNotify(false);
|
|
if (AxialRotate != null) AxialRotate.SetIsOnWithoutNotify(false);
|
|
if (FreeMove != null) FreeMove.SetIsOnWithoutNotify(false);
|
|
}
|
|
|
|
void FMove(bool Trigger)
|
|
{
|
|
if (selectionManager.HasOneSelection())
|
|
{
|
|
var ghostManager = GhostManagerV2.getInstance();
|
|
if (ghostManager != null && CurrentSelection is MonoBehaviour mb)
|
|
{
|
|
ghostManager.SetupExistingBlock(mb.gameObject, new FreeMoveStrategy());
|
|
}
|
|
}
|
|
}
|
|
|
|
void AMove(bool Trigger)
|
|
{
|
|
if (selectionManager.HasOneSelection())
|
|
{
|
|
if (currentGizmosMove == null)
|
|
{
|
|
currentGizmosMove = Instantiate(gizmosMovePrefab, CurrentSelection.transform);
|
|
GizmosArray = currentGizmosMove.GetComponentsInChildren<Transform>().Where(x => x != currentGizmosMove.transform).ToArray();
|
|
currentGizmosMove.transform.parent = CurrentSelection.transform;
|
|
currentGizmosMove.transform.localPosition = Vector3.zero;
|
|
currentGizmosMove.transform.localRotation = Quaternion.identity;
|
|
}
|
|
GizmoMoveElement[] currnetMoveElements = currentGizmosMove.GetComponentsInChildren<GizmoMoveElement>();
|
|
foreach (GizmoMoveElement currnetMoveElement in currnetMoveElements)
|
|
{
|
|
// add listener to each axis inteaction
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
void ARot(bool Trigger)
|
|
{
|
|
if (Trigger && selectionManager.HasOneSelection())
|
|
{
|
|
if (currentGizmosRotate == null)
|
|
{
|
|
currentGizmosRotate = Instantiate(gizmosRotatePrefab, CurrentSelection.transform);
|
|
GizmosArray = currentGizmosRotate.GetComponentsInChildren<Transform>().Where(x => x != currentGizmosRotate.transform).ToArray();
|
|
currentGizmosRotate.transform.parent = CurrentSelection.transform;
|
|
|
|
// Reset pivot when spawning a new gizmo
|
|
lastSelectionForRot = CurrentSelection.transform.gameObject;
|
|
currentRotationPivotIndex = -1;
|
|
}
|
|
|
|
UpdateRotationGizmoPosition();
|
|
}
|
|
else if (!Trigger && currentGizmosRotate != null)
|
|
{
|
|
Destroy(currentGizmosRotate);
|
|
currentGizmosRotate = null;
|
|
}
|
|
}
|
|
|
|
public void CycleRotationPivot()
|
|
{
|
|
if (currentGizmosRotate != null && selectionManager.HasOneSelection())
|
|
{
|
|
currentRotationPivotIndex++;
|
|
UpdateRotationGizmoPosition();
|
|
}
|
|
}
|
|
|
|
private void UpdateRotationGizmoPosition()
|
|
{
|
|
if (CurrentSelection == null || currentGizmosRotate == null) return;
|
|
|
|
SocketPoint[] sockets = CurrentSelection.transform.GetComponentsInChildren<SocketPoint>();
|
|
if (currentRotationPivotIndex >= sockets.Length)
|
|
{
|
|
currentRotationPivotIndex = -1; // -1 means center of the link
|
|
}
|
|
|
|
Vector3 pivotPosition = currentRotationPivotIndex == -1
|
|
? CurrentSelection.transform.position
|
|
: sockets[currentRotationPivotIndex].GetTransform().position;
|
|
|
|
currentGizmosRotate.transform.position = pivotPosition;
|
|
currentGizmosRotate.transform.rotation = CurrentSelection.transform.rotation;
|
|
}
|
|
|
|
void SetCurrent(IInteractable interactable)
|
|
{
|
|
CurrentSelection = interactable;
|
|
}
|
|
}
|