< Summary

Class:DCL.Components.UIText
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIText/UIText.cs
Covered lines:65
Uncovered lines:6
Coverable lines:71
Total lines:181
Line coverage:91.5% (65 of 71)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
UIText()0%110100%
GetClassId()0%2100%
AttachTo(...)0%2100%
DetachFrom(...)0%2100%
ApplyChanges()0%4.054085.71%
RefreshDCLSize(...)0%6.016093.75%
Dispose()0%220100%
ApplyModelChanges(...)0%1010096.55%

File(s)

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

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.Helpers;
 3using DCL.Models;
 4using System.Collections;
 5using System.Collections.Generic;
 6using Newtonsoft.Json;
 7using TMPro;
 8using UnityEngine;
 9
 10namespace DCL.Components
 11{
 12    public class UIText : UIShape<UITextReferencesContainer, UIText.Model>
 13    {
 14        [System.Serializable]
 15        new public class Model : UIShape.Model
 16        {
 17            public float outlineWidth = 0f;
 1618            public Color outlineColor = Color.white;
 19
 1620            public Color color = Color.white;
 21
 22            //These values exist in the SDK but we are doing nothing with these values
 1623            public string fontWeight = "normal";
 24            //
 25
 26            public bool adaptWidth = false;
 27            public bool adaptHeight = false;
 1628            public float fontSize = 100f;
 29            public bool fontAutoSize = false;
 30
 31            public string font;
 1632            public string value = "";
 33            public float lineSpacing = 0f;
 34            public int lineCount = 0;
 1635            public string hTextAlign = "bottom";
 1636            public string vTextAlign = "left";
 37            public bool textWrapping = false;
 38
 39            public float shadowBlur = 0f;
 40            public float shadowOffsetX = 0f;
 41            public float shadowOffsetY = 0f;
 1642            public Color shadowColor = new Color(1, 1, 1);
 43
 44            public float paddingTop = 0f;
 45            public float paddingRight = 0f;
 46            public float paddingBottom = 0f;
 47            public float paddingLeft = 0f;
 48
 49            public override BaseModel GetDataFromJSON(string json)
 50            {
 451                Model model = Utils.SafeFromJson<Model>(json);
 452                return model;
 53            }
 54        }
 55
 456        public override string referencesContainerPrefabName => "UIText";
 57
 1258        public UIText() { model = new Model(); }
 59
 060        public override int GetClassId() { return (int) CLASS_ID.UI_TEXT_SHAPE; }
 61
 062        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) { Debug.LogError("Abo
 63
 064        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 65
 66        public override IEnumerator ApplyChanges(BaseModel newModel)
 67        {
 868            model = (Model) newModel;
 69
 70            // We avoid using even yield break; as this instruction skips a frame and we don't want that.
 871            if ( !DCLFont.IsFontLoaded(scene, model.font) )
 72            {
 073                yield return DCLFont.WaitUntilFontIsReady(scene, model.font);
 74            }
 75
 876            DCLFont.SetFontFromComponent(scene, model.font, referencesContainer.text);
 877            ApplyModelChanges(referencesContainer.text, model);
 878            MarkLayoutDirty();
 879        }
 80
 81        protected override void RefreshDCLSize(RectTransform parentTransform = null)
 82        {
 1683            if (parentTransform == null)
 84            {
 085                parentTransform = referencesContainer.GetComponentInParent<RectTransform>();
 86            }
 87
 1688            if (model.adaptWidth || model.adaptHeight)
 289                referencesContainer.text.ForceMeshUpdate(false);
 90
 1691            Bounds b = referencesContainer.text.textBounds;
 92
 93            float width, height;
 94
 1695            if (model.adaptWidth)
 96            {
 297                width = b.size.x;
 298            }
 99            else
 100            {
 14101                width = model.width.GetScaledValue(parentTransform.rect.width);
 102            }
 103
 16104            if (model.adaptHeight)
 105            {
 2106                height = b.size.y;
 2107            }
 108            else
 109            {
 14110                height = model.height.GetScaledValue(parentTransform.rect.height);
 111            }
 112
 16113            referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
 16114            referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
 16115        }
 116
 117        public override void Dispose()
 118        {
 4119            if (referencesContainer != null)
 4120                Utils.SafeDestroy(referencesContainer.gameObject);
 121
 4122            base.Dispose();
 4123        }
 124
 125        private static void ApplyModelChanges(TMP_Text text, Model model)
 126        {
 8127            text.text = model.value;
 128
 8129            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 8130            text.fontSize = (int) model.fontSize;
 8131            text.richText = true;
 8132            text.overflowMode = TextOverflowModes.Overflow;
 8133            text.enableAutoSizing = model.fontAutoSize;
 134
 8135            text.margin =
 136                new Vector4
 137                (
 138                    (int) model.paddingLeft,
 139                    (int) model.paddingTop,
 140                    (int) model.paddingRight,
 141                    (int) model.paddingBottom
 142                );
 143
 8144            text.alignment = TextShape.GetAlignment(model.vTextAlign, model.hTextAlign);
 8145            text.lineSpacing = model.lineSpacing;
 146
 8147            if (model.lineCount != 0)
 148            {
 2149                text.maxVisibleLines = Mathf.Max(model.lineCount, 1);
 2150            }
 151            else
 152            {
 6153                text.maxVisibleLines = int.MaxValue;
 154            }
 155
 8156            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 157
 8158            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 159            {
 2160                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 2161                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 2162                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 2163            }
 6164            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 165            {
 2166                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 167            }
 168
 8169            if (model.outlineWidth > 0f)
 170            {
 1171                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 1172                text.outlineWidth = model.outlineWidth;
 1173                text.outlineColor = model.outlineColor;
 1174            }
 7175            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 176            {
 0177                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 178            }
 7179        }
 180    }
 181}