< 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:245
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%6.046090%
ApplyModelChanges()0%17.1313070.97%
GetAlignment(...)0%13130100%
ApplyCurrentModel()0%2100%
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
 18            //These values exist in the SDK but we are doing nothing with these values
 3719            public string fontWeight = "normal";
 20            public bool resizeToFit = false;
 21            public float zIndex = 0;
 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
 1499            yield return ApplyModelChanges(scene, text, model);
 14100            if (entity.meshRootGameObject == null)
 10101                entity.meshesInfo.meshRootGameObject = gameObject;
 14102            entity.OnShapeUpdated?.Invoke(entity);
 14103        }
 104
 105        public static IEnumerator ApplyModelChanges(IParcelScene scene, TMP_Text text, Model model)
 106        {
 14107            if (!string.IsNullOrEmpty(model.font))
 108            {
 3109                yield return DCLFont.SetFontFromComponent(scene, model.font, text);
 110            }
 111
 14112            text.text = model.value;
 113
 14114            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 14115            text.fontSize = (int) model.fontSize;
 14116            text.richText = true;
 14117            text.overflowMode = TextOverflowModes.Overflow;
 14118            text.enableAutoSizing = model.fontAutoSize;
 119
 14120            text.margin =
 121                new Vector4
 122                (
 123                    (int) model.paddingLeft,
 124                    (int) model.paddingTop,
 125                    (int) model.paddingRight,
 126                    (int) model.paddingBottom
 127                );
 128
 14129            text.alignment = GetAlignment(model.vTextAlign, model.hTextAlign);
 14130            text.lineSpacing = model.lineSpacing;
 131
 14132            if (model.lineCount != 0)
 133            {
 1134                text.maxVisibleLines = Mathf.Max(model.lineCount, 1);
 1135            }
 136            else
 137            {
 13138                text.maxVisibleLines = int.MaxValue;
 139            }
 140
 14141            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 142
 14143            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 144            {
 0145                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 0146                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 0147                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 0148            }
 14149            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 150            {
 9151                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 152            }
 153
 14154            if (model.outlineWidth > 0f)
 155            {
 0156                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 0157                text.outlineWidth = model.outlineWidth;
 0158                text.outlineColor = model.outlineColor;
 0159            }
 14160            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 161            {
 0162                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 163            }
 14164        }
 165
 166        public static TextAlignmentOptions GetAlignment(string vTextAlign, string hTextAlign)
 167        {
 43168            vTextAlign = vTextAlign.ToLower();
 43169            hTextAlign = hTextAlign.ToLower();
 170
 171            switch (vTextAlign)
 172            {
 173                case "top":
 174                    switch (hTextAlign)
 175                    {
 176                        case "left":
 1177                            return TextAlignmentOptions.TopLeft;
 178                        case "right":
 1179                            return TextAlignmentOptions.TopRight;
 180                        default:
 1181                            return TextAlignmentOptions.Top;
 182                    }
 183
 184                case "bottom":
 185                    switch (hTextAlign)
 186                    {
 187                        case "left":
 1188                            return TextAlignmentOptions.BottomLeft;
 189                        case "right":
 1190                            return TextAlignmentOptions.BottomRight;
 191                        default:
 1192                            return TextAlignmentOptions.Bottom;
 193                    }
 194
 195                default: // center
 196                    switch (hTextAlign)
 197                    {
 198                        case "left":
 1199                            return TextAlignmentOptions.Left;
 200                        case "right":
 1201                            return TextAlignmentOptions.Right;
 202                        default:
 35203                            return TextAlignmentOptions.Center;
 204                    }
 205            }
 206        }
 207
 0208        private void ApplyCurrentModel() { ApplyModelChanges(scene, text, cachedModel); }
 209
 210        private void PrepareRectTransform()
 211        {
 14212            rectTransform.anchorMin = Vector2.zero;
 14213            rectTransform.anchorMax = Vector2.one;
 14214            rectTransform.offsetMin = Vector2.zero;
 14215            rectTransform.offsetMax = Vector2.zero;
 216
 217            // NOTE: previously width and height weren't working (setting sizeDelta before anchors and offset result in
 218            // sizeDelta being reset to 0,0)
 219            // to fix textWrapping and avoid backwards compatibility issues as result of the size being properly set (li
 220            // we only set it if textWrapping is enabled.
 14221            if (cachedModel.textWrapping)
 222            {
 0223                rectTransform.sizeDelta = new Vector2(cachedModel.width, cachedModel.height);
 0224            }
 225            else
 226            {
 14227                rectTransform.sizeDelta = Vector2.zero;
 228            }
 14229        }
 230
 0231        public override int GetClassId() { return (int) CLASS_ID.UI_TEXT_SHAPE; }
 232
 233        public override void Cleanup()
 234        {
 7235            text.text = string.Empty;
 7236            base.Cleanup();
 7237        }
 238
 239        private void OnDestroy()
 240        {
 10241            base.Cleanup();
 10242            Destroy(cachedFontMaterial);
 10243        }
 244    }
 245}