| | 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 incompatibleWarningPanel; |
| | 22 | | [SerializeField] private GameObject hideWarningPanel; |
| | 23 | | [SerializeField] private GameObject loadingSpinner; |
| | 24 | | [SerializeField] internal RectTransform amountContainer; |
| | 25 | | [SerializeField] internal Animator loadingAnimator; |
| | 26 | | [SerializeField] internal TextMeshProUGUI amountText; |
| | 27 | | [SerializeField] internal GameObject root; |
| | 28 | | [SerializeField] private GameObject disabledOverlay; |
| | 29 | | [SerializeField] internal Material grayScaleMaterial; |
| | 30 | | [SerializeField] internal Button selectButton; |
| | 31 | |
|
| | 32 | | private bool selectedValue; |
| | 33 | |
|
| | 34 | | private AvatarEditorHUDView view; |
| | 35 | |
|
| | 36 | | private Func<WearableItem, bool> getEquippedWearablesReplacedByFunc; |
| | 37 | | private Func<WearableItem, bool> getEquippedWearablesHiddenBy; |
| | 38 | | private Func<WearableItem, bool> getBodyShapeCompatibility; |
| | 39 | |
|
| 0 | 40 | | public string collectionId { get; set; } |
| | 41 | |
|
| | 42 | | public bool selected |
| | 43 | | { |
| 0 | 44 | | get { return selectedValue; } |
| | 45 | | set |
| | 46 | | { |
| 11327 | 47 | | selectedValue = value; |
| 11327 | 48 | | SetSelection(selectedValue); |
| 11327 | 49 | | } |
| | 50 | | } |
| | 51 | |
|
| | 52 | | protected virtual void SetSelection(bool isSelected) |
| | 53 | | { |
| 11327 | 54 | | if (selectionHighlight != null) |
| 11327 | 55 | | selectionHighlight.enabled = isSelected; |
| 11327 | 56 | | } |
| | 57 | |
|
| 16002 | 58 | | public void SetLoadingSpinner(bool isActive) { loadingSpinner?.SetActive(isActive); } |
| | 59 | |
|
| | 60 | | protected new virtual void Awake() |
| | 61 | | { |
| 846 | 62 | | base.Awake(); |
| 846 | 63 | | thumbnail.sprite = null; |
| 846 | 64 | | warningPanel.SetActive(false); |
| 846 | 65 | | hideWarningPanel.SetActive(false); |
| 846 | 66 | | incompatibleWarningPanel.SetActive(false); |
| 846 | 67 | | if (!EnvironmentSettings.RUNNING_TESTS) |
| 0 | 68 | | view = GetComponentInParent<AvatarEditorHUDView>(); |
| 846 | 69 | | } |
| | 70 | |
|
| | 71 | | protected override void OnClick() |
| | 72 | | { |
| 0 | 73 | | OnClicked?.Invoke(this); |
| 0 | 74 | | warningPanel.SetActive(false); |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | public virtual void Initialize(WearableItem w, bool isSelected, int amount, NFTItemToggleSkin skin) |
| | 78 | | { |
| 6447 | 79 | | root.gameObject.SetActive(true); |
| | 80 | |
|
| 6447 | 81 | | wearableItem = w; |
| 6447 | 82 | | selected = isSelected; |
| 6447 | 83 | | amountContainer.gameObject.SetActive(amount > 1); |
| 6447 | 84 | | amountText.text = $"x{amount.ToString()}"; |
| | 85 | |
|
| 6447 | 86 | | if (gameObject.activeInHierarchy) |
| 66 | 87 | | GetThumbnail(); |
| 6447 | 88 | | } |
| | 89 | |
|
| | 90 | | public void OnPointerEnter(PointerEventData eventData) |
| | 91 | | { |
| 0 | 92 | | incompatibleWarningPanel.SetActive(false); |
| 0 | 93 | | warningPanel.SetActive(false); |
| 0 | 94 | | hideWarningPanel.SetActive(false); |
| 0 | 95 | | if(IsIncompatible()) |
| | 96 | | { |
| 0 | 97 | | incompatibleWarningPanel.SetActive(true); |
| 0 | 98 | | } |
| 0 | 99 | | else if(IsReplacingWearables()) |
| | 100 | | { |
| 0 | 101 | | warningPanel.SetActive(true); |
| 0 | 102 | | } |
| 0 | 103 | | else if(IsHidingWearables()) |
| | 104 | | { |
| 0 | 105 | | hideWarningPanel.SetActive(true); |
| | 106 | | } |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | private bool IsIncompatible() |
| | 110 | | { |
| 0 | 111 | | if(getBodyShapeCompatibility == null) return false; |
| 0 | 112 | | return getBodyShapeCompatibility(wearableItem); |
| | 113 | | } |
| | 114 | |
|
| | 115 | | private bool IsReplacingWearables() |
| | 116 | | { |
| 0 | 117 | | if (getEquippedWearablesReplacedByFunc == null) return false; |
| 0 | 118 | | return getEquippedWearablesReplacedByFunc(wearableItem); |
| | 119 | | } |
| | 120 | |
|
| | 121 | | private bool IsHidingWearables() |
| | 122 | | { |
| 0 | 123 | | if (getEquippedWearablesHiddenBy == null) return false; |
| 0 | 124 | | return getEquippedWearablesHiddenBy(wearableItem); |
| | 125 | | } |
| | 126 | |
|
| | 127 | | public void OnPointerExit(PointerEventData eventData) |
| | 128 | | { |
| 0 | 129 | | warningPanel.SetActive(false); |
| 0 | 130 | | hideWarningPanel.SetActive(false); |
| 0 | 131 | | incompatibleWarningPanel.SetActive(false); |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | public void SetHideOtherWerablesToastStrategy(Func<WearableItem, bool> function) => |
| 0 | 135 | | getEquippedWearablesHiddenBy = function; |
| | 136 | |
|
| | 137 | | public void SetBodyShapeCompatibilityStrategy(Func<WearableItem, bool> function) => |
| 0 | 138 | | getBodyShapeCompatibility = function; |
| | 139 | |
|
| | 140 | | public void SetReplaceOtherWearablesToastStrategy(Func<WearableItem, bool> function) => |
| 0 | 141 | | getEquippedWearablesReplacedByFunc = function; |
| | 142 | |
|
| | 143 | | private void OnEnable() |
| | 144 | | { |
| 846 | 145 | | GetThumbnail(); |
| 846 | 146 | | } |
| | 147 | |
|
| | 148 | | protected virtual void OnDestroy() |
| | 149 | | { |
| 0 | 150 | | OnClicked = null; |
| 0 | 151 | | } |
| | 152 | |
|
| 0 | 153 | | protected void CallOnSellClicked() { OnSellClicked?.Invoke(this); } |
| | 154 | |
|
| | 155 | | private void SetColorScale() |
| | 156 | | { |
| 0 | 157 | | if(getBodyShapeCompatibility != null && getBodyShapeCompatibility(wearableItem)) |
| | 158 | | { |
| 0 | 159 | | thumbnail.material = grayScaleMaterial; |
| 0 | 160 | | thumbnail.SetMaterialDirty(); |
| 0 | 161 | | disabledOverlay.SetActive(true); |
| 0 | 162 | | selectButton.interactable = false; |
| 0 | 163 | | } |
| | 164 | | else |
| | 165 | | { |
| 0 | 166 | | thumbnail.material = null; |
| 0 | 167 | | disabledOverlay.SetActive(false); |
| 0 | 168 | | selectButton.interactable = true; |
| | 169 | | } |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | private void GetThumbnail() |
| | 173 | | { |
| 912 | 174 | | string url = wearableItem?.ComposeThumbnailUrl(); |
| | 175 | |
|
| 912 | 176 | | if (wearableItem == null || string.IsNullOrEmpty(url)) |
| | 177 | | { |
| 780 | 178 | | SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED); |
| 780 | 179 | | return; |
| | 180 | | } |
| | 181 | |
|
| 132 | 182 | | ThumbnailsManager.GetThumbnail(url, OnThumbnailReady); |
| 132 | 183 | | } |
| | 184 | |
|
| | 185 | | private void OnThumbnailReady(Asset_Texture texture) |
| | 186 | | { |
| 0 | 187 | | SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED); |
| | 188 | |
|
| 0 | 189 | | thumbnail.sprite = ThumbnailsManager.GetOrCreateSpriteFromTexture(texture.texture, out var wasCreated); |
| | 190 | |
|
| 0 | 191 | | if (view != null && wasCreated) |
| | 192 | | { |
| 0 | 193 | | if (view.avatarEditorCanvas.enabled) |
| 0 | 194 | | AudioScriptableObjects.listItemAppear.Play(true); |
| | 195 | | } |
| 0 | 196 | | SetColorScale(); |
| 0 | 197 | | } |
| | 198 | |
|
| | 199 | | private void SetLoadingAnimationTrigger(int id) |
| | 200 | | { |
| 780 | 201 | | if (!loadingAnimator.isInitialized || loadingAnimator.runtimeAnimatorController == null) |
| 752 | 202 | | return; |
| | 203 | |
|
| 28 | 204 | | loadingAnimator.SetTrigger(id); |
| 28 | 205 | | } |
| | 206 | |
|
| | 207 | | public void Hide() |
| | 208 | | { |
| 58209 | 209 | | root.gameObject.SetActive(false); |
| 58209 | 210 | | } |
| | 211 | | public void SetCallbacks(Action<ItemToggle> toggleClicked, Action<ItemToggle> sellClicked) |
| | 212 | | { |
| 0 | 213 | | OnClicked = toggleClicked; |
| 0 | 214 | | OnSellClicked = sellClicked; |
| 0 | 215 | | } |
| | 216 | | } |