< 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:64
Uncovered lines:6
Coverable lines:70
Total lines:180
Line coverage:91.4% (64 of 70)
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%330100%
RefreshDCLSize(...)0%6.016093.75%
Dispose()0%220100%
ApplyModelChanges()0%13.0513093.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
 870            yield return ApplyModelChanges(scene, referencesContainer.text, model);
 71
 872            RefreshAll();
 873        }
 74
 75        protected override void RefreshDCLSize(RectTransform parentTransform = null)
 76        {
 1677            if (parentTransform == null)
 78            {
 079                parentTransform = referencesContainer.GetComponentInParent<RectTransform>();
 80            }
 81
 1682            if (model.adaptWidth || model.adaptHeight)
 283                referencesContainer.text.ForceMeshUpdate(false);
 84
 1685            Bounds b = referencesContainer.text.textBounds;
 86
 87            float width, height;
 88
 1689            if (model.adaptWidth)
 90            {
 291                width = b.size.x;
 292            }
 93            else
 94            {
 1495                width = model.width.GetScaledValue(parentTransform.rect.width);
 96            }
 97
 1698            if (model.adaptHeight)
 99            {
 2100                height = b.size.y;
 2101            }
 102            else
 103            {
 14104                height = model.height.GetScaledValue(parentTransform.rect.height);
 105            }
 106
 16107            referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
 16108            referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
 16109        }
 110
 111        public override void Dispose()
 112        {
 4113            if (referencesContainer != null)
 4114                Utils.SafeDestroy(referencesContainer.gameObject);
 115
 4116            base.Dispose();
 4117        }
 118
 119        private static IEnumerator ApplyModelChanges(IParcelScene scene, TMP_Text text, Model model)
 120        {
 8121            if (!string.IsNullOrEmpty(model.font))
 122            {
 0123                yield return DCLFont.SetFontFromComponent(scene, model.font, text);
 124            }
 125
 8126            text.text = model.value;
 127
 8128            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 8129            text.fontSize = (int) model.fontSize;
 8130            text.richText = true;
 8131            text.overflowMode = TextOverflowModes.Overflow;
 8132            text.enableAutoSizing = model.fontAutoSize;
 133
 8134            text.margin =
 135                new Vector4
 136                (
 137                    (int) model.paddingLeft,
 138                    (int) model.paddingTop,
 139                    (int) model.paddingRight,
 140                    (int) model.paddingBottom
 141                );
 142
 8143            text.alignment = TextShape.GetAlignment(model.vTextAlign, model.hTextAlign);
 8144            text.lineSpacing = model.lineSpacing;
 145
 8146            if (model.lineCount != 0)
 147            {
 2148                text.maxVisibleLines = Mathf.Max(model.lineCount, 1);
 2149            }
 150            else
 151            {
 6152                text.maxVisibleLines = int.MaxValue;
 153            }
 154
 8155            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 156
 8157            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 158            {
 2159                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 2160                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 2161                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 2162            }
 6163            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 164            {
 2165                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 166            }
 167
 8168            if (model.outlineWidth > 0f)
 169            {
 1170                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 1171                text.outlineWidth = model.outlineWidth;
 1172                text.outlineColor = model.outlineColor;
 1173            }
 7174            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 175            {
 0176                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 177            }
 8178        }
 179    }
 180}