< Summary

Class:DCL.Components.UIImage
Assembly:DCL.Components.UI
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIImage/UIImage.cs
Covered lines:47
Uncovered lines:7
Coverable lines:54
Total lines:140
Line coverage:87% (47 of 54)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
UIImage()0%110100%
GetClassId()0%2100%
AttachTo(...)0%2100%
DetachFrom(...)0%2100%
ApplyChanges(...)0%9.089090%
ConfigureUVRect(...)0%440100%
Dispose()0%4.134080%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIImage/UIImage.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.Helpers;
 3using DCL.Models;
 4using System.Collections;
 5using System.Collections.Generic;
 6using UnityEngine;
 7using UnityEngine.UI;
 8
 9namespace DCL.Components
 10{
 11    public class UIImage : UIShape<UIImageReferencesContainer, UIImage.Model>
 12    {
 13        [System.Serializable]
 14        new public class Model : UIShape.Model
 15        {
 16            public string source;
 17            public float sourceLeft = 0f;
 18            public float sourceTop = 0f;
 6919            public float sourceWidth = 1f;
 6920            public float sourceHeight = 1f;
 21            public float paddingTop = 0f;
 22            public float paddingRight = 0f;
 23            public float paddingBottom = 0f;
 24            public float paddingLeft = 0f;
 6925            public bool sizeInPixels = true;
 26
 1927            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 28        }
 29
 1530        public override string referencesContainerPrefabName => "UIImage";
 31
 32        DCLTexture dclTexture = null;
 33
 1534        public UIImage()
 35        {
 1536            model = new Model();
 1537        }
 38
 039        public override int GetClassId() { return (int) CLASS_ID.UI_IMAGE_SHAPE; }
 40
 041        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) { Debug.LogError("Abo
 42
 043        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 44
 45        Coroutine fetchRoutine;
 46
 47        public override IEnumerator ApplyChanges(BaseModel newModel)
 48        {
 3449            RectTransform parentRecTransform = referencesContainer.GetComponentInParent<RectTransform>();
 50
 51            // Fetch texture
 3452            if (!string.IsNullOrEmpty(model.source))
 53            {
 1854                if (dclTexture == null || (dclTexture != null && dclTexture.id != model.source))
 55                {
 1056                    if (fetchRoutine != null)
 57                    {
 058                        CoroutineStarter.Stop(fetchRoutine);
 059                        fetchRoutine = null;
 60                    }
 61
 1062                    IEnumerator fetchIEnum = DCLTexture.FetchTextureComponent(scene, model.source, (downloadedTexture) =
 63                    {
 964                        referencesContainer.image.texture = downloadedTexture.texture;
 965                        fetchRoutine = null;
 966                        dclTexture?.DetachFrom(this);
 967                        dclTexture = downloadedTexture;
 968                        dclTexture.AttachTo(this);
 69
 970                        ConfigureUVRect(parentRecTransform, dclTexture?.resizingFactor ?? 1);
 971                    });
 72
 1073                    fetchRoutine = CoroutineStarter.Start(fetchIEnum);
 74                }
 75            }
 76            else
 77            {
 1678                referencesContainer.image.texture = null;
 1679                dclTexture?.DetachFrom(this);
 1680                dclTexture = null;
 81            }
 82
 3483            referencesContainer.image.enabled = model.visible;
 3484            referencesContainer.image.color = Color.white;
 85
 3486            ConfigureUVRect(parentRecTransform, dclTexture?.resizingFactor ?? 1);
 87
 88            // Apply padding
 3489            referencesContainer.paddingLayoutGroup.padding.bottom = Mathf.RoundToInt(model.paddingBottom);
 3490            referencesContainer.paddingLayoutGroup.padding.top = Mathf.RoundToInt(model.paddingTop);
 3491            referencesContainer.paddingLayoutGroup.padding.left = Mathf.RoundToInt(model.paddingLeft);
 3492            referencesContainer.paddingLayoutGroup.padding.right = Mathf.RoundToInt(model.paddingRight);
 93
 3494            Utils.ForceRebuildLayoutImmediate(parentRecTransform);
 3495            return null;
 96        }
 97
 98        private void ConfigureUVRect(RectTransform parentRecTransform, float resizingFactor)
 99        {
 43100            if (referencesContainer.image.texture == null)
 26101                return;
 102
 103            // Configure uv rect
 17104            Vector2 normalizedSourceCoordinates = new Vector2(
 105                model.sourceLeft * resizingFactor / referencesContainer.image.texture.width,
 106                -model.sourceTop * resizingFactor / referencesContainer.image.texture.height);
 107
 17108            Vector2 normalizedSourceSize = new Vector2(
 109                model.sourceWidth * resizingFactor * (model.sizeInPixels ? 1f : parentRecTransform.rect.width) /
 110                referencesContainer.image.texture.width ,
 111                model.sourceHeight * resizingFactor * (model.sizeInPixels ? 1f : parentRecTransform.rect.height) /
 112                referencesContainer.image.texture.height);
 113
 17114            referencesContainer.image.uvRect = new Rect(normalizedSourceCoordinates.x,
 115                normalizedSourceCoordinates.y + (1 - normalizedSourceSize.y),
 116                normalizedSourceSize.x,
 117                normalizedSourceSize.y);
 17118        }
 119
 120        public override void Dispose()
 121        {
 1122            if (fetchRoutine != null)
 123            {
 0124                CoroutineStarter.Stop(fetchRoutine);
 0125                fetchRoutine = null;
 126            }
 127
 1128            dclTexture?.DetachFrom(this);
 129
 1130            if (referencesContainer != null)
 131            {
 1132                referencesContainer.image.texture = null;
 1133                Utils.SafeDestroy(referencesContainer.gameObject);
 1134                referencesContainer = null;
 135            }
 136
 1137            base.Dispose();
 1138        }
 139    }
 140}