Files
2026-07-21 08:56:10 +03:00

79 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum GizmoDirection
{
X,
Y,
Z
}
public class GizmoMoveElement : MonoBehaviour
{
[SerializeField] private GizmoDirection direction;
private Transform attachedTransform;
private AxisMoveStrategy moveStrategy;
private GhostManagerV2 ghostManager;
private Material OGMat;
[SerializeField] private Material SelectedMat;
private void Start()
{
OGMat = GetComponent<Renderer>().material;
ghostManager = GhostManagerV2.getInstance();
attachedTransform = transform.GetComponentInParent<IInteractable>().transform;
}
public GizmoDirection getDirection()
{
return direction;
}
public void onSelectEnter()
{
// create a new movement strategy for the selected axis and pass it to the ghost
var lockedRotation = attachedTransform.rotation;
moveStrategy = new AxisMoveStrategy(lockedRotation);
ghostManager.SetMovementStrategy(moveStrategy);
Debug.Log("Grabbed the element");
if (!InputModeManager.Has(InputMode.Gizmo))
InputModeManager.Push(InputMode.Gizmo);
ghostManager.SetupExistingBlock(attachedTransform.gameObject, moveStrategy);
moveStrategy.BeginDrag(GetAxisDirection(), ghostManager.GhostTransform.position);
GetComponent<MeshRenderer>().material = SelectedMat;
}
public void onSelectExit()
{
if (InputModeManager.Has(InputMode.Gizmo))
InputModeManager.Pop(InputMode.Gizmo);
if (moveStrategy != null)
{
Debug.Log("Let go of the element");
moveStrategy.EndDrag();
ghostManager.TryCommitOrCancel();
}
}
public void ResetMat()
{
GetComponent<MeshRenderer>().material = OGMat;
}
private Vector3 GetAxisDirection()
{
return direction switch
{
GizmoDirection.X => attachedTransform.right,
GizmoDirection.Y => attachedTransform.up,
GizmoDirection.Z => attachedTransform.forward,
_ => Vector3.zero,
};
}
}