< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Start()0%110100%
LoadChunkImage()0%2100%
UpdateCulling()0%42600%
OnDestroy()0%2.152066.67%

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
 98022        private void Start() { targetImage.color = Color.clear; }
 23
 24        public virtual WebRequestAsyncOperation LoadChunkImage()
 25        {
 026            isLoadingOrLoaded = true;
 27
 028            string url = $"{MAP_API_BASE}?center={center.x},{center.y}&width={size.x}&height={size.y}&size={tileSize}";
 29
 030            Texture2D result = null;
 31
 032            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        {
 047            if (owner == null)
 048                return;
 49
 050            if (rt == null)
 051                rt = transform as RectTransform;
 52
 053            Vector2 myMinCoords = rt.TransformPoint(new Vector3(rt.rect.xMin, rt.rect.yMin));
 054            Vector2 myMaxCoords = rt.TransformPoint(new Vector3(rt.rect.xMax, rt.rect.yMax));
 55
 056            Vector2 viewMinCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMin, owner.viewport.r
 057            Vector2 viewMaxCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMax, owner.viewport.r
 58
 59#if UNITY_EDITOR
 060            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
 066            float size = (viewMaxCoords - viewMinCoords).magnitude;
 67
 068            Rect viewportRect = new Rect(viewMinCoords, viewMaxCoords - viewMinCoords);
 069            viewportRect.min -= Vector2.one * size;
 070            viewportRect.max += Vector2.one * size;
 71
 72#if UNITY_EDITOR
 073            if (VERBOSE)
 74            {
 075                Utils.DrawRectGizmo(viewportRect, Color.blue, 5f);
 76            }
 77#endif
 78
 079            Rect myRect = new Rect(myMinCoords, myMaxCoords - myMinCoords);
 080            bool visible = viewportRect.Overlaps(myRect, true);
 81
 082            targetImage.enabled = visible;
 83
 084            if (!isLoadingOrLoaded)
 85            {
 086                loadOp = LoadChunkImage();
 87            }
 088        }
 89
 90        private void OnDestroy()
 91        {
 49092            if (loadOp != null)
 093                loadOp.Dispose();
 49094        }
 95    }
 96}