| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Builder; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public class BIWScaleGizmos : BIWGizmos |
| | 7 | | { |
| | 8 | | const float MINIMUN_SCALE_ALLOWED = 0.01f; |
| | 9 | |
|
| | 10 | | [SerializeField] internal BIWGizmosAxis axisProportionalScale = null; |
| | 11 | |
|
| | 12 | | internal Vector2 lastMousePosition; |
| | 13 | | private Vector2 initialMousePosition; |
| | 14 | | private Vector3 initialHitPoint; |
| | 15 | | internal Vector3 lastHitPoint; |
| 48 | 16 | | private Dictionary<Transform, Vector3> entitiesOriginalPositions = new Dictionary<Transform, Vector3>(); |
| | 17 | |
|
| | 18 | | public override void Initialize(Camera camera, Transform cameraTransform) |
| | 19 | | { |
| 43 | 20 | | base.Initialize(camera, cameraTransform); |
| 43 | 21 | | axisProportionalScale.SetGizmo(this); |
| 43 | 22 | | } |
| | 23 | |
|
| 8 | 24 | | public override void SetSnapFactor(SnapInfo snapInfo) { snapFactor = snapInfo.scale; } |
| | 25 | |
|
| | 26 | | public override float TransformEntity(Transform entityTransform, IBIWGizmosAxis axis, float axisValue) |
| | 27 | | { |
| 2 | 28 | | Vector3 scaleDirection = GetScaleDirection(entityTransform, axis); |
| | 29 | |
|
| | 30 | | // In order to avoid to make the scale of each selected entity dependent of the 'entityTransform' parent, |
| | 31 | | // we temporally move all entities to the same position as 'entityTransform' before calculate the new scale. |
| 4 | 32 | | foreach (Transform entity in entityTransform) |
| | 33 | | { |
| 0 | 34 | | entitiesOriginalPositions.Add(entity, entity.transform.position); |
| 0 | 35 | | entity.transform.position = entityTransform.position; |
| | 36 | | } |
| | 37 | |
|
| 2 | 38 | | if (axis == axisProportionalScale) |
| | 39 | | { |
| | 40 | | // New scale calculation (for proportional scale gizmo) |
| 1 | 41 | | entityTransform.localScale = GetNewScale(entityTransform, axisValue, scaleDirection, false); |
| 1 | 42 | | } |
| | 43 | | else |
| | 44 | | { |
| | 45 | | // New scale calculation (for XYZ-axis scale gizmo) |
| 2 | 46 | | foreach (var originalEntity in entitiesOriginalPositions) |
| | 47 | | { |
| 0 | 48 | | Transform entity = originalEntity.Key; |
| 0 | 49 | | entity.transform.SetParent(null); |
| 0 | 50 | | entity.localScale = GetNewScale(entity.transform, axisValue, scaleDirection, true); |
| 0 | 51 | | entity.SetParent(entityTransform); |
| | 52 | | } |
| | 53 | | } |
| | 54 | |
|
| | 55 | | // Once the new scale has been calculated, we restore the original positions of all the selected entities. |
| 4 | 56 | | foreach (var originalEntity in entitiesOriginalPositions) |
| | 57 | | { |
| 0 | 58 | | Transform entity = originalEntity.Key; |
| 0 | 59 | | Vector3 originalPosition = originalEntity.Value; |
| | 60 | |
|
| 0 | 61 | | entity.position = originalPosition; |
| | 62 | | } |
| | 63 | |
|
| 2 | 64 | | entitiesOriginalPositions.Clear(); |
| | 65 | |
|
| 2 | 66 | | return axisValue; |
| | 67 | | } |
| | 68 | |
|
| | 69 | | private Vector3 GetScaleDirection(Transform entityTransform, IBIWGizmosAxis axis) |
| | 70 | | { |
| 2 | 71 | | Vector3 scaleDirection = activeAxis.axisTransform.forward; |
| 2 | 72 | | if (axis == axisProportionalScale) |
| | 73 | | { |
| 1 | 74 | | scaleDirection = Vector3.one; |
| 1 | 75 | | float inputDirection = (lastMousePosition.x - initialMousePosition.x) + (lastMousePosition.y - initialMouseP |
| 1 | 76 | | if (inputDirection < 0) |
| 0 | 77 | | scaleDirection = -Vector3.one; |
| | 78 | |
|
| 1 | 79 | | initialMousePosition = lastMousePosition; |
| 1 | 80 | | initialHitPoint = lastHitPoint; |
| 1 | 81 | | } |
| 1 | 82 | | else if (worldOrientedGizmos) |
| | 83 | | { |
| 1 | 84 | | scaleDirection = entityTransform.rotation * activeAxis.axisTransform.forward; |
| 1 | 85 | | scaleDirection.x = Mathf.Abs(scaleDirection.x); |
| 1 | 86 | | scaleDirection.y = Mathf.Abs(scaleDirection.y); |
| 1 | 87 | | scaleDirection.z = Mathf.Abs(scaleDirection.z); |
| | 88 | | } |
| | 89 | |
|
| 2 | 90 | | return scaleDirection; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | private Vector3 GetNewScale(Transform entityTransform, float axisValue, Vector3 scaleDirection, bool applyRounding) |
| | 94 | | { |
| 1 | 95 | | Vector3 initialEntityScale = entityTransform.localScale; |
| 1 | 96 | | if (applyRounding && snapFactor > 0) |
| 0 | 97 | | initialEntityScale = GetScaleRoundedToSnapFactor(entityTransform.localScale, axisValue); |
| | 98 | |
|
| 1 | 99 | | Vector3 newScale = initialEntityScale + scaleDirection * axisValue; |
| | 100 | |
|
| 1 | 101 | | if (Mathf.Abs(newScale.x) < MINIMUN_SCALE_ALLOWED || Mathf.Abs(newScale.y) < MINIMUN_SCALE_ALLOWED) |
| 0 | 102 | | newScale += scaleDirection * MINIMUN_SCALE_ALLOWED; |
| | 103 | |
|
| 1 | 104 | | return newScale; |
| | 105 | | } |
| | 106 | |
|
| | 107 | | private Vector3 GetScaleRoundedToSnapFactor(Vector3 scale, float axisValue) |
| | 108 | | { |
| 0 | 109 | | Vector3 activeAxisVector = GetActiveAxisVector(); |
| | 110 | |
|
| 0 | 111 | | scale = new Vector3( |
| | 112 | | activeAxisVector == Vector3.right ? (axisValue >= 0 ? Mathf.Floor(scale.x / snapFactor) : Mathf.Ceil(scale.x |
| | 113 | | activeAxisVector == Vector3.up ? (axisValue >= 0 ? Mathf.Floor(scale.y / snapFactor) : Mathf.Ceil(scale.y / |
| | 114 | | activeAxisVector == Vector3.back ? (axisValue >= 0 ? Mathf.Floor(scale.z / snapFactor) : Mathf.Ceil(scale.z |
| | 115 | |
|
| 0 | 116 | | return scale; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | internal override void SetPreviousAxisValue(float axisValue, float transformValue) |
| | 120 | | { |
| 1 | 121 | | if (activeAxis == axisProportionalScale) |
| 0 | 122 | | previousAxisValue = 0; |
| | 123 | | else |
| 1 | 124 | | previousAxisValue = axisValue; |
| 1 | 125 | | } |
| | 126 | |
|
| | 127 | | internal override float GetHitPointToAxisValue(IBIWGizmosAxis axis, Vector3 hitPoint, Vector2 mousePosition) |
| | 128 | | { |
| 1 | 129 | | if (axis == axisProportionalScale) |
| | 130 | | { |
| 1 | 131 | | if (startDragging) |
| | 132 | | { |
| 0 | 133 | | initialMousePosition = mousePosition; |
| 0 | 134 | | initialHitPoint = hitPoint; |
| | 135 | | } |
| | 136 | |
|
| 1 | 137 | | lastMousePosition = mousePosition; |
| 1 | 138 | | lastHitPoint = hitPoint; |
| 1 | 139 | | return Vector3.Distance(initialHitPoint, hitPoint); |
| | 140 | | } |
| | 141 | |
|
| 0 | 142 | | return axis.axisTransform.InverseTransformPoint(hitPoint).z; |
| | 143 | | } |
| | 144 | | } |