| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.UIElements.Image; |
| | 3 | | using Decentraland.Common; |
| | 4 | | using JetBrains.Annotations; |
| | 5 | |
|
| | 6 | | namespace DCL.ECSComponents.Utils |
| | 7 | | { |
| | 8 | | public struct UITextureUpdater |
| | 9 | | { |
| | 10 | | private TextureUnion lastTexture; |
| | 11 | |
|
| | 12 | | private AssetPromise_Texture lastPromise; |
| | 13 | | private readonly IUITextureConsumer target; |
| | 14 | | private readonly AssetPromiseKeeper_Texture texturePromiseKeeper; |
| | 15 | |
|
| | 16 | | public UITextureUpdater(IUITextureConsumer target, AssetPromiseKeeper_Texture texturePromiseKeeper) |
| | 17 | | { |
| 1 | 18 | | this.target = target; |
| 1 | 19 | | this.texturePromiseKeeper = texturePromiseKeeper; |
| 1 | 20 | | lastPromise = null; |
| 1 | 21 | | lastTexture = null; |
| 1 | 22 | | } |
| | 23 | |
|
| | 24 | | public void Update([CanBeNull] TextureUnion texture, IParcelScene parcelScene) |
| | 25 | | { |
| 1 | 26 | | if (Equals(lastTexture, texture)) |
| 1 | 27 | | return; |
| | 28 | |
|
| 0 | 29 | | lastTexture = texture; |
| | 30 | |
|
| 0 | 31 | | var prevPromise = lastPromise; |
| 0 | 32 | | texturePromiseKeeper.Forget(prevPromise); |
| | 33 | |
|
| 0 | 34 | | if (texture == null) |
| | 35 | | { |
| 0 | 36 | | target.Texture = null; |
| 0 | 37 | | lastPromise = null; |
| | 38 | | } |
| | 39 | | else |
| | 40 | | { |
| 0 | 41 | | lastPromise = new AssetPromise_Texture(texture.GetTextureUrl(parcelScene), texture.GetWrapMode(), textur |
| 0 | 42 | | lastPromise.OnSuccessEvent += OnTextureDownloaded; |
| 0 | 43 | | texturePromiseKeeper.Keep(lastPromise); |
| | 44 | | } |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | private void OnTextureDownloaded(Asset_Texture texture) |
| | 48 | | { |
| 0 | 49 | | target.Texture = texture.texture; |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | public void Dispose() |
| | 53 | | { |
| 0 | 54 | | texturePromiseKeeper.Forget(lastPromise); |
| 0 | 55 | | } |
| | 56 | | } |
| | 57 | | } |