< Summary

Class:DCL.Components.TextShape
Assembly:DCL.Components.TextShape
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:241
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%14.110065.52%
GetAlignment(...)0%13130100%
PrepareRectTransform()0%2.042077.78%
GetClassId()0%110100%
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            public bool billboard;
 18
 19            [Header("Font Properties")]
 3920            public string value = "";
 21
 3922            public bool visible = true;
 23
 3924            public Color color = Color.white;
 3925            public float opacity = 1f;
 3926            public float fontSize = 100f;
 27            public bool fontAutoSize = false;
 28            public string font;
 29
 30            [Header("Text box properties")]
 3931            public string hTextAlign = "bottom";
 32
 3933            public string vTextAlign = "left";
 3934            public float width = 1f;
 3935            public float height = 0.2f;
 36            public float paddingTop = 0f;
 37            public float paddingRight = 0f;
 38            public float paddingBottom = 0f;
 39            public float paddingLeft = 0f;
 40            public float lineSpacing = 0f;
 41            public int lineCount = 0;
 42            public bool textWrapping = false;
 43
 44            [Header("Text shadow properties")]
 45            public float shadowBlur = 0f;
 46
 47            public float shadowOffsetX = 0f;
 48            public float shadowOffsetY = 0f;
 3949            public Color shadowColor = new Color(1, 1, 1);
 50
 51            [Header("Text outline properties")]
 52            public float outlineWidth = 0f;
 53
 3954            public Color outlineColor = Color.white;
 55
 1556            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 57        }
 58
 59        public TextMeshPro text;
 60        public RectTransform rectTransform;
 61        private Model cachedModel;
 62        private Material cachedFontMaterial;
 63
 564        public override string componentName => "text";
 65
 66        private void Awake()
 67        {
 1168            model = new Model();
 1169            cachedFontMaterial = new Material(text.fontSharedMaterial);
 1170            text.fontSharedMaterial = cachedFontMaterial;
 1171            text.text = string.Empty;
 1172        }
 73
 74        public void Update()
 75        {
 3376            if (cachedModel.billboard && Camera.main != null)
 77            {
 078                transform.forward = Camera.main.transform.forward;
 79            }
 3380        }
 81
 082        new public Model GetModel() { return cachedModel; }
 83
 84        public override IEnumerator ApplyChanges(BaseModel newModel)
 85        {
 1586            if (rectTransform == null)
 087                yield break;
 88
 1589            Model model = (Model) newModel;
 1590            cachedModel = model;
 1591            PrepareRectTransform();
 92
 93            // We avoid using even yield break; as this instruction skips a frame and we don't want that.
 1594            if ( !DCLFont.IsFontLoaded(scene, model.font) )
 95            {
 096                yield return DCLFont.WaitUntilFontIsReady(scene, model.font);
 97            }
 98
 1599            DCLFont.SetFontFromComponent(scene, model.font, text);
 15100            ApplyModelChanges(text, model);
 101
 15102            if (entity.meshRootGameObject == null)
 11103                entity.meshesInfo.meshRootGameObject = gameObject;
 104
 15105            entity.OnShapeUpdated?.Invoke(entity);
 15106        }
 107
 108        public static void ApplyModelChanges(TMP_Text text, Model model)
 109        {
 15110            text.text = model.value;
 111
 15112            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 15113            text.fontSize = (int) model.fontSize;
 15114            text.richText = true;
 15115            text.overflowMode = TextOverflowModes.Overflow;
 15116            text.enableAutoSizing = model.fontAutoSize;
 117
 15118            text.margin =
 119                new Vector4
 120                (
 121                    (int) model.paddingLeft,
 122                    (int) model.paddingTop,
 123                    (int) model.paddingRight,
 124                    (int) model.paddingBottom
 125                );
 126
 15127            text.alignment = GetAlignment(model.vTextAlign, model.hTextAlign);
 15128            text.lineSpacing = model.lineSpacing;
 129
 15130            if (model.lineCount != 0)
 131            {
 1132                text.maxVisibleLines = Mathf.Max(model.lineCount, 1);
 1133            }
 134            else
 135            {
 14136                text.maxVisibleLines = int.MaxValue;
 137            }
 138
 15139            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 140
 15141            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 142            {
 0143                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 0144                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 0145                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 0146            }
 15147            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 148            {
 0149                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 150            }
 151
 15152            if (model.outlineWidth > 0f)
 153            {
 0154                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 0155                text.outlineWidth = model.outlineWidth;
 0156                text.outlineColor = model.outlineColor;
 0157            }
 15158            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 159            {
 0160                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 161            }
 15162        }
 163
 164        public static TextAlignmentOptions GetAlignment(string vTextAlign, string hTextAlign)
 165        {
 44166            vTextAlign = vTextAlign.ToLower();
 44167            hTextAlign = hTextAlign.ToLower();
 168
 169            switch (vTextAlign)
 170            {
 171                case "top":
 172                    switch (hTextAlign)
 173                    {
 174                        case "left":
 1175                            return TextAlignmentOptions.TopLeft;
 176                        case "right":
 1177                            return TextAlignmentOptions.TopRight;
 178                        default:
 1179                            return TextAlignmentOptions.Top;
 180                    }
 181
 182                case "bottom":
 183                    switch (hTextAlign)
 184                    {
 185                        case "left":
 1186                            return TextAlignmentOptions.BottomLeft;
 187                        case "right":
 1188                            return TextAlignmentOptions.BottomRight;
 189                        default:
 1190                            return TextAlignmentOptions.Bottom;
 191                    }
 192
 193                default: // center
 194                    switch (hTextAlign)
 195                    {
 196                        case "left":
 1197                            return TextAlignmentOptions.Left;
 198                        case "right":
 1199                            return TextAlignmentOptions.Right;
 200                        default:
 36201                            return TextAlignmentOptions.Center;
 202                    }
 203            }
 204        }
 205
 206        private void PrepareRectTransform()
 207        {
 15208            rectTransform.anchorMin = Vector2.zero;
 15209            rectTransform.anchorMax = Vector2.one;
 15210            rectTransform.offsetMin = Vector2.zero;
 15211            rectTransform.offsetMax = Vector2.zero;
 212
 213            // NOTE: previously width and height weren't working (setting sizeDelta before anchors and offset result in
 214            // sizeDelta being reset to 0,0)
 215            // to fix textWrapping and avoid backwards compatibility issues as result of the size being properly set (li
 216            // we only set it if textWrapping is enabled.
 15217            if (cachedModel.textWrapping)
 218            {
 0219                rectTransform.sizeDelta = new Vector2(cachedModel.width, cachedModel.height);
 0220            }
 221            else
 222            {
 15223                rectTransform.sizeDelta = Vector2.zero;
 224            }
 15225        }
 226
 1227        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.TEXT_SHAPE; }
 228
 229        public override void Cleanup()
 230        {
 8231            text.text = string.Empty;
 8232            base.Cleanup();
 8233        }
 234
 235        private void OnDestroy()
 236        {
 11237            base.Cleanup();
 11238            Destroy(cachedFontMaterial);
 11239        }
 240    }
 241}