| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | public class NFTItemInfo : MonoBehaviour |
| | 11 | | { |
| | 12 | | [Serializable] |
| | 13 | | internal class IconToGameObjectMap |
| | 14 | | { |
| | 15 | | public string iconId; |
| | 16 | | public GameObject gameObject; |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public class Model |
| | 20 | | { |
| | 21 | | public string name; |
| | 22 | | public string thumbnail; |
| | 23 | | public List<string> iconIds; |
| | 24 | | public string description; |
| | 25 | | public int issuedId; |
| | 26 | | public int issuedTotal; |
| | 27 | | public bool isInL2; |
| | 28 | |
|
| | 29 | | public bool Equals(Model other) |
| | 30 | | { |
| 0 | 31 | | return name == other.name |
| | 32 | | && thumbnail == other.thumbnail |
| | 33 | | && iconIds.SequenceEqual(other.iconIds) |
| | 34 | | && description == other.description |
| | 35 | | && issuedId == other.issuedId |
| | 36 | | && issuedTotal == other.issuedTotal; |
| | 37 | | } |
| | 38 | |
|
| | 39 | | public static Model FromWearableItem(WearableItem wearable) |
| | 40 | | { |
| 72 | 41 | | var iconsIds = wearable.data.representations.SelectMany(x => x.bodyShapes).ToList(); |
| 36 | 42 | | iconsIds.Add(wearable.data.category); |
| | 43 | |
|
| 36 | 44 | | return new Model() |
| | 45 | | { |
| | 46 | | name = wearable.GetName(), |
| | 47 | | thumbnail = wearable.baseUrl + wearable.thumbnail, |
| | 48 | | iconIds = iconsIds, |
| | 49 | | description = wearable.description, |
| | 50 | | issuedId = wearable.issuedId, |
| | 51 | | issuedTotal = wearable.GetIssuedCountFromRarity(wearable.rarity), |
| | 52 | | isInL2 = wearable.IsInL2() |
| | 53 | | }; |
| | 54 | | } |
| | 55 | | } |
| | 56 | |
|
| | 57 | | [SerializeField] internal TextMeshProUGUI name; |
| | 58 | | [SerializeField] internal Image thumbnail; |
| | 59 | | [SerializeField] internal IconToGameObjectMap[] icons; |
| | 60 | | [SerializeField] internal TextMeshProUGUI description; |
| | 61 | | [SerializeField] internal TextMeshProUGUI minted; |
| | 62 | | [SerializeField] internal GameObject ethNetwork; |
| | 63 | | [SerializeField] internal GameObject l2Network; |
| | 64 | |
|
| | 65 | | private Model currentModel; |
| | 66 | | private AssetPromise_Texture thumbnailPromise; |
| | 67 | |
|
| | 68 | | public void SetModel(Model newModel) |
| | 69 | | { |
| 36 | 70 | | if (newModel == null) |
| 0 | 71 | | return; |
| | 72 | |
|
| 36 | 73 | | if (currentModel != null && newModel.Equals(currentModel)) |
| 0 | 74 | | return; |
| | 75 | |
|
| 36 | 76 | | currentModel = newModel; |
| | 77 | |
|
| 36 | 78 | | name.text = currentModel.name; |
| | 79 | |
|
| 1296 | 80 | | foreach (var icon in icons) |
| | 81 | | { |
| 612 | 82 | | icon.gameObject.SetActive(currentModel.iconIds.Contains(icon.iconId)); |
| | 83 | | } |
| | 84 | |
|
| 36 | 85 | | if (!string.IsNullOrEmpty(currentModel.description)) |
| 36 | 86 | | description.text = currentModel.description; |
| | 87 | | else |
| 0 | 88 | | description.text = "No description."; |
| | 89 | |
|
| 36 | 90 | | minted.text = $"{currentModel.issuedId} / {currentModel.issuedTotal}"; |
| | 91 | |
|
| 36 | 92 | | Utils.InverseTransformChildTraversal<LayoutGroup>((x) => |
| | 93 | | { |
| 324 | 94 | | RectTransform rt = x.transform as RectTransform; |
| 324 | 95 | | Utils.ForceRebuildLayoutImmediate(rt); |
| 324 | 96 | | }, transform); |
| | 97 | |
|
| | 98 | |
|
| 36 | 99 | | ethNetwork.SetActive(!currentModel.isInL2); |
| 36 | 100 | | l2Network.SetActive(currentModel.isInL2); |
| | 101 | |
|
| 36 | 102 | | if (gameObject.activeInHierarchy) |
| 0 | 103 | | GetThumbnail(); |
| 36 | 104 | | } |
| | 105 | |
|
| 112 | 106 | | public void SetActive(bool active) { gameObject.SetActive(active); } |
| | 107 | |
|
| | 108 | | private void UpdateItemThumbnail(Asset_Texture texture) |
| | 109 | | { |
| 0 | 110 | | if (thumbnail.sprite != null) |
| | 111 | | { |
| 0 | 112 | | Destroy(thumbnail.sprite); |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | thumbnail.sprite = ThumbnailsManager.CreateSpriteFromTexture(texture.texture); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | private void GetThumbnail() |
| | 119 | | { |
| 0 | 120 | | if (currentModel == null) |
| 0 | 121 | | return; |
| | 122 | |
|
| | 123 | | //NOTE(Brian): Get before forget to prevent referenceCount == 0 and asset unload |
| 0 | 124 | | var newThumbnailPromise = ThumbnailsManager.GetThumbnail(currentModel.thumbnail, UpdateItemThumbnail); |
| 0 | 125 | | ThumbnailsManager.ForgetThumbnail(thumbnailPromise); |
| 0 | 126 | | thumbnailPromise = newThumbnailPromise; |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | private void ForgetThumbnail() |
| | 130 | | { |
| 0 | 131 | | ThumbnailsManager.ForgetThumbnail(thumbnailPromise); |
| 0 | 132 | | thumbnailPromise = null; |
| 0 | 133 | | } |
| | 134 | |
|
| 0 | 135 | | private void OnEnable() { GetThumbnail(); } |
| | 136 | |
|
| 0 | 137 | | private void OnDisable() { ForgetThumbnail(); } |
| | 138 | | } |