< 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:84
Uncovered lines:16
Coverable lines:100
Total lines:257
Line coverage:84% (84 of 100)
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%8.098088.89%
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            }
 85
 2586            var opacity = cachedModel.visible ? cachedModel.opacity : 0;
 2587            var totalSize = cachedModel.fontSize;
 2588            var totalScale = text.transform.lossyScale.sqrMagnitude;
 2589            bool isVisible = opacity > 0 && totalSize > 0 && totalScale > 0 && entity.isInsideSceneBoundaries;
 90
 2591            text.enabled = isVisible;
 2592            meshRenderer.enabled = isVisible;
 2593        }
 94
 095        new public Model GetModel() { return cachedModel; }
 96
 97        public override IEnumerator ApplyChanges(BaseModel newModel)
 98        {
 1599            if (rectTransform == null)
 0100                yield break;
 101
 15102            Model model = (Model) newModel;
 15103            cachedModel = model;
 15104            PrepareRectTransform();
 105
 106            // We avoid using even yield break; as this instruction skips a frame and we don't want that.
 15107            if ( !DCLFont.IsFontLoaded(scene, model.font) )
 108            {
 0109                yield return DCLFont.WaitUntilFontIsReady(scene, model.font);
 110            }
 111
 15112            DCLFont.SetFontFromComponent(scene, model.font, text);
 113
 15114            ApplyModelChanges(text, model);
 115
 15116            if (entity.meshRootGameObject == null)
 11117                entity.meshesInfo.meshRootGameObject = gameObject;
 118
 15119            entity.OnShapeUpdated?.Invoke(entity);
 15120        }
 121
 122        public static void ApplyModelChanges(TMP_Text text, Model model)
 123        {
 15124            text.text = model.value;
 125
 15126            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 15127            text.fontSize = (int) model.fontSize;
 15128            text.richText = true;
 15129            text.overflowMode = TextOverflowModes.Overflow;
 15130            text.enableAutoSizing = model.fontAutoSize;
 131
 15132            text.margin =
 133                new Vector4
 134                (
 135                    (int) model.paddingLeft,
 136                    (int) model.paddingTop,
 137                    (int) model.paddingRight,
 138                    (int) model.paddingBottom
 139                );
 140
 15141            text.alignment = GetAlignment(model.vTextAlign, model.hTextAlign);
 15142            text.lineSpacing = model.lineSpacing;
 143
 15144            if (model.lineCount != 0)
 145            {
 1146                text.maxVisibleLines = Mathf.Max(model.lineCount, 1);
 1147            }
 148            else
 149            {
 14150                text.maxVisibleLines = int.MaxValue;
 151            }
 152
 15153            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 154
 15155            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 156            {
 0157                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 0158                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 0159                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 0160            }
 15161            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 162            {
 0163                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 164            }
 165
 15166            if (model.outlineWidth > 0f)
 167            {
 0168                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 0169                text.outlineWidth = model.outlineWidth;
 0170                text.outlineColor = model.outlineColor;
 0171            }
 15172            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 173            {
 0174                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 175            }
 15176        }
 177
 178        public static TextAlignmentOptions GetAlignment(string vTextAlign, string hTextAlign)
 179        {
 44180            vTextAlign = vTextAlign.ToLower();
 44181            hTextAlign = hTextAlign.ToLower();
 182
 183            switch (vTextAlign)
 184            {
 185                case "top":
 186                    switch (hTextAlign)
 187                    {
 188                        case "left":
 1189                            return TextAlignmentOptions.TopLeft;
 190                        case "right":
 1191                            return TextAlignmentOptions.TopRight;
 192                        default:
 1193                            return TextAlignmentOptions.Top;
 194                    }
 195
 196                case "bottom":
 197                    switch (hTextAlign)
 198                    {
 199                        case "left":
 1200                            return TextAlignmentOptions.BottomLeft;
 201                        case "right":
 1202                            return TextAlignmentOptions.BottomRight;
 203                        default:
 1204                            return TextAlignmentOptions.Bottom;
 205                    }
 206
 207                default: // center
 208                    switch (hTextAlign)
 209                    {
 210                        case "left":
 1211                            return TextAlignmentOptions.Left;
 212                        case "right":
 1213                            return TextAlignmentOptions.Right;
 214                        default:
 36215                            return TextAlignmentOptions.Center;
 216                    }
 217            }
 218        }
 219
 220        private void PrepareRectTransform()
 221        {
 15222            rectTransform.anchorMin = Vector2.zero;
 15223            rectTransform.anchorMax = Vector2.one;
 15224            rectTransform.offsetMin = Vector2.zero;
 15225            rectTransform.offsetMax = Vector2.zero;
 226
 227            // NOTE: previously width and height weren't working (setting sizeDelta before anchors and offset result in
 228            // sizeDelta being reset to 0,0)
 229            // to fix textWrapping and avoid backwards compatibility issues as result of the size being properly set (li
 230            // we only set it if textWrapping is enabled.
 15231            if (cachedModel.textWrapping)
 232            {
 0233                rectTransform.sizeDelta = new Vector2(cachedModel.width, cachedModel.height);
 0234            }
 235            else
 236            {
 15237                rectTransform.sizeDelta = Vector2.zero;
 238            }
 15239        }
 240
 1241        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.TEXT_SHAPE; }
 242
 243        public override void Cleanup()
 244        {
 8245            text.text = string.Empty;
 8246            base.Cleanup();
 8247        }
 248
 249        private void OnDestroy()
 250        {
 11251            Environment.i.platform.updateEventHandler?.RemoveListener(IUpdateEventHandler.EventType.Update, OnUpdate);
 252
 11253            base.Cleanup();
 11254            Destroy(cachedFontMaterial);
 11255        }
 256    }
 257}