< 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:73
Uncovered lines:8
Coverable lines:81
Total lines:221
Line coverage:90.1% (73 of 81)
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%6.046090%
ApplyModelChanges()0%13.0113096.77%
GetAlignment(...)0%13130100%
ApplyCurrentModel()0%2100%
PrepareRectTransform()0%2.042077.78%
GetClassId()0%2100%

File(s)

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

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using System.Linq;
 4using DCL.Controllers;
 5using DCL.Helpers;
 6using DCL.Models;
 7using TMPro;
 8using UnityEngine;
 9
 10namespace DCL.Components
 11{
 12    public class TextShape : BaseComponent
 13    {
 14        [System.Serializable]
 15        public class Model : BaseModel
 16        {
 17            public bool billboard;
 18
 19            [Header("Font Properties")]
 9620            public string value = "";
 21
 9622            public bool visible = true;
 23
 9624            public Color color = Color.white;
 9625            public float opacity = 1f;
 9626            public float fontSize = 100f;
 27            public bool fontAutoSize = false;
 9628            public string fontWeight = "normal";
 29            public string font;
 30
 31            [Header("Text box properties")]
 9632            public string hTextAlign = "bottom";
 33
 9634            public string vTextAlign = "left";
 9635            public float width = 1f;
 9636            public float height = 0.2f;
 37            public bool adaptWidth = false;
 38            public bool adaptHeight = false;
 39            public float paddingTop = 0f;
 40            public float paddingRight = 0f;
 41            public float paddingBottom = 0f;
 42            public float paddingLeft = 0f;
 43            public float lineSpacing = 0f;
 44            public int lineCount = 0;
 45            public bool textWrapping = false;
 46
 47            [Header("Text shadow properties")]
 48            public float shadowBlur = 0f;
 49
 50            public float shadowOffsetX = 0f;
 51            public float shadowOffsetY = 0f;
 9652            public Color shadowColor = new Color(1, 1, 1);
 53
 54            [Header("Text outline properties")]
 55            public float outlineWidth = 0f;
 56
 9657            public Color outlineColor = Color.white;
 58
 1859            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 60        }
 61
 62        public TextMeshPro text;
 63        public RectTransform rectTransform;
 64        private Model cachedModel;
 65
 2066        private void Awake() { model = new Model(); }
 67
 68        public void Update()
 69        {
 2570            if (cachedModel.billboard && Camera.main != null)
 71            {
 072                transform.forward = Camera.main.transform.forward;
 73            }
 2574        }
 75
 076        new public Model GetModel() { return cachedModel; }
 77
 78        public override IEnumerator ApplyChanges(BaseModel newModel)
 79        {
 1480            if (rectTransform == null)
 081                yield break;
 82
 1483            Model model = (Model) newModel;
 1484            cachedModel = model;
 1485            PrepareRectTransform();
 86
 1487            yield return ApplyModelChanges(scene, text, model);
 1488            if (entity.meshRootGameObject == null)
 1089                entity.meshesInfo.meshRootGameObject = gameObject;
 1490            entity.OnShapeUpdated?.Invoke(entity);
 1491        }
 92
 93        public static IEnumerator ApplyModelChanges(IParcelScene scene, TMP_Text text, Model model)
 94        {
 3495            if (!string.IsNullOrEmpty(model.font))
 96            {
 397                yield return DCLFont.SetFontFromComponent(scene, model.font, text);
 98            }
 99
 34100            text.text = model.value;
 101
 34102            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 34103            text.fontSize = (int) model.fontSize;
 34104            text.richText = true;
 34105            text.overflowMode = TextOverflowModes.Overflow;
 34106            text.enableAutoSizing = model.fontAutoSize;
 107
 34108            text.margin =
 109                new Vector4
 110                (
 111                    (int) model.paddingLeft,
 112                    (int) model.paddingTop,
 113                    (int) model.paddingRight,
 114                    (int) model.paddingBottom
 115                );
 116
 34117            text.alignment = GetAlignment(model.vTextAlign, model.hTextAlign);
 34118            text.lineSpacing = model.lineSpacing;
 119
 34120            if (model.lineCount != 0)
 121            {
 3122                text.maxVisibleLines = Mathf.Max(model.lineCount, 1);
 3123            }
 124            else
 125            {
 31126                text.maxVisibleLines = int.MaxValue;
 127            }
 128
 34129            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 130
 34131            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 132            {
 2133                text.fontMaterial.EnableKeyword("UNDERLAY_ON");
 2134                text.fontMaterial.SetColor("_UnderlayColor", model.shadowColor);
 2135                text.fontMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 2136            }
 32137            else if (text.fontMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 138            {
 10139                text.fontMaterial.DisableKeyword("UNDERLAY_ON");
 140            }
 141
 34142            if (model.outlineWidth > 0f)
 143            {
 1144                text.fontMaterial.EnableKeyword("OUTLINE_ON");
 1145                text.outlineWidth = model.outlineWidth;
 1146                text.outlineColor = model.outlineColor;
 1147            }
 33148            else if (text.fontMaterial.IsKeywordEnabled("OUTLINE_ON"))
 149            {
 0150                text.fontMaterial.DisableKeyword("OUTLINE_ON");
 151            }
 34152        }
 153
 154        public static TextAlignmentOptions GetAlignment(string vTextAlign, string hTextAlign)
 155        {
 43156            vTextAlign = vTextAlign.ToLower();
 43157            hTextAlign = hTextAlign.ToLower();
 158
 159            switch (vTextAlign)
 160            {
 161                case "top":
 162                    switch (hTextAlign)
 163                    {
 164                        case "left":
 1165                            return TextAlignmentOptions.TopLeft;
 166                        case "right":
 1167                            return TextAlignmentOptions.TopRight;
 168                        default:
 1169                            return TextAlignmentOptions.Top;
 170                    }
 171
 172                case "bottom":
 173                    switch (hTextAlign)
 174                    {
 175                        case "left":
 1176                            return TextAlignmentOptions.BottomLeft;
 177                        case "right":
 1178                            return TextAlignmentOptions.BottomRight;
 179                        default:
 1180                            return TextAlignmentOptions.Bottom;
 181                    }
 182
 183                default: // center
 184                    switch (hTextAlign)
 185                    {
 186                        case "left":
 1187                            return TextAlignmentOptions.Left;
 188                        case "right":
 1189                            return TextAlignmentOptions.Right;
 190                        default:
 35191                            return TextAlignmentOptions.Center;
 192                    }
 193            }
 194        }
 195
 0196        private void ApplyCurrentModel() { ApplyModelChanges(scene, text, cachedModel); }
 197
 198        private void PrepareRectTransform()
 199        {
 14200            rectTransform.anchorMin = Vector2.zero;
 14201            rectTransform.anchorMax = Vector2.one;
 14202            rectTransform.offsetMin = Vector2.zero;
 14203            rectTransform.offsetMax = Vector2.zero;
 204
 205            // NOTE: previously width and height weren't working (setting sizeDelta before anchors and offset result in
 206            // sizeDelta being reset to 0,0)
 207            // to fix textWrapping and avoid backwards compatibility issues as result of the size being properly set (li
 208            // we only set it if textWrapping is enabled.
 14209            if (cachedModel.textWrapping)
 210            {
 0211                rectTransform.sizeDelta = new Vector2(cachedModel.width, cachedModel.height);
 0212            }
 213            else
 214            {
 14215                rectTransform.sizeDelta = Vector2.zero;
 216            }
 14217        }
 218
 0219        public override int GetClassId() { return (int) CLASS_ID.UI_TEXT_SHAPE; }
 220    }
 221}