< Summary

Class:DCL.MapChunk
Assembly:MapRenderer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/MapChunk.cs
Covered lines:27
Uncovered lines:11
Coverable lines:38
Total lines:96
Line coverage:71% (27 of 38)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Start()0%110100%
LoadChunkImage()0%110100%
UpdateCulling()0%6.196082.61%
OnDestroy()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/MapChunk.cs

#LineLine coverage
 1using DCL.Helpers;
 2using UnityEngine;
 3using UnityEngine.UI;
 4
 5namespace DCL
 6{
 7    public class MapChunk : MonoBehaviour
 8    {
 9        private bool VERBOSE = false;
 10        const string MAP_API_BASE = "https://api.decentraland.org/v1/map.png";
 11
 12        public RawImage targetImage;
 13
 14        [System.NonSerialized] public Vector2Int center;
 15        [System.NonSerialized] public Vector2Int size;
 16        [System.NonSerialized] public int tileSize;
 17        [System.NonSerialized] public MapAtlas owner;
 18        protected RectTransform rt;
 19        protected bool isLoadingOrLoaded = false;
 20        private WebRequestAsyncOperation loadOp;
 21
 1087822        private void Start() { targetImage.color = Color.clear; }
 23
 24        public virtual WebRequestAsyncOperation LoadChunkImage()
 25        {
 4926            isLoadingOrLoaded = true;
 27
 4928            string url = $"{MAP_API_BASE}?center={center.x},{center.y}&width={size.x}&height={size.y}&size={tileSize}";
 29
 4930            Texture2D result = null;
 31
 4932            return Utils.FetchTexture(url, false, (x) =>
 33            {
 034                result = x;
 35
 036                if (result == null)
 037                    return;
 38
 039                targetImage.texture = result;
 040                targetImage.SetNativeSize();
 041                targetImage.color = Color.white;
 042            });
 43        }
 44
 45        public void UpdateCulling()
 46        {
 9847            if (owner == null)
 048                return;
 49
 9850            if (rt == null)
 4951                rt = transform as RectTransform;
 52
 9853            Vector2 myMinCoords = rt.TransformPoint(new Vector3(rt.rect.xMin, rt.rect.yMin));
 9854            Vector2 myMaxCoords = rt.TransformPoint(new Vector3(rt.rect.xMax, rt.rect.yMax));
 55
 9856            Vector2 viewMinCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMin, owner.viewport.r
 9857            Vector2 viewMaxCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMax, owner.viewport.r
 58
 59#if UNITY_EDITOR
 9860            if (VERBOSE)
 61            {
 062                var rtWorldRect = new Rect(myMinCoords.x, myMinCoords.y, myMaxCoords.x - myMinCoords.x, myMaxCoords.y - 
 063                Utils.DrawRectGizmo(rtWorldRect, Color.red, 5f);
 64            }
 65#endif
 9866            float size = (viewMaxCoords - viewMinCoords).magnitude;
 67
 9868            Rect viewportRect = new Rect(viewMinCoords, viewMaxCoords - viewMinCoords);
 9869            viewportRect.min -= Vector2.one * size;
 9870            viewportRect.max += Vector2.one * size;
 71
 72#if UNITY_EDITOR
 9873            if (VERBOSE)
 74            {
 075                Utils.DrawRectGizmo(viewportRect, Color.blue, 5f);
 76            }
 77#endif
 78
 9879            Rect myRect = new Rect(myMinCoords, myMaxCoords - myMinCoords);
 9880            bool visible = viewportRect.Overlaps(myRect, true);
 81
 9882            targetImage.enabled = visible;
 83
 9884            if (!isLoadingOrLoaded)
 85            {
 4986                loadOp = LoadChunkImage();
 87            }
 9888        }
 89
 90        private void OnDestroy()
 91        {
 543992            if (loadOp != null)
 4993                loadOp.Dispose();
 543994        }
 95    }
 96}