< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
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
 22        public virtual WebRequestAsyncOperation LoadChunkImage()
 23        {
 14724            isLoadingOrLoaded = true;
 25
 14726            string url = $"{MAP_API_BASE}?center={center.x},{center.y}&width={size.x}&height={size.y}&size={tileSize}";
 27
 14728            Texture2D result = null;
 29
 14730            return Utils.FetchTexture(url, false, (x) =>
 31            {
 032                result = x;
 33
 034                if (result == null)
 035                    return;
 36
 037                targetImage.texture = result;
 038                targetImage.texture.wrapMode = TextureWrapMode.Clamp;
 039                targetImage.SetNativeSize();
 040                targetImage.color = Color.white;
 041            });
 42        }
 43
 44        public void UpdateCulling()
 45        {
 24546            if (owner == null)
 047                return;
 48
 24549            if (rt == null)
 14750                rt = transform as RectTransform;
 51
 24552            Vector2 myMinCoords = rt.TransformPoint(new Vector3(rt.rect.xMin, rt.rect.yMin));
 24553            Vector2 myMaxCoords = rt.TransformPoint(new Vector3(rt.rect.xMax, rt.rect.yMax));
 54
 24555            Vector2 viewMinCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMin, owner.viewport.r
 24556            Vector2 viewMaxCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMax, owner.viewport.r
 57
 58#if UNITY_EDITOR
 24559            if (VERBOSE)
 60            {
 061                var rtWorldRect = new Rect(myMinCoords.x, myMinCoords.y, myMaxCoords.x - myMinCoords.x, myMaxCoords.y - 
 062                Utils.DrawRectGizmo(rtWorldRect, Color.red, 5f);
 63            }
 64#endif
 24565            float size = (viewMaxCoords - viewMinCoords).magnitude;
 66
 24567            Rect viewportRect = new Rect(viewMinCoords, viewMaxCoords - viewMinCoords);
 24568            viewportRect.min -= Vector2.one * size;
 24569            viewportRect.max += Vector2.one * size;
 70
 71#if UNITY_EDITOR
 24572            if (VERBOSE)
 73            {
 074                Utils.DrawRectGizmo(viewportRect, Color.blue, 5f);
 75            }
 76#endif
 77
 24578            Rect myRect = new Rect(myMinCoords, myMaxCoords - myMinCoords);
 24579            bool visible = viewportRect.Overlaps(myRect, true);
 80
 24581            targetImage.enabled = visible;
 82
 24583            if (!isLoadingOrLoaded)
 84            {
 14785                loadOp = LoadChunkImage();
 86            }
 24587        }
 88
 89        private void OnDestroy()
 90        {
 63791            if (loadOp != null)
 9892                loadOp.Dispose();
 63793        }
 94    }
 95}