< 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:78
Uncovered lines:16
Coverable lines:94
Total lines:249
Line coverage:82.9% (78 of 94)
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%
OnUpdate()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%220100%

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;
 8
 9namespace DCL.Components
 10{
 11    public class TextShape : BaseComponent
 12    {
 13        [Serializable]
 14        public class Model : BaseModel
 15        {
 16            public bool billboard;
 17
 18            [Header("Font Properties")]
 3919            public string value = "";
 20
 3921            public bool visible = true;
 22
 3923            public Color color = Color.white;
 3924            public float opacity = 1f;
 3925            public float fontSize = 100f;
 26            public bool fontAutoSize = false;
 27            public string font;
 28
 29            [Header("Text box properties")]
 3930            public string hTextAlign = "bottom";
 31
 3932            public string vTextAlign = "left";
 3933            public float width = 1f;
 3934            public float height = 0.2f;
 35            public float paddingTop = 0f;
 36            public float paddingRight = 0f;
 37            public float paddingBottom = 0f;
 38            public float paddingLeft = 0f;
 39            public float lineSpacing = 0f;
 40            public int lineCount = 0;
 41            public bool textWrapping = false;
 42
 43            [Header("Text shadow properties")]
 44            public float shadowBlur = 0f;
 45
 46            public float shadowOffsetX = 0f;
 47            public float shadowOffsetY = 0f;
 3948            public Color shadowColor = new Color(1, 1, 1);
 49
 50            [Header("Text outline properties")]
 51            public float outlineWidth = 0f;
 52
 3953            public Color outlineColor = Color.white;
 54
 1555            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 56        }
 57
 58        public MeshRenderer meshRenderer;
 59        public TextMeshPro text;
 60        public RectTransform rectTransform;
 61        private Model cachedModel;
 62        private Material cachedFontMaterial;
 63        private Camera mainCamera;
 64
 565        public override string componentName => "text";
 66
 67        private void Awake()
 68        {
 1169            model = new Model();
 1170            mainCamera = Camera.main;
 71
 1172            cachedFontMaterial = new Material(text.fontSharedMaterial);
 1173            text.fontSharedMaterial = cachedFontMaterial;
 1174            text.text = string.Empty;
 75
 1176            Environment.i.platform.updateEventHandler.AddListener(IUpdateEventHandler.EventType.Update, OnUpdate);
 1177        }
 78
 79        private void OnUpdate()
 80        {
 2581            if (cachedModel.billboard && mainCamera != null)
 82            {
 083                transform.forward = mainCamera.transform.forward;
 84            }
 2585        }
 86
 087        new public Model GetModel() { return cachedModel; }
 88
 89        public override IEnumerator ApplyChanges(BaseModel newModel)
 90        {
 1591            if (rectTransform == null)
 092                yield break;
 93
 1594            Model model = (Model) newModel;
 1595            cachedModel = model;
 1596            PrepareRectTransform();
 97
 98            // We avoid using even yield break; as this instruction skips a frame and we don't want that.
 1599            if ( !DCLFont.IsFontLoaded(scene, model.font) )
 100            {
 0101                yield return DCLFont.WaitUntilFontIsReady(scene, model.font);
 102            }
 103
 15104            DCLFont.SetFontFromComponent(scene, model.font, text);
 105
 15106            ApplyModelChanges(text, model);
 107
 15108            if (entity.meshRootGameObject == null)
 11109                entity.meshesInfo.meshRootGameObject = gameObject;
 110
 15111            entity.OnShapeUpdated?.Invoke(entity);
 15112        }
 113
 114        public static void ApplyModelChanges(TMP_Text text, Model model)
 115        {
 15116            text.text = model.value;
 117
 15118            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 15119            text.fontSize = (int) model.fontSize;
 15120            text.richText = true;
 15121            text.overflowMode = TextOverflowModes.Overflow;
 15122            text.enableAutoSizing = model.fontAutoSize;
 123
 15124            text.margin =
 125                new Vector4
 126                (
 127                    (int) model.paddingLeft,
 128                    (int) model.paddingTop,
 129                    (int) model.paddingRight,
 130                    (int) model.paddingBottom
 131                );
 132
 15133            text.alignment = GetAlignment(model.vTextAlign, model.hTextAlign);
 15134            text.lineSpacing = model.lineSpacing;
 135
 15136            if (model.lineCount != 0)
 137            {
 1138                text.maxVisibleLines = Mathf.Max(model.lineCount, 1);
 1139            }
 140            else
 141            {
 14142                text.maxVisibleLines = int.MaxValue;
 143            }
 144
 15145            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 146
 15147            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            }
 15153            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 154            {
 0155                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 156            }
 157
 15158            if (model.outlineWidth > 0f)
 159            {
 0160                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 0161                text.outlineWidth = model.outlineWidth;
 0162                text.outlineColor = model.outlineColor;
 0163            }
 15164            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 165            {
 0166                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 167            }
 15168        }
 169
 170        public static TextAlignmentOptions GetAlignment(string vTextAlign, string hTextAlign)
 171        {
 44172            vTextAlign = vTextAlign.ToLower();
 44173            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:
 36207                            return TextAlignmentOptions.Center;
 208                    }
 209            }
 210        }
 211
 212        private void PrepareRectTransform()
 213        {
 15214            rectTransform.anchorMin = Vector2.zero;
 15215            rectTransform.anchorMax = Vector2.one;
 15216            rectTransform.offsetMin = Vector2.zero;
 15217            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.
 15223            if (cachedModel.textWrapping)
 224            {
 0225                rectTransform.sizeDelta = new Vector2(cachedModel.width, cachedModel.height);
 0226            }
 227            else
 228            {
 15229                rectTransform.sizeDelta = Vector2.zero;
 230            }
 15231        }
 232
 1233        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.TEXT_SHAPE; }
 234
 235        public override void Cleanup()
 236        {
 8237            text.text = string.Empty;
 8238            base.Cleanup();
 8239        }
 240
 241        private void OnDestroy()
 242        {
 11243            Environment.i.platform.updateEventHandler?.RemoveListener(IUpdateEventHandler.EventType.Update, OnUpdate);
 244
 11245            base.Cleanup();
 11246            Destroy(cachedFontMaterial);
 11247        }
 248    }
 249}