| | 1 | | using DCL.Helpers; |
| | 2 | | using MainScripts.DCL.Helpers.Utils; |
| | 3 | | using System; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | | using Object = UnityEngine.Object; |
| | 7 | |
|
| | 8 | | public interface IImageComponentView |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// It will be triggered when the sprite has been loaded. |
| | 12 | | /// </summary> |
| | 13 | | event Action<Sprite> OnLoaded; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// Set an image directly from a sprite. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="sprite">A sprite.</param> |
| | 19 | | void SetImage(Sprite sprite, bool cleanLastLoadedUri = true); |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Set an image from a 2D texture, |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="texture">2D texture.</param> |
| | 25 | | void SetImage(Texture2D texture); |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Set an image from an uri. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="uri">Url of the image.</param> |
| | 31 | | void SetImage(string uri); |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Indicates if we want to cache the last uri request. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="isEnabled">True for caching the last uri request.</param> |
| | 37 | | void SetLastUriRequestCached(bool isEnabled); |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Resize the image size to fit into the parent. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="fitParent">True to fit the size.</param> |
| | 43 | | void SetFitParent(bool fitParent); |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Active or deactive the loading indicator. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="isVisible">True for showing the loading indicator and hiding the image.</param> |
| | 49 | | void SetLoadingIndicatorVisible(bool isVisible); |
| | 50 | | } |
| | 51 | |
|
| | 52 | | public class ImageComponentView : BaseComponentView, IImageComponentView, IComponentModelConfig<ImageComponentModel> |
| | 53 | | { |
| | 54 | | [Header("Prefab References")] |
| | 55 | | [SerializeField] internal Image image; |
| | 56 | | [SerializeField] internal GameObject loadingIndicator; |
| | 57 | |
|
| | 58 | | [Header("Configuration")] |
| | 59 | | [SerializeField] internal ImageComponentModel model; |
| | 60 | |
|
| | 61 | | public event Action<Sprite> OnLoaded; |
| | 62 | |
|
| | 63 | | internal Sprite currentSprite; |
| 3807 | 64 | | internal ILazyTextureObserver imageObserver = new LazyTextureObserver(); |
| | 65 | | internal Vector2 lastParentSize; |
| | 66 | | internal string currentUriLoading = null; |
| | 67 | | internal string lastLoadedUri = null; |
| | 68 | |
|
| 117 | 69 | | public Image ImageComponent => image; |
| 3916 | 70 | | public bool UseLoadingIndicator { get; set; } = true; |
| | 71 | |
|
| | 72 | | public void Start() |
| | 73 | | { |
| 501 | 74 | | image.useSpriteMesh = false; |
| 501 | 75 | | imageObserver.AddListener(OnImageObserverUpdated); |
| 501 | 76 | | } |
| | 77 | |
|
| | 78 | | private void LateUpdate() |
| | 79 | | { |
| 2428 | 80 | | if (model.fitParent && HasParentSizeChanged()) |
| 142 | 81 | | SetFitParent(model.fitParent); |
| 2428 | 82 | | } |
| | 83 | |
|
| | 84 | | public virtual void Configure(ImageComponentModel newModel) |
| | 85 | | { |
| 43 | 86 | | model = newModel; |
| 43 | 87 | | RefreshControl(); |
| 43 | 88 | | } |
| | 89 | |
|
| | 90 | | public override void RefreshControl() |
| | 91 | | { |
| 43 | 92 | | if (model == null) |
| 0 | 93 | | return; |
| | 94 | |
|
| 43 | 95 | | SetLastUriRequestCached(model.lastUriCached); |
| 43 | 96 | | if (model.sprite != null) |
| 1 | 97 | | SetImage(model.sprite); |
| 42 | 98 | | else if (model.texture != null) |
| 0 | 99 | | SetImage(model.texture); |
| 42 | 100 | | else if (!string.IsNullOrEmpty(model.uri)) |
| 0 | 101 | | SetImage(model.uri); |
| | 102 | | else |
| 42 | 103 | | SetImage(sprite: null); |
| 42 | 104 | | } |
| | 105 | |
|
| | 106 | | public override void Dispose() |
| | 107 | | { |
| 4324 | 108 | | currentUriLoading = null; |
| 4324 | 109 | | lastLoadedUri = null; |
| 4324 | 110 | | imageObserver.RemoveListener(OnImageObserverUpdated); |
| | 111 | |
|
| 4324 | 112 | | DestroyInterntally(currentSprite); |
| | 113 | |
|
| 4324 | 114 | | base.Dispose(); |
| 4324 | 115 | | } |
| | 116 | |
|
| | 117 | | private static void DestroyInterntally(Object obj) |
| | 118 | | { |
| | 119 | | #if UNITY_EDITOR |
| 4363 | 120 | | DestroyImmediate(obj); |
| | 121 | | #else |
| | 122 | | Destroy(obj); |
| | 123 | | #endif |
| 4363 | 124 | | } |
| | 125 | |
|
| | 126 | | public void SetImage(Sprite sprite, bool cleanLastLoadedUri = true) |
| | 127 | | { |
| 176 | 128 | | model.sprite = sprite; |
| | 129 | |
|
| 176 | 130 | | if (image == null) |
| 0 | 131 | | return; |
| | 132 | |
|
| 176 | 133 | | image.sprite = sprite; |
| | 134 | |
|
| 176 | 135 | | if (cleanLastLoadedUri) |
| 137 | 136 | | lastLoadedUri = null; |
| | 137 | |
|
| 176 | 138 | | SetFitParent(model.fitParent); |
| 176 | 139 | | } |
| | 140 | |
|
| | 141 | | public void SetImage(Texture2D texture) |
| | 142 | | { |
| 27 | 143 | | model.texture = texture; |
| | 144 | |
|
| 27 | 145 | | if (!Application.isPlaying) |
| | 146 | | { |
| 0 | 147 | | OnImageObserverUpdated(texture); |
| 0 | 148 | | return; |
| | 149 | | } |
| | 150 | |
|
| 27 | 151 | | if (UseLoadingIndicator) |
| 27 | 152 | | SetLoadingIndicatorVisible(true); |
| | 153 | |
|
| 27 | 154 | | imageObserver.RefreshWithTexture(texture); |
| | 155 | |
|
| 27 | 156 | | lastLoadedUri = null; |
| 27 | 157 | | SetFitParent(model.fitParent); |
| 27 | 158 | | } |
| | 159 | |
|
| | 160 | | public virtual void SetImage(string uri) |
| | 161 | | { |
| 82 | 162 | | if (model.lastUriCached && uri == lastLoadedUri) |
| 0 | 163 | | return; |
| | 164 | |
|
| 82 | 165 | | model.uri = uri; |
| | 166 | |
|
| 82 | 167 | | if (!Application.isPlaying) |
| 0 | 168 | | return; |
| | 169 | |
|
| 82 | 170 | | if (UseLoadingIndicator) |
| 82 | 171 | | SetLoadingIndicatorVisible(true); |
| | 172 | |
|
| 82 | 173 | | if (!string.IsNullOrEmpty(uri)) |
| | 174 | | { |
| 52 | 175 | | currentUriLoading = uri; |
| 52 | 176 | | imageObserver.RefreshWithUri(uri); |
| | 177 | | } |
| | 178 | | else |
| | 179 | | { |
| 30 | 180 | | lastLoadedUri = null; |
| 30 | 181 | | OnImageObserverUpdated(null); |
| | 182 | | } |
| 30 | 183 | | } |
| | 184 | |
|
| 86 | 185 | | public void SetLastUriRequestCached(bool isEnabled) { model.lastUriCached = isEnabled; } |
| | 186 | |
|
| | 187 | | public void SetFitParent(bool fitParent) |
| | 188 | | { |
| 347 | 189 | | model.fitParent = fitParent; |
| | 190 | |
|
| 347 | 191 | | if (fitParent) |
| 211 | 192 | | ResizeFillParent(); |
| 347 | 193 | | } |
| | 194 | |
|
| | 195 | | public void SetLoadingIndicatorVisible(bool isVisible) |
| | 196 | | { |
| 174 | 197 | | image.enabled = !isVisible; |
| 174 | 198 | | loadingIndicator.SetActive(isVisible); |
| 174 | 199 | | } |
| | 200 | |
|
| | 201 | | internal void OnImageObserverUpdated(Texture2D texture) |
| | 202 | | { |
| 39 | 203 | | DestroyInterntally(currentSprite); |
| | 204 | |
|
| 39 | 205 | | currentSprite = texture != null ? Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2E |
| | 206 | |
|
| 39 | 207 | | SetImage(currentSprite, false); |
| 39 | 208 | | SetLoadingIndicatorVisible(false); |
| 39 | 209 | | lastLoadedUri = currentUriLoading; |
| 39 | 210 | | currentUriLoading = null; |
| 39 | 211 | | OnLoaded?.Invoke(currentSprite); |
| 1 | 212 | | } |
| | 213 | |
|
| | 214 | | internal void ResizeFillParent() |
| | 215 | | { |
| 211 | 216 | | RectTransform imageRectTransform = (RectTransform)image.transform; |
| | 217 | |
|
| 211 | 218 | | imageRectTransform.anchorMin = Vector2Ext.OneHalf; |
| 211 | 219 | | imageRectTransform.anchorMax = Vector2Ext.OneHalf; |
| 211 | 220 | | imageRectTransform.pivot = Vector2Ext.OneHalf; |
| 211 | 221 | | imageRectTransform.localPosition = Vector2.zero; |
| | 222 | |
|
| 211 | 223 | | if (transform.parent == null) |
| 1 | 224 | | return; |
| | 225 | |
|
| 210 | 226 | | RectTransform parent = transform.parent as RectTransform; |
| | 227 | |
|
| | 228 | | float h, w; |
| 210 | 229 | | h = parent.rect.height; |
| 210 | 230 | | w = h * (image.mainTexture != null ? (image.mainTexture.width / (float)image.mainTexture.height) : 1); |
| | 231 | |
|
| 210 | 232 | | if ((parent.rect.width - w) > 0) |
| | 233 | | { |
| 153 | 234 | | w = parent.rect.width; |
| 153 | 235 | | h = w * (image.mainTexture != null ? (image.mainTexture.height / (float)image.mainTexture.width) : 1); |
| | 236 | | } |
| | 237 | |
|
| 210 | 238 | | imageRectTransform.sizeDelta = new Vector2(w, h); |
| 210 | 239 | | } |
| | 240 | |
|
| | 241 | | internal bool HasParentSizeChanged() |
| | 242 | | { |
| 394 | 243 | | Transform imageParent = transform.parent; |
| 394 | 244 | | if (imageParent != null) |
| | 245 | | { |
| 394 | 246 | | Vector2 currentParentSize = ((RectTransform)imageParent).rect.size; |
| | 247 | |
|
| 394 | 248 | | if (lastParentSize != currentParentSize) |
| | 249 | | { |
| 142 | 250 | | lastParentSize = currentParentSize; |
| 142 | 251 | | return true; |
| | 252 | | } |
| | 253 | | } |
| | 254 | |
|
| 252 | 255 | | return false; |
| | 256 | | } |
| | 257 | | } |