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