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