| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class AssetPromise_Font : AssetPromise<Asset_Font> |
| | 10 | | { |
| | 11 | | private const string RESOURCE_FONT_FOLDER = "Fonts & Materials"; |
| | 12 | | private const string DEFAULT_SANS_SERIF_HEAVY = "Inter-Heavy SDF"; |
| | 13 | | private const string DEFAULT_SANS_SERIF_BOLD = "Inter-Bold SDF"; |
| | 14 | | private const string DEFAULT_SANS_SERIF_SEMIBOLD = "Inter-SemiBold SDF"; |
| | 15 | | private const string DEFAULT_SANS_SERIF = "Inter-Regular SDF"; |
| | 16 | |
|
| 38 | 17 | | private readonly Dictionary<string, string> fontsMapping = new Dictionary<string, string>() |
| | 18 | | { |
| | 19 | | { "builtin:SF-UI-Text-Regular SDF", DEFAULT_SANS_SERIF }, |
| | 20 | | { "builtin:SF-UI-Text-Heavy SDF", DEFAULT_SANS_SERIF_HEAVY }, |
| | 21 | | { "builtin:SF-UI-Text-Semibold SDF", DEFAULT_SANS_SERIF_SEMIBOLD }, |
| | 22 | | { "builtin:LiberationSans SDF", "LiberationSans SDF" }, |
| | 23 | | { "SansSerif", DEFAULT_SANS_SERIF }, |
| | 24 | | { "SansSerif_Heavy", DEFAULT_SANS_SERIF_HEAVY }, |
| | 25 | | { "SansSerif_Bold", DEFAULT_SANS_SERIF_BOLD }, |
| | 26 | | { "SansSerif_SemiBold", DEFAULT_SANS_SERIF_SEMIBOLD }, |
| | 27 | | }; |
| | 28 | |
|
| | 29 | | private string src; |
| | 30 | | private Coroutine fontCoroutine; |
| | 31 | |
|
| 38 | 32 | | public AssetPromise_Font(string src) |
| | 33 | | { |
| 38 | 34 | | this.src = src; |
| 38 | 35 | | } |
| | 36 | |
|
| 17 | 37 | | protected override void OnAfterLoadOrReuse() { } |
| | 38 | |
|
| 39 | 39 | | protected override void OnBeforeLoadOrReuse() { } |
| | 40 | |
|
| | 41 | | protected override void OnCancelLoading() |
| | 42 | | { |
| 22 | 43 | | CoroutineStarter.Stop(fontCoroutine); |
| 22 | 44 | | } |
| | 45 | |
|
| | 46 | | public override void Cleanup() |
| | 47 | | { |
| 34 | 48 | | base.Cleanup(); |
| 34 | 49 | | CoroutineStarter.Stop(fontCoroutine); |
| 34 | 50 | | } |
| | 51 | |
|
| | 52 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) |
| | 53 | | { |
| 33 | 54 | | if (fontsMapping.TryGetValue(src, out string fontResourceName)) |
| | 55 | | { |
| 33 | 56 | | fontCoroutine = CoroutineStarter.Start(GetFontFromResources(OnSuccess, OnFail, fontResourceName)); |
| 33 | 57 | | } |
| | 58 | | else |
| | 59 | | { |
| 0 | 60 | | OnFail?.Invoke(new Exception("Font doesn't correspond with any know font")); |
| | 61 | | } |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | protected override bool AddToLibrary() |
| | 65 | | { |
| 11 | 66 | | if (!library.Add(asset)) |
| | 67 | | { |
| 0 | 68 | | return false; |
| | 69 | | } |
| | 70 | |
|
| 11 | 71 | | asset = library.Get(asset.id); |
| 11 | 72 | | return true; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public override object GetId() |
| | 76 | | { |
| 197 | 77 | | return src; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | IEnumerator GetFontFromResources(Action OnSuccess, Action<Exception> OnFail, string fontResourceName) |
| | 81 | | { |
| 33 | 82 | | ResourceRequest request = Resources.LoadAsync($"{RESOURCE_FONT_FOLDER}/{fontResourceName}", |
| | 83 | | typeof(TMP_FontAsset)); |
| | 84 | |
|
| 33 | 85 | | yield return request; |
| | 86 | |
|
| 11 | 87 | | if (request.asset != null) |
| | 88 | | { |
| 11 | 89 | | asset.font = request.asset as TMP_FontAsset; |
| 11 | 90 | | OnSuccess?.Invoke(); |
| 11 | 91 | | } |
| | 92 | | else |
| | 93 | | { |
| 0 | 94 | | string message = $"couldn't fetch font from resources {fontResourceName}"; |
| 0 | 95 | | Debug.Log(message); |
| 0 | 96 | | OnFail?.Invoke(new Exception(message)); |
| | 97 | | } |
| 11 | 98 | | } |
| | 99 | | } |
| | 100 | | } |