< Summary

Class:DCL.Controllers.SceneBoundsFeedbackStyle_RedBox
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/SceneBoundariesController/SceneBoundsFeedbackStyle_RedBox.cs
Covered lines:47
Uncovered lines:15
Coverable lines:62
Total lines:147
Line coverage:75.8% (47 of 62)
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%7.087088.24%
AddInvalidMeshEffect(...)0%6.346078.95%
PutBoxAroundRenderer(...)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        {
 1113            public List<GameObject> wireframeObjects = new List<GameObject>();
 14            public MeshesInfo meshesInfo;
 15            public System.Action OnResetAll;
 16
 3317            public InvalidMeshInfo(MeshesInfo meshesInfo) { this.meshesInfo = meshesInfo; }
 18
 19            public void ResetAll()
 20            {
 421                if (meshesInfo.meshRootGameObject == null)
 022                    return;
 23
 424                int count = wireframeObjects.Count;
 25
 1626                for (int i = 0; i < count; i++)
 27                {
 428                    Utils.SafeDestroy(wireframeObjects[i]);
 29                }
 30
 431                OnResetAll?.Invoke();
 032            }
 33        }
 34
 35        const string WIREFRAME_PREFAB_NAME = "Prefabs/WireframeCubeMesh";
 36
 37        private Dictionary<GameObject, InvalidMeshInfo> invalidMeshesInfo;
 38        private HashSet<Renderer> invalidRenderers;
 39
 2040        public SceneBoundsFeedbackStyle_RedBox()
 41        {
 2042            invalidMeshesInfo = new Dictionary<GameObject, InvalidMeshInfo>();
 2043            invalidRenderers = new HashSet<Renderer>();
 2044        }
 45
 46        public void ApplyFeedback(MeshesInfo meshesInfo, bool isInsideBoundaries)
 47        {
 2448            if (isInsideBoundaries)
 49            {
 1350                RemoveInvalidMeshEffect(meshesInfo);
 1351                return;
 52            }
 53
 1154            AddInvalidMeshEffect(meshesInfo);
 1155        }
 56
 57        public void CleanFeedback()
 58        {
 059            IEnumerable<MeshesInfo> distinctMeshesInfo = invalidMeshesInfo.Values.Select(x => x.meshesInfo).Distinct();
 60
 061            foreach (var info in distinctMeshesInfo)
 62            {
 063                RemoveInvalidMeshEffect(info);
 64            }
 65
 066            invalidMeshesInfo.Clear();
 067            invalidRenderers.Clear();
 068        }
 69
 70        void RemoveInvalidMeshEffect(MeshesInfo meshesInfo)
 71        {
 1372            if (meshesInfo == null)
 073                return;
 74
 1375            if (!invalidMeshesInfo.ContainsKey(meshesInfo.innerGameObject))
 976                return;
 77
 478            PoolableObject poolableObject = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject);
 79
 480            if (poolableObject != null)
 081                poolableObject.OnRelease -= invalidMeshesInfo[meshesInfo.innerGameObject].ResetAll;
 82
 483            var renderers = meshesInfo.renderers;
 84
 485            if (renderers != null)
 86            {
 1687                for (int i = 0; i < renderers.Length; i++)
 88                {
 489                    if (invalidRenderers.Contains(renderers[i]))
 490                        invalidRenderers.Remove(renderers[i]);
 91                }
 92            }
 93
 494            invalidMeshesInfo[meshesInfo.innerGameObject].ResetAll();
 495            invalidMeshesInfo.Remove(meshesInfo.innerGameObject);
 496        }
 97
 98        void AddInvalidMeshEffect(MeshesInfo meshesInfo)
 99        {
 11100            if (meshesInfo == null)
 0101                return;
 102
 11103            if (invalidMeshesInfo.ContainsKey(meshesInfo.innerGameObject))
 0104                return;
 105
 11106            InvalidMeshInfo invalidMeshInfo = new InvalidMeshInfo(meshesInfo);
 11107            PoolableObject poolableObject = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject);
 108
 11109            if (poolableObject != null)
 110            {
 0111                poolableObject.OnRelease -= invalidMeshInfo.ResetAll;
 0112                poolableObject.OnRelease += invalidMeshInfo.ResetAll;
 113            }
 114
 115            // Apply invalid effect
 11116            Renderer[] entityRenderers = meshesInfo.renderers;
 117
 44118            for (int i = 0; i < entityRenderers.Length; i++)
 119            {
 11120                if (invalidRenderers.Contains(entityRenderers[i]))
 121                    continue;
 122
 11123                var box = PutBoxAroundRenderer(entityRenderers[i], meshesInfo.innerGameObject.transform);
 124
 11125                invalidMeshInfo.wireframeObjects.Add(box);
 11126                invalidRenderers.Add(entityRenderers[i]);
 127            }
 128
 11129            invalidMeshesInfo.Add(meshesInfo.innerGameObject, invalidMeshInfo);
 11130        }
 131
 132        private GameObject PutBoxAroundRenderer(Renderer target, Transform parent)
 133        {
 134            // Wireframe that shows the boundaries to the dev (We don't use the GameObject.Instantiate(prefab, parent)
 135            // overload because we need to set the position and scale before parenting, to deal with scaled objects)
 11136            GameObject wireframeObject = Object.Instantiate(Resources.Load<GameObject>(WIREFRAME_PREFAB_NAME));
 11137            wireframeObject.transform.position = target.bounds.center;
 11138            wireframeObject.transform.localScale = target.bounds.size * 1.01f;
 11139            wireframeObject.transform.SetParent(parent);
 11140            return wireframeObject;
 141        }
 142        public List<Material> GetOriginalMaterials(MeshesInfo meshesInfo)
 143        {
 0144            return meshesInfo.renderers.SelectMany((x) => x.sharedMaterials).ToList();
 145        }
 146    }
 147}