< 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:59
Uncovered lines:47
Coverable lines:106
Total lines:232
Line coverage:55.6% (59 of 106)
Covered branches:0
Total branches:0
Covered methods:8
Total methods:13
Method coverage:61.5% (8 of 13)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
GetDataFromPb(...)0%12603500%
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.Helpers;
 3using DCL.Models;
 4using System.Collections;
 5using TMPro;
 6using UnityEngine;
 7using Decentraland.Sdk.Ecs6;
 8using MainScripts.DCL.Components;
 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            public bool adaptWidth = false;
 23            public bool adaptHeight = false;
 1624            public float fontSize = 100f;
 25            public bool fontAutoSize = false;
 26
 27            public string font;
 1628            public string value = "";
 29            public float lineSpacing = 0f;
 30            public int lineCount = 0;
 1631            public string hTextAlign = "bottom";
 1632            public string vTextAlign = "left";
 33            public bool textWrapping = false;
 34
 35            public float shadowBlur = 0f;
 36            public float shadowOffsetX = 0f;
 37            public float shadowOffsetY = 0f;
 1638            public Color shadowColor = new Color(1, 1, 1);
 39
 40            public float paddingTop = 0f;
 41            public float paddingRight = 0f;
 42            public float paddingBottom = 0f;
 43            public float paddingLeft = 0f;
 44
 45            public override BaseModel GetDataFromJSON(string json) =>
 446                Utils.SafeFromJson<Model>(json);
 47
 48            public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel)
 49            {
 050                if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.UiText)
 051                    return Utils.SafeUnimplemented<UIText, Model>(expected: ComponentBodyPayload.PayloadOneofCase.UiText
 52
 053                var pb = new Model();
 054                if (pbModel.UiText.HasName) pb.name = pbModel.UiText.Name;
 055                if (pbModel.UiText.HasParentComponent) pb.parentComponent = pbModel.UiText.ParentComponent;
 056                if (pbModel.UiText.HasVisible) pb.visible = pbModel.UiText.Visible;
 057                if (pbModel.UiText.HasOpacity) pb.opacity = pbModel.UiText.Opacity;
 058                if (pbModel.UiText.HasHAlign) pb.hAlign = pbModel.UiText.HAlign;
 059                if (pbModel.UiText.HasVAlign) pb.vAlign = pbModel.UiText.VAlign;
 060                if (pbModel.UiText.Width != null) pb.width = SDK6DataMapExtensions.FromProtobuf(pb.width, pbModel.UiText
 061                if (pbModel.UiText.Height != null) pb.height = SDK6DataMapExtensions.FromProtobuf(pb.height, pbModel.UiT
 062                if (pbModel.UiText.PositionX != null) pb.positionX = SDK6DataMapExtensions.FromProtobuf(pb.positionX, pb
 063                if (pbModel.UiText.PositionY != null) pb.positionY = SDK6DataMapExtensions.FromProtobuf(pb.positionY, pb
 064                if (pbModel.UiText.HasIsPointerBlocker) pb.isPointerBlocker = pbModel.UiText.IsPointerBlocker;
 65
 066                if (pbModel.UiText.HasOutlineWidth) pb.outlineWidth = pbModel.UiText.OutlineWidth;
 067                if (pbModel.UiText.OutlineColor != null) pb.outlineColor = pbModel.UiText.OutlineColor.AsUnityColor();
 068                if (pbModel.UiText.Color != null) pb.color = pbModel.UiText.Color.AsUnityColor();
 069                if (pbModel.UiText.HasAdaptWidth) pb.adaptWidth = pbModel.UiText.AdaptWidth;
 070                if (pbModel.UiText.HasAdaptHeight) pb.adaptHeight = pbModel.UiText.AdaptHeight;
 071                if (pbModel.UiText.HasFontSize) pb.fontSize = pbModel.UiText.FontSize;
 072                if (pbModel.UiText.HasFontAutoSize) pb.fontAutoSize = pbModel.UiText.FontAutoSize;
 073                if (pbModel.UiText.HasFont) pb.font = pbModel.UiText.Font;
 074                if (pbModel.UiText.HasValue) pb.value = pbModel.UiText.Value;
 075                if (pbModel.UiText.HasLineSpacing) pb.lineSpacing = pbModel.UiText.LineSpacing;
 076                if (pbModel.UiText.HasLineCount) pb.lineCount = pbModel.UiText.LineCount;
 077                if (pbModel.UiText.HasHTextAlign) pb.hTextAlign = pbModel.UiText.HTextAlign;
 078                if (pbModel.UiText.HasVTextAlign) pb.vTextAlign = pbModel.UiText.VTextAlign;
 079                if (pbModel.UiText.HasTextWrapping) pb.textWrapping = pbModel.UiText.TextWrapping;
 080                if (pbModel.UiText.HasShadowBlur) pb.shadowBlur = pbModel.UiText.ShadowBlur;
 081                if (pbModel.UiText.HasShadowOffsetX) pb.shadowOffsetX = pbModel.UiText.ShadowOffsetX;
 082                if (pbModel.UiText.HasShadowOffsetY) pb.shadowOffsetY = pbModel.UiText.ShadowOffsetY;
 083                if (pbModel.UiText.ShadowColor != null) pb.shadowColor = pbModel.UiText.ShadowColor.AsUnityColor();
 084                if (pbModel.UiText.HasPaddingTop) pb.paddingTop = pbModel.UiText.PaddingTop;
 085                if (pbModel.UiText.HasPaddingRight) pb.paddingRight = pbModel.UiText.PaddingRight;
 086                if (pbModel.UiText.HasPaddingBottom) pb.paddingBottom = pbModel.UiText.PaddingBottom;
 087                if (pbModel.UiText.HasPaddingLeft) pb.paddingLeft = pbModel.UiText.PaddingLeft;
 88
 089                return pb;
 90            }
 91        }
 92
 493        public override string referencesContainerPrefabName => "UIText";
 94
 495        public UIText()
 96        {
 497            model = new Model();
 498        }
 99
 100        public override int GetClassId() =>
 0101            (int) CLASS_ID.UI_TEXT_SHAPE;
 102
 0103        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) { Debug.LogError("Abo
 104
 0105        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 106
 107        public override IEnumerator ApplyChanges(BaseModel baseModel)
 108        {
 8109            model = (Model) baseModel;
 110
 111            // We avoid using even yield break; as this instruction skips a frame and we don't want that.
 8112            if ( !DCLFont.IsFontLoaded(scene, model.font) )
 0113                yield return DCLFont.WaitUntilFontIsReady(scene, model.font);
 114
 8115            DCLFont.SetFontFromComponent(scene, model.font, referencesContainer.text);
 8116            bool shouldMarkDirty = ShouldMarkDirty();
 117
 8118            ApplyModelChanges(referencesContainer.text, model);
 16119            if (shouldMarkDirty) MarkLayoutDirty();
 8120        }
 121        private bool ShouldMarkDirty()
 122        {
 8123            TextMeshProUGUI text = referencesContainer.text;
 124
 8125            return model.value != text.text
 126                   || Math.Abs(model.fontSize - text.fontSize) > float.Epsilon
 127                   || model.fontAutoSize != text.enableAutoSizing
 128                   || Math.Abs(model.lineSpacing - text.lineSpacing) > float.Epsilon
 129                   || new Vector4((int) model.paddingLeft, (int) model.paddingTop, (int) model.paddingRight, (int) model
 130        }
 131
 132        protected override void RefreshDCLSize(RectTransform parentTransform = null)
 133        {
 16134            if (parentTransform == null)
 135            {
 0136                parentTransform = referencesContainer.GetComponentInParent<RectTransform>();
 137            }
 138
 16139            if (model.adaptWidth || model.adaptHeight)
 2140                referencesContainer.text.ForceMeshUpdate(false);
 141
 16142            Bounds b = referencesContainer.text.textBounds;
 143
 144            float width, height;
 145
 16146            if (model.adaptWidth)
 147            {
 2148                width = b.size.x;
 149            }
 150            else
 151            {
 14152                width = model.width.GetScaledValue(parentTransform.rect.width);
 153            }
 154
 16155            if (model.adaptHeight)
 156            {
 2157                height = b.size.y;
 158            }
 159            else
 160            {
 14161                height = model.height.GetScaledValue(parentTransform.rect.height);
 162            }
 163
 16164            referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
 16165            referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
 16166        }
 167
 168        public override void Dispose()
 169        {
 0170            if (referencesContainer != null)
 0171                Utils.SafeDestroy(referencesContainer.gameObject);
 172
 0173            base.Dispose();
 0174        }
 175
 176        private static void ApplyModelChanges(TMP_Text text, Model model)
 177        {
 8178            text.text = model.value;
 179
 8180            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 8181            text.fontSize = (int) model.fontSize;
 8182            text.richText = true;
 8183            text.overflowMode = TextOverflowModes.Overflow;
 8184            text.enableAutoSizing = model.fontAutoSize;
 185
 8186            text.margin =
 187                new Vector4
 188                (
 189                    (int) model.paddingLeft,
 190                    (int) model.paddingTop,
 191                    (int) model.paddingRight,
 192                    (int) model.paddingBottom
 193                );
 194
 8195            text.alignment = TextShape.GetAlignment(model.vTextAlign, model.hTextAlign);
 8196            text.lineSpacing = model.lineSpacing;
 197
 8198            if (model.lineCount != 0)
 199            {
 2200                text.maxVisibleLines = Mathf.Max(model.lineCount, 1);
 201            }
 202            else
 203            {
 6204                text.maxVisibleLines = int.MaxValue;
 205            }
 206
 8207            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 208
 8209            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 210            {
 2211                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 2212                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 2213                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 214            }
 6215            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 216            {
 2217                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 218            }
 219
 8220            if (model.outlineWidth > 0f)
 221            {
 1222                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 1223                text.outlineWidth = model.outlineWidth;
 1224                text.outlineColor = model.outlineColor;
 225            }
 7226            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 227            {
 0228                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 229            }
 7230        }
 231    }
 232}