< Summary

Class:DCL.Components.UIText
Assembly:DCL.Components.UI
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIText/UIText.cs
Covered lines:58
Uncovered lines:10
Coverable lines:68
Total lines:191
Line coverage:85.2% (58 of 68)
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%5.035088.89%
ShouldMarkDirty()0%550100%
RefreshDCLSize(...)0%6.016092.86%
Dispose()0%6200%
ApplyModelChanges(...)0%10.0110096.15%

File(s)

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

#LineLine coverage
 1using System;
 2using DCL.Controllers;
 3using DCL.Helpers;
 4using DCL.Models;
 5using System.Collections;
 6using System.Collections.Generic;
 7using Newtonsoft.Json;
 8using TMPro;
 9using UnityEngine;
 10
 11namespace DCL.Components
 12{
 13    public class UIText : UIShape<UITextReferencesContainer, UIText.Model>
 14    {
 15        [System.Serializable]
 16        new public class Model : UIShape.Model
 17        {
 18            public float outlineWidth = 0f;
 1619            public Color outlineColor = Color.white;
 20
 1621            public Color color = Color.white;
 22
 23            public bool adaptWidth = false;
 24            public bool adaptHeight = false;
 1625            public float fontSize = 100f;
 26            public bool fontAutoSize = false;
 27
 28            public string font;
 1629            public string value = "";
 30            public float lineSpacing = 0f;
 31            public int lineCount = 0;
 1632            public string hTextAlign = "bottom";
 1633            public string vTextAlign = "left";
 34            public bool textWrapping = false;
 35
 36            public float shadowBlur = 0f;
 37            public float shadowOffsetX = 0f;
 38            public float shadowOffsetY = 0f;
 1639            public Color shadowColor = new Color(1, 1, 1);
 40
 41            public float paddingTop = 0f;
 42            public float paddingRight = 0f;
 43            public float paddingBottom = 0f;
 44            public float paddingLeft = 0f;
 45
 46            public override BaseModel GetDataFromJSON(string json)
 47            {
 448                Model model = Utils.SafeFromJson<Model>(json);
 49
 450                return model;
 51            }
 52        }
 53
 454        public override string referencesContainerPrefabName => "UIText";
 55
 1256        public UIText() { model = new Model(); }
 57
 058        public override int GetClassId() { return (int) CLASS_ID.UI_TEXT_SHAPE; }
 59
 060        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) { Debug.LogError("Abo
 61
 062        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 63
 64        public override IEnumerator ApplyChanges(BaseModel baseModel)
 65        {
 866            model = (Model) baseModel;
 67
 68            // We avoid using even yield break; as this instruction skips a frame and we don't want that.
 869            if ( !DCLFont.IsFontLoaded(scene, model.font) )
 70            {
 071                yield return DCLFont.WaitUntilFontIsReady(scene, model.font);
 72            }
 73
 874            DCLFont.SetFontFromComponent(scene, model.font, referencesContainer.text);
 875            bool shouldMarkDirty = ShouldMarkDirty();
 76
 877            ApplyModelChanges(referencesContainer.text, model);
 1678            if (shouldMarkDirty) MarkLayoutDirty();
 879        }
 80        private bool ShouldMarkDirty()
 81        {
 882            TextMeshProUGUI text = referencesContainer.text;
 83
 884            return model.value != text.text
 85                   || Math.Abs(model.fontSize - text.fontSize) > float.Epsilon
 86                   || model.fontAutoSize != text.enableAutoSizing
 87                   || Math.Abs(model.lineSpacing - text.lineSpacing) > float.Epsilon
 88                   || new Vector4((int) model.paddingLeft, (int) model.paddingTop, (int) model.paddingRight, (int) model
 89        }
 90
 91        protected override void RefreshDCLSize(RectTransform parentTransform = null)
 92        {
 1693            if (parentTransform == null)
 94            {
 095                parentTransform = referencesContainer.GetComponentInParent<RectTransform>();
 96            }
 97
 1698            if (model.adaptWidth || model.adaptHeight)
 299                referencesContainer.text.ForceMeshUpdate(false);
 100
 16101            Bounds b = referencesContainer.text.textBounds;
 102
 103            float width, height;
 104
 16105            if (model.adaptWidth)
 106            {
 2107                width = b.size.x;
 108            }
 109            else
 110            {
 14111                width = model.width.GetScaledValue(parentTransform.rect.width);
 112            }
 113
 16114            if (model.adaptHeight)
 115            {
 2116                height = b.size.y;
 117            }
 118            else
 119            {
 14120                height = model.height.GetScaledValue(parentTransform.rect.height);
 121            }
 122
 16123            referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
 16124            referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
 16125        }
 126
 127        public override void Dispose()
 128        {
 0129            if (referencesContainer != null)
 0130                Utils.SafeDestroy(referencesContainer.gameObject);
 131
 0132            base.Dispose();
 0133        }
 134
 135        private static void ApplyModelChanges(TMP_Text text, Model model)
 136        {
 8137            text.text = model.value;
 138
 8139            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 8140            text.fontSize = (int) model.fontSize;
 8141            text.richText = true;
 8142            text.overflowMode = TextOverflowModes.Overflow;
 8143            text.enableAutoSizing = model.fontAutoSize;
 144
 8145            text.margin =
 146                new Vector4
 147                (
 148                    (int) model.paddingLeft,
 149                    (int) model.paddingTop,
 150                    (int) model.paddingRight,
 151                    (int) model.paddingBottom
 152                );
 153
 8154            text.alignment = TextShape.GetAlignment(model.vTextAlign, model.hTextAlign);
 8155            text.lineSpacing = model.lineSpacing;
 156
 8157            if (model.lineCount != 0)
 158            {
 2159                text.maxVisibleLines = Mathf.Max(model.lineCount, 1);
 160            }
 161            else
 162            {
 6163                text.maxVisibleLines = int.MaxValue;
 164            }
 165
 8166            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 167
 8168            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 169            {
 2170                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 2171                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 2172                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 173            }
 6174            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 175            {
 2176                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 177            }
 178
 8179            if (model.outlineWidth > 0f)
 180            {
 1181                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 1182                text.outlineWidth = model.outlineWidth;
 1183                text.outlineColor = model.outlineColor;
 184            }
 7185            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 186            {
 0187                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 188            }
 7189        }
 190    }
 191}