| | 1 | | using DCL.Configuration; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace Builder.Gizmos |
| | 5 | | { |
| | 6 | | public abstract class DCLBuilderGizmo : MonoBehaviour |
| | 7 | | { |
| 597 | 8 | | [SerializeField] private string gizmoType = string.Empty; |
| | 9 | | [SerializeField] protected DCLBuilderGizmoAxis axisX; |
| | 10 | | [SerializeField] protected DCLBuilderGizmoAxis axisY; |
| | 11 | | [SerializeField] protected DCLBuilderGizmoAxis axisZ; |
| | 12 | |
|
| 0 | 13 | | public bool initialized { get; private set; } |
| | 14 | | protected float snapFactor = 0; |
| | 15 | |
|
| 597 | 16 | | protected bool worldOrientedGizmos = true; |
| | 17 | | private Transform targetTransform = null; |
| | 18 | |
|
| | 19 | | protected Camera builderCamera; |
| | 20 | | protected Transform cameraHolderTransform; |
| | 21 | |
|
| | 22 | | private Vector3 relativeScaleRatio; |
| | 23 | | protected bool startDragging = false; |
| | 24 | | protected float prevAxisValue; |
| | 25 | |
|
| 0 | 26 | | public DCLBuilderGizmoAxis activeAxis { protected set; get; } |
| | 27 | |
|
| | 28 | | public abstract void SetSnapFactor(DCLBuilderGizmoManager.SnapInfo snapInfo); |
| | 29 | | public abstract float TransformEntity(Transform targetTransform, DCLBuilderGizmoAxis axis, float axisValue); |
| | 30 | |
|
| | 31 | | public virtual void Initialize(Camera camera, Transform cameraHolderTransform) |
| | 32 | | { |
| 3 | 33 | | initialized = true; |
| 3 | 34 | | relativeScaleRatio = transform.localScale / GetCameraPlaneDistance(cameraHolderTransform, transform.position |
| 3 | 35 | | builderCamera = camera; |
| 3 | 36 | | this.cameraHolderTransform = cameraHolderTransform; |
| 3 | 37 | | axisX.SetGizmo(this); |
| 3 | 38 | | axisY.SetGizmo(this); |
| 3 | 39 | | axisZ.SetGizmo(this); |
| 3 | 40 | | } |
| | 41 | |
|
| | 42 | | public Vector3 GetActiveAxisVector() |
| | 43 | | { |
| 0 | 44 | | if (activeAxis == axisX) |
| 0 | 45 | | return Vector3.right; |
| 0 | 46 | | if (activeAxis == axisY) |
| 0 | 47 | | return Vector3.up; |
| 0 | 48 | | if (activeAxis == axisZ) |
| 0 | 49 | | return Vector3.back; |
| | 50 | |
|
| 0 | 51 | | return Vector3.zero; |
| | 52 | | } |
| | 53 | |
|
| 24 | 54 | | public void ForceRelativeScaleRatio() { relativeScaleRatio = new Vector3(BuilderInWorldSettings.GIZMOS_RELATIVE_ |
| | 55 | |
|
| 0 | 56 | | public string GetGizmoType() { return gizmoType; } |
| | 57 | |
|
| | 58 | | public virtual void SetTargetTransform(Transform entityTransform) |
| | 59 | | { |
| 0 | 60 | | targetTransform = entityTransform; |
| 0 | 61 | | SetPositionToTarget(); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | public virtual void OnBeginDrag(DCLBuilderGizmoAxis axis, Transform entityTransform) |
| | 65 | | { |
| 0 | 66 | | startDragging = true; |
| 0 | 67 | | targetTransform = entityTransform; |
| 0 | 68 | | activeAxis = axis; |
| 0 | 69 | | axis.SetColorHighlight(); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | public virtual float OnDrag(Vector3 hitPoint, Vector2 mousePosition) |
| | 73 | | { |
| 0 | 74 | | float axisValue = GetHitPointToAxisValue(activeAxis, hitPoint, mousePosition); |
| | 75 | |
|
| 0 | 76 | | if (startDragging) |
| | 77 | | { |
| 0 | 78 | | startDragging = false; |
| 0 | 79 | | prevAxisValue = axisValue; |
| | 80 | | } |
| | 81 | |
|
| 0 | 82 | | float transformValue = axisValue - prevAxisValue; |
| 0 | 83 | | if (Mathf.Abs(transformValue) >= snapFactor) |
| | 84 | | { |
| 0 | 85 | | if (snapFactor > 0) |
| | 86 | | { |
| 0 | 87 | | float sign = Mathf.Sign(transformValue); |
| 0 | 88 | | transformValue = transformValue + (Mathf.Abs(transformValue) % snapFactor) * -sign; |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | SetPreviousAxisValue(axisValue, transformValue); |
| 0 | 92 | | return TransformEntity(targetTransform, activeAxis, transformValue); |
| | 93 | | } |
| 0 | 94 | | return 0; |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | public virtual void OnEndDrag() { activeAxis.SetColorDefault(); } |
| | 98 | |
|
| | 99 | | public virtual bool RaycastHit(Ray ray, out Vector3 hitPoint) |
| | 100 | | { |
| 0 | 101 | | Vector3 hitPos = ray.GetPoint(Vector3.Distance(ray.origin, activeAxis.transform.position)); |
| 0 | 102 | | Vector3 hitOffset = (hitPos - activeAxis.transform.position); |
| 0 | 103 | | hitPoint = activeAxis.transform.position + Vector3.Project(hitOffset, activeAxis.transform.forward); |
| 0 | 104 | | return true; |
| | 105 | | } |
| | 106 | |
|
| | 107 | | protected virtual float GetHitPointToAxisValue(DCLBuilderGizmoAxis axis, Vector3 hitPoint, Vector2 mousePosition |
| | 108 | | { |
| 0 | 109 | | Vector3 dir = (hitPoint - axis.transform.position).normalized; |
| 0 | 110 | | float sign = Vector3.Angle(dir, axis.transform.forward) == 180 ? -1 : 1; |
| 0 | 111 | | return Vector3.Distance(activeAxis.transform.position, hitPoint) * sign; |
| | 112 | | } |
| | 113 | |
|
| 0 | 114 | | protected virtual void SetPreviousAxisValue(float axisValue, float transformValue) { prevAxisValue = axisValue - |
| | 115 | |
|
| | 116 | | private void SetPositionToTarget() |
| | 117 | | { |
| 0 | 118 | | if (targetTransform) |
| | 119 | | { |
| 0 | 120 | | transform.position = targetTransform.position; |
| 0 | 121 | | if (!worldOrientedGizmos) |
| | 122 | | { |
| 0 | 123 | | transform.rotation = targetTransform.rotation; |
| | 124 | | } |
| | 125 | | } |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | private void Update() |
| | 129 | | { |
| 0 | 130 | | SetPositionToTarget(); |
| 0 | 131 | | if (builderCamera) |
| | 132 | | { |
| 0 | 133 | | float dist = GetCameraPlaneDistance(cameraHolderTransform, transform.position); |
| 0 | 134 | | transform.localScale = relativeScaleRatio * dist; |
| | 135 | | } |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | private static float GetCameraPlaneDistance(Transform cameraTransform, Vector3 objectPosition) |
| | 139 | | { |
| 3 | 140 | | Plane plane = new Plane(cameraTransform.forward, cameraTransform.position); |
| 3 | 141 | | return plane.GetDistanceToPoint(objectPosition); |
| | 142 | | } |
| | 143 | | } |
| | 144 | | } |