| | 1 | | using DCL; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.EventSystems; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | public class CatalogItemAdapter : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler, IPointerEnterHandler, |
| | 7 | | { |
| | 8 | | public RawImage thumbnailImg; |
| | 9 | | public Image backgroundImg; |
| | 10 | | public CanvasGroup canvasGroup; |
| | 11 | | public GameObject lockedGO; |
| | 12 | | public GameObject loadingGO; |
| | 13 | |
|
| | 14 | | [Header("Smart Items")] |
| | 15 | | public GameObject smartItemGO; |
| | 16 | |
|
| | 17 | | public Color smartItemColor; |
| | 18 | | public Color normalColor; |
| | 19 | |
|
| | 20 | | [Header("Favorites")] |
| | 21 | | public Color offFavoriteColor; |
| | 22 | |
|
| | 23 | | public Color onFavoriteColor; |
| | 24 | | public Image favImg; |
| | 25 | |
|
| | 26 | | public System.Action<CatalogItem> OnCatalogItemClicked; |
| | 27 | | public System.Action<CatalogItem, CatalogItemAdapter> OnCatalogItemFavorite; |
| | 28 | | public System.Action<CatalogItem, CatalogItemAdapter, BaseEventData> OnAdapterStartDrag; |
| | 29 | | public System.Action<PointerEventData> OnAdapterDrag, OnAdapterEndDrag; |
| | 30 | | public System.Action<PointerEventData, CatalogItemAdapter> OnPointerEnterInAdapter; |
| | 31 | | public System.Action<PointerEventData, CatalogItemAdapter> OnPointerExitInAdapter; |
| | 32 | |
|
| | 33 | | private CatalogItem catalogItem; |
| | 34 | | private string loadedThumbnailURL; |
| | 35 | | private AssetPromise_Texture loadedThumbnailPromise; |
| | 36 | |
|
| | 37 | | private const float ADAPTER_DRAGGING_SIZE_SCALE = 0.75f; |
| | 38 | |
|
| 0 | 39 | | public CatalogItem GetContent() { return catalogItem; } |
| | 40 | |
|
| | 41 | | public void SetContent(CatalogItem catalogItem) |
| | 42 | | { |
| 9 | 43 | | this.catalogItem = catalogItem; |
| | 44 | |
|
| 9 | 45 | | if (favImg != null) |
| 9 | 46 | | favImg.color = catalogItem.IsFavorite() ? onFavoriteColor : offFavoriteColor; |
| | 47 | |
|
| 9 | 48 | | if (backgroundImg != null) |
| 0 | 49 | | backgroundImg.color = catalogItem.IsSmartItem() ? smartItemColor : normalColor; |
| 9 | 50 | | smartItemGO.SetActive(catalogItem.IsSmartItem()); |
| | 51 | |
|
| 9 | 52 | | GetThumbnail(); |
| | 53 | |
|
| 9 | 54 | | lockedGO.gameObject.SetActive(false); |
| | 55 | |
|
| 9 | 56 | | if (catalogItem.IsNFT() && BIWNFTController.i.IsNFTInUse(catalogItem.id)) |
| 0 | 57 | | lockedGO.gameObject.SetActive(true); |
| 9 | 58 | | } |
| | 59 | |
|
| | 60 | | private void GetThumbnail() |
| | 61 | | { |
| 9 | 62 | | SetLoadingActive(true); |
| | 63 | |
|
| 9 | 64 | | if (catalogItem == null) |
| 0 | 65 | | return; |
| | 66 | |
|
| 9 | 67 | | var url = catalogItem.GetThumbnailUrl(); |
| | 68 | |
|
| 9 | 69 | | if (string.IsNullOrEmpty(url) || url == loadedThumbnailURL) |
| 8 | 70 | | return; |
| | 71 | |
|
| 1 | 72 | | ClearThumbnailPromise(); |
| 1 | 73 | | loadedThumbnailPromise = new AssetPromise_Texture(url); |
| | 74 | |
|
| 1 | 75 | | loadedThumbnailPromise.OnSuccessEvent += x => |
| | 76 | | { |
| 0 | 77 | | loadedThumbnailURL = url; |
| 0 | 78 | | SetThumbnail(x); |
| 0 | 79 | | }; |
| | 80 | |
|
| 1 | 81 | | loadedThumbnailPromise.OnFailEvent += (x, error) => |
| | 82 | | { |
| 0 | 83 | | Debug.Log($"Error downloading: {url}, Exception: {error}"); |
| 0 | 84 | | SetLoadingActive(false); |
| 0 | 85 | | }; |
| | 86 | |
|
| 1 | 87 | | AssetPromiseKeeper_Texture.i.Keep(loadedThumbnailPromise); |
| 1 | 88 | | } |
| | 89 | |
|
| | 90 | | private void ClearThumbnailPromise() |
| | 91 | | { |
| 6 | 92 | | if (loadedThumbnailPromise != null) |
| | 93 | | { |
| 1 | 94 | | loadedThumbnailPromise.ClearEvents(); |
| 1 | 95 | | AssetPromiseKeeper_Texture.i.Forget(loadedThumbnailPromise); |
| 1 | 96 | | loadedThumbnailPromise = null; |
| | 97 | | } |
| 6 | 98 | | } |
| | 99 | |
|
| | 100 | | private void SetLoadingActive(bool isActive) |
| | 101 | | { |
| 9 | 102 | | if (loadingGO == null) |
| 9 | 103 | | return; |
| | 104 | |
|
| 0 | 105 | | loadingGO.SetActive(isActive); |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | public void EnableDragMode(Vector2 sizeDelta) |
| | 109 | | { |
| 4 | 110 | | RectTransform newAdapterRT = GetComponent<RectTransform>(); |
| 4 | 111 | | canvasGroup.blocksRaycasts = false; |
| 4 | 112 | | canvasGroup.alpha = 0.6f; |
| | 113 | |
|
| 4 | 114 | | if (favImg != null) |
| 4 | 115 | | favImg.enabled = false; |
| | 116 | |
|
| 4 | 117 | | if (backgroundImg != null) |
| 0 | 118 | | backgroundImg.enabled = false; |
| | 119 | |
|
| 4 | 120 | | newAdapterRT.sizeDelta = sizeDelta * ADAPTER_DRAGGING_SIZE_SCALE; |
| 4 | 121 | | } |
| | 122 | |
|
| | 123 | | public void SetFavorite(bool isOn) |
| | 124 | | { |
| 0 | 125 | | if (isOn) |
| 0 | 126 | | favImg.color = onFavoriteColor; |
| | 127 | | else |
| 0 | 128 | | favImg.color = offFavoriteColor; |
| 0 | 129 | | } |
| | 130 | |
|
| 18 | 131 | | public void AdapterStartDragging(BaseEventData baseEventData) { OnAdapterStartDrag?.Invoke(catalogItem, this, baseEv |
| | 132 | |
|
| 0 | 133 | | public void FavoriteIconClicked() { OnCatalogItemFavorite?.Invoke(catalogItem, this); } |
| | 134 | |
|
| | 135 | | public void SceneObjectClicked() |
| | 136 | | { |
| 0 | 137 | | if (!lockedGO.gameObject.activeSelf) |
| 0 | 138 | | OnCatalogItemClicked?.Invoke(catalogItem); |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | public void SetThumbnail(Asset_Texture texture) |
| | 142 | | { |
| 0 | 143 | | if (thumbnailImg != null) |
| | 144 | | { |
| 0 | 145 | | thumbnailImg.enabled = true; |
| 0 | 146 | | thumbnailImg.texture = texture.texture; |
| 0 | 147 | | favImg.gameObject.SetActive(true); |
| | 148 | |
|
| 0 | 149 | | if (gameObject.activeInHierarchy && ItemAdapterIsOnScreen()) |
| 0 | 150 | | AudioScriptableObjects.listItemAppear.Play(); |
| | 151 | | } |
| | 152 | |
|
| 0 | 153 | | SetLoadingActive(false); |
| 0 | 154 | | } |
| | 155 | |
|
| 0 | 156 | | public void OnBeginDrag(PointerEventData eventData) { AdapterStartDragging(eventData); } |
| | 157 | |
|
| 0 | 158 | | public void OnDrag(PointerEventData eventData) { OnAdapterDrag?.Invoke(eventData); } |
| | 159 | |
|
| 0 | 160 | | public void OnEndDrag(PointerEventData eventData) { OnAdapterEndDrag?.Invoke(eventData); } |
| | 161 | |
|
| 0 | 162 | | public void OnPointerEnter(PointerEventData eventData) { OnPointerEnterInAdapter?.Invoke(eventData, this); } |
| | 163 | |
|
| 0 | 164 | | public void OnPointerExit(PointerEventData eventData) { OnPointerExitInAdapter?.Invoke(eventData, this); } |
| | 165 | |
|
| 0 | 166 | | private bool ItemAdapterIsOnScreen() { return (transform.position.y > 0 && transform.position.y < Screen.height); } |
| | 167 | |
|
| 10 | 168 | | private void OnDestroy() { ClearThumbnailPromise(); } |
| | 169 | | } |