| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | public interface IImageComponentView |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// It will be triggered when the sprite has been loaded. |
| | 10 | | /// </summary> |
| | 11 | | event Action<Sprite> OnLoaded; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Fill the model and updates the image with this data. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="model">Data to configure the image.</param> |
| | 17 | | void Configure(ImageComponentModel model); |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Set an image directly from a sprite. |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="sprite">A sprite.</param> |
| | 23 | | void SetImage(Sprite sprite); |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Set an image from a 2D texture, |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="texture">2D texture.</param> |
| | 29 | | void SetImage(Texture2D texture); |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Set an image from an uri. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="uri">Url of the image.</param> |
| | 35 | | void SetImage(string uri); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Active or deactive the loading indicator. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="isVisible">True for showing the loading indicator and hiding the image.</param> |
| | 41 | | void SetLoadingIndicatorVisible(bool isVisible); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | public class ImageComponentView : BaseComponentView, IImageComponentView |
| | 45 | | { |
| | 46 | | [Header("Prefab References")] |
| | 47 | | [SerializeField] internal Image image; |
| | 48 | | [SerializeField] internal GameObject loadingIndicator; |
| | 49 | |
|
| | 50 | | [Header("Configuration")] |
| | 51 | | [SerializeField] internal ImageComponentModel model; |
| | 52 | |
|
| | 53 | | public event Action<Sprite> OnLoaded; |
| | 54 | |
|
| | 55 | | internal Sprite currentSprite; |
| 462 | 56 | | internal ILazyTextureObserver imageObserver = new LazyTextureObserver(); |
| | 57 | |
|
| | 58 | | public override void PostInitialization() |
| | 59 | | { |
| 63 | 60 | | imageObserver.AddListener(OnImageObserverUpdated); |
| 63 | 61 | | Configure(model); |
| 63 | 62 | | } |
| | 63 | |
|
| | 64 | | public void Configure(ImageComponentModel model) |
| | 65 | | { |
| 64 | 66 | | this.model = model; |
| 64 | 67 | | RefreshControl(); |
| 64 | 68 | | } |
| | 69 | |
|
| | 70 | | public override void RefreshControl() |
| | 71 | | { |
| 65 | 72 | | if (model == null) |
| 0 | 73 | | return; |
| | 74 | |
|
| 65 | 75 | | if (model.sprite != null) |
| 65 | 76 | | SetImage(model.sprite); |
| 0 | 77 | | else if (model.texture != null) |
| 0 | 78 | | SetImage(model.texture); |
| 0 | 79 | | else if (!string.IsNullOrEmpty(model.uri)) |
| 0 | 80 | | SetImage(model.uri); |
| | 81 | | else |
| 0 | 82 | | SetImage(sprite: null); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public override void Dispose() |
| | 86 | | { |
| 99 | 87 | | base.Dispose(); |
| | 88 | |
|
| 99 | 89 | | imageObserver.RemoveListener(OnImageObserverUpdated); |
| 99 | 90 | | Destroy(currentSprite); |
| 99 | 91 | | } |
| | 92 | |
|
| | 93 | | public void SetImage(Sprite sprite) |
| | 94 | | { |
| 86 | 95 | | model.sprite = sprite; |
| | 96 | |
|
| 86 | 97 | | if (image == null) |
| 0 | 98 | | return; |
| | 99 | |
|
| 86 | 100 | | image.sprite = sprite; |
| 86 | 101 | | } |
| | 102 | |
|
| | 103 | | public void SetImage(Texture2D texture) |
| | 104 | | { |
| 0 | 105 | | model.texture = texture; |
| | 106 | |
|
| 0 | 107 | | if (!Application.isPlaying) |
| | 108 | | { |
| 0 | 109 | | OnImageObserverUpdated(texture); |
| 0 | 110 | | return; |
| | 111 | | } |
| | 112 | |
|
| 0 | 113 | | imageObserver.RefreshWithTexture(texture); |
| 0 | 114 | | SetLoadingIndicatorVisible(true); |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | public void SetImage(string uri) |
| | 118 | | { |
| 0 | 119 | | model.uri = uri; |
| | 120 | |
|
| 0 | 121 | | if (!Application.isPlaying) |
| 0 | 122 | | return; |
| | 123 | |
|
| 0 | 124 | | imageObserver.RefreshWithUri(uri); |
| 0 | 125 | | SetLoadingIndicatorVisible(true); |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | public void SetLoadingIndicatorVisible(bool isVisible) |
| | 129 | | { |
| 4 | 130 | | image.enabled = !isVisible; |
| 4 | 131 | | loadingIndicator.SetActive(isVisible); |
| 4 | 132 | | } |
| | 133 | |
|
| | 134 | | internal void OnImageObserverUpdated(Texture texture) |
| | 135 | | { |
| 0 | 136 | | if (Application.isPlaying) |
| 0 | 137 | | Destroy(currentSprite); |
| | 138 | | else |
| 0 | 139 | | DestroyImmediate(currentSprite); |
| | 140 | |
|
| 0 | 141 | | currentSprite = Sprite.Create((Texture2D)texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5 |
| 0 | 142 | | SetImage(currentSprite); |
| 0 | 143 | | SetLoadingIndicatorVisible(false); |
| 0 | 144 | | OnLoaded?.Invoke(currentSprite); |
| 0 | 145 | | } |
| | 146 | | } |