| | 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 | | { |
| 0 | 11 | | private static readonly string ANIMATION_LOADED = "Loaded"; |
| 0 | 12 | | private static readonly string ANIMATION_LOADING_IDLE = "LoadingIdle"; |
| | 13 | |
|
| | 14 | | public event Action<ItemToggle> OnClicked; |
| | 15 | | public event Action<ItemToggle> OnSellClicked; |
| | 16 | |
|
| 0 | 17 | | public WearableItem wearableItem { get; private set; } |
| | 18 | |
|
| | 19 | | public Image thumbnail; |
| | 20 | | public Image selectionHighlight; |
| | 21 | | [SerializeField] private GameObject warningPanel; |
| | 22 | | [SerializeField] private GameObject incompatibleWarningPanel; |
| | 23 | | [SerializeField] private GameObject hideWarningPanel; |
| | 24 | | [SerializeField] private GameObject loadingSpinner; |
| | 25 | | [SerializeField] internal RectTransform amountContainer; |
| | 26 | | [SerializeField] internal Animation loadingAnimation; |
| | 27 | | [SerializeField] internal TextMeshProUGUI amountText; |
| | 28 | | [SerializeField] internal GameObject root; |
| | 29 | | [SerializeField] private GameObject disabledOverlay; |
| | 30 | | [SerializeField] internal Material grayScaleMaterial; |
| | 31 | | [SerializeField] internal Button selectButton; |
| | 32 | |
|
| | 33 | | private bool selectedValue; |
| | 34 | |
|
| | 35 | | private AvatarEditorHUDView view; |
| | 36 | |
|
| | 37 | | private Func<WearableItem, bool> getEquippedWearablesReplacedByFunc; |
| | 38 | | private Func<WearableItem, bool> getEquippedWearablesHiddenBy; |
| | 39 | | private Func<WearableItem, bool> getBodyShapeCompatibility; |
| | 40 | |
|
| 0 | 41 | | public string collectionId { get; set; } |
| | 42 | |
|
| | 43 | | public bool selected |
| | 44 | | { |
| 0 | 45 | | get { return selectedValue; } |
| | 46 | | set |
| | 47 | | { |
| 0 | 48 | | selectedValue = value; |
| 0 | 49 | | SetSelection(selectedValue); |
| 0 | 50 | | } |
| | 51 | | } |
| | 52 | |
|
| | 53 | | protected virtual void SetSelection(bool isSelected) |
| | 54 | | { |
| 0 | 55 | | if (selectionHighlight != null) |
| 0 | 56 | | selectionHighlight.enabled = isSelected; |
| 0 | 57 | | } |
| | 58 | |
|
| 0 | 59 | | public void SetLoadingSpinner(bool isActive) { loadingSpinner?.SetActive(isActive); } |
| | 60 | |
|
| | 61 | | protected new virtual void Awake() |
| | 62 | | { |
| 0 | 63 | | base.Awake(); |
| 0 | 64 | | thumbnail.sprite = null; |
| 0 | 65 | | warningPanel.SetActive(false); |
| 0 | 66 | | hideWarningPanel.SetActive(false); |
| 0 | 67 | | incompatibleWarningPanel.SetActive(false); |
| 0 | 68 | | if (!EnvironmentSettings.RUNNING_TESTS) |
| 0 | 69 | | view = GetComponentInParent<AvatarEditorHUDView>(); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | protected override void OnClick() |
| | 73 | | { |
| 0 | 74 | | OnClicked?.Invoke(this); |
| 0 | 75 | | warningPanel.SetActive(false); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | public virtual void Initialize(WearableItem w, bool isSelected, int amount, NFTItemToggleSkin skin) |
| | 79 | | { |
| 0 | 80 | | root.gameObject.SetActive(true); |
| | 81 | |
|
| 0 | 82 | | wearableItem = w; |
| 0 | 83 | | selected = isSelected; |
| 0 | 84 | | amountContainer.gameObject.SetActive(amount > 1); |
| 0 | 85 | | amountText.text = $"x{amount.ToString()}"; |
| | 86 | |
|
| 0 | 87 | | UpdateThumbnail(); |
| 0 | 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); |
| | 98 | | } |
| 0 | 99 | | else if(IsReplacingWearables()) |
| | 100 | | { |
| 0 | 101 | | warningPanel.SetActive(true); |
| | 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 | | { |
| 0 | 145 | | UpdateThumbnail(); |
| 0 | 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; |
| | 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 UpdateThumbnail() |
| | 173 | | { |
| 0 | 174 | | string url = wearableItem?.ComposeThumbnailUrl(); |
| | 175 | |
|
| 0 | 176 | | if (wearableItem == null || string.IsNullOrEmpty(url)) |
| | 177 | | { |
| 0 | 178 | | SetLoadingAnimation(ANIMATION_LOADED); |
| 0 | 179 | | return; |
| | 180 | | } |
| | 181 | |
|
| 0 | 182 | | SetLoadingAnimation(ANIMATION_LOADING_IDLE); |
| | 183 | |
|
| 0 | 184 | | ThumbnailsManager.GetThumbnail(url, OnThumbnailReady); |
| 0 | 185 | | } |
| | 186 | |
|
| | 187 | | private void OnThumbnailReady(Asset_Texture texture) |
| | 188 | | { |
| 0 | 189 | | SetLoadingAnimation(ANIMATION_LOADED); |
| | 190 | |
|
| 0 | 191 | | thumbnail.sprite = ThumbnailsManager.GetOrCreateSpriteFromTexture(texture.texture, out var wasCreated); |
| | 192 | |
|
| 0 | 193 | | if (view != null && wasCreated) |
| | 194 | | { |
| 0 | 195 | | if (view.avatarEditorCanvas.enabled) |
| 0 | 196 | | AudioScriptableObjects.listItemAppear.Play(true); |
| | 197 | | } |
| 0 | 198 | | SetColorScale(); |
| 0 | 199 | | } |
| | 200 | |
|
| | 201 | | private void SetLoadingAnimation(string id) |
| | 202 | | { |
| 0 | 203 | | if (!loadingAnimation.isActiveAndEnabled) |
| 0 | 204 | | return; |
| | 205 | |
|
| 0 | 206 | | loadingAnimation.Play(id); |
| 0 | 207 | | } |
| | 208 | |
|
| | 209 | | public void Hide() |
| | 210 | | { |
| 0 | 211 | | root.gameObject.SetActive(false); |
| 0 | 212 | | } |
| | 213 | | public void SetCallbacks(Action<ItemToggle> toggleClicked, Action<ItemToggle> sellClicked) |
| | 214 | | { |
| 0 | 215 | | OnClicked = toggleClicked; |
| 0 | 216 | | OnSellClicked = sellClicked; |
| 0 | 217 | | } |
| | 218 | | } |