< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWGizmosController()0%2100%
Initialize(...)0%220100%
Dispose()0%110100%
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 : IBIWController
 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 Initialize(BIWContext context)
 55    {
 7256        base.Initialize(context);
 7257        gizmosGO = GameObject.Instantiate(context.projectReferencesAsset.gizmosPrefab, context.projectReferencesAsset.gi
 7258        gizmos = gizmosGO.GetComponentsInChildren<IBIWGizmos>(true);
 59
 7260        raycastController = context.raycastController;
 61
 7262        raycastController.OnGizmosAxisPressed += OnGizmosAxisPressed;
 7263        BIWInputWrapper.OnMouseUp += OnMouseUp;
 7264        BIWInputWrapper.OnMouseDrag += OnMouseDrag;
 65
 7266        if (context.sceneReferences.cameraController.TryGetCameraStateByType<FreeCameraMovement>(out CameraStateBase cam
 7267            freeCameraMovement = (FreeCameraMovement)cameraState;
 68
 69        // NOTE(Adrian): Take into account that right now to get the relative scale of the gizmos, we set the gizmos in 
 7270        InitializeGizmos(context.sceneReferences.mainCamera, freeCameraMovement.transform);
 7271        ForceRelativeScaleRatio();
 7272    }
 73
 74    public override void Dispose()
 75    {
 7276        base.Dispose();
 7277        UnityEngine.Object.Destroy(gizmosGO);
 7278        raycastController.OnGizmosAxisPressed -= OnGizmosAxisPressed;
 7279        BIWInputWrapper.OnMouseUp -= OnMouseUp;
 7280        BIWInputWrapper.OnMouseDrag -= OnMouseDrag;
 7281    }
 82
 83    public string GetSelectedGizmo()
 84    {
 6585        if (IsGizmoActive())
 2186            return activeGizmo.GetGizmoType();
 87
 4488        return DCL.Components.DCLGizmos.Gizmo.NONE;
 89    }
 90
 91    public void SetSnapFactor(float position, float rotation, float scale)
 92    {
 5193        snapInfo.position = position;
 5194        snapInfo.rotation = rotation;
 5195        snapInfo.scale = scale;
 96
 5197        if (activeGizmo != null)
 5198            activeGizmo.SetSnapFactor(snapInfo);
 5199    }
 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    {
 576139        for (int i = 0; i < gizmos.Length; i++)
 140        {
 216141            gizmos[i].ForceRelativeScaleRatio();
 142        }
 72143    }
 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    {
 167157        if (activeGizmo != null)
 115158            activeGizmo.currentGameObject.SetActive(false);
 159
 167160        if (setInactiveGizmos)
 1161            SetGizmoType(DCL.Components.DCLGizmos.Gizmo.NONE);
 162
 167163        gizmosGO.gameObject.SetActive(false);
 167164    }
 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    {
 58186        HideGizmo();
 187
 58188        if (gizmoType != DCL.Components.DCLGizmos.Gizmo.NONE)
 189        {
 57190            bool wasGizmoActive = IsGizmoActive();
 191
 142192            for (int i = 0; i < gizmos.Length; i++)
 193            {
 71194                if (gizmos[i].GetGizmoType() == gizmoType)
 195                {
 57196                    activeGizmo = gizmos[i];
 57197                    activeGizmo.SetSnapFactor(snapInfo);
 57198                    break;
 199                }
 200            }
 201
 57202            bool areEntitiesSelected = selectedEntities != null && selectedEntities.Count > 0;
 203
 57204            if (wasGizmoActive && areEntitiesSelected)
 0205                ShowGizmo();
 206            else
 57207                GizmoStatusUpdate();
 57208        }
 209        else
 210        {
 1211            activeGizmo = null;
 212        }
 1213    }
 214
 215    private void InitializeGizmos(Camera camera, Transform cameraTransform)
 216    {
 576217        for (int i = 0; i < gizmos.Length; i++)
 218        {
 216219            if (!gizmos[i].initialized)
 216220                gizmos[i].Initialize(camera, cameraTransform);
 221        }
 72222    }
 223
 224    public void SetSelectedEntities(Transform selectionParent, List<BIWEntity> entities)
 225    {
 2226        selectedEntities = entities;
 2227        selectedEntitiesParent = selectionParent;
 2228        GizmoStatusUpdate();
 2229    }
 230
 0231    private void OnGizmosAxisPressed(BIWGizmosAxis pressedAxis) { OnBeginDrag(pressedAxis); }
 232
 233    internal void OnMouseUp(int buttonId, Vector3 mousePosition)
 234    {
 1235        if (!isTransformingObject)
 0236            return;
 237
 1238        if (buttonId == 0)
 1239            OnEndDrag();
 1240    }
 241
 242    internal void OnMouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY)
 243    {
 1244        if (buttonId != 0)
 1245            return;
 246
 0247        bool hasMouseMoved = (axisX != 0 || axisY != 0);
 0248        if (!isTransformingObject || !hasMouseMoved)
 0249            return;
 250
 251        Vector3 hit;
 0252        if (RaycastHit(raycastController.GetMouseRay(mousePosition), out hit))
 0253            OnDrag(hit, mousePosition);
 0254    }
 255
 256    private void CheckGizmoHover(Vector3 mousePosition)
 257    {
 258        RaycastHit hit;
 2259        if (raycastController.RaycastToGizmos(mousePosition, out hit))
 260        {
 0261            BIWGizmosAxis gizmoAxis = hit.collider.gameObject.GetComponent<BIWGizmosAxis>();
 0262            SetAxisHover(gizmoAxis);
 0263        }
 264        else
 265        {
 2266            SetAxisHover(null);
 267        }
 2268    }
 269
 270    private void GizmoStatusUpdate()
 271    {
 59272        if (!IsGizmoActive())
 0273            return;
 274
 59275        if (selectedEntities == null || selectedEntities.Count == 0)
 58276            HideGizmo();
 277        else
 1278            ShowGizmo();
 1279    }
 280
 281    public class SnapInfo
 282    {
 283        public float position = 0;
 284        public float rotation = 0;
 285        public float scale = 0;
 286    }
 287}