| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Configuration; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.EventSystems; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | public class ItemToggle : UIButton, IPointerEnterHandler, IPointerExitHandler |
| | 11 | | { |
| 1 | 12 | | private static readonly int LOADING_ANIMATOR_TRIGGER_LOADED = Animator.StringToHash("Loaded"); |
| | 13 | |
|
| | 14 | | public event System.Action<ItemToggle> OnClicked; |
| | 15 | | public event System.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 loadingSpinner; |
| | 23 | | [SerializeField] internal RectTransform amountContainer; |
| | 24 | | [SerializeField] internal Animator loadingAnimator; |
| | 25 | | [SerializeField] internal TextMeshProUGUI amountText; |
| | 26 | |
|
| | 27 | | private bool selectedValue; |
| | 28 | |
|
| | 29 | | private string loadedThumbnailURL; |
| | 30 | | private AssetPromise_Texture loadedThumbnailPromise; |
| | 31 | |
|
| | 32 | | private AvatarEditorHUDView view; |
| | 33 | |
|
| | 34 | | //Todo change this for a confirmation popup or implement it in a more elegant way |
| | 35 | | public static Func<WearableItem, List<WearableItem>> getEquippedWearablesReplacedByFunc; |
| | 36 | |
|
| | 37 | | public bool selected |
| | 38 | | { |
| 0 | 39 | | get { return selectedValue; } |
| | 40 | | set |
| | 41 | | { |
| 7915 | 42 | | selectedValue = value; |
| 7915 | 43 | | SetSelection(selectedValue); |
| 7915 | 44 | | } |
| | 45 | | } |
| | 46 | |
|
| | 47 | | protected virtual void SetSelection(bool isSelected) |
| | 48 | | { |
| 7915 | 49 | | if (selectionHighlight != null) |
| 7915 | 50 | | selectionHighlight.enabled = isSelected; |
| 7915 | 51 | | } |
| | 52 | |
|
| 2718 | 53 | | public void SetLoadingSpinner(bool isActive) { loadingSpinner?.SetActive(isActive); } |
| | 54 | |
|
| | 55 | | protected new virtual void Awake() |
| | 56 | | { |
| 276 | 57 | | base.Awake(); |
| 276 | 58 | | thumbnail.sprite = null; |
| 276 | 59 | | warningPanel.SetActive(false); |
| | 60 | |
|
| 276 | 61 | | if (!EnvironmentSettings.RUNNING_TESTS) |
| 0 | 62 | | view = GetComponentInParent<AvatarEditorHUDView>(); |
| 276 | 63 | | } |
| | 64 | |
|
| | 65 | | protected override void OnClick() |
| | 66 | | { |
| 0 | 67 | | OnClicked?.Invoke(this); |
| 0 | 68 | | warningPanel.SetActive(false); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public virtual void Initialize(WearableItem w, bool isSelected, int amount) |
| | 72 | | { |
| 3896 | 73 | | wearableItem = w; |
| 3896 | 74 | | selected = isSelected; |
| 3896 | 75 | | amountContainer.gameObject.SetActive(amount > 1); |
| 3896 | 76 | | amountText.text = $"x{amount.ToString()}"; |
| | 77 | |
|
| 3896 | 78 | | if (gameObject.activeInHierarchy) |
| 276 | 79 | | GetThumbnail(); |
| 3896 | 80 | | } |
| | 81 | |
|
| | 82 | | public void OnPointerEnter(PointerEventData eventData) |
| | 83 | | { |
| 0 | 84 | | List<WearableItem> toReplace = getEquippedWearablesReplacedByFunc(wearableItem); |
| 0 | 85 | | if (wearableItem == null || toReplace.Count == 0) |
| 0 | 86 | | return; |
| 0 | 87 | | if (toReplace.Count == 1) |
| | 88 | | { |
| 0 | 89 | | WearableItem w = toReplace[0]; |
| 0 | 90 | | if (w.data.category == wearableItem.data.category) |
| 0 | 91 | | return; |
| | 92 | | } |
| | 93 | |
|
| 0 | 94 | | warningPanel.SetActive(true); |
| 0 | 95 | | } |
| | 96 | |
|
| 0 | 97 | | public void OnPointerExit(PointerEventData eventData) { warningPanel.SetActive(false); } |
| | 98 | |
|
| | 99 | | private void OnThumbnailReady(Asset_Texture texture) |
| | 100 | | { |
| 0 | 101 | | loadingAnimator.SetTrigger(LOADING_ANIMATOR_TRIGGER_LOADED); |
| | 102 | |
|
| 0 | 103 | | if (thumbnail.sprite != null) |
| 0 | 104 | | Destroy(thumbnail.sprite); |
| | 105 | |
|
| 0 | 106 | | thumbnail.sprite = ThumbnailsManager.CreateSpriteFromTexture(texture.texture); |
| | 107 | |
|
| 0 | 108 | | if (view != null) |
| | 109 | | { |
| 0 | 110 | | if (view.avatarEditorCanvas.enabled) |
| 0 | 111 | | AudioScriptableObjects.listItemAppear.Play(true); |
| | 112 | | } |
| 0 | 113 | | } |
| | 114 | |
|
| 552 | 115 | | private void OnEnable() { GetThumbnail(); } |
| | 116 | |
|
| | 117 | | protected virtual void OnDestroy() |
| | 118 | | { |
| 276 | 119 | | ForgetThumbnail(); |
| 276 | 120 | | OnClicked = null; |
| 276 | 121 | | } |
| | 122 | |
|
| 0 | 123 | | protected void CallOnSellClicked() { OnSellClicked?.Invoke(this); } |
| | 124 | |
|
| | 125 | | private void GetThumbnail() |
| | 126 | | { |
| 552 | 127 | | string url = wearableItem?.ComposeThumbnailUrl(); |
| | 128 | |
|
| 552 | 129 | | if (url == loadedThumbnailURL) |
| | 130 | | { |
| 276 | 131 | | loadingAnimator.SetTrigger(LOADING_ANIMATOR_TRIGGER_LOADED); |
| 276 | 132 | | return; |
| | 133 | | } |
| | 134 | |
|
| 276 | 135 | | if (wearableItem == null || string.IsNullOrEmpty(url)) |
| | 136 | | { |
| 0 | 137 | | loadingAnimator.SetTrigger(LOADING_ANIMATOR_TRIGGER_LOADED); |
| 0 | 138 | | return; |
| | 139 | | } |
| | 140 | |
|
| 276 | 141 | | loadedThumbnailURL = url; |
| 276 | 142 | | var newLoadedThumbnailPromise = ThumbnailsManager.GetThumbnail(url, OnThumbnailReady); |
| 276 | 143 | | ThumbnailsManager.ForgetThumbnail(loadedThumbnailPromise); |
| 276 | 144 | | loadedThumbnailPromise = newLoadedThumbnailPromise; |
| 276 | 145 | | } |
| | 146 | |
|
| | 147 | | private void ForgetThumbnail() |
| | 148 | | { |
| 276 | 149 | | ThumbnailsManager.ForgetThumbnail(loadedThumbnailPromise); |
| 276 | 150 | | loadedThumbnailURL = null; |
| 276 | 151 | | loadedThumbnailPromise = null; |
| 276 | 152 | | } |
| | 153 | | } |