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