| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECSRuntime; |
| | 3 | | using DCL.Models; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Helpers.NFT; |
| | 6 | | using NFTShape_Internal; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL.ECSComponents |
| | 10 | | { |
| | 11 | | public class ECSNFTShapeComponentHandler : IECSComponentHandler<PBNFTShape> |
| | 12 | | { |
| | 13 | | internal INFTShapeFrameFactory factory; |
| | 14 | |
|
| | 15 | | internal INFTInfoRetriever infoRetriever; |
| | 16 | | internal INFTAssetRetriever assetRetriever; |
| | 17 | | internal INFTShapeFrame shapeFrame; |
| | 18 | |
|
| | 19 | | private PBNFTShape model; |
| | 20 | |
|
| | 21 | | public DataStore_ECS7 dataStore; |
| | 22 | |
|
| 12 | 23 | | public ECSNFTShapeComponentHandler(DataStore_ECS7 dataStore, INFTShapeFrameFactory factory, INFTInfoRetriever in |
| | 24 | | { |
| 12 | 25 | | this.dataStore = dataStore; |
| 12 | 26 | | this.factory = factory; |
| 12 | 27 | | this.infoRetriever = infoRetriever; |
| 12 | 28 | | this.assetRetriever = assetRetriever; |
| 12 | 29 | | } |
| | 30 | |
|
| 0 | 31 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { } |
| | 32 | |
|
| | 33 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 34 | | { |
| 12 | 35 | | infoRetriever.Dispose(); |
| 12 | 36 | | assetRetriever.Dispose(); |
| 12 | 37 | | DisposeShapeFrame(entity); |
| 12 | 38 | | } |
| | 39 | |
|
| | 40 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBNFTShape model) |
| | 41 | | { |
| | 42 | | // We create the frame gameobject |
| 3 | 43 | | if(shapeFrame == null || this.model.Style != model.Style) |
| 3 | 44 | | CreateNFTShapeFrame(entity, model); |
| | 45 | |
|
| | 46 | | // We apply the model to the frame |
| 3 | 47 | | ApplyModel(model); |
| | 48 | |
|
| | 49 | | // We load the NFT image |
| 3 | 50 | | LoadNFT(model); |
| 3 | 51 | | } |
| | 52 | |
|
| | 53 | | private void DisposeShapeFrame(IDCLEntity entity) |
| | 54 | | { |
| 12 | 55 | | if (shapeFrame == null) |
| 4 | 56 | | return; |
| | 57 | |
|
| 8 | 58 | | dataStore.RemoveShapeReady(entity.entityId); |
| 8 | 59 | | shapeFrame.Dispose(); |
| 8 | 60 | | ECSComponentsUtils.DisposeMeshInfo(entity.meshesInfo); |
| 8 | 61 | | GameObject.Destroy(shapeFrame.gameObject); |
| 8 | 62 | | } |
| | 63 | |
|
| | 64 | | internal async void LoadNFT(PBNFTShape model) |
| | 65 | | { |
| 7 | 66 | | NFTInfo info = await infoRetriever.FetchNFTInfo(model.Src); |
| | 67 | |
|
| 7 | 68 | | if (info == null) |
| | 69 | | { |
| 4 | 70 | | LoadFailed(); |
| 4 | 71 | | return; |
| | 72 | | } |
| | 73 | |
|
| 3 | 74 | | INFTAsset nftAsset = await assetRetriever.LoadNFTAsset(info.previewImageUrl); |
| | 75 | |
|
| 3 | 76 | | if (nftAsset == null) |
| | 77 | | { |
| 1 | 78 | | LoadFailed(); |
| 1 | 79 | | return; |
| | 80 | | } |
| | 81 | |
|
| 2 | 82 | | shapeFrame.SetImage(info.name, info.imageUrl, nftAsset); |
| 7 | 83 | | } |
| | 84 | |
|
| | 85 | | private void CreateNFTShapeFrame(IDCLEntity entity,PBNFTShape model) |
| | 86 | | { |
| 3 | 87 | | if (shapeFrame != null) |
| 0 | 88 | | DisposeShapeFrame(entity); |
| | 89 | |
|
| 3 | 90 | | shapeFrame = factory.InstantiateLoaderController(model.Style); |
| | 91 | |
|
| 3 | 92 | | entity.meshesInfo.meshRootGameObject = shapeFrame.gameObject; |
| 3 | 93 | | entity.meshesInfo.currentShape = shapeFrame.shape; |
| | 94 | |
|
| 3 | 95 | | entity.meshesInfo.meshRootGameObject.name = "NFT mesh"; |
| 3 | 96 | | entity.meshesInfo.meshRootGameObject.transform.SetParent(entity.gameObject.transform); |
| 3 | 97 | | entity.meshesInfo.meshRootGameObject.transform.ResetLocalTRS(); |
| | 98 | |
|
| 3 | 99 | | dataStore.AddShapeReady(entity.entityId, entity.meshesInfo.meshRootGameObject); |
| 3 | 100 | | } |
| | 101 | |
|
| | 102 | | private void LoadFailed() |
| | 103 | | { |
| 5 | 104 | | factory.InstantiateErrorFeedback(shapeFrame.gameObject); |
| 5 | 105 | | shapeFrame.FailLoading(); |
| 5 | 106 | | } |
| | 107 | |
|
| | 108 | | internal void ApplyModel(PBNFTShape model) |
| | 109 | | { |
| 4 | 110 | | shapeFrame.SetVisibility(model.GetVisible()); |
| 4 | 111 | | shapeFrame.SetHasCollisions(model.GetWithCollisions()); |
| 4 | 112 | | shapeFrame.SetPointerBlocker(model.GetIsPointerBlocker()); |
| 4 | 113 | | UpdateBackgroundColor(model); |
| | 114 | |
|
| 4 | 115 | | this.model = model; |
| 4 | 116 | | } |
| | 117 | |
|
| | 118 | | internal void UpdateBackgroundColor(PBNFTShape model) |
| | 119 | | { |
| 4 | 120 | | if (this.model != null && model.Color.Equals(this.model.Color)) |
| 0 | 121 | | return; |
| | 122 | |
|
| 4 | 123 | | shapeFrame.UpdateBackgroundColor( new UnityEngine.Color(model.Color.R, model.Color.G,model.Color.B,1)); |
| 4 | 124 | | } |
| | 125 | | } |
| | 126 | | } |