| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using System.Collections; |
| | 5 | | using UnityEngine; |
| | 6 | | using Decentraland.Sdk.Ecs6; |
| | 7 | | using MainScripts.DCL.Components; |
| | 8 | |
|
| | 9 | | namespace DCL.Components |
| | 10 | | { |
| | 11 | | public class UIImage : UIShape<UIImageReferencesContainer, UIImage.Model> |
| | 12 | | { |
| | 13 | | [System.Serializable] |
| | 14 | | public new class Model : UIShape.Model |
| | 15 | | { |
| | 16 | | public string source; |
| | 17 | | public float sourceLeft = 0f; |
| | 18 | | public float sourceTop = 0f; |
| 76 | 19 | | public float sourceWidth = 1f; |
| 76 | 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; |
| 76 | 25 | | public bool sizeInPixels = true; |
| | 26 | |
|
| | 27 | | public override BaseModel GetDataFromJSON(string json) => |
| 21 | 28 | | Utils.SafeFromJson<Model>(json); |
| | 29 | |
|
| | 30 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 31 | | { |
| 0 | 32 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.UiImage) |
| 0 | 33 | | return Utils.SafeUnimplemented<UIImage, Model>(expected: ComponentBodyPayload.PayloadOneofCase.UiIma |
| | 34 | |
|
| 0 | 35 | | var pb = new Model(); |
| 0 | 36 | | if (pbModel.UiImage.HasName) pb.name = pbModel.UiImage.Name; |
| 0 | 37 | | if (pbModel.UiImage.HasParentComponent) pb.parentComponent = pbModel.UiImage.ParentComponent; |
| 0 | 38 | | if (pbModel.UiImage.HasVisible) pb.visible = pbModel.UiImage.Visible; |
| 0 | 39 | | if (pbModel.UiImage.HasOpacity) pb.opacity = pbModel.UiImage.Opacity; |
| 0 | 40 | | if (pbModel.UiImage.HasHAlign) pb.hAlign = pbModel.UiImage.HAlign; |
| 0 | 41 | | if (pbModel.UiImage.HasVAlign) pb.vAlign = pbModel.UiImage.VAlign; |
| 0 | 42 | | if (pbModel.UiImage.Width != null) pb.width = SDK6DataMapExtensions.FromProtobuf(pb.width, pbModel.UiIma |
| 0 | 43 | | if (pbModel.UiImage.Height != null) pb.height = SDK6DataMapExtensions.FromProtobuf(pb.height, pbModel.Ui |
| 0 | 44 | | if (pbModel.UiImage.PositionX != null) pb.positionX = SDK6DataMapExtensions.FromProtobuf(pb.positionX, p |
| 0 | 45 | | if (pbModel.UiImage.PositionY != null) pb.positionY = SDK6DataMapExtensions.FromProtobuf(pb.positionY, p |
| 0 | 46 | | if (pbModel.UiImage.HasIsPointerBlocker) pb.isPointerBlocker = pbModel.UiImage.IsPointerBlocker; |
| | 47 | |
|
| 0 | 48 | | if (pbModel.UiImage.HasSource) pb.source = pbModel.UiImage.Source; |
| 0 | 49 | | if (pbModel.UiImage.HasSourceLeft) pb.sourceLeft = pbModel.UiImage.SourceLeft; |
| 0 | 50 | | if (pbModel.UiImage.HasSourceTop) pb.sourceTop = pbModel.UiImage.SourceTop; |
| 0 | 51 | | if (pbModel.UiImage.HasSourceWidth) pb.sourceWidth = pbModel.UiImage.SourceWidth; |
| 0 | 52 | | if (pbModel.UiImage.HasSourceHeight) pb.sourceHeight = pbModel.UiImage.SourceHeight; |
| 0 | 53 | | if (pbModel.UiImage.HasPaddingTop) pb.paddingTop = pbModel.UiImage.PaddingTop; |
| 0 | 54 | | if (pbModel.UiImage.HasPaddingRight) pb.paddingRight = pbModel.UiImage.PaddingRight; |
| 0 | 55 | | if (pbModel.UiImage.HasPaddingBottom) pb.paddingBottom = pbModel.UiImage.PaddingBottom; |
| 0 | 56 | | if (pbModel.UiImage.HasPaddingLeft) pb.paddingLeft = pbModel.UiImage.PaddingLeft; |
| 0 | 57 | | if (pbModel.UiImage.HasSizeInPixels) pb.sizeInPixels = pbModel.UiImage.SizeInPixels; |
| | 58 | |
|
| 0 | 59 | | if (pbModel.UiImage.HasOnClick) pb.onClick = pbModel.UiImage.OnClick; |
| | 60 | |
|
| | 61 | |
|
| 0 | 62 | | return pb; |
| | 63 | | } |
| | 64 | | } |
| | 65 | |
|
| 17 | 66 | | public override string referencesContainerPrefabName => "UIImage"; |
| | 67 | |
|
| | 68 | | DCLTexture dclTexture = null; |
| 17 | 69 | | private readonly DCLTexture.Fetcher dclTextureFetcher = new DCLTexture.Fetcher(); |
| | 70 | | private bool isDisposed; |
| | 71 | |
|
| 17 | 72 | | public UIImage() |
| | 73 | | { |
| 17 | 74 | | model = new Model(); |
| 17 | 75 | | } |
| | 76 | |
|
| | 77 | | public override int GetClassId() => |
| 0 | 78 | | (int) CLASS_ID.UI_IMAGE_SHAPE; |
| | 79 | |
|
| | 80 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) => |
| 0 | 81 | | Debug.LogError("Aborted UIImageShape attachment to an entity. UIShapes shouldn't be attached to entities."); |
| | 82 | |
|
| 0 | 83 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { } |
| | 84 | |
|
| | 85 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 86 | | { |
| | 87 | | // Fetch texture |
| 37 | 88 | | if (!string.IsNullOrEmpty(model.source)) |
| | 89 | | { |
| 21 | 90 | | if (dclTexture == null || (dclTexture != null && dclTexture.id != model.source)) |
| | 91 | | { |
| 13 | 92 | | dclTextureFetcher.Fetch(scene, model.source, |
| | 93 | | fetchedDCLTexture => |
| | 94 | | { |
| 12 | 95 | | if (isDisposed) |
| 0 | 96 | | return false; |
| | 97 | |
|
| 12 | 98 | | referencesContainer.image.texture = fetchedDCLTexture.texture; |
| 12 | 99 | | dclTexture?.DetachFrom(this); |
| 12 | 100 | | dclTexture = fetchedDCLTexture; |
| 12 | 101 | | dclTexture.AttachTo(this); |
| | 102 | |
|
| 12 | 103 | | ConfigureImage(); |
| 12 | 104 | | return true; |
| | 105 | | }) |
| | 106 | | .Forget(); |
| | 107 | |
|
| 13 | 108 | | return null; |
| | 109 | | } |
| | 110 | | } |
| | 111 | | else |
| | 112 | | { |
| 16 | 113 | | referencesContainer.image.texture = null; |
| 16 | 114 | | dclTexture?.DetachFrom(this); |
| 16 | 115 | | dclTexture = null; |
| | 116 | | } |
| | 117 | |
|
| 24 | 118 | | ConfigureImage(); |
| 24 | 119 | | return null; |
| | 120 | | } |
| | 121 | |
|
| | 122 | | private void ConfigureImage() |
| | 123 | | { |
| 36 | 124 | | RectTransform parentRecTransform = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 125 | |
|
| 36 | 126 | | ConfigureUVRect(parentRecTransform, dclTexture?.resizingFactor ?? 1); |
| | 127 | |
|
| 36 | 128 | | referencesContainer.image.enabled = model.visible; |
| 36 | 129 | | referencesContainer.image.color = Color.white; |
| | 130 | |
|
| 36 | 131 | | ConfigureUVRect(parentRecTransform, dclTexture?.resizingFactor ?? 1); |
| | 132 | |
|
| | 133 | | // Apply padding |
| 36 | 134 | | referencesContainer.paddingLayoutGroup.padding.bottom = Mathf.RoundToInt(model.paddingBottom); |
| 36 | 135 | | referencesContainer.paddingLayoutGroup.padding.top = Mathf.RoundToInt(model.paddingTop); |
| 36 | 136 | | referencesContainer.paddingLayoutGroup.padding.left = Mathf.RoundToInt(model.paddingLeft); |
| 36 | 137 | | referencesContainer.paddingLayoutGroup.padding.right = Mathf.RoundToInt(model.paddingRight); |
| | 138 | |
|
| 36 | 139 | | Utils.ForceRebuildLayoutImmediate(parentRecTransform); |
| 36 | 140 | | } |
| | 141 | |
|
| | 142 | | private void ConfigureUVRect(RectTransform parentRecTransform, float resizingFactor) |
| | 143 | | { |
| 72 | 144 | | if (referencesContainer.image.texture == null) |
| 32 | 145 | | return; |
| | 146 | |
|
| | 147 | | // Configure uv rect |
| 40 | 148 | | Vector2 normalizedSourceCoordinates = new Vector2( |
| | 149 | | model.sourceLeft * resizingFactor / referencesContainer.image.texture.width, |
| | 150 | | -model.sourceTop * resizingFactor / referencesContainer.image.texture.height); |
| | 151 | |
|
| 40 | 152 | | Vector2 normalizedSourceSize = new Vector2( |
| | 153 | | model.sourceWidth * resizingFactor * (model.sizeInPixels ? 1f : parentRecTransform.rect.width) / |
| | 154 | | referencesContainer.image.texture.width , |
| | 155 | | model.sourceHeight * resizingFactor * (model.sizeInPixels ? 1f : parentRecTransform.rect.height) / |
| | 156 | | referencesContainer.image.texture.height); |
| | 157 | |
|
| 40 | 158 | | referencesContainer.image.uvRect = new Rect(normalizedSourceCoordinates.x, |
| | 159 | | normalizedSourceCoordinates.y + (1 - normalizedSourceSize.y), |
| | 160 | | normalizedSourceSize.x, |
| | 161 | | normalizedSourceSize.y); |
| 40 | 162 | | } |
| | 163 | |
|
| | 164 | | public override void Dispose() |
| | 165 | | { |
| 2 | 166 | | isDisposed = true; |
| | 167 | |
|
| 2 | 168 | | dclTextureFetcher.Dispose(); |
| | 169 | |
|
| 2 | 170 | | dclTexture?.DetachFrom(this); |
| | 171 | |
|
| 2 | 172 | | if (referencesContainer != null) |
| | 173 | | { |
| 2 | 174 | | referencesContainer.image.texture = null; |
| 2 | 175 | | Utils.SafeDestroy(referencesContainer.gameObject); |
| 2 | 176 | | referencesContainer = null; |
| | 177 | | } |
| | 178 | |
|
| 2 | 179 | | base.Dispose(); |
| 2 | 180 | | } |
| | 181 | | } |
| | 182 | | } |