< 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:59
Uncovered lines:14
Coverable lines:73
Total lines:169
Line coverage:80.8% (59 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%20400%
RemoveInvalidMeshEffect(...)0%9.059091.67%
AddInvalidMeshEffect(...)0%8.088089.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        {
 2213            public List<GameObject> wireframeObjects = new List<GameObject>();
 14            public MeshesInfo meshesInfo;
 15            public System.Action OnResetAll;
 16
 6617            public InvalidMeshInfo(MeshesInfo meshesInfo) { this.meshesInfo = meshesInfo; }
 18
 19            public void ResetAll()
 20            {
 2221                if (meshesInfo.meshRootGameObject == null)
 022                    return;
 23
 2224                int count = wireframeObjects.Count;
 25
 13626                for (int i = 0; i < count; i++)
 27                {
 4628                    Utils.SafeDestroy(wireframeObjects[i]);
 29                }
 30
 2231                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
 2640        public SceneBoundsFeedbackStyle_RedBox()
 41        {
 2642            invalidMeshesInfo = new Dictionary<GameObject, InvalidMeshInfo>();
 2643            invalidObjects = new HashSet<Bounds>();
 2644        }
 45
 46        public void ApplyFeedback(MeshesInfo meshesInfo, bool isInsideBoundaries)
 47        {
 8248            if (isInsideBoundaries)
 3549                RemoveInvalidMeshEffect(meshesInfo);
 50            else
 4751                AddInvalidMeshEffect(meshesInfo);
 4752        }
 53
 54        public void CleanFeedback()
 55        {
 056            IEnumerable<MeshesInfo> distinctMeshesInfo = invalidMeshesInfo.Values.Select(x => x.meshesInfo).Distinct();
 57
 058            foreach (var info in distinctMeshesInfo)
 59            {
 060                RemoveInvalidMeshEffect(info);
 61            }
 62
 063            invalidMeshesInfo.Clear();
 064            invalidObjects.Clear();
 065        }
 66
 67        void RemoveInvalidMeshEffect(MeshesInfo meshesInfo)
 68        {
 3569            if (meshesInfo == null)
 070                return;
 71
 3572            if (!invalidMeshesInfo.ContainsKey(meshesInfo.innerGameObject))
 1373                return;
 74
 2275            PoolableObject poolableObject = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject);
 76
 2277            if (poolableObject != null)
 078                poolableObject.OnRelease -= invalidMeshesInfo[meshesInfo.innerGameObject].ResetAll;
 79
 2280            var renderers = meshesInfo.renderers;
 81
 2282            if (renderers != null)
 83            {
 9884                for (int i = 0; i < renderers.Length; i++)
 85                {
 2786                    Bounds target = renderers[i].bounds;
 87
 2788                    if (invalidObjects.Contains(target))
 2189                        invalidObjects.Remove(target);
 90                }
 91            }
 92
 10093            foreach (Collider collider in meshesInfo.colliders)
 94            {
 2895                Bounds target = collider.bounds;
 96
 2897                if (invalidObjects.Contains(target))
 698                    invalidObjects.Remove(target);
 99            }
 100
 22101            invalidMeshesInfo[meshesInfo.innerGameObject].ResetAll();
 22102            invalidMeshesInfo.Remove(meshesInfo.innerGameObject);
 22103        }
 104
 105        void AddInvalidMeshEffect(MeshesInfo meshesInfo)
 106        {
 47107            if (meshesInfo == null)
 0108                return;
 109
 47110            if (invalidMeshesInfo.ContainsKey(meshesInfo.innerGameObject))
 25111                return;
 112
 22113            InvalidMeshInfo invalidMeshInfo = new InvalidMeshInfo(meshesInfo);
 22114            PoolableObject poolableObject = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject);
 115
 22116            if (poolableObject != null)
 117            {
 0118                poolableObject.OnRelease -= invalidMeshInfo.ResetAll;
 0119                poolableObject.OnRelease += invalidMeshInfo.ResetAll;
 120            }
 121
 122            // Apply invalid effect
 22123            Renderer[] entityRenderers = meshesInfo.renderers;
 124
 98125            for (int i = 0; i < entityRenderers.Length; i++)
 126            {
 27127                Bounds target = entityRenderers[i].bounds;
 128
 27129                if (invalidObjects.Contains(target))
 130                    continue;
 131
 25132                var box = PutBoxAroundObject(target, meshesInfo.innerGameObject.transform);
 133
 25134                invalidMeshInfo.wireframeObjects.Add(box);
 25135                invalidObjects.Add(target);
 136            }
 137
 100138            foreach (Collider collider in meshesInfo.colliders)
 139            {
 28140                Bounds target = collider.bounds;
 141
 28142                if (invalidObjects.Contains(target))
 143                    continue;
 144
 21145                var box = PutBoxAroundObject(target, meshesInfo.innerGameObject.transform);
 146
 21147                invalidMeshInfo.wireframeObjects.Add(box);
 21148                invalidObjects.Add(target);
 149            }
 150
 22151            invalidMeshesInfo.Add(meshesInfo.innerGameObject, invalidMeshInfo);
 22152        }
 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)
 46158            GameObject wireframeObject = Object.Instantiate(Resources.Load<GameObject>(WIREFRAME_PREFAB_NAME));
 46159            wireframeObject.transform.position = target.center;
 46160            wireframeObject.transform.localScale = target.size * 1.01f;
 46161            wireframeObject.transform.SetParent(parent);
 46162            return wireframeObject;
 163        }
 164        public List<Material> GetOriginalMaterials(MeshesInfo meshesInfo)
 165        {
 0166            return meshesInfo.renderers.SelectMany((x) => x.sharedMaterials).ToList();
 167        }
 168    }
 169}