| | 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_FOLDER = "Fonts & Materials"; |
| | 14 | |
|
| | 15 | | private const string DEFAULT_SANS_SERIF_HEAVY = "Inter-Heavy SDF"; |
| | 16 | | private const string DEFAULT_SANS_SERIF_BOLD = "Inter-Bold SDF"; |
| | 17 | | private const string DEFAULT_SANS_SERIF_SEMIBOLD = "Inter-SemiBold SDF"; |
| | 18 | | private const string DEFAULT_SANS_SERIF = "Inter-Regular SDF"; |
| | 19 | |
|
| 3 | 20 | | private readonly Dictionary<string, string> fontsMapping = new Dictionary<string, string>() |
| | 21 | | { |
| | 22 | | { "builtin:SF-UI-Text-Regular SDF", DEFAULT_SANS_SERIF }, |
| | 23 | | { "builtin:SF-UI-Text-Heavy SDF", DEFAULT_SANS_SERIF_HEAVY }, |
| | 24 | | { "builtin:SF-UI-Text-Semibold SDF", DEFAULT_SANS_SERIF_SEMIBOLD }, |
| | 25 | | { "builtin:LiberationSans SDF", "LiberationSans SDF" }, |
| | 26 | | { "SansSerif", DEFAULT_SANS_SERIF }, |
| | 27 | | { "SansSerif_Heavy", DEFAULT_SANS_SERIF_HEAVY }, |
| | 28 | | { "SansSerif_Bold", DEFAULT_SANS_SERIF_BOLD }, |
| | 29 | | { "SansSerif_SemiBold", DEFAULT_SANS_SERIF_SEMIBOLD }, |
| | 30 | | }; |
| | 31 | |
|
| | 32 | | [System.Serializable] |
| | 33 | | public class Model : BaseModel |
| | 34 | | { |
| | 35 | | public string src; |
| | 36 | |
|
| 3 | 37 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | public bool loaded { private set; get; } = false; |
| 0 | 41 | | public bool error { private set; get; } = false; |
| | 42 | |
|
| 0 | 43 | | public TMP_FontAsset fontAsset { private set; get; } |
| | 44 | |
|
| 9 | 45 | | public DCLFont() { model = new Model(); } |
| | 46 | |
|
| 0 | 47 | | public override int GetClassId() { return (int) CLASS_ID.FONT; } |
| | 48 | |
|
| | 49 | | public static bool IsFontLoaded(IParcelScene scene, string componentId) |
| | 50 | | { |
| 34 | 51 | | if ( string.IsNullOrEmpty(componentId)) |
| 31 | 52 | | return true; |
| | 53 | |
|
| 3 | 54 | | if (!scene.disposableComponents.ContainsKey(componentId)) |
| | 55 | | { |
| 0 | 56 | | Debug.Log($"couldn't fetch font, the DCLFont component with id {componentId} doesn't exist"); |
| 0 | 57 | | return false; |
| | 58 | | } |
| | 59 | |
|
| 3 | 60 | | DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont; |
| | 61 | |
|
| 3 | 62 | | if (fontComponent == null) |
| | 63 | | { |
| 0 | 64 | | Debug.Log($"couldn't fetch font, the shared component with id {componentId} is NOT a DCLFont"); |
| 0 | 65 | | return false; |
| | 66 | | } |
| | 67 | |
|
| 3 | 68 | | return true; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | public static IEnumerator WaitUntilFontIsReady(IParcelScene scene, string componentId) |
| | 72 | | { |
| 0 | 73 | | if ( string.IsNullOrEmpty(componentId)) |
| 0 | 74 | | yield break; |
| | 75 | |
|
| 0 | 76 | | DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont; |
| | 77 | |
|
| 0 | 78 | | while (!fontComponent.loaded && !fontComponent.error) |
| | 79 | | { |
| 0 | 80 | | yield return null; |
| | 81 | | } |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | public static void SetFontFromComponent(IParcelScene scene, string componentId, TMP_Text text) |
| | 85 | | { |
| 34 | 86 | | if ( string.IsNullOrEmpty(componentId)) |
| 31 | 87 | | return; |
| | 88 | |
|
| 3 | 89 | | DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont; |
| | 90 | |
|
| 3 | 91 | | if (!fontComponent.error) |
| | 92 | | { |
| 2 | 93 | | text.font = fontComponent.fontAsset; |
| | 94 | | } |
| 3 | 95 | | } |
| | 96 | |
|
| | 97 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 98 | | { |
| 3 | 99 | | Model model = (Model) newModel; |
| | 100 | |
|
| 3 | 101 | | if (string.IsNullOrEmpty(model.src)) |
| | 102 | | { |
| 0 | 103 | | error = true; |
| 0 | 104 | | yield break; |
| | 105 | | } |
| | 106 | |
|
| 3 | 107 | | if (fontsMapping.TryGetValue(model.src, out string fontResourceName)) |
| | 108 | | { |
| 2 | 109 | | ResourceRequest request = Resources.LoadAsync($"{RESOURCE_FONT_FOLDER}/{fontResourceName}", |
| | 110 | | typeof(TMP_FontAsset)); |
| | 111 | |
|
| 2 | 112 | | yield return request; |
| | 113 | |
|
| 2 | 114 | | if (request.asset != null) |
| | 115 | | { |
| 2 | 116 | | fontAsset = request.asset as TMP_FontAsset; |
| 2 | 117 | | } |
| | 118 | | else |
| | 119 | | { |
| 0 | 120 | | Debug.Log($"couldn't fetch font from resources {fontResourceName}"); |
| | 121 | | } |
| | 122 | |
|
| 2 | 123 | | loaded = true; |
| 2 | 124 | | error = fontAsset == null; |
| 2 | 125 | | } |
| | 126 | | else |
| | 127 | | { |
| | 128 | | // NOTE: only support fonts in resources |
| 1 | 129 | | error = true; |
| | 130 | | } |
| 3 | 131 | | } |
| | 132 | | } |
| | 133 | | } |