< Summary

Class:BIWGizmos
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Gizmos/BIWGizmos.cs
Covered lines:34
Uncovered lines:33
Coverable lines:67
Total lines:144
Line coverage:50.7% (34 of 67)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWGizmos()0%110100%
Initialize(...)0%110100%
GetActiveAxisVector()0%5.264057.14%
ForceRelativeScaleRatio()0%110100%
GetGizmoType()0%110100%
SetTargetTransform(...)0%110100%
OnBeginDrag(...)0%110100%
OnDrag(...)0%20400%
OnEndDrag()0%2.52050%
RaycastHit(...)0%2100%
GetHitPointToAxisValue(...)0%12300%
SetPreviousAxisValue(...)0%2100%
SetPositionToTarget()0%3.333066.67%
Update()0%6200%
GetCameraPlaneDistance(...)0%110100%

File(s)

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

#LineLine coverage
 1using DCL.Builder;
 2using DCL.Configuration;
 3using UnityEngine;
 4
 5public abstract class BIWGizmos : MonoBehaviour, IBIWGizmos
 6{
 1417    [SerializeField] private string gizmoType = string.Empty;
 8    [SerializeField] protected BIWGizmosAxis axisX;
 9    [SerializeField] protected BIWGizmosAxis axisY;
 10    [SerializeField] protected BIWGizmosAxis axisZ;
 11
 12912    public bool initialized => initializedValue;
 9013    public GameObject currentGameObject { get; private set; }
 014    public bool initializedValue { get; private set; }
 15
 16    protected float snapFactor = 0;
 17
 14118    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
 028    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    {
 12935        this.currentGameObject = gameObject;
 12936        initializedValue = true;
 12937        relativeScaleRatio = transform.localScale / GetCameraPlaneDistance(cameraHolderTransform, transform.position);
 12938        builderCamera = camera;
 12939        this.cameraHolderTransform = cameraHolderTransform;
 12940        axisX.SetGizmo(this);
 12941        axisY.SetGizmo(this);
 12942        axisZ.SetGizmo(this);
 12943    }
 44
 45    public Vector3 GetActiveAxisVector()
 46    {
 147        if (activeAxis == axisX)
 048            return Vector3.right;
 149        if (activeAxis == axisY)
 050            return  Vector3.up;
 151        if (activeAxis == axisZ)
 052            return  Vector3.back;
 53
 154        return Vector3.zero;
 55    }
 56
 25857    public void ForceRelativeScaleRatio() { relativeScaleRatio = new Vector3(BIWSettings.GIZMOS_RELATIVE_SCALE_RATIO, BI
 58
 8159    public string GetGizmoType() { return gizmoType; }
 60
 61    public virtual void SetTargetTransform(Transform entityTransform)
 62    {
 163        targetTransform = entityTransform;
 164        SetPositionToTarget();
 165    }
 66
 67    public virtual void OnBeginDrag(IBIWGizmosAxis axis, Transform entityTransform)
 68    {
 169        startDragging = true;
 170        targetTransform = entityTransform;
 171        activeAxis = axis;
 172        axis.SetColorHighlight();
 173    }
 74
 75    public float OnDrag(Vector3 hitPoint, Vector2 mousePosition)
 76    {
 077        float axisValue = GetHitPointToAxisValue(activeAxis, hitPoint, mousePosition);
 78
 079        if (startDragging)
 80        {
 081            startDragging = false;
 082            previousAxisValue = axisValue;
 83        }
 84
 085        float transformValue = axisValue - previousAxisValue;
 086        if (Mathf.Abs(transformValue) >= snapFactor)
 87        {
 088            if (snapFactor > 0)
 89            {
 090                float sign = Mathf.Sign(transformValue);
 091                transformValue = transformValue + (Mathf.Abs(transformValue) % snapFactor) * -sign;
 92            }
 93
 094            SetPreviousAxisValue(axisValue, transformValue);
 095            return TransformEntity(targetTransform, activeAxis, transformValue);
 96        }
 097        return 0;
 98    }
 99
 1100    public void OnEndDrag() { activeAxis?.SetColorDefault(); }
 101
 102    public virtual bool RaycastHit(Ray ray, out Vector3 hitPoint)
 103    {
 0104        Vector3 hitPos = ray.GetPoint(Vector3.Distance(ray.origin, activeAxis.axisTransform.position));
 0105        Vector3 hitOffset = (hitPos - activeAxis.axisTransform.position);
 0106        hitPoint = activeAxis.axisTransform.position + Vector3.Project(hitOffset, activeAxis.axisTransform.forward);
 0107        return true;
 108    }
 109
 110    internal virtual float GetHitPointToAxisValue(IBIWGizmosAxis axis, Vector3 hitPoint, Vector2 mousePosition)
 111    {
 0112        Vector3 dir = (hitPoint - axis.axisTransform.position).normalized;
 0113        float sign = Vector3.Angle(dir, axis.axisTransform.forward) == 180 ? -1 : 1;
 0114        return Vector3.Distance(activeAxis.axisTransform.position, hitPoint) * sign;
 115    }
 116
 0117    internal virtual void SetPreviousAxisValue(float axisValue, float transformValue) { previousAxisValue = axisValue - 
 118
 119    private void SetPositionToTarget()
 120    {
 1121        if (!targetTransform)
 0122            return;
 123
 1124        transform.position = targetTransform.position;
 1125        if (!worldOrientedGizmos)
 0126            transform.rotation = targetTransform.rotation;
 1127    }
 128
 129    private void Update()
 130    {
 0131        SetPositionToTarget();
 0132        if (!builderCamera)
 0133            return;
 134
 0135        float dist = GetCameraPlaneDistance(cameraHolderTransform, transform.position);
 0136        transform.localScale = relativeScaleRatio * dist;
 0137    }
 138
 139    private static float GetCameraPlaneDistance(Transform cameraTransform, Vector3 objectPosition)
 140    {
 129141        Plane plane = new Plane(cameraTransform.forward, cameraTransform.position);
 129142        return plane.GetDistanceToPoint(objectPosition);
 143    }
 144}