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