| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace NFTShape_Internal |
| | 6 | | { |
| | 7 | | public class NFTAsset_Gif : INFTAsset |
| | 8 | | { |
| | 9 | | private const int RESOLUTION_HQ = 512; |
| 0 | 10 | | public bool isHQ => hqGifPromise != null; |
| 0 | 11 | | public ITexture previewAsset => previewGif; |
| 0 | 12 | | public ITexture hqAsset => hqGifPromise?.asset; |
| | 13 | |
|
| | 14 | | private AssetPromise_Gif hqGifPromise; |
| | 15 | |
|
| | 16 | | private readonly GifPlayer gifPlayer; |
| | 17 | | private readonly Asset_Gif previewGif; |
| | 18 | | public event Action<Texture2D> OnTextureUpdate; |
| | 19 | |
|
| 2 | 20 | | public NFTAsset_Gif(Asset_Gif previewGif) |
| | 21 | | { |
| 2 | 22 | | this.previewGif = previewGif; |
| 2 | 23 | | this.gifPlayer = new GifPlayer(previewGif); |
| | 24 | |
|
| 2 | 25 | | gifPlayer.Play(); |
| 2 | 26 | | gifPlayer.OnFrameTextureChanged += OnFrameTextureChanged; |
| 2 | 27 | | } |
| | 28 | |
|
| | 29 | | void OnFrameTextureChanged(Texture2D texture) |
| | 30 | | { |
| 0 | 31 | | OnTextureUpdate?.Invoke(texture); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Dispose() |
| | 35 | | { |
| 0 | 36 | | if (hqGifPromise != null) |
| | 37 | | { |
| 0 | 38 | | AssetPromiseKeeper_Gif.i.Forget(hqGifPromise); |
| 0 | 39 | | hqGifPromise = null; |
| | 40 | | } |
| | 41 | |
|
| 0 | 42 | | gifPlayer.Dispose(); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | public void FetchAndSetHQAsset(string url, Action onSuccess, Action<Exception> onFail) |
| | 46 | | { |
| 0 | 47 | | hqGifPromise = new AssetPromise_Gif($"{url}=s{RESOLUTION_HQ}"); |
| | 48 | |
|
| 0 | 49 | | hqGifPromise.OnSuccessEvent += (asset) => |
| | 50 | | { |
| 0 | 51 | | gifPlayer?.SetGif(asset); |
| 0 | 52 | | onSuccess?.Invoke(); |
| 0 | 53 | | }; |
| 0 | 54 | | hqGifPromise.OnFailEvent += (asset, error) => |
| | 55 | | { |
| 0 | 56 | | hqGifPromise = null; |
| 0 | 57 | | onFail?.Invoke(error); |
| 0 | 58 | | }; |
| | 59 | |
|
| 0 | 60 | | AssetPromiseKeeper_Gif.i.Keep(hqGifPromise); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | public void RestorePreviewAsset() |
| | 64 | | { |
| 0 | 65 | | gifPlayer?.SetGif(previewGif); |
| | 66 | |
|
| 0 | 67 | | if (hqGifPromise != null) |
| | 68 | | { |
| 0 | 69 | | AssetPromiseKeeper_Gif.i.Forget(hqGifPromise); |
| 0 | 70 | | hqGifPromise = null; |
| | 71 | | } |
| 0 | 72 | | } |
| | 73 | | } |
| | 74 | | } |