< Summary

Class:DCL.Controllers.SceneBoundsFeedbackStyle_RedBox
Assembly:DCL.Runtime
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/SceneBoundariesController/SceneBoundsFeedbackStyle_RedBox.cs
Covered lines:64
Uncovered lines:10
Coverable lines:74
Total lines:170
Line coverage:86.4% (64 of 74)
Covered branches:0
Total branches:0
Covered methods:8
Total methods:9
Method coverage:88.8% (8 of 9)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
InvalidMeshInfo(...)0%110100%
ResetAll()0%4.134080%
SceneBoundsFeedbackStyle_RedBox()0%110100%
ApplyFeedback(...)0%220100%
CleanFeedback()0%4.254075%
RemoveInvalidMeshEffect(...)0%10.210087.5%
AddInvalidMeshEffect(...)0%9.19089.29%
PutBoxAroundObject(...)0%110100%
GetOriginalMaterials(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/SceneBoundariesController/SceneBoundsFeedbackStyle_RedBox.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using DCL.Helpers;
 4using DCL.Models;
 5using UnityEngine;
 6
 7namespace DCL.Controllers
 8{
 9    public class SceneBoundsFeedbackStyle_RedBox : ISceneBoundsFeedbackStyle
 10    {
 11        class InvalidMeshInfo
 12        {
 1913            public List<GameObject> wireframeObjects = new List<GameObject>();
 14            public MeshesInfo meshesInfo;
 15            public System.Action OnResetAll;
 16
 5717            public InvalidMeshInfo(MeshesInfo meshesInfo) { this.meshesInfo = meshesInfo; }
 18
 19            public void ResetAll()
 20            {
 1921                if (meshesInfo.meshRootGameObject == null)
 022                    return;
 23
 1924                int count = wireframeObjects.Count;
 25
 12426                for (int i = 0; i < count; i++)
 27                {
 4328                    wireframeObjects[i].transform.parent = null;
 4329                    Utils.SafeDestroy(wireframeObjects[i]);
 30                }
 31
 1932                OnResetAll?.Invoke();
 033            }
 34        }
 35
 36        const string WIREFRAME_PREFAB_NAME = "Prefabs/WireframeCubeMesh";
 37
 38        private Dictionary<GameObject, InvalidMeshInfo> invalidMeshesInfo;
 39        private HashSet<Bounds> invalidObjects;
 40
 4741        public SceneBoundsFeedbackStyle_RedBox()
 42        {
 4743            invalidMeshesInfo = new Dictionary<GameObject, InvalidMeshInfo>();
 4744            invalidObjects = new HashSet<Bounds>();
 4745        }
 46
 47        public void ApplyFeedback(MeshesInfo meshesInfo, bool isInsideBoundaries)
 48        {
 9849            if (isInsideBoundaries)
 4850                RemoveInvalidMeshEffect(meshesInfo);
 51            else
 5052                AddInvalidMeshEffect(meshesInfo);
 5053        }
 54
 55        public void CleanFeedback()
 56        {
 1657            IEnumerable<MeshesInfo> distinctMeshesInfo = invalidMeshesInfo.Values.Select(x => x.meshesInfo).Distinct();
 58
 3259            foreach (var info in distinctMeshesInfo)
 60            {
 061                RemoveInvalidMeshEffect(info);
 62            }
 63
 1664            invalidMeshesInfo.Clear();
 1665            invalidObjects.Clear();
 1666        }
 67
 68        void RemoveInvalidMeshEffect(MeshesInfo meshesInfo)
 69        {
 4870            if (meshesInfo == null)
 071                return;
 72
 4873            if (meshesInfo.innerGameObject == null || !invalidMeshesInfo.ContainsKey(meshesInfo.innerGameObject))
 2974                return;
 75
 1976            PoolableObject poolableObject = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject);
 77
 1978            if (poolableObject != null)
 079                poolableObject.OnRelease -= invalidMeshesInfo[meshesInfo.innerGameObject].ResetAll;
 80
 1981            var renderers = meshesInfo.renderers;
 82
 1983            if (renderers != null)
 84            {
 9285                for (int i = 0; i < renderers.Length; i++)
 86                {
 2787                    Bounds target = renderers[i].bounds;
 88
 2789                    if (invalidObjects.Contains(target))
 1390                        invalidObjects.Remove(target);
 91                }
 92            }
 93
 9494            foreach (Collider collider in meshesInfo.colliders)
 95            {
 2896                Bounds target = collider.bounds;
 97
 2898                if (invalidObjects.Contains(target))
 099                    invalidObjects.Remove(target);
 100            }
 101
 19102            invalidMeshesInfo[meshesInfo.innerGameObject].ResetAll();
 19103            invalidMeshesInfo.Remove(meshesInfo.innerGameObject);
 19104        }
 105
 106        void AddInvalidMeshEffect(MeshesInfo meshesInfo)
 107        {
 50108            if (meshesInfo == null)
 0109                return;
 110
 50111            if (meshesInfo.innerGameObject == null || invalidMeshesInfo.ContainsKey(meshesInfo.innerGameObject))
 31112                return;
 113
 19114            InvalidMeshInfo invalidMeshInfo = new InvalidMeshInfo(meshesInfo);
 19115            PoolableObject poolableObject = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject);
 116
 19117            if (poolableObject != null)
 118            {
 0119                poolableObject.OnRelease -= invalidMeshInfo.ResetAll;
 0120                poolableObject.OnRelease += invalidMeshInfo.ResetAll;
 121            }
 122
 123            // Apply invalid effect
 19124            Renderer[] entityRenderers = meshesInfo.renderers;
 125
 92126            for (int i = 0; i < entityRenderers.Length; i++)
 127            {
 27128                Bounds target = entityRenderers[i].bounds;
 129
 27130                if (invalidObjects.Contains(target))
 131                    continue;
 132
 25133                var box = PutBoxAroundObject(target, meshesInfo.innerGameObject.transform);
 134
 25135                invalidMeshInfo.wireframeObjects.Add(box);
 25136                invalidObjects.Add(target);
 137            }
 138
 94139            foreach (Collider collider in meshesInfo.colliders)
 140            {
 28141                Bounds target = collider.bounds;
 142
 28143                if (invalidObjects.Contains(target))
 144                    continue;
 145
 18146                var box = PutBoxAroundObject(target, meshesInfo.innerGameObject.transform);
 147
 18148                invalidMeshInfo.wireframeObjects.Add(box);
 18149                invalidObjects.Add(target);
 150            }
 151
 19152            invalidMeshesInfo.Add(meshesInfo.innerGameObject, invalidMeshInfo);
 19153        }
 154
 155        private GameObject PutBoxAroundObject(Bounds target, Transform parent)
 156        {
 157            // Wireframe that shows the boundaries to the dev (We don't use the GameObject.Instantiate(prefab, parent)
 158            // overload because we need to set the position and scale before parenting, to deal with scaled objects)
 43159            GameObject wireframeObject = Object.Instantiate(Resources.Load<GameObject>(WIREFRAME_PREFAB_NAME));
 43160            wireframeObject.transform.position = target.center;
 43161            wireframeObject.transform.localScale = target.size * 1.01f;
 43162            wireframeObject.transform.SetParent(parent);
 43163            return wireframeObject;
 164        }
 165        public List<Material> GetOriginalMaterials(MeshesInfo meshesInfo)
 166        {
 0167            return meshesInfo.renderers.SelectMany((x) => x.sharedMaterials).ToList();
 168        }
 169    }
 170}