< Summary

Class:BIWGizmosController
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/Gizmos/BIWGizmosController.cs
Covered lines:103
Uncovered lines:21
Coverable lines:124
Total lines:286
Line coverage:83% (103 of 124)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWGizmosController()0%2100%
Init(...)0%220100%
Dispose()0%220100%
GetSelectedGizmo()0%220100%
SetSnapFactor(...)0%220100%
OnBeginDrag(...)0%220100%
OnDrag(...)0%330100%
OnEndDrag()0%550100%
HasAxisHover()0%110100%
SetAxisHover(...)0%4.594066.67%
ForceRelativeScaleRatio()0%220100%
ShowGizmo()0%220100%
HideGizmo(...)0%330100%
IsGizmoActive()0%110100%
RaycastHit(...)0%6200%
Update()0%220100%
SetGizmoType(...)0%6.016094.12%
InitializeGizmos(...)0%330100%
SetSelectedEntities(...)0%110100%
OnGizmosAxisPressed(...)0%2100%
OnMouseUp(...)0%3.073080%
OnMouseDrag(...)0%21.196025%
CheckGizmoHover(...)0%2.52050%
GizmoStatusUpdate()0%4.074083.33%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/Gizmos/BIWGizmosController.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL;
 5using DCL.Camera;
 6using UnityEngine;
 7
 8public interface IBIWGizmosController
 9{
 10    delegate void GizmoTransformDelegate(string gizmoType);
 11
 12    event GizmoTransformDelegate OnGizmoTransformObjectStart;
 13    event GizmoTransformDelegate OnGizmoTransformObject;
 14    event GizmoTransformDelegate OnGizmoTransformObjectEnd;
 15    event Action<Vector3> OnChangeTransformValue;
 16
 17    IBIWGizmos activeGizmo { get;  set; }
 18
 19    string GetSelectedGizmo();
 20    void SetSnapFactor(float position, float rotation, float scale);
 21    void SetSelectedEntities(Transform selectionParent, List<BIWEntity> entities);
 22    void ShowGizmo();
 23    void HideGizmo(bool setInactiveGizmos = false);
 24    bool IsGizmoActive();
 25    void ForceRelativeScaleRatio();
 26    bool HasAxisHover();
 27    void SetGizmoType(string gizmoType);
 28}
 29
 30public class BIWGizmosController : BIWController, IBIWGizmosController
 31{
 32    public event IBIWGizmosController.GizmoTransformDelegate OnGizmoTransformObjectStart;
 33    public event IBIWGizmosController.GizmoTransformDelegate OnGizmoTransformObject;
 34    public event IBIWGizmosController.GizmoTransformDelegate OnGizmoTransformObjectEnd;
 35
 36    public event Action<Vector3> OnChangeTransformValue;
 37
 38    private IBIWRaycastController raycastController;
 39
 40    private IBIWGizmos[] gizmos;
 41
 42    internal bool isTransformingObject;
 043    public IBIWGizmos activeGizmo { get; set; }
 44
 045    private SnapInfo snapInfo = new SnapInfo();
 46
 47    private BIWGizmosAxis hoveredAxis = null;
 48
 49    private Transform selectedEntitiesParent;
 50    private List<BIWEntity> selectedEntities;
 51    private GameObject gizmosGO;
 52    private FreeCameraMovement freeCameraMovement;
 53
 54    public override void Init(BIWContext context)
 55    {
 7356        base.Init(context);
 7357        gizmosGO = GameObject.Instantiate(context.projectReferencesAsset.gizmosPrefab, context.projectReferencesAsset.gi
 7358        gizmos = gizmosGO.GetComponentsInChildren<IBIWGizmos>(true);
 59
 7360        raycastController = context.raycastController;
 61
 7362        raycastController.OnGizmosAxisPressed += OnGizmosAxisPressed;
 7363        BIWInputWrapper.OnMouseUp += OnMouseUp;
 7364        BIWInputWrapper.OnMouseDrag += OnMouseDrag;
 7365        if (context.sceneReferences.cameraController.TryGetCameraStateByType<FreeCameraMovement>(out CameraStateBase cam
 7366            freeCameraMovement = (FreeCameraMovement)cameraState;
 67
 68        // NOTE(Adrian): Take into account that right now to get the relative scale of the gizmos, we set the gizmos in 
 7369        InitializeGizmos(context.sceneReferences.mainCamera, freeCameraMovement.transform);
 7370        ForceRelativeScaleRatio();
 7371    }
 72
 73    public override void Dispose()
 74    {
 7475        base.Dispose();
 7476        if (gizmosGO != null)
 7477            GameObject.Destroy(gizmosGO);
 7478        raycastController.OnGizmosAxisPressed -= OnGizmosAxisPressed;
 7479        BIWInputWrapper.OnMouseUp -= OnMouseUp;
 7480        BIWInputWrapper.OnMouseDrag -= OnMouseDrag;
 7481    }
 82
 83    public string GetSelectedGizmo()
 84    {
 6485        if (IsGizmoActive())
 2186            return activeGizmo.GetGizmoType();
 87
 4388        return DCL.Components.DCLGizmos.Gizmo.NONE;
 89    }
 90
 91    public void SetSnapFactor(float position, float rotation, float scale)
 92    {
 5093        snapInfo.position = position;
 5094        snapInfo.rotation = rotation;
 5095        snapInfo.scale = scale;
 96
 5097        if (activeGizmo != null)
 5098            activeGizmo.SetSnapFactor(snapInfo);
 5099    }
 100
 101    internal void OnBeginDrag(BIWGizmosAxis hittedAxis)
 102    {
 1103        isTransformingObject = true;
 1104        activeGizmo = hittedAxis.GetGizmo();
 1105        activeGizmo.OnBeginDrag(hittedAxis, selectedEntitiesParent);
 1106        freeCameraMovement.SetCameraCanMove(false);
 1107        OnGizmoTransformObjectStart?.Invoke(activeGizmo.GetGizmoType());
 1108    }
 109
 110    internal void OnDrag(Vector3 hitPoint, Vector2 mousePosition)
 111    {
 1112        float value = activeGizmo.OnDrag(hitPoint, mousePosition);
 1113        OnGizmoTransformObject?.Invoke(activeGizmo.GetGizmoType());
 1114        OnChangeTransformValue?.Invoke(value * activeGizmo.GetActiveAxisVector());
 1115    }
 116
 117    internal void OnEndDrag()
 118    {
 2119        activeGizmo?.OnEndDrag();
 2120        freeCameraMovement.SetCameraCanMove(true);
 2121        OnGizmoTransformObjectEnd?.Invoke(activeGizmo?.GetGizmoType());
 2122        isTransformingObject = false;
 2123    }
 124
 1125    public bool HasAxisHover() { return hoveredAxis != null; }
 126
 127    private void SetAxisHover(BIWGizmosAxis axis)
 128    {
 2129        if (hoveredAxis != null && hoveredAxis != axis)
 0130            hoveredAxis.SetColorDefault();
 2131        else if (axis != null)
 0132            axis.SetColorHighlight();
 133
 2134        hoveredAxis = axis;
 2135    }
 136
 137    public void ForceRelativeScaleRatio()
 138    {
 584139        for (int i = 0; i < gizmos.Length; i++)
 140        {
 219141            gizmos[i].ForceRelativeScaleRatio();
 142        }
 73143    }
 144
 145    public void ShowGizmo()
 146    {
 1147        gizmosGO.gameObject.SetActive(true);
 1148        if (activeGizmo != null)
 149        {
 1150            activeGizmo.SetTargetTransform(selectedEntitiesParent);
 1151            activeGizmo.currentGameObject.SetActive(true);
 152        }
 1153    }
 154
 155    public void HideGizmo(bool setInactiveGizmos = false)
 156    {
 164157        if (activeGizmo != null)
 113158            activeGizmo.currentGameObject.SetActive(false);
 159
 164160        if (setInactiveGizmos)
 1161            SetGizmoType(DCL.Components.DCLGizmos.Gizmo.NONE);
 162
 164163        gizmosGO.gameObject.SetActive(false);
 164164    }
 165
 1166    public bool IsGizmoActive() { return activeGizmo != null; }
 167
 168    internal bool RaycastHit(Ray ray, out Vector3 hitPoint)
 169    {
 0170        if (activeGizmo != null)
 0171            return activeGizmo.RaycastHit(ray, out hitPoint);
 172
 0173        hitPoint = Vector3.zero;
 0174        return false;
 175    }
 176
 177    public override void Update()
 178    {
 2179        base.Update();
 2180        if (!isTransformingObject)
 2181            CheckGizmoHover(Input.mousePosition);
 2182    }
 183
 184    public void SetGizmoType(string gizmoType)
 185    {
 57186        HideGizmo();
 187
 57188        if (gizmoType != DCL.Components.DCLGizmos.Gizmo.NONE)
 189        {
 56190            bool wasGizmoActive = IsGizmoActive();
 191
 140192            for (int i = 0; i < gizmos.Length; i++)
 193            {
 70194                if (gizmos[i].GetGizmoType() == gizmoType)
 195                {
 56196                    activeGizmo = gizmos[i];
 56197                    activeGizmo.SetSnapFactor(snapInfo);
 56198                    break;
 199                }
 200            }
 201
 56202            bool areEntitiesSelected = selectedEntities != null && selectedEntities.Count > 0;
 203
 56204            if (wasGizmoActive && areEntitiesSelected)
 0205                ShowGizmo();
 206            else
 56207                GizmoStatusUpdate();
 56208        }
 209        else
 210        {
 1211            activeGizmo = null;
 212        }
 1213    }
 214
 215    private void InitializeGizmos(Camera camera, Transform cameraTransform)
 216    {
 584217        for (int i = 0; i < gizmos.Length; i++)
 218        {
 219219            if (!gizmos[i].initialized)
 219220                gizmos[i].Initialize(camera, cameraTransform);
 221        }
 73222    }
 223
 224    public void SetSelectedEntities(Transform selectionParent, List<BIWEntity> entities)
 225    {
 2226        selectedEntities = entities;
 2227        selectedEntitiesParent = selectionParent;
 2228        GizmoStatusUpdate();
 2229    }
 0230    private void OnGizmosAxisPressed(BIWGizmosAxis pressedAxis) { OnBeginDrag(pressedAxis); }
 231
 232    internal void OnMouseUp(int buttonId, Vector3 mousePosition)
 233    {
 1234        if (!isTransformingObject)
 0235            return;
 236
 1237        if (buttonId == 0)
 1238            OnEndDrag();
 1239    }
 240
 241    internal void OnMouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY)
 242    {
 1243        if (buttonId != 0)
 1244            return;
 245
 0246        bool hasMouseMoved = (axisX != 0 || axisY != 0);
 0247        if (!isTransformingObject || !hasMouseMoved)
 0248            return;
 249
 250        Vector3 hit;
 0251        if (RaycastHit(raycastController.GetMouseRay(mousePosition), out hit))
 0252            OnDrag(hit, mousePosition);
 0253    }
 254
 255    private void CheckGizmoHover(Vector3 mousePosition)
 256    {
 257        RaycastHit hit;
 2258        if (raycastController.RaycastToGizmos(mousePosition, out hit))
 259        {
 0260            BIWGizmosAxis gizmoAxis = hit.collider.gameObject.GetComponent<BIWGizmosAxis>();
 0261            SetAxisHover(gizmoAxis);
 0262        }
 263        else
 264        {
 2265            SetAxisHover(null);
 266        }
 2267    }
 268
 269    private void GizmoStatusUpdate()
 270    {
 58271        if (!IsGizmoActive())
 0272            return;
 273
 58274        if (selectedEntities == null || selectedEntities.Count == 0)
 57275            HideGizmo();
 276        else
 1277            ShowGizmo();
 1278    }
 279
 280    public class SnapInfo
 281    {
 282        public float position = 0;
 283        public float rotation = 0;
 284        public float scale = 0;
 285    }
 286}