| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Models; |
| | 3 | | using UnityEngine; |
| | 4 | | using Decentraland.Sdk.Ecs6; |
| | 5 | | using MainScripts.DCL.Components; |
| | 6 | |
|
| | 7 | | namespace DCL.Components |
| | 8 | | { |
| | 9 | | public class NFTShape : LoadableShape<LoadWrapper_NFT, NFTShape.Model> |
| | 10 | | { |
| | 11 | | [System.Serializable] |
| | 12 | | public new class Model : LoadableShape.Model |
| | 13 | | { |
| 33 | 14 | | public Color color = new (0.6404918f, 0.611472f, 0.8584906f); // "light purple" default, same as in explorer |
| | 15 | | public int style; |
| | 16 | |
|
| | 17 | | public override BaseModel GetDataFromJSON(string json) => |
| 12 | 18 | | Utils.SafeFromJson<Model>(json); |
| | 19 | |
|
| | 20 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 21 | | { |
| 0 | 22 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.NftShape) |
| 0 | 23 | | return Utils.SafeUnimplemented<NFTShape, Model>(expected: ComponentBodyPayload.PayloadOneofCase.NftS |
| | 24 | |
|
| 0 | 25 | | var pb = new Model(); |
| 0 | 26 | | if (pbModel.NftShape.Color != null) pb.color = pbModel.NftShape.Color.AsUnityColor(); |
| 0 | 27 | | if (pbModel.NftShape.HasStyle) pb.style = (int)pbModel.NftShape.Style; |
| 0 | 28 | | if (pbModel.NftShape.HasSrc) pb.src = pbModel.NftShape.Src; |
| 0 | 29 | | if (pbModel.NftShape.HasVisible) pb.visible = pbModel.NftShape.Visible; |
| 0 | 30 | | if (pbModel.NftShape.HasWithCollisions) pb.withCollisions = pbModel.NftShape.WithCollisions; |
| 0 | 31 | | if (pbModel.NftShape.HasIsPointerBlocker) pb.isPointerBlocker = pbModel.NftShape.IsPointerBlocker; |
| | 32 | |
|
| 0 | 33 | | return pb; |
| | 34 | | } |
| | 35 | | } |
| | 36 | |
|
| 7 | 37 | | public override string componentName => "NFT Shape"; |
| | 38 | |
|
| | 39 | | private INFTInfoRetriever infoRetriever; |
| | 40 | | private INFTAssetRetriever assetRetriever; |
| | 41 | |
|
| 9 | 42 | | public NFTShape(INFTInfoRetriever infoRetriever, INFTAssetRetriever assetRetriever) |
| | 43 | | { |
| 9 | 44 | | model = new Model(); |
| 9 | 45 | | this.infoRetriever = infoRetriever; |
| 9 | 46 | | this.assetRetriever = assetRetriever; |
| 9 | 47 | | } |
| | 48 | |
|
| 0 | 49 | | public override int GetClassId() { return (int) CLASS_ID.NFT_SHAPE; } |
| | 50 | |
|
| | 51 | | protected override void AttachShape(IDCLEntity entity) |
| | 52 | | { |
| 7 | 53 | | if (string.IsNullOrEmpty(model.src)) |
| | 54 | | { |
| | 55 | | #if UNITY_EDITOR |
| 0 | 56 | | Debug.LogError($"NFT SHAPE with url '{model.src}' couldn't be loaded."); |
| | 57 | | #endif |
| 0 | 58 | | return; |
| | 59 | | } |
| | 60 | |
|
| 7 | 61 | | entity.meshesInfo.meshRootGameObject = NFTShapeFactory.InstantiateLoaderController(model.style); |
| 7 | 62 | | entity.meshesInfo.currentShape = this; |
| | 63 | |
|
| 7 | 64 | | entity.meshRootGameObject.name = componentName + " mesh"; |
| 7 | 65 | | entity.meshRootGameObject.transform.SetParent(entity.gameObject.transform); |
| 7 | 66 | | entity.meshRootGameObject.transform.ResetLocalTRS(); |
| | 67 | |
|
| 7 | 68 | | var loaderController = entity.meshRootGameObject.GetComponent<NFTShapeLoaderController>(); |
| | 69 | |
|
| 7 | 70 | | if (loaderController) |
| 7 | 71 | | loaderController.Initialize(infoRetriever, assetRetriever); |
| | 72 | |
|
| 7 | 73 | | entity.OnShapeUpdated += UpdateBackgroundColor; |
| | 74 | |
|
| 7 | 75 | | var loadableShape = Environment.i.world.state.GetOrAddLoaderForEntity<LoadWrapper_NFT>(entity); |
| | 76 | |
|
| 7 | 77 | | loadableShape.entity = entity; |
| | 78 | |
|
| 7 | 79 | | bool initialVisibility = model.visible; |
| 7 | 80 | | if (!DataStore.i.debugConfig.isDebugMode.Get()) |
| 4 | 81 | | initialVisibility &= entity.isInsideSceneBoundaries; |
| 7 | 82 | | loadableShape.initialVisibility = initialVisibility; |
| | 83 | |
|
| 7 | 84 | | loadableShape.withCollisions = model.withCollisions && entity.isInsideSceneBoundaries; |
| 7 | 85 | | loadableShape.backgroundColor = model.color; |
| | 86 | |
|
| 7 | 87 | | loadableShape.Load(model.src, OnLoadCompleted, OnLoadFailed); |
| 7 | 88 | | } |
| | 89 | |
|
| | 90 | | protected override void DetachShape(IDCLEntity entity) |
| | 91 | | { |
| 6 | 92 | | if (entity == null || entity.meshRootGameObject == null) |
| 0 | 93 | | return; |
| | 94 | |
|
| 6 | 95 | | entity.OnShapeUpdated -= UpdateBackgroundColor; |
| | 96 | |
|
| 6 | 97 | | base.DetachShape(entity); |
| 6 | 98 | | } |
| | 99 | |
|
| 14 | 100 | | protected override void ConfigureColliders(IDCLEntity entity) { CollidersManager.i.ConfigureColliders(entity.mes |
| | 101 | |
|
| | 102 | | void UpdateBackgroundColor(IDCLEntity entity) |
| | 103 | | { |
| 15 | 104 | | if (previousModel is NFTShape.Model && model.color == previousModel.color) |
| 14 | 105 | | return; |
| | 106 | |
|
| 1 | 107 | | var loadableShape = Environment.i.world.state.GetLoaderForEntity(entity) as LoadWrapper_NFT; |
| 1 | 108 | | loadableShape?.loaderController.UpdateBackgroundColor(model.color); |
| 1 | 109 | | } |
| | 110 | |
|
| | 111 | | public override string ToString() |
| | 112 | | { |
| 0 | 113 | | if (model == null) |
| 0 | 114 | | return base.ToString(); |
| | 115 | |
|
| 0 | 116 | | return $"{componentName} (src = {model.src})"; |
| | 117 | | } |
| | 118 | | } |
| | 119 | | } |