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