< Summary

Class:BIWScaleGizmos
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Gizmos/BIWScaleGizmos.cs
Covered lines:41
Uncovered lines:19
Coverable lines:60
Total lines:144
Line coverage:68.3% (41 of 60)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWScaleGizmos()0%110100%
Initialize(...)0%110100%
SetSnapFactor(...)0%110100%
TransformEntity(...)0%10.56050%
GetScaleDirection(...)0%44093.33%
GetNewScale(...)0%5.585071.43%
GetScaleRoundedToSnapFactor(...)0%1101000%
SetPreviousAxisValue(...)0%2.062075%
GetHitPointToAxisValue(...)0%3.473062.5%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Gizmos/BIWScaleGizmos.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using DCL.Builder;
 4using UnityEngine;
 5
 6public 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;
 4816    private Dictionary<Transform, Vector3> entitiesOriginalPositions = new Dictionary<Transform, Vector3>();
 17
 18    public override void Initialize(Camera camera, Transform cameraTransform)
 19    {
 4320        base.Initialize(camera, cameraTransform);
 4321        axisProportionalScale.SetGizmo(this);
 4322    }
 23
 824    public override void SetSnapFactor(SnapInfo snapInfo) { snapFactor = snapInfo.scale; }
 25
 26    public override float TransformEntity(Transform entityTransform, IBIWGizmosAxis axis, float axisValue)
 27    {
 228        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.
 432        foreach (Transform entity in entityTransform)
 33        {
 034            entitiesOriginalPositions.Add(entity, entity.transform.position);
 035            entity.transform.position = entityTransform.position;
 36        }
 37
 238        if (axis == axisProportionalScale)
 39        {
 40            // New scale calculation (for proportional scale gizmo)
 141            entityTransform.localScale = GetNewScale(entityTransform, axisValue, scaleDirection, false);
 142        }
 43        else
 44        {
 45            // New scale calculation (for XYZ-axis scale gizmo)
 246            foreach (var originalEntity in entitiesOriginalPositions)
 47            {
 048                Transform entity = originalEntity.Key;
 049                entity.transform.SetParent(null);
 050                entity.localScale = GetNewScale(entity.transform, axisValue, scaleDirection, true);
 051                entity.SetParent(entityTransform);
 52            }
 53        }
 54
 55        // Once the new scale has been calculated, we restore the original positions of all the selected entities.
 456        foreach (var originalEntity in entitiesOriginalPositions)
 57        {
 058            Transform entity = originalEntity.Key;
 059            Vector3 originalPosition = originalEntity.Value;
 60
 061            entity.position = originalPosition;
 62        }
 63
 264        entitiesOriginalPositions.Clear();
 65
 266        return axisValue;
 67    }
 68
 69    private Vector3 GetScaleDirection(Transform entityTransform, IBIWGizmosAxis axis)
 70    {
 271        Vector3 scaleDirection = activeAxis.axisTransform.forward;
 272        if (axis == axisProportionalScale)
 73        {
 174            scaleDirection = Vector3.one;
 175            float inputDirection = (lastMousePosition.x - initialMousePosition.x) + (lastMousePosition.y - initialMouseP
 176            if (inputDirection < 0)
 077                scaleDirection = -Vector3.one;
 78
 179            initialMousePosition = lastMousePosition;
 180            initialHitPoint = lastHitPoint;
 181        }
 182        else if (worldOrientedGizmos)
 83        {
 184            scaleDirection = entityTransform.rotation * activeAxis.axisTransform.forward;
 185            scaleDirection.x = Mathf.Abs(scaleDirection.x);
 186            scaleDirection.y = Mathf.Abs(scaleDirection.y);
 187            scaleDirection.z = Mathf.Abs(scaleDirection.z);
 88        }
 89
 290        return scaleDirection;
 91    }
 92
 93    private Vector3 GetNewScale(Transform entityTransform, float axisValue, Vector3 scaleDirection, bool applyRounding)
 94    {
 195        Vector3 initialEntityScale = entityTransform.localScale;
 196        if (applyRounding && snapFactor > 0)
 097            initialEntityScale = GetScaleRoundedToSnapFactor(entityTransform.localScale, axisValue);
 98
 199        Vector3 newScale = initialEntityScale + scaleDirection * axisValue;
 100
 1101        if (Mathf.Abs(newScale.x) < MINIMUN_SCALE_ALLOWED || Mathf.Abs(newScale.y) < MINIMUN_SCALE_ALLOWED)
 0102            newScale += scaleDirection * MINIMUN_SCALE_ALLOWED;
 103
 1104        return newScale;
 105    }
 106
 107    private Vector3 GetScaleRoundedToSnapFactor(Vector3 scale, float axisValue)
 108    {
 0109        Vector3 activeAxisVector = GetActiveAxisVector();
 110
 0111        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
 0116        return scale;
 117    }
 118
 119    internal override void SetPreviousAxisValue(float axisValue, float transformValue)
 120    {
 1121        if (activeAxis == axisProportionalScale)
 0122            previousAxisValue = 0;
 123        else
 1124            previousAxisValue = axisValue;
 1125    }
 126
 127    internal override float GetHitPointToAxisValue(IBIWGizmosAxis axis, Vector3 hitPoint, Vector2 mousePosition)
 128    {
 1129        if (axis == axisProportionalScale)
 130        {
 1131            if (startDragging)
 132            {
 0133                initialMousePosition = mousePosition;
 0134                initialHitPoint = hitPoint;
 135            }
 136
 1137            lastMousePosition = mousePosition;
 1138            lastHitPoint = hitPoint;
 1139            return Vector3.Distance(initialHitPoint, hitPoint);
 140        }
 141
 0142        return axis.axisTransform.InverseTransformPoint(hitPoint).z;
 143    }
 144}