< Summary

Class:DCL.Components.DCLFont
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Font/DCLFont.cs
Covered lines:28
Uncovered lines:17
Coverable lines:45
Total lines:117
Line coverage:62.2% (28 of 45)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetDataFromJSON(...)0%110100%
DCLFont()0%110100%
GetClassId()0%2100%
IsFontLoaded(...)0%5.024060%
WaitUntilFontIsReady()0%42600%
SetFontFromComponent(...)0%330100%
ApplyChanges()0%6.26082.35%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Font/DCLFont.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using DCL.Controllers;
 4using DCL.Helpers;
 5using DCL.Models;
 6using TMPro;
 7using UnityEngine;
 8
 9namespace DCL.Components
 10{
 11    public class DCLFont : BaseDisposable
 12    {
 13        const string RESOURCE_FONT_PREFIX = "builtin:";
 14        const string RESOURCE_FONT_FOLDER = "Fonts & Materials";
 15
 16        [System.Serializable]
 17        public class Model : BaseModel
 18        {
 19            public string src;
 20
 321            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 22        }
 23
 024        public bool loaded { private set; get; } = false;
 025        public bool error { private set; get; } = false;
 26
 027        public TMP_FontAsset fontAsset { private set; get; }
 28
 929        public DCLFont() { model = new Model(); }
 30
 031        public override int GetClassId() { return (int) CLASS_ID.FONT; }
 32
 33        public static bool IsFontLoaded(IParcelScene scene, string componentId)
 34        {
 3435            if ( string.IsNullOrEmpty(componentId))
 3136                return true;
 37
 338            if (!scene.disposableComponents.ContainsKey(componentId))
 39            {
 040                Debug.Log($"couldn't fetch font, the DCLFont component with id {componentId} doesn't exist");
 041                return false;
 42            }
 43
 344            DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont;
 45
 346            if (fontComponent == null)
 47            {
 048                Debug.Log($"couldn't fetch font, the shared component with id {componentId} is NOT a DCLFont");
 049                return false;
 50            }
 51
 352            return true;
 53        }
 54
 55        public static IEnumerator WaitUntilFontIsReady(IParcelScene scene, string componentId)
 56        {
 057            if ( string.IsNullOrEmpty(componentId))
 058                yield break;
 59
 060            DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont;
 61
 062            while (!fontComponent.loaded && !fontComponent.error)
 63            {
 064                yield return null;
 65            }
 066        }
 67
 68        public static void SetFontFromComponent(IParcelScene scene, string componentId, TMP_Text text)
 69        {
 3470            if ( string.IsNullOrEmpty(componentId))
 3171                return;
 72
 373            DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont;
 74
 375            if (!fontComponent.error)
 76            {
 277                text.font = fontComponent.fontAsset;
 78            }
 379        }
 80
 81        public override IEnumerator ApplyChanges(BaseModel newModel)
 82        {
 383            Model model = (Model) newModel;
 84
 385            if (string.IsNullOrEmpty(model.src))
 86            {
 087                error = true;
 088                yield break;
 89            }
 90
 391            if (model.src.StartsWith(RESOURCE_FONT_PREFIX))
 92            {
 293                string resourceName = model.src.Substring(RESOURCE_FONT_PREFIX.Length);
 94
 295                ResourceRequest request = Resources.LoadAsync(string.Format("{0}/{1}", RESOURCE_FONT_FOLDER, resourceNam
 296                yield return request;
 97
 298                if (request.asset != null)
 99                {
 2100                    fontAsset = request.asset as TMP_FontAsset;
 2101                }
 102                else
 103                {
 0104                    Debug.Log($"couldn't fetch font from resources {resourceName}");
 105                }
 106
 2107                loaded = true;
 2108                error = fontAsset == null;
 2109            }
 110            else
 111            {
 112                // NOTE: only support fonts in resources
 1113                error = true;
 114            }
 3115        }
 116    }
 117}