| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | namespace DCL.Components |
| | 10 | | { |
| | 11 | | public class UIImage : UIShape<UIImageReferencesContainer, UIImage.Model> |
| | 12 | | { |
| | 13 | | [System.Serializable] |
| | 14 | | new public class Model : UIShape.Model |
| | 15 | | { |
| | 16 | | public string source; |
| | 17 | | public float sourceLeft = 0f; |
| | 18 | | public float sourceTop = 0f; |
| 69 | 19 | | public float sourceWidth = 1f; |
| 69 | 20 | | public float sourceHeight = 1f; |
| | 21 | | public float paddingTop = 0f; |
| | 22 | | public float paddingRight = 0f; |
| | 23 | | public float paddingBottom = 0f; |
| | 24 | | public float paddingLeft = 0f; |
| 69 | 25 | | public bool sizeInPixels = true; |
| | 26 | |
|
| 19 | 27 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 28 | | } |
| | 29 | |
|
| 15 | 30 | | public override string referencesContainerPrefabName => "UIImage"; |
| | 31 | |
|
| | 32 | | DCLTexture dclTexture = null; |
| | 33 | |
|
| 45 | 34 | | public UIImage() { model = new Model(); } |
| | 35 | |
|
| 0 | 36 | | public override int GetClassId() { return (int) CLASS_ID.UI_IMAGE_SHAPE; } |
| | 37 | |
|
| 0 | 38 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) { Debug.LogError("Abo |
| | 39 | |
|
| 0 | 40 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { } |
| | 41 | |
|
| | 42 | | Coroutine fetchRoutine; |
| | 43 | |
|
| | 44 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 45 | | { |
| 34 | 46 | | RectTransform parentRecTransform = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 47 | |
|
| | 48 | | // Fetch texture |
| 34 | 49 | | if (!string.IsNullOrEmpty(model.source)) |
| | 50 | | { |
| 18 | 51 | | if (dclTexture == null || (dclTexture != null && dclTexture.id != model.source)) |
| | 52 | | { |
| 10 | 53 | | if (fetchRoutine != null) |
| | 54 | | { |
| 0 | 55 | | CoroutineStarter.Stop(fetchRoutine); |
| 0 | 56 | | fetchRoutine = null; |
| | 57 | | } |
| | 58 | |
|
| 10 | 59 | | IEnumerator fetchIEnum = DCLTexture.FetchTextureComponent(scene, model.source, (downloadedTexture) = |
| | 60 | | { |
| 9 | 61 | | referencesContainer.image.texture = downloadedTexture.texture; |
| 9 | 62 | | fetchRoutine = null; |
| 9 | 63 | | dclTexture?.DetachFrom(this); |
| 9 | 64 | | dclTexture = downloadedTexture; |
| 9 | 65 | | dclTexture.AttachTo(this); |
| | 66 | |
|
| 9 | 67 | | ConfigureUVRect(parentRecTransform); |
| 9 | 68 | | }); |
| | 69 | |
|
| 10 | 70 | | fetchRoutine = CoroutineStarter.Start(fetchIEnum); |
| | 71 | | } |
| 10 | 72 | | } |
| | 73 | | else |
| | 74 | | { |
| 16 | 75 | | referencesContainer.image.texture = null; |
| 16 | 76 | | dclTexture?.DetachFrom(this); |
| 16 | 77 | | dclTexture = null; |
| | 78 | | } |
| | 79 | |
|
| 34 | 80 | | referencesContainer.image.enabled = model.visible; |
| 34 | 81 | | referencesContainer.image.color = Color.white; |
| | 82 | |
|
| 34 | 83 | | ConfigureUVRect(parentRecTransform); |
| | 84 | |
|
| | 85 | | // Apply padding |
| 34 | 86 | | referencesContainer.paddingLayoutGroup.padding.bottom = Mathf.RoundToInt(model.paddingBottom); |
| 34 | 87 | | referencesContainer.paddingLayoutGroup.padding.top = Mathf.RoundToInt(model.paddingTop); |
| 34 | 88 | | referencesContainer.paddingLayoutGroup.padding.left = Mathf.RoundToInt(model.paddingLeft); |
| 34 | 89 | | referencesContainer.paddingLayoutGroup.padding.right = Mathf.RoundToInt(model.paddingRight); |
| | 90 | |
|
| 34 | 91 | | Utils.ForceRebuildLayoutImmediate(parentRecTransform); |
| 34 | 92 | | return null; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | private void ConfigureUVRect(RectTransform parentRecTransform) |
| | 96 | | { |
| 43 | 97 | | if (referencesContainer.image.texture == null) |
| 26 | 98 | | return; |
| | 99 | |
|
| | 100 | | // Configure uv rect |
| 17 | 101 | | Vector2 normalizedSourceCoordinates = new Vector2( |
| | 102 | | model.sourceLeft / referencesContainer.image.texture.width, |
| | 103 | | -model.sourceTop / referencesContainer.image.texture.height); |
| | 104 | |
|
| 17 | 105 | | Vector2 normalizedSourceSize = new Vector2( |
| | 106 | | model.sourceWidth * (model.sizeInPixels ? 1f : parentRecTransform.rect.width) / |
| | 107 | | referencesContainer.image.texture.width, |
| | 108 | | model.sourceHeight * (model.sizeInPixels ? 1f : parentRecTransform.rect.height) / |
| | 109 | | referencesContainer.image.texture.height); |
| | 110 | |
|
| 17 | 111 | | referencesContainer.image.uvRect = new Rect(normalizedSourceCoordinates.x, |
| | 112 | | normalizedSourceCoordinates.y + (1 - normalizedSourceSize.y), |
| | 113 | | normalizedSourceSize.x, |
| | 114 | | normalizedSourceSize.y); |
| 17 | 115 | | } |
| | 116 | |
|
| | 117 | | public override void Dispose() |
| | 118 | | { |
| 1 | 119 | | if (fetchRoutine != null) |
| | 120 | | { |
| 0 | 121 | | CoroutineStarter.Stop(fetchRoutine); |
| 0 | 122 | | fetchRoutine = null; |
| | 123 | | } |
| | 124 | |
|
| 1 | 125 | | dclTexture?.DetachFrom(this); |
| | 126 | |
|
| 1 | 127 | | if (referencesContainer != null) |
| 1 | 128 | | Utils.SafeDestroy(referencesContainer.gameObject); |
| | 129 | |
|
| 1 | 130 | | base.Dispose(); |
| 1 | 131 | | } |
| | 132 | | } |
| | 133 | | } |