< Summary

Class:SceneBoundsFeedbackStyle_BIW
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/SceneBoundariesController/SceneBoundsFeedbackStyle_BIW.cs
Covered lines:7
Uncovered lines:54
Coverable lines:61
Total lines:138
Line coverage:11.4% (7 of 61)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
InvalidMeshInfo(...)0%2100%
ResetMaterials()0%20400%
SceneBoundsFeedbackStyle_BIW()0%110100%
ApplyFeedback(...)0%6200%
CleanFeedback()0%2.152066.67%
GetOriginalMaterials(...)0%6200%
RemoveInvalidMeshEffect(...)0%56700%
AddInvalidMeshEffect(...)0%30500%
WasGameObjectInAValidPosition(...)0%2100%

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using DCL;
 3using DCL.Controllers;
 4using DCL.Helpers;
 5using DCL.Models;
 6using UnityEngine;
 7using UnityEngine.XR;
 8
 9public class SceneBoundsFeedbackStyle_BIW : ISceneBoundsFeedbackStyle
 10{
 11    class InvalidMeshInfo
 12    {
 013        public List<GameObject> wireframeObjects = new List<GameObject>();
 14        public MeshesInfo meshesInfo;
 15        public System.Action OnResetMaterials;
 16
 017        public InvalidMeshInfo(MeshesInfo meshesInfo) { this.meshesInfo = meshesInfo; }
 18
 19        public void ResetMaterials()
 20        {
 021            if (meshesInfo.meshRootGameObject == null)
 022                return;
 23
 024            int wireframeObjectscount = wireframeObjects.Count;
 025            for (int i = 0; i < wireframeObjectscount; i++)
 26            {
 027                Utils.SafeDestroy(wireframeObjects[i]);
 28            }
 29
 030            OnResetMaterials?.Invoke();
 031        }
 32    }
 33
 34    const string WIREFRAME_PREFAB_NAME = "Prefabs/WireframeCubeMesh";
 35
 936    Dictionary<GameObject, InvalidMeshInfo> invalidMeshesInfo = new Dictionary<GameObject, InvalidMeshInfo>();
 937    HashSet<Renderer> invalidSubmeshes = new HashSet<Renderer>();
 938    private readonly List<MeshesInfo> currentMeshesInvalidated = new List<MeshesInfo>();
 39
 2740    public SceneBoundsFeedbackStyle_BIW() { invalidMeshesInfo = new Dictionary<GameObject, InvalidMeshInfo>(); }
 41
 42    public void ApplyFeedback(MeshesInfo meshesInfo, bool isInsideBoundaries)
 43    {
 044        if (isInsideBoundaries)
 45        {
 046            RemoveInvalidMeshEffect(meshesInfo);
 047            return;
 48        }
 49
 050        AddInvalidMeshEffect(meshesInfo);
 051    }
 52
 53    public void CleanFeedback()
 54    {
 455        for (int i = 0; i < currentMeshesInvalidated.Count; i++)
 56        {
 057            RemoveInvalidMeshEffect(currentMeshesInvalidated[i]);
 58        }
 59
 260        currentMeshesInvalidated.Clear();
 261    }
 62
 63    public List<Material> GetOriginalMaterials(MeshesInfo meshesInfo)
 64    {
 065        List<Material> result = new List<Material>();
 66
 067        for (int i = 0; i < meshesInfo.renderers.Length; i++)
 68        {
 069            result.AddRange(meshesInfo.renderers[i].sharedMaterials);
 70        }
 71
 072        return result;
 73    }
 74
 75    void RemoveInvalidMeshEffect(MeshesInfo meshesInfo)
 76    {
 077        if (meshesInfo == null || WasGameObjectInAValidPosition(meshesInfo.innerGameObject))
 078            return;
 79
 080        PoolableObject shapePoolableObjectBehaviour = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject);
 81
 082        if (shapePoolableObjectBehaviour != null)
 083            shapePoolableObjectBehaviour.OnRelease -= invalidMeshesInfo[meshesInfo.innerGameObject].ResetMaterials;
 84
 085        var renderers = meshesInfo.renderers;
 86
 087        if (renderers != null)
 88        {
 089            for (int i = 0; i < renderers.Length; i++)
 90            {
 091                if (invalidSubmeshes.Contains(renderers[i]))
 092                    invalidSubmeshes.Remove(renderers[i]);
 93            }
 94        }
 95
 096        invalidMeshesInfo[meshesInfo.innerGameObject].ResetMaterials();
 097        currentMeshesInvalidated.Remove(meshesInfo);
 098    }
 99
 100    void AddInvalidMeshEffect(MeshesInfo meshesInfo)
 101    {
 0102        if (currentMeshesInvalidated.Contains(meshesInfo))
 0103            return;
 0104        if (!WasGameObjectInAValidPosition(meshesInfo.innerGameObject))
 0105            return;
 106
 0107        InvalidMeshInfo invalidMeshInfo = new InvalidMeshInfo(meshesInfo);
 108
 0109        invalidMeshInfo.OnResetMaterials = () => { invalidMeshesInfo.Remove(meshesInfo.innerGameObject); };
 110
 0111        PoolableObject shapePoolableObjectBehaviour = PoolManager.i.GetPoolable(meshesInfo.meshRootGameObject);
 0112        if (shapePoolableObjectBehaviour != null)
 113        {
 0114            shapePoolableObjectBehaviour.OnRelease -= invalidMeshInfo.ResetMaterials;
 0115            shapePoolableObjectBehaviour.OnRelease += invalidMeshInfo.ResetMaterials;
 116        }
 117
 118        // Apply invalid mesh
 0119        Renderer[] entityRenderers = meshesInfo.renderers;
 0120        for (int i = 0; i < entityRenderers.Length; i++)
 121        {
 122            // Wireframe that shows the boundaries to the dev (We don't use the GameObject.Instantiate(prefab, parent)
 123            // overload because we need to set the position and scale before parenting, to deal with scaled objects)
 0124            GameObject wireframeObject = GameObject.Instantiate(Resources.Load<GameObject>(WIREFRAME_PREFAB_NAME));
 0125            wireframeObject.transform.position = entityRenderers[i].bounds.center;
 0126            wireframeObject.transform.localScale = entityRenderers[i].bounds.size * 1.01f;
 0127            wireframeObject.transform.SetParent(meshesInfo.innerGameObject.transform);
 128
 0129            invalidMeshInfo.wireframeObjects.Add(wireframeObject);
 0130            entityRenderers[i].enabled = true;
 131        }
 132
 0133        currentMeshesInvalidated.Add(meshesInfo);
 0134        invalidMeshesInfo.Add(meshesInfo.innerGameObject, invalidMeshInfo);
 0135    }
 136
 0137    public bool WasGameObjectInAValidPosition(GameObject gameObject) { return !invalidMeshesInfo.ContainsKey(gameObject)
 138}