| | 1 | | using System; |
| | 2 | | using TMPro; |
| | 3 | | using UIComponents.Scripts.Components; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | namespace DCL.Backpack |
| | 8 | | { |
| | 9 | | public class WearableGridItemComponentView : BaseComponentView<WearableGridItemModel> |
| | 10 | | { |
| | 11 | | [SerializeField] internal NftRarityBackgroundSO rarityNftBackgrounds; |
| | 12 | | [SerializeField] internal NFTTypeIconsAndColors nftTypesIcons; |
| | 13 | | [SerializeField] internal Image nftBackground; |
| | 14 | | [SerializeField] internal Image categoryImage; |
| | 15 | | [SerializeField] internal Image categoryBackground; |
| | 16 | | [SerializeField] internal GameObject selectedContainer; |
| | 17 | | [SerializeField] internal GameObject equippedContainer; |
| | 18 | | [SerializeField] internal GameObject isNewContainer; |
| | 19 | | [SerializeField] internal ImageComponentView image; |
| | 20 | | [SerializeField] internal Button interactButton; |
| | 21 | | [SerializeField] internal Material grayScaleMaterial; |
| | 22 | | [SerializeField] internal GameObject incompatibleContainer; |
| | 23 | | [SerializeField] internal GameObject incompatibleTooltip; |
| | 24 | | [SerializeField] internal GameObject smartWearableFlag; |
| | 25 | | [SerializeField] internal GameObject secondaryItem; |
| | 26 | | [SerializeField] internal GameObject amountLabel; |
| | 27 | | [SerializeField] internal TMP_Text amountText; |
| | 28 | |
|
| | 29 | | private IButtonDoubleClick interactDoubleClick; |
| | 30 | | private string lastThumbnailUrl; |
| | 31 | |
|
| 3 | 32 | | public WearableGridItemModel Model => model; |
| | 33 | |
|
| | 34 | | private int clicked = 0; |
| | 35 | | private float clickTime = 0; |
| | 36 | |
|
| | 37 | | public event Action<WearableGridItemModel> OnSelected; |
| | 38 | | public event Action<WearableGridItemModel> OnEquipped; |
| | 39 | | public event Action<WearableGridItemModel> OnUnequipped; |
| | 40 | |
|
| | 41 | | public override void Awake() |
| | 42 | | { |
| 65 | 43 | | base.Awake(); |
| 65 | 44 | | image.OnLoaded += PlayLoadingSound; |
| 65 | 45 | | InitializeInteractButton(); |
| 65 | 46 | | } |
| | 47 | |
|
| | 48 | | private void InitializeInteractButton() |
| | 49 | | { |
| 65 | 50 | | interactDoubleClick = interactButton.gameObject.GetComponent<IButtonDoubleClick>(); |
| 65 | 51 | | interactDoubleClick.AlwaysPerformSingleClick = true; |
| 73 | 52 | | interactDoubleClick.OnClick += () => { OnSelected?.Invoke(model); }; |
| 65 | 53 | | interactDoubleClick.OnDoubleClick += () => |
| | 54 | | { |
| 0 | 55 | | if (model.IsEquipped) |
| | 56 | | { |
| 0 | 57 | | if (!model.UnEquipAllowed) |
| 0 | 58 | | return; |
| | 59 | |
|
| 0 | 60 | | OnUnequipped?.Invoke(model); |
| | 61 | | } |
| | 62 | | else |
| 0 | 63 | | OnEquipped?.Invoke(model); |
| 0 | 64 | | }; |
| 65 | 65 | | } |
| | 66 | |
|
| | 67 | | public override void Dispose() => |
| 86 | 68 | | image.OnLoaded -= PlayLoadingSound; |
| | 69 | |
|
| | 70 | | public override void OnFocus() |
| | 71 | | { |
| 1 | 72 | | base.OnFocus(); |
| 1 | 73 | | RefreshControl(); |
| 1 | 74 | | selectedContainer.SetActive(true); |
| 1 | 75 | | } |
| | 76 | |
|
| | 77 | | public override void OnLoseFocus() |
| | 78 | | { |
| 83 | 79 | | base.OnLoseFocus(); |
| 83 | 80 | | selectedContainer.SetActive(false); |
| 83 | 81 | | RefreshControl(); |
| 83 | 82 | | incompatibleTooltip.SetActive(false); |
| 83 | 83 | | } |
| | 84 | |
|
| | 85 | | public override void RefreshControl() |
| | 86 | | { |
| 117 | 87 | | amountText.text = model.Amount; |
| 117 | 88 | | secondaryItem.SetActive(!string.IsNullOrEmpty(amountText.text)); |
| 117 | 89 | | amountLabel.SetActive(!string.IsNullOrEmpty(amountText.text)); |
| 117 | 90 | | selectedContainer.SetActive(model.IsSelected); |
| 117 | 91 | | equippedContainer.SetActive(model.IsEquipped); |
| 117 | 92 | | isNewContainer.SetActive(model.IsNew); |
| 117 | 93 | | smartWearableFlag.SetActive(model.IsSmartWearable); |
| | 94 | | // we gotta check for url changes, otherwise the image component will start a "loading" state, even if the u |
| 117 | 95 | | if (lastThumbnailUrl != model.ImageUrl) |
| | 96 | | { |
| 30 | 97 | | image.SetImage(model.ImageUrl); |
| 30 | 98 | | lastThumbnailUrl = model.ImageUrl; |
| | 99 | | } |
| | 100 | |
|
| 117 | 101 | | string nftRarity = model.Rarity.ToString().ToLower(); |
| 117 | 102 | | nftBackground.sprite = rarityNftBackgrounds.GetRarityImage(nftRarity); |
| 117 | 103 | | categoryBackground.color = nftTypesIcons.GetColor(nftRarity); |
| 117 | 104 | | categoryImage.sprite = nftTypesIcons.GetTypeImage(model.Category); |
| | 105 | |
|
| 117 | 106 | | if (model.IsCompatibleWithBodyShape) |
| 117 | 107 | | image.ImageComponent.material = null; |
| | 108 | | else |
| 0 | 109 | | image.ImageComponent.material = grayScaleMaterial; |
| | 110 | |
|
| 117 | 111 | | incompatibleContainer.SetActive(!model.IsCompatibleWithBodyShape); |
| 117 | 112 | | interactButton.interactable = model.IsCompatibleWithBodyShape; |
| 117 | 113 | | incompatibleTooltip.SetActive(!model.IsCompatibleWithBodyShape && isFocused); |
| 117 | 114 | | } |
| | 115 | |
|
| | 116 | | private void PlayLoadingSound(Sprite sprt) |
| | 117 | | { |
| 0 | 118 | | AudioScriptableObjects.listItemAppear.Play(true); |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | public void Unselect() |
| | 122 | | { |
| 2 | 123 | | model.IsSelected = false; |
| 2 | 124 | | RefreshControl(); |
| 2 | 125 | | } |
| | 126 | |
|
| | 127 | | public void Select() |
| | 128 | | { |
| 1 | 129 | | model.IsSelected = true; |
| 1 | 130 | | RefreshControl(); |
| 1 | 131 | | } |
| | 132 | | } |
| | 133 | | } |