| | 1 | | using UnityEngine.UIElements; |
| | 2 | |
|
| | 3 | | namespace DCL.ECSComponents.Utils |
| | 4 | | { |
| | 5 | | public struct UIFontUpdater |
| | 6 | | { |
| | 7 | | private Font? lastFont; |
| | 8 | | private AssetPromise_Font lastPromise; |
| | 9 | | private readonly VisualElement targetElement; |
| | 10 | | private readonly AssetPromiseKeeper_Font fontPromiseKeeper; |
| | 11 | |
|
| | 12 | | public UIFontUpdater(VisualElement targetElement, AssetPromiseKeeper_Font fontPromiseKeeper) |
| | 13 | | { |
| 13 | 14 | | this.targetElement = targetElement; |
| 13 | 15 | | this.fontPromiseKeeper = fontPromiseKeeper; |
| 13 | 16 | | lastFont = null; |
| 13 | 17 | | lastPromise = null; |
| 13 | 18 | | } |
| | 19 | |
|
| | 20 | | public void Update(Font newFont) |
| | 21 | | { |
| 10 | 22 | | if (lastFont == newFont) |
| 0 | 23 | | return; |
| | 24 | |
|
| 10 | 25 | | lastFont = newFont; |
| 10 | 26 | | var prevPromise = lastPromise; |
| | 27 | |
|
| 10 | 28 | | lastPromise = new AssetPromise_Font(newFont.ToFontName()); |
| 10 | 29 | | lastPromise.OnSuccessEvent += ChangeFont; |
| 10 | 30 | | fontPromiseKeeper.Keep(lastPromise); |
| 10 | 31 | | fontPromiseKeeper.Forget(prevPromise); |
| 10 | 32 | | } |
| | 33 | |
|
| | 34 | | private void ChangeFont(Asset_Font font) |
| | 35 | | { |
| 9 | 36 | | targetElement.style.unityFont = font.font.sourceFontFile; |
| 9 | 37 | | } |
| | 38 | |
|
| | 39 | | public void Dispose() |
| | 40 | | { |
| 6 | 41 | | fontPromiseKeeper.Forget(lastPromise); |
| 6 | 42 | | } |
| | 43 | | } |
| | 44 | | } |