< 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:77
Uncovered lines:17
Coverable lines:94
Total lines:266
Line coverage:81.9% (77 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%110100%
ApplyChanges()0%7.187084.62%
ApplyModelChanges(...)0%12.9110069.23%
GetAlignment(...)0%990100%
PrepareRectTransform()0%2.012087.5%
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        private bool cameraFound;
 65
 566        public override string componentName => "text";
 67
 68
 69        private bool CameraFound
 70        {
 71            get
 72            {
 073                if (!cameraFound)
 74                {
 075                    mainCamera = Camera.main;
 076                    if (mainCamera != null)
 077                        cameraFound = true;
 78                }
 79
 080                return cameraFound;
 81            }
 82        }
 83
 84
 85        private void Awake()
 86        {
 1187            model = new Model();
 88
 1189            cachedFontMaterial = new Material(text.fontSharedMaterial);
 1190            text.fontSharedMaterial = cachedFontMaterial;
 1191            text.text = string.Empty;
 92
 1193            Environment.i.platform.updateEventHandler.AddListener(IUpdateEventHandler.EventType.Update, OnUpdate);
 1194        }
 95
 96        private void OnUpdate()
 97        {
 98            // Cameras are not detected while loading, so we can not load the camera on Awake or Start
 2599            if (cachedModel.billboard && CameraFound)
 0100                transform.forward = mainCamera.transform.forward;
 25101        }
 102
 103
 17104        new public Model GetModel() { return cachedModel; }
 105
 106        public override IEnumerator ApplyChanges(BaseModel newModel)
 107        {
 15108            if (rectTransform == null)
 0109                yield break;
 110
 15111            Model model = (Model)newModel;
 15112            cachedModel = model;
 15113            PrepareRectTransform();
 114
 115            // We avoid using even yield break; as this instruction skips a frame and we don't want that.
 15116            if (!DCLFont.IsFontLoaded(scene, model.font))
 117            {
 0118                yield return DCLFont.WaitUntilFontIsReady(scene, model.font);
 119            }
 120
 15121            DCLFont.SetFontFromComponent(scene, model.font, text);
 122
 15123            ApplyModelChanges(text, model);
 124
 15125            if (entity.meshRootGameObject == null)
 11126                entity.meshesInfo.meshRootGameObject = gameObject;
 127
 15128            entity.OnShapeUpdated?.Invoke(entity);
 15129        }
 130
 131        public static void ApplyModelChanges(TMP_Text text, Model model)
 132        {
 15133            text.text = model.value;
 134
 15135            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 15136            text.fontSize = (int)model.fontSize;
 15137            text.richText = true;
 15138            text.overflowMode = TextOverflowModes.Overflow;
 15139            text.enableAutoSizing = model.fontAutoSize;
 140
 15141            text.margin =
 142                new Vector4
 143                (
 144                    (int)model.paddingLeft,
 145                    (int)model.paddingTop,
 146                    (int)model.paddingRight,
 147                    (int)model.paddingBottom
 148                );
 149
 15150            text.alignment = GetAlignment(model.vTextAlign, model.hTextAlign);
 15151            text.lineSpacing = model.lineSpacing;
 152
 15153            if (model.lineCount != 0)
 154            {
 1155                text.maxVisibleLines = Mathf.Max(model.lineCount, 1);
 156            }
 157            else
 158            {
 14159                text.maxVisibleLines = int.MaxValue;
 160            }
 161
 15162            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 163
 15164            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 165            {
 0166                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 0167                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 0168                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 169            }
 15170            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 171            {
 0172                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 173            }
 174
 15175            if (model.outlineWidth > 0f)
 176            {
 0177                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 0178                text.outlineWidth = model.outlineWidth;
 0179                text.outlineColor = model.outlineColor;
 180            }
 15181            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 182            {
 0183                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 184            }
 15185        }
 186
 187        public static TextAlignmentOptions GetAlignment(string vTextAlign, string hTextAlign)
 188        {
 44189            vTextAlign = vTextAlign.ToLower();
 44190            hTextAlign = hTextAlign.ToLower();
 191
 192            switch (vTextAlign)
 193            {
 194                case "top":
 195                    switch (hTextAlign)
 196                    {
 197                        case "left":
 1198                            return TextAlignmentOptions.TopLeft;
 199                        case "right":
 1200                            return TextAlignmentOptions.TopRight;
 201                        default:
 1202                            return TextAlignmentOptions.Top;
 203                    }
 204
 205                case "bottom":
 206                    switch (hTextAlign)
 207                    {
 208                        case "left":
 1209                            return TextAlignmentOptions.BottomLeft;
 210                        case "right":
 1211                            return TextAlignmentOptions.BottomRight;
 212                        default:
 1213                            return TextAlignmentOptions.Bottom;
 214                    }
 215
 216                default: // center
 217                    switch (hTextAlign)
 218                    {
 219                        case "left":
 1220                            return TextAlignmentOptions.Left;
 221                        case "right":
 1222                            return TextAlignmentOptions.Right;
 223                        default:
 36224                            return TextAlignmentOptions.Center;
 225                    }
 226            }
 227        }
 228
 229        private void PrepareRectTransform()
 230        {
 15231            rectTransform.anchorMin = Vector2.zero;
 15232            rectTransform.anchorMax = Vector2.one;
 15233            rectTransform.offsetMin = Vector2.zero;
 15234            rectTransform.offsetMax = Vector2.zero;
 235
 236            // NOTE: previously width and height weren't working (setting sizeDelta before anchors and offset result in
 237            // sizeDelta being reset to 0,0)
 238            // to fix textWrapping and avoid backwards compatibility issues as result of the size being properly set (li
 239            // we only set it if textWrapping is enabled.
 15240            if (cachedModel.textWrapping)
 241            {
 0242                rectTransform.sizeDelta = new Vector2(cachedModel.width, cachedModel.height);
 243            }
 244            else
 245            {
 15246                rectTransform.sizeDelta = Vector2.zero;
 247            }
 15248        }
 249
 1250        public override int GetClassId() { return (int)CLASS_ID_COMPONENT.TEXT_SHAPE; }
 251
 252        public override void Cleanup()
 253        {
 8254            text.text = string.Empty;
 8255            base.Cleanup();
 8256        }
 257
 258        private void OnDestroy()
 259        {
 11260            Environment.i.platform.updateEventHandler?.RemoveListener(IUpdateEventHandler.EventType.Update, OnUpdate);
 261
 11262            base.Cleanup();
 11263            Destroy(cachedFontMaterial);
 11264        }
 265    }
 266}