< Summary

Class:DCL.Components.TextShape
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/TextShape/TextShape.cs
Covered lines:75
Uncovered lines:16
Coverable lines:91
Total lines:247
Line coverage:82.4% (75 of 91)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
Awake()0%110100%
Update()0%3.333066.67%
GetModel()0%2100%
ApplyChanges()0%7.187084.62%
ApplyModelChanges(...)0%12.9910068.97%
GetAlignment(...)0%13130100%
PrepareRectTransform()0%2.042077.78%
GetClassId()0%2100%
Cleanup()0%110100%
OnDestroy()0%110100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL.Controllers;
 4using DCL.Helpers;
 5using DCL.Models;
 6using TMPro;
 7using UnityEngine;
 8using Object = UnityEngine.Object;
 9
 10namespace DCL.Components
 11{
 12    public class TextShape : BaseComponent
 13    {
 14        [Serializable]
 15        public class Model : BaseModel
 16        {
 17            //These values exist in the SDK but we are doing nothing with these values
 3718            public string fontWeight = "normal";
 19            public bool resizeToFit = false;
 20            public float zIndex = 0;
 21
 22            public bool isPickable = false;
 23            //
 24
 25            public bool billboard;
 26
 27            [Header("Font Properties")]
 3728            public string value = "";
 29
 3730            public bool visible = true;
 31
 3732            public Color color = Color.white;
 3733            public float opacity = 1f;
 3734            public float fontSize = 100f;
 35            public bool fontAutoSize = false;
 36            public string font;
 37
 38            [Header("Text box properties")]
 3739            public string hTextAlign = "bottom";
 40
 3741            public string vTextAlign = "left";
 3742            public float width = 1f;
 3743            public float height = 0.2f;
 44            public float paddingTop = 0f;
 45            public float paddingRight = 0f;
 46            public float paddingBottom = 0f;
 47            public float paddingLeft = 0f;
 48            public float lineSpacing = 0f;
 49            public int lineCount = 0;
 50            public bool textWrapping = false;
 51
 52            [Header("Text shadow properties")]
 53            public float shadowBlur = 0f;
 54
 55            public float shadowOffsetX = 0f;
 56            public float shadowOffsetY = 0f;
 3757            public Color shadowColor = new Color(1, 1, 1);
 58
 59            [Header("Text outline properties")]
 60            public float outlineWidth = 0f;
 61
 3762            public Color outlineColor = Color.white;
 63
 1464            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 65        }
 66
 67        public TextMeshPro text;
 68        public RectTransform rectTransform;
 69        private Model cachedModel;
 70        private Material cachedFontMaterial;
 71
 72        private void Awake()
 73        {
 1074            model = new Model();
 1075            cachedFontMaterial = new Material(text.fontSharedMaterial);
 1076            text.fontSharedMaterial = cachedFontMaterial;
 1077            text.text = string.Empty;
 1078        }
 79
 80        public void Update()
 81        {
 2582            if (cachedModel.billboard && Camera.main != null)
 83            {
 084                transform.forward = Camera.main.transform.forward;
 85            }
 2586        }
 87
 088        new public Model GetModel() { return cachedModel; }
 89
 90        public override IEnumerator ApplyChanges(BaseModel newModel)
 91        {
 1492            if (rectTransform == null)
 093                yield break;
 94
 1495            Model model = (Model) newModel;
 1496            cachedModel = model;
 1497            PrepareRectTransform();
 98
 99            // We avoid using even yield break; as this instruction skips a frame and we don't want that.
 14100            if ( !DCLFont.IsFontLoaded(scene, model.font) )
 101            {
 0102                yield return DCLFont.WaitUntilFontIsReady(scene, model.font);
 103            }
 104
 14105            DCLFont.SetFontFromComponent(scene, model.font, text);
 14106            ApplyModelChanges(text, model);
 107
 14108            if (entity.meshRootGameObject == null)
 10109                entity.meshesInfo.meshRootGameObject = gameObject;
 110
 14111            entity.OnShapeUpdated?.Invoke(entity);
 14112        }
 113
 114        public static void ApplyModelChanges(TMP_Text text, Model model)
 115        {
 14116            text.text = model.value;
 117
 14118            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 14119            text.fontSize = (int) model.fontSize;
 14120            text.richText = true;
 14121            text.overflowMode = TextOverflowModes.Overflow;
 14122            text.enableAutoSizing = model.fontAutoSize;
 123
 14124            text.margin =
 125                new Vector4
 126                (
 127                    (int) model.paddingLeft,
 128                    (int) model.paddingTop,
 129                    (int) model.paddingRight,
 130                    (int) model.paddingBottom
 131                );
 132
 14133            text.alignment = GetAlignment(model.vTextAlign, model.hTextAlign);
 14134            text.lineSpacing = model.lineSpacing;
 135
 14136            if (model.lineCount != 0)
 137            {
 1138                text.maxVisibleLines = Mathf.Max(model.lineCount, 1);
 1139            }
 140            else
 141            {
 13142                text.maxVisibleLines = int.MaxValue;
 143            }
 144
 14145            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 146
 14147            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 148            {
 0149                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 0150                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 0151                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 0152            }
 14153            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 154            {
 9155                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 156            }
 157
 14158            if (model.outlineWidth > 0f)
 159            {
 0160                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 0161                text.outlineWidth = model.outlineWidth;
 0162                text.outlineColor = model.outlineColor;
 0163            }
 14164            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 165            {
 0166                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 167            }
 14168        }
 169
 170        public static TextAlignmentOptions GetAlignment(string vTextAlign, string hTextAlign)
 171        {
 43172            vTextAlign = vTextAlign.ToLower();
 43173            hTextAlign = hTextAlign.ToLower();
 174
 175            switch (vTextAlign)
 176            {
 177                case "top":
 178                    switch (hTextAlign)
 179                    {
 180                        case "left":
 1181                            return TextAlignmentOptions.TopLeft;
 182                        case "right":
 1183                            return TextAlignmentOptions.TopRight;
 184                        default:
 1185                            return TextAlignmentOptions.Top;
 186                    }
 187
 188                case "bottom":
 189                    switch (hTextAlign)
 190                    {
 191                        case "left":
 1192                            return TextAlignmentOptions.BottomLeft;
 193                        case "right":
 1194                            return TextAlignmentOptions.BottomRight;
 195                        default:
 1196                            return TextAlignmentOptions.Bottom;
 197                    }
 198
 199                default: // center
 200                    switch (hTextAlign)
 201                    {
 202                        case "left":
 1203                            return TextAlignmentOptions.Left;
 204                        case "right":
 1205                            return TextAlignmentOptions.Right;
 206                        default:
 35207                            return TextAlignmentOptions.Center;
 208                    }
 209            }
 210        }
 211
 212        private void PrepareRectTransform()
 213        {
 14214            rectTransform.anchorMin = Vector2.zero;
 14215            rectTransform.anchorMax = Vector2.one;
 14216            rectTransform.offsetMin = Vector2.zero;
 14217            rectTransform.offsetMax = Vector2.zero;
 218
 219            // NOTE: previously width and height weren't working (setting sizeDelta before anchors and offset result in
 220            // sizeDelta being reset to 0,0)
 221            // to fix textWrapping and avoid backwards compatibility issues as result of the size being properly set (li
 222            // we only set it if textWrapping is enabled.
 14223            if (cachedModel.textWrapping)
 224            {
 0225                rectTransform.sizeDelta = new Vector2(cachedModel.width, cachedModel.height);
 0226            }
 227            else
 228            {
 14229                rectTransform.sizeDelta = Vector2.zero;
 230            }
 14231        }
 232
 0233        public override int GetClassId() { return (int) CLASS_ID.UI_TEXT_SHAPE; }
 234
 235        public override void Cleanup()
 236        {
 7237            text.text = string.Empty;
 7238            base.Cleanup();
 7239        }
 240
 241        private void OnDestroy()
 242        {
 10243            base.Cleanup();
 10244            Destroy(cachedFontMaterial);
 10245        }
 246    }
 247}