< 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 IWebRequestAsyncOperation loadOp;
 21
 22        public virtual IWebRequestAsyncOperation 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        {
 14746            if (owner == null)
 047                return;
 48
 14749            if (rt == null)
 14750                rt = transform as RectTransform;
 51
 14752            Vector2 myMinCoords = rt.TransformPoint(new Vector3(rt.rect.xMin, rt.rect.yMin));
 14753            Vector2 myMaxCoords = rt.TransformPoint(new Vector3(rt.rect.xMax, rt.rect.yMax));
 54
 14755            Vector2 viewMinCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMin, owner.viewport.r
 14756            Vector2 viewMaxCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMax, owner.viewport.r
 57
 58#if UNITY_EDITOR
 14759            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
 14765            float size = (viewMaxCoords - viewMinCoords).magnitude;
 66
 14767            Rect viewportRect = new Rect(viewMinCoords, viewMaxCoords - viewMinCoords);
 14768            viewportRect.min -= Vector2.one * size;
 14769            viewportRect.max += Vector2.one * size;
 70
 71#if UNITY_EDITOR
 14772            if (VERBOSE)
 73            {
 074                Utils.DrawRectGizmo(viewportRect, Color.blue, 5f);
 75            }
 76#endif
 77
 14778            Rect myRect = new Rect(myMinCoords, myMaxCoords - myMinCoords);
 14779            bool visible = viewportRect.Overlaps(myRect, true);
 80
 14781            targetImage.enabled = visible;
 82
 14783            if (!isLoadingOrLoaded)
 84            {
 14785                loadOp = LoadChunkImage();
 86            }
 14787        }
 88
 89        private void OnDestroy()
 90        {
 63791            if (loadOp != null)
 14792                loadOp.Dispose();
 63793        }
 94    }
 95}