< Summary

Class:DCL.Components.UIImage
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIImage/UIImage.cs
Covered lines:44
Uncovered lines:7
Coverable lines:51
Total lines:133
Line coverage:86.2% (44 of 51)
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%7.047090.48%
ConfigureUVRect(...)0%440100%
Dispose()0%4.254075%

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
 4534        public UIImage() { model = new Model(); }
 35
 036        public override int GetClassId() { return (int) CLASS_ID.UI_IMAGE_SHAPE; }
 37
 038        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) { Debug.LogError("Abo
 39
 040        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 41
 42        Coroutine fetchRoutine;
 43
 44        public override IEnumerator ApplyChanges(BaseModel newModel)
 45        {
 3446            RectTransform parentRecTransform = referencesContainer.GetComponentInParent<RectTransform>();
 47
 48            // Fetch texture
 3449            if (!string.IsNullOrEmpty(model.source))
 50            {
 1851                if (dclTexture == null || (dclTexture != null && dclTexture.id != model.source))
 52                {
 1053                    if (fetchRoutine != null)
 54                    {
 055                        CoroutineStarter.Stop(fetchRoutine);
 056                        fetchRoutine = null;
 57                    }
 58
 1059                    IEnumerator fetchIEnum = DCLTexture.FetchTextureComponent(scene, model.source, (downloadedTexture) =
 60                    {
 961                        referencesContainer.image.texture = downloadedTexture.texture;
 962                        fetchRoutine = null;
 963                        dclTexture?.DetachFrom(this);
 964                        dclTexture = downloadedTexture;
 965                        dclTexture.AttachTo(this);
 66
 967                        ConfigureUVRect(parentRecTransform);
 968                    });
 69
 1070                    fetchRoutine = CoroutineStarter.Start(fetchIEnum);
 71                }
 1072            }
 73            else
 74            {
 1675                referencesContainer.image.texture = null;
 1676                dclTexture?.DetachFrom(this);
 1677                dclTexture = null;
 78            }
 79
 3480            referencesContainer.image.enabled = model.visible;
 3481            referencesContainer.image.color = Color.white;
 82
 3483            ConfigureUVRect(parentRecTransform);
 84
 85            // Apply padding
 3486            referencesContainer.paddingLayoutGroup.padding.bottom = Mathf.RoundToInt(model.paddingBottom);
 3487            referencesContainer.paddingLayoutGroup.padding.top = Mathf.RoundToInt(model.paddingTop);
 3488            referencesContainer.paddingLayoutGroup.padding.left = Mathf.RoundToInt(model.paddingLeft);
 3489            referencesContainer.paddingLayoutGroup.padding.right = Mathf.RoundToInt(model.paddingRight);
 90
 3491            Utils.ForceRebuildLayoutImmediate(parentRecTransform);
 3492            return null;
 93        }
 94
 95        private void ConfigureUVRect(RectTransform parentRecTransform)
 96        {
 4397            if (referencesContainer.image.texture == null)
 2698                return;
 99
 100            // Configure uv rect
 17101            Vector2 normalizedSourceCoordinates = new Vector2(
 102                model.sourceLeft / referencesContainer.image.texture.width,
 103                -model.sourceTop / referencesContainer.image.texture.height);
 104
 17105            Vector2 normalizedSourceSize = new Vector2(
 106                model.sourceWidth * (model.sizeInPixels ? 1f : parentRecTransform.rect.width) /
 107                referencesContainer.image.texture.width,
 108                model.sourceHeight * (model.sizeInPixels ? 1f : parentRecTransform.rect.height) /
 109                referencesContainer.image.texture.height);
 110
 17111            referencesContainer.image.uvRect = new Rect(normalizedSourceCoordinates.x,
 112                normalizedSourceCoordinates.y + (1 - normalizedSourceSize.y),
 113                normalizedSourceSize.x,
 114                normalizedSourceSize.y);
 17115        }
 116
 117        public override void Dispose()
 118        {
 1119            if (fetchRoutine != null)
 120            {
 0121                CoroutineStarter.Stop(fetchRoutine);
 0122                fetchRoutine = null;
 123            }
 124
 1125            dclTexture?.DetachFrom(this);
 126
 1127            if (referencesContainer != null)
 1128                Utils.SafeDestroy(referencesContainer.gameObject);
 129
 1130            base.Dispose();
 1131        }
 132    }
 133}