< Summary

Class:DCL.MapChunk
Assembly:MapRenderer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MapRenderer/MapChunk.cs
Covered lines:2
Uncovered lines:40
Coverable lines:42
Total lines:100
Line coverage:4.7% (2 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
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
 22        public virtual WebRequestAsyncOperation LoadChunkImage()
 23        {
 024            isLoadingOrLoaded = true;
 25
 026            string url = $"{MAP_API_BASE}?center={center.x},{center.y}&width={size.x}&height={size.y}&size={tileSize}";
 27
 028            Texture2D result = null;
 29
 030            return Utils.FetchTexture(url, true, (x) =>
 31            {
 032                result = x;
 33
 034                if (result == null)
 035                    return;
 36
 037                var newTexture = new Texture2D(result.width, result.height, result.format, true);
 038                newTexture.SetPixels32(result.GetPixels32(0), 0);
 039                newTexture.Apply(true);
 040                Destroy(result);
 41
 042                targetImage.texture = newTexture;
 043                targetImage.texture.wrapMode = TextureWrapMode.Clamp;
 044                targetImage.SetNativeSize();
 045                targetImage.color = Color.white;
 046            });
 47        }
 48
 49        public void UpdateCulling()
 50        {
 051            if (owner == null)
 052                return;
 53
 054            if (rt == null)
 055                rt = transform as RectTransform;
 56
 057            Vector2 myMinCoords = rt.TransformPoint(new Vector3(rt.rect.xMin, rt.rect.yMin));
 058            Vector2 myMaxCoords = rt.TransformPoint(new Vector3(rt.rect.xMax, rt.rect.yMax));
 59
 060            Vector2 viewMinCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMin, owner.viewport.r
 061            Vector2 viewMaxCoords = owner.viewport.TransformPoint(new Vector3(owner.viewport.rect.xMax, owner.viewport.r
 62
 63#if UNITY_EDITOR
 064            if (VERBOSE)
 65            {
 066                var rtWorldRect = new Rect(myMinCoords.x, myMinCoords.y, myMaxCoords.x - myMinCoords.x, myMaxCoords.y - 
 067                Utils.DrawRectGizmo(rtWorldRect, Color.red, 5f);
 68            }
 69#endif
 070            float size = (viewMaxCoords - viewMinCoords).magnitude;
 71
 072            Rect viewportRect = new Rect(viewMinCoords, viewMaxCoords - viewMinCoords);
 073            viewportRect.min -= Vector2.one * size;
 074            viewportRect.max += Vector2.one * size;
 75
 76#if UNITY_EDITOR
 077            if (VERBOSE)
 78            {
 079                Utils.DrawRectGizmo(viewportRect, Color.blue, 5f);
 80            }
 81#endif
 82
 083            Rect myRect = new Rect(myMinCoords, myMaxCoords - myMinCoords);
 084            bool visible = viewportRect.Overlaps(myRect, true);
 85
 086            targetImage.enabled = visible;
 87
 088            if (!isLoadingOrLoaded)
 89            {
 090                loadOp = LoadChunkImage();
 91            }
 092        }
 93
 94        private void OnDestroy()
 95        {
 49096            if (loadOp != null)
 097                loadOp.Dispose();
 49098        }
 99    }
 100}