| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | public class ItemToggle : UIButton, IPointerEnterHandler, IPointerExitHandler |
| | 10 | | { |
| 1 | 11 | | private static readonly int LOADING_ANIMATOR_TRIGGER_LOADED = Animator.StringToHash("Loaded"); |
| | 12 | |
|
| | 13 | | public event Action<ItemToggle> OnClicked; |
| | 14 | | public event Action<ItemToggle> OnSellClicked; |
| | 15 | |
|
| 0 | 16 | | public WearableItem wearableItem { get; private set; } |
| | 17 | |
|
| | 18 | | public Image thumbnail; |
| | 19 | | public Image selectionHighlight; |
| | 20 | | [SerializeField] private GameObject warningPanel; |
| | 21 | | [SerializeField] private GameObject hideWarningPanel; |
| | 22 | | [SerializeField] private GameObject loadingSpinner; |
| | 23 | | [SerializeField] internal RectTransform amountContainer; |
| | 24 | | [SerializeField] internal Animator loadingAnimator; |
| | 25 | | [SerializeField] internal TextMeshProUGUI amountText; |
| | 26 | | [SerializeField] internal GameObject root; |
| | 27 | |
|
| | 28 | | private bool selectedValue; |
| | 29 | |
|
| | 30 | | private AvatarEditorHUDView view; |
| | 31 | |
|
| | 32 | | private Func<WearableItem, bool> getEquippedWearablesReplacedByFunc; |
| | 33 | | private Func<WearableItem, bool> getEquippedWearablesHiddenBy; |
| | 34 | |
|
| 0 | 35 | | public string collectionId { get; set; } |
| | 36 | |
|
| | 37 | | public bool selected |
| | 38 | | { |
| 0 | 39 | | get { return selectedValue; } |
| | 40 | | set |
| | 41 | | { |
| 6762 | 42 | | selectedValue = value; |
| 6762 | 43 | | SetSelection(selectedValue); |
| 6762 | 44 | | } |
| | 45 | | } |
| | 46 | |
|
| | 47 | | protected virtual void SetSelection(bool isSelected) |
| | 48 | | { |
| 6762 | 49 | | if (selectionHighlight != null) |
| 6762 | 50 | | selectionHighlight.enabled = isSelected; |
| 6762 | 51 | | } |
| | 52 | |
|
| 8706 | 53 | | public void SetLoadingSpinner(bool isActive) { loadingSpinner?.SetActive(isActive); } |
| | 54 | |
|
| | 55 | | protected new virtual void Awake() |
| | 56 | | { |
| 846 | 57 | | base.Awake(); |
| 846 | 58 | | thumbnail.sprite = null; |
| 846 | 59 | | warningPanel.SetActive(false); |
| 846 | 60 | | hideWarningPanel.SetActive(false); |
| | 61 | |
|
| 846 | 62 | | if (!EnvironmentSettings.RUNNING_TESTS) |
| 0 | 63 | | view = GetComponentInParent<AvatarEditorHUDView>(); |
| 846 | 64 | | } |
| | 65 | |
|
| | 66 | | protected override void OnClick() |
| | 67 | | { |
| 0 | 68 | | OnClicked?.Invoke(this); |
| 0 | 69 | | warningPanel.SetActive(false); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | public virtual void Initialize(WearableItem w, bool isSelected, int amount, NFTItemToggleSkin skin) |
| | 73 | | { |
| 2815 | 74 | | root.gameObject.SetActive(true); |
| | 75 | |
|
| 2815 | 76 | | wearableItem = w; |
| 2815 | 77 | | selected = isSelected; |
| 2815 | 78 | | amountContainer.gameObject.SetActive(amount > 1); |
| 2815 | 79 | | amountText.text = $"x{amount.ToString()}"; |
| | 80 | |
|
| 2815 | 81 | | if (gameObject.activeInHierarchy) |
| 34 | 82 | | GetThumbnail(); |
| 2815 | 83 | | } |
| | 84 | |
|
| | 85 | | public void OnPointerEnter(PointerEventData eventData) |
| | 86 | | { |
| 0 | 87 | | if (!ShowReplacementWarningPanel()) |
| 0 | 88 | | ShowHidingWarningPanel(); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private bool ShowReplacementWarningPanel() |
| | 92 | | { |
| 0 | 93 | | if (getEquippedWearablesReplacedByFunc == null) return false; |
| 0 | 94 | | var shouldShow = getEquippedWearablesReplacedByFunc(wearableItem); |
| 0 | 95 | | warningPanel.SetActive(shouldShow); |
| 0 | 96 | | return shouldShow; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | private bool ShowHidingWarningPanel() |
| | 100 | | { |
| 0 | 101 | | if (getEquippedWearablesHiddenBy == null) return false; |
| 0 | 102 | | var shouldShow = getEquippedWearablesHiddenBy(wearableItem); |
| 0 | 103 | | hideWarningPanel.SetActive(shouldShow); |
| 0 | 104 | | return shouldShow; |
| | 105 | | } |
| | 106 | |
|
| | 107 | | public void OnPointerExit(PointerEventData eventData) |
| | 108 | | { |
| 0 | 109 | | warningPanel.SetActive(false); |
| 0 | 110 | | hideWarningPanel.SetActive(false); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | public void SetHideOtherWerablesToastStrategy(Func<WearableItem, bool> function) => |
| 0 | 114 | | getEquippedWearablesHiddenBy = function; |
| | 115 | |
|
| | 116 | | public void SetReplaceOtherWearablesToastStrategy(Func<WearableItem, bool> function) => |
| 0 | 117 | | getEquippedWearablesReplacedByFunc = function; |
| | 118 | |
|
| 1692 | 119 | | private void OnEnable() { GetThumbnail(); } |
| | 120 | |
|
| | 121 | | protected virtual void OnDestroy() |
| | 122 | | { |
| 0 | 123 | | OnClicked = null; |
| 0 | 124 | | } |
| | 125 | |
|
| 0 | 126 | | protected void CallOnSellClicked() { OnSellClicked?.Invoke(this); } |
| | 127 | |
|
| | 128 | | private void GetThumbnail() |
| | 129 | | { |
| 880 | 130 | | string url = wearableItem?.ComposeThumbnailUrl(); |
| | 131 | |
|
| 880 | 132 | | if (wearableItem == null || string.IsNullOrEmpty(url)) |
| | 133 | | { |
| 768 | 134 | | SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED); |
| 768 | 135 | | return; |
| | 136 | | } |
| | 137 | |
|
| 112 | 138 | | ThumbnailsManager.GetThumbnail(url, OnThumbnailReady); |
| 112 | 139 | | } |
| | 140 | |
|
| | 141 | | private void OnThumbnailReady(Asset_Texture texture) |
| | 142 | | { |
| 0 | 143 | | SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED); |
| | 144 | |
|
| 0 | 145 | | thumbnail.sprite = ThumbnailsManager.GetOrCreateSpriteFromTexture(texture.texture, out var wasCreated); |
| | 146 | |
|
| 0 | 147 | | if (view != null && wasCreated) |
| | 148 | | { |
| 0 | 149 | | if (view.avatarEditorCanvas.enabled) |
| 0 | 150 | | AudioScriptableObjects.listItemAppear.Play(true); |
| | 151 | | } |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | private void SetLoadingAnimationTrigger(int id) |
| | 155 | | { |
| 768 | 156 | | if (!loadingAnimator.isInitialized || loadingAnimator.runtimeAnimatorController == null) |
| 752 | 157 | | return; |
| | 158 | |
|
| 16 | 159 | | loadingAnimator.SetTrigger(id); |
| 16 | 160 | | } |
| | 161 | |
|
| | 162 | | public void Hide() |
| | 163 | | { |
| 61841 | 164 | | root.gameObject.SetActive(false); |
| 61841 | 165 | | } |
| | 166 | | public void SetCallbacks(Action<ItemToggle> toggleClicked, Action<ItemToggle> sellClicked) |
| | 167 | | { |
| 0 | 168 | | OnClicked = toggleClicked; |
| 0 | 169 | | OnSellClicked = sellClicked; |
| 0 | 170 | | } |
| | 171 | | } |