< 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:63
Uncovered lines:10
Coverable lines:73
Total lines:169
Line coverage:86.3% (63 of 73)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
InvalidMeshInfo(...)0%110100%
ResetAll()0%4.184077.78%
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        {
 2513            public List<GameObject> wireframeObjects = new List<GameObject>();
 14            public MeshesInfo meshesInfo;
 15            public System.Action OnResetAll;
 16
 7517            public InvalidMeshInfo(MeshesInfo meshesInfo) { this.meshesInfo = meshesInfo; }
 18
 19            public void ResetAll()
 20            {
 2521                if (meshesInfo.meshRootGameObject == null)
 022                    return;
 23
 2524                int count = wireframeObjects.Count;
 25
 16626                for (int i = 0; i < count; i++)
 27                {
 5828                    Utils.SafeDestroy(wireframeObjects[i]);
 29                }
 30
 2531                OnResetAll?.Invoke();
 032            }
 33        }
 34
 35        const string WIREFRAME_PREFAB_NAME = "Prefabs/WireframeCubeMesh";
 36
 37        private Dictionary<GameObject, InvalidMeshInfo> invalidMeshesInfo;
 38        private HashSet<Bounds> invalidObjects;
 39
 4740        public SceneBoundsFeedbackStyle_RedBox()
 41        {
 4742            invalidMeshesInfo = new Dictionary<GameObject, InvalidMeshInfo>();
 4743            invalidObjects = new HashSet<Bounds>();
 4744        }
 45
 46        public void ApplyFeedback(MeshesInfo meshesInfo, bool isInsideBoundaries)
 47        {
 9448            if (isInsideBoundaries)
 3949                RemoveInvalidMeshEffect(meshesInfo);
 50            else
 5551                AddInvalidMeshEffect(meshesInfo);
 5552        }
 53
 54        public void CleanFeedback()
 55        {
 1656            IEnumerable<MeshesInfo> distinctMeshesInfo = invalidMeshesInfo.Values.Select(x => x.meshesInfo).Distinct();
 57
 3258            foreach (var info in distinctMeshesInfo)
 59            {
 060                RemoveInvalidMeshEffect(info);
 61            }
 62
 1663            invalidMeshesInfo.Clear();
 1664            invalidObjects.Clear();
 1665        }
 66
 67        void RemoveInvalidMeshEffect(MeshesInfo meshesInfo)
 68        {
 3969            if (meshesInfo == null)
 070                return;
 71
 3972            if (meshesInfo.innerGameObject == null || !invalidMeshesInfo.ContainsKey(meshesInfo.innerGameObject))
 1473                return;
 74
 2575            PoolableObject poolableObject = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject);
 76
 2577            if (poolableObject != null)
 078                poolableObject.OnRelease -= invalidMeshesInfo[meshesInfo.innerGameObject].ResetAll;
 79
 2580            var renderers = meshesInfo.renderers;
 81
 2582            if (renderers != null)
 83            {
 11684                for (int i = 0; i < renderers.Length; i++)
 85                {
 3386                    Bounds target = renderers[i].bounds;
 87
 3388                    if (invalidObjects.Contains(target))
 1889                        invalidObjects.Remove(target);
 90                }
 91            }
 92
 11893            foreach (Collider collider in meshesInfo.colliders)
 94            {
 3495                Bounds target = collider.bounds;
 96
 3497                if (invalidObjects.Contains(target))
 098                    invalidObjects.Remove(target);
 99            }
 100
 25101            invalidMeshesInfo[meshesInfo.innerGameObject].ResetAll();
 25102            invalidMeshesInfo.Remove(meshesInfo.innerGameObject);
 25103        }
 104
 105        void AddInvalidMeshEffect(MeshesInfo meshesInfo)
 106        {
 55107            if (meshesInfo == null)
 0108                return;
 109
 55110            if (meshesInfo.innerGameObject == null || invalidMeshesInfo.ContainsKey(meshesInfo.innerGameObject))
 30111                return;
 112
 25113            InvalidMeshInfo invalidMeshInfo = new InvalidMeshInfo(meshesInfo);
 25114            PoolableObject poolableObject = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject);
 115
 25116            if (poolableObject != null)
 117            {
 0118                poolableObject.OnRelease -= invalidMeshInfo.ResetAll;
 0119                poolableObject.OnRelease += invalidMeshInfo.ResetAll;
 120            }
 121
 122            // Apply invalid effect
 25123            Renderer[] entityRenderers = meshesInfo.renderers;
 124
 116125            for (int i = 0; i < entityRenderers.Length; i++)
 126            {
 33127                Bounds target = entityRenderers[i].bounds;
 128
 33129                if (invalidObjects.Contains(target))
 130                    continue;
 131
 31132                var box = PutBoxAroundObject(target, meshesInfo.innerGameObject.transform);
 133
 31134                invalidMeshInfo.wireframeObjects.Add(box);
 31135                invalidObjects.Add(target);
 136            }
 137
 118138            foreach (Collider collider in meshesInfo.colliders)
 139            {
 34140                Bounds target = collider.bounds;
 141
 34142                if (invalidObjects.Contains(target))
 143                    continue;
 144
 27145                var box = PutBoxAroundObject(target, meshesInfo.innerGameObject.transform);
 146
 27147                invalidMeshInfo.wireframeObjects.Add(box);
 27148                invalidObjects.Add(target);
 149            }
 150
 25151            invalidMeshesInfo.Add(meshesInfo.innerGameObject, invalidMeshInfo);
 25152        }
 153
 154        private GameObject PutBoxAroundObject(Bounds target, Transform parent)
 155        {
 156            // Wireframe that shows the boundaries to the dev (We don't use the GameObject.Instantiate(prefab, parent)
 157            // overload because we need to set the position and scale before parenting, to deal with scaled objects)
 58158            GameObject wireframeObject = Object.Instantiate(Resources.Load<GameObject>(WIREFRAME_PREFAB_NAME));
 58159            wireframeObject.transform.position = target.center;
 58160            wireframeObject.transform.localScale = target.size * 1.01f;
 58161            wireframeObject.transform.SetParent(parent);
 58162            return wireframeObject;
 163        }
 164        public List<Material> GetOriginalMaterials(MeshesInfo meshesInfo)
 165        {
 0166            return meshesInfo.renderers.SelectMany((x) => x.sharedMaterials).ToList();
 167        }
 168    }
 169}