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