< Summary

Class:BIWRaycastController
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Controllers/BIWRaycast.cs
Covered lines:26
Uncovered lines:45
Coverable lines:71
Total lines:181
Line coverage:36.6% (26 of 71)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Initialize(...)0%110100%
Dispose()0%110100%
OnMouseDown(...)0%6200%
GetEntityOnPointer()0%5.024060%
RayCastFloor(...)0%2.152066.67%
Raycast(...)0%6200%
GetCloserUnselectedVoxelEntityOnPointer()0%56700%
GetFloorPointAtMouse(...)0%3.213071.43%
GetMouseRay(...)0%110100%
CheckGizmosRaycast(...)0%20400%
RaycastToGizmos(...)0%2100%
RaycastToGizmos(...)0%2100%
CompareSelectionHit(...)0%2100%
IsGizmoHit(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Controllers/BIWRaycast.cs

#LineLine coverage
 1using DCL.Builder;
 2using DCL.Configuration;
 3using UnityEngine;
 4
 5public class BIWRaycastController : BIWController, IBIWRaycastController
 6{
 7    public event System.Action<IBIWGizmosAxis> OnGizmosAxisPressed;
 8
 9    private Camera builderCamera;
 10    private IBIWEntityHandler entityHandler;
 11    private IBIWModeController modeController;
 12
 13    private const float RAYCAST_MAX_DISTANCE = 10000f;
 14
 015    public LayerMask gizmoMask { get; private set; }
 16
 17    public override void Initialize(IContext context)
 18    {
 3119        base.Initialize(context);
 20
 3121        entityHandler = context.editorContext.entityHandler;
 3122        modeController = context.editorContext.modeController;
 3123        gizmoMask = BIWSettings.GIZMOS_LAYER;
 3124        BIWInputWrapper.OnMouseDown += OnMouseDown;
 25
 3126        builderCamera = context.sceneReferences.mainCamera;
 3127    }
 28
 29    public override void Dispose()
 30    {
 3131        base.Dispose();
 3132        BIWInputWrapper.OnMouseDown -= OnMouseDown;
 3133    }
 34
 35    private void OnMouseDown(int buttonId, Vector3 mousePosition)
 36    {
 037        if (buttonId != 0)
 038            return;
 39
 040        CheckGizmosRaycast(mousePosition);
 041    }
 42
 43    public BIWEntity GetEntityOnPointer()
 44    {
 145        Camera camera = Camera.main;
 46
 147        if (camera == null)
 048            return null;
 49
 50        RaycastHit hit;
 151        UnityEngine.Ray ray = camera.ScreenPointToRay(modeController.GetMousePosition());
 152        float distanceToSelect = modeController.GetMaxDistanceToSelectEntities();
 53
 154        if (Physics.Raycast(ray, out hit, distanceToSelect, BIWSettings.COLLIDER_SELECTION_LAYER))
 55        {
 056            string entityID = hit.collider.gameObject.name;
 57
 058            if (sceneToEdit.entities.ContainsKey(entityID))
 59            {
 060                return entityHandler.GetConvertedEntity(sceneToEdit.entities[entityID]);
 61            }
 62        }
 63
 164        return null;
 65    }
 66
 67    public bool RayCastFloor(out Vector3 position)
 68    {
 69        RaycastHit hit;
 70
 271        UnityEngine.Ray ray = GetMouseRay(Input.mousePosition);
 72
 273        if (Physics.Raycast(ray, out hit, RAYCAST_MAX_DISTANCE, BIWSettings.GROUND_LAYER))
 74        {
 275            position = hit.point;
 276            return true;
 77        }
 78
 079        position = Vector3.zero;
 080        return false;
 81    }
 82
 83    public bool Raycast(Vector3 mousePosition, LayerMask mask, out RaycastHit hitInfo, System.Func<RaycastHit[], Raycast
 84    {
 85        RaycastHit[] hits;
 086        hits = Physics.RaycastAll(GetMouseRay(mousePosition), RAYCAST_MAX_DISTANCE, mask);
 087        if (hits.Length > 0)
 88        {
 089            hitInfo = hitComparer(hits);
 090            return true;
 91        }
 92
 093        hitInfo = new RaycastHit();
 094        return false;
 95    }
 96
 97    public VoxelEntityHit GetCloserUnselectedVoxelEntityOnPointer()
 98    {
 99        RaycastHit[] hits;
 0100        UnityEngine.Ray ray = GetMouseRay(Input.mousePosition);
 101
 0102        float currentDistance = 9999;
 0103        VoxelEntityHit voxelEntityHit = null;
 104
 0105        hits = Physics.RaycastAll(ray, BIWSettings.RAYCAST_MAX_DISTANCE, BIWSettings.COLLIDER_SELECTION_LAYER);
 106
 0107        foreach (RaycastHit hit in hits)
 108        {
 0109            string entityID = hit.collider.gameObject.name;
 110
 0111            if (sceneToEdit.entities.ContainsKey(entityID))
 112            {
 0113                BIWEntity entityToCheck = entityHandler.GetConvertedEntity(sceneToEdit.entities[entityID]);
 114
 0115                if (entityToCheck == null)
 116                    continue;
 117
 0118                if (entityToCheck.isSelected || !entityToCheck.gameObject.CompareTag(BIWSettings.VOXEL_TAG))
 119                    continue;
 120
 0121                Camera camera = Camera.main;
 0122                if (Vector3.Distance(camera.transform.position, entityToCheck.rootEntity.gameObject.transform.position) 
 123                    continue;
 124
 0125                voxelEntityHit = new VoxelEntityHit(entityToCheck, hit);
 0126                currentDistance = Vector3.Distance(camera.transform.position, entityToCheck.rootEntity.gameObject.transf
 127            }
 128        }
 129
 0130        return voxelEntityHit;
 131    }
 132
 133    public Vector3 GetFloorPointAtMouse(Vector3 mousePosition)
 134    {
 1135        Camera camera = context.sceneReferences.mainCamera;
 136
 1137        if ( camera == null )
 0138            return Vector3.zero;
 139
 140        RaycastHit hit;
 1141        UnityEngine.Ray ray = camera.ScreenPointToRay(mousePosition);
 142
 1143        if (Physics.Raycast(ray, out hit, RAYCAST_MAX_DISTANCE, BIWSettings.GROUND_LAYER))
 1144            return hit.point;
 145
 0146        return Vector3.zero;
 147    }
 148
 2149    public Ray GetMouseRay(Vector3 mousePosition) { return builderCamera.ScreenPointToRay(mousePosition); }
 150
 151    #region Gizmos
 152
 153    private void CheckGizmosRaycast(Vector3 mousePosition)
 154    {
 155        RaycastHit hit;
 0156        if (Raycast(mousePosition, gizmoMask, out hit, CompareSelectionHit))
 157        {
 0158            BIWGizmosAxis gizmosAxis = hit.collider.gameObject.GetComponent<BIWGizmosAxis>();
 0159            if (gizmosAxis != null)
 0160                OnGizmosAxisPressed?.Invoke(gizmosAxis);
 161        }
 0162    }
 163
 0164    public bool RaycastToGizmos(Ray ray, out RaycastHit hitInfo) { return Physics.Raycast(ray, out hitInfo, RAYCAST_MAX_
 165
 0166    public bool RaycastToGizmos(Vector3 mousePosition, out RaycastHit hitInfo) { return RaycastToGizmos(GetMouseRay(mous
 167
 168    private RaycastHit CompareSelectionHit(RaycastHit[] hits)
 169    {
 0170        RaycastHit closestHit = hits[0];
 171
 0172        if (IsGizmoHit(closestHit)) // Gizmos has always priority
 0173            return closestHit;
 174
 175        return closestHit;
 176    }
 177
 0178    private bool IsGizmoHit(RaycastHit hit) { return hit.collider.gameObject.GetComponent<BIWGizmosAxis>() != null; }
 179
 180    #endregion
 181}