| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Models; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace 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 | |
|
| 3 | 21 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 22 | | } |
| | 23 | |
|
| 0 | 24 | | public bool loaded { private set; get; } = false; |
| 0 | 25 | | public bool error { private set; get; } = false; |
| | 26 | |
|
| 0 | 27 | | public TMP_FontAsset fontAsset { private set; get; } |
| | 28 | |
|
| 9 | 29 | | public DCLFont() { model = new Model(); } |
| | 30 | |
|
| 0 | 31 | | public override int GetClassId() { return (int) CLASS_ID.FONT; } |
| | 32 | |
|
| | 33 | | public static bool IsFontLoaded(IParcelScene scene, string componentId) |
| | 34 | | { |
| 34 | 35 | | if ( string.IsNullOrEmpty(componentId)) |
| 31 | 36 | | return true; |
| | 37 | |
|
| 3 | 38 | | if (!scene.disposableComponents.ContainsKey(componentId)) |
| | 39 | | { |
| 0 | 40 | | Debug.Log($"couldn't fetch font, the DCLFont component with id {componentId} doesn't exist"); |
| 0 | 41 | | return false; |
| | 42 | | } |
| | 43 | |
|
| 3 | 44 | | DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont; |
| | 45 | |
|
| 3 | 46 | | if (fontComponent == null) |
| | 47 | | { |
| 0 | 48 | | Debug.Log($"couldn't fetch font, the shared component with id {componentId} is NOT a DCLFont"); |
| 0 | 49 | | return false; |
| | 50 | | } |
| | 51 | |
|
| 3 | 52 | | return true; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public static IEnumerator WaitUntilFontIsReady(IParcelScene scene, string componentId) |
| | 56 | | { |
| 0 | 57 | | if ( string.IsNullOrEmpty(componentId)) |
| 0 | 58 | | yield break; |
| | 59 | |
|
| 0 | 60 | | DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont; |
| | 61 | |
|
| 0 | 62 | | while (!fontComponent.loaded && !fontComponent.error) |
| | 63 | | { |
| 0 | 64 | | yield return null; |
| | 65 | | } |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public static void SetFontFromComponent(IParcelScene scene, string componentId, TMP_Text text) |
| | 69 | | { |
| 34 | 70 | | if ( string.IsNullOrEmpty(componentId)) |
| 31 | 71 | | return; |
| | 72 | |
|
| 3 | 73 | | DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont; |
| | 74 | |
|
| 3 | 75 | | if (!fontComponent.error) |
| | 76 | | { |
| 2 | 77 | | text.font = fontComponent.fontAsset; |
| | 78 | | } |
| 3 | 79 | | } |
| | 80 | |
|
| | 81 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 82 | | { |
| 3 | 83 | | Model model = (Model) newModel; |
| | 84 | |
|
| 3 | 85 | | if (string.IsNullOrEmpty(model.src)) |
| | 86 | | { |
| 0 | 87 | | error = true; |
| 0 | 88 | | yield break; |
| | 89 | | } |
| | 90 | |
|
| 3 | 91 | | if (model.src.StartsWith(RESOURCE_FONT_PREFIX)) |
| | 92 | | { |
| 2 | 93 | | string resourceName = model.src.Substring(RESOURCE_FONT_PREFIX.Length); |
| | 94 | |
|
| 2 | 95 | | ResourceRequest request = Resources.LoadAsync(string.Format("{0}/{1}", RESOURCE_FONT_FOLDER, resourceNam |
| 2 | 96 | | yield return request; |
| | 97 | |
|
| 2 | 98 | | if (request.asset != null) |
| | 99 | | { |
| 2 | 100 | | fontAsset = request.asset as TMP_FontAsset; |
| 2 | 101 | | } |
| | 102 | | else |
| | 103 | | { |
| 0 | 104 | | Debug.Log($"couldn't fetch font from resources {resourceName}"); |
| | 105 | | } |
| | 106 | |
|
| 2 | 107 | | loaded = true; |
| 2 | 108 | | error = fontAsset == null; |
| 2 | 109 | | } |
| | 110 | | else |
| | 111 | | { |
| | 112 | | // NOTE: only support fonts in resources |
| 1 | 113 | | error = true; |
| | 114 | | } |
| 3 | 115 | | } |
| | 116 | | } |
| | 117 | | } |