| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 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); |
| | 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 | | /// Resize the image size to fit into the parent. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="fitParent">True to fit the size.</param> |
| | 36 | | void SetFitParent(bool fitParent); |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Active or deactive the loading indicator. |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="isVisible">True for showing the loading indicator and hiding the image.</param> |
| | 42 | | void SetLoadingIndicatorVisible(bool isVisible); |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public class ImageComponentView : BaseComponentView, IImageComponentView, IComponentModelConfig |
| | 46 | | { |
| | 47 | | [Header("Prefab References")] |
| | 48 | | [SerializeField] internal Image image; |
| | 49 | | [SerializeField] internal GameObject loadingIndicator; |
| | 50 | |
|
| | 51 | | [Header("Configuration")] |
| | 52 | | [SerializeField] internal ImageComponentModel model; |
| | 53 | |
|
| | 54 | | public event Action<Sprite> OnLoaded; |
| | 55 | |
|
| | 56 | | internal Sprite currentSprite; |
| 1107 | 57 | | internal ILazyTextureObserver imageObserver = new LazyTextureObserver(); |
| | 58 | | internal Vector2 lastParentSize; |
| | 59 | |
|
| 260 | 60 | | public override void Start() { imageObserver.AddListener(OnImageObserverUpdated); } |
| | 61 | |
|
| | 62 | | private void LateUpdate() |
| | 63 | | { |
| 223 | 64 | | if (model.fitParent && HasParentSizeChanged()) |
| 2 | 65 | | SetFitParent(model.fitParent); |
| 223 | 66 | | } |
| | 67 | |
|
| | 68 | | public void Configure(BaseComponentModel newModel) |
| | 69 | | { |
| 1 | 70 | | model = (ImageComponentModel)newModel; |
| 1 | 71 | | RefreshControl(); |
| 1 | 72 | | } |
| | 73 | |
|
| | 74 | | public override void RefreshControl() |
| | 75 | | { |
| 1 | 76 | | if (model == null) |
| 0 | 77 | | return; |
| | 78 | |
|
| 1 | 79 | | if (model.sprite != null) |
| 1 | 80 | | SetImage(model.sprite); |
| 0 | 81 | | else if (model.texture != null) |
| 0 | 82 | | SetImage(model.texture); |
| 0 | 83 | | else if (!string.IsNullOrEmpty(model.uri)) |
| 0 | 84 | | SetImage(model.uri); |
| | 85 | | else |
| 0 | 86 | | SetImage(sprite: null); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | public override void Dispose() |
| | 90 | | { |
| 1126 | 91 | | base.Dispose(); |
| | 92 | |
|
| 1126 | 93 | | imageObserver.RemoveListener(OnImageObserverUpdated); |
| 1126 | 94 | | Destroy(currentSprite); |
| 1126 | 95 | | } |
| | 96 | |
|
| | 97 | | public void SetImage(Sprite sprite) |
| | 98 | | { |
| 40 | 99 | | model.sprite = sprite; |
| | 100 | |
|
| 40 | 101 | | if (image == null) |
| 0 | 102 | | return; |
| | 103 | |
|
| 40 | 104 | | image.sprite = sprite; |
| 40 | 105 | | SetFitParent(model.fitParent); |
| 40 | 106 | | } |
| | 107 | |
|
| | 108 | | public void SetImage(Texture2D texture) |
| | 109 | | { |
| 5 | 110 | | if (model.texture != texture) |
| | 111 | | { |
| 5 | 112 | | model.texture = texture; |
| | 113 | |
|
| 5 | 114 | | if (!Application.isPlaying) |
| | 115 | | { |
| 0 | 116 | | OnImageObserverUpdated(texture); |
| 0 | 117 | | return; |
| | 118 | | } |
| | 119 | |
|
| 5 | 120 | | SetLoadingIndicatorVisible(true); |
| 5 | 121 | | imageObserver.RefreshWithTexture(texture); |
| | 122 | | } |
| | 123 | |
|
| 5 | 124 | | SetFitParent(model.fitParent); |
| 5 | 125 | | } |
| | 126 | |
|
| | 127 | | public void SetImage(string uri) |
| | 128 | | { |
| 6 | 129 | | model.uri = uri; |
| | 130 | |
|
| 6 | 131 | | if (!Application.isPlaying) |
| 0 | 132 | | return; |
| | 133 | |
|
| 6 | 134 | | SetLoadingIndicatorVisible(true); |
| 6 | 135 | | if (!string.IsNullOrEmpty(uri)) |
| 6 | 136 | | imageObserver.RefreshWithUri(uri); |
| | 137 | | else |
| 0 | 138 | | OnImageObserverUpdated(null); |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | public void SetFitParent(bool fitParent) |
| | 142 | | { |
| 49 | 143 | | model.fitParent = fitParent; |
| | 144 | |
|
| 49 | 145 | | if (fitParent) |
| 39 | 146 | | ResizeFillParent(); |
| 49 | 147 | | } |
| | 148 | |
|
| | 149 | | public void SetLoadingIndicatorVisible(bool isVisible) |
| | 150 | | { |
| 17 | 151 | | image.enabled = !isVisible; |
| 17 | 152 | | loadingIndicator.SetActive(isVisible); |
| 17 | 153 | | } |
| | 154 | |
|
| | 155 | | internal void OnImageObserverUpdated(Texture texture) |
| | 156 | | { |
| 1 | 157 | | if (Application.isPlaying) |
| 1 | 158 | | Destroy(currentSprite); |
| | 159 | | else |
| 0 | 160 | | DestroyImmediate(currentSprite); |
| | 161 | |
|
| 1 | 162 | | currentSprite = texture != null ? Sprite.Create((Texture2D)texture, new Rect(0, 0, texture.width, texture.height |
| 1 | 163 | | SetImage(currentSprite); |
| 1 | 164 | | SetLoadingIndicatorVisible(false); |
| 1 | 165 | | OnLoaded?.Invoke(currentSprite); |
| 1 | 166 | | } |
| | 167 | |
|
| | 168 | | internal void ResizeFillParent() |
| | 169 | | { |
| 39 | 170 | | RectTransform imageRectTransform = (RectTransform)image.transform; |
| | 171 | |
|
| 39 | 172 | | imageRectTransform.anchorMin = new Vector2(0.5f, 0.5f); |
| 39 | 173 | | imageRectTransform.anchorMax = new Vector2(0.5f, 0.5f); |
| 39 | 174 | | imageRectTransform.pivot = new Vector2(0.5f, 0.5f); |
| 39 | 175 | | imageRectTransform.localPosition = Vector2.zero; |
| | 176 | |
|
| 39 | 177 | | if (transform.parent == null) |
| 1 | 178 | | return; |
| | 179 | |
|
| 38 | 180 | | RectTransform parent = transform.parent as RectTransform; |
| | 181 | |
|
| | 182 | | float h, w; |
| 38 | 183 | | h = parent.rect.height; |
| 38 | 184 | | w = h * (image.mainTexture != null ? (image.mainTexture.width / (float)image.mainTexture.height) : 1); |
| | 185 | |
|
| 38 | 186 | | if ((parent.rect.width - w) > 0) |
| | 187 | | { |
| 26 | 188 | | w = parent.rect.width; |
| 26 | 189 | | h = w * (image.mainTexture != null ? (image.mainTexture.height / (float)image.mainTexture.width) : 1); |
| | 190 | | } |
| | 191 | |
|
| 38 | 192 | | imageRectTransform.sizeDelta = new Vector2(w, h); |
| 38 | 193 | | } |
| | 194 | |
|
| | 195 | | internal bool HasParentSizeChanged() |
| | 196 | | { |
| 58 | 197 | | Transform imageParent = transform.parent; |
| 58 | 198 | | if (imageParent != null) |
| | 199 | | { |
| 58 | 200 | | Vector2 currentParentSize = ((RectTransform)imageParent).rect.size; |
| | 201 | |
|
| 58 | 202 | | if (lastParentSize != currentParentSize) |
| | 203 | | { |
| 2 | 204 | | lastParentSize = currentParentSize; |
| 2 | 205 | | return true; |
| | 206 | | } |
| | 207 | | } |
| | 208 | |
|
| 56 | 209 | | return false; |
| | 210 | | } |
| | 211 | | } |