| | 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 hideWarningPanel; |
| | 23 | | [SerializeField] private GameObject loadingSpinner; |
| | 24 | | [SerializeField] internal RectTransform amountContainer; |
| | 25 | | [SerializeField] internal Animator loadingAnimator; |
| | 26 | | [SerializeField] internal TextMeshProUGUI amountText; |
| | 27 | |
|
| | 28 | | private bool selectedValue; |
| | 29 | |
|
| | 30 | | private string loadedThumbnailURL; |
| | 31 | | private AssetPromise_Texture loadedThumbnailPromise; |
| | 32 | |
|
| | 33 | | private AvatarEditorHUDView view; |
| | 34 | |
|
| | 35 | | private Func<WearableItem, bool> getEquippedWearablesReplacedByFunc; |
| | 36 | | private Func<WearableItem, bool> getEquippedWearablesHiddenBy; |
| | 37 | |
|
| | 38 | | public bool selected |
| | 39 | | { |
| 0 | 40 | | get { return selectedValue; } |
| | 41 | | set |
| | 42 | | { |
| 8259 | 43 | | selectedValue = value; |
| 8259 | 44 | | SetSelection(selectedValue); |
| 8259 | 45 | | } |
| | 46 | | } |
| | 47 | |
|
| | 48 | | protected virtual void SetSelection(bool isSelected) |
| | 49 | | { |
| 8259 | 50 | | if (selectionHighlight != null) |
| 8259 | 51 | | selectionHighlight.enabled = isSelected; |
| 8259 | 52 | | } |
| | 53 | |
|
| 3718 | 54 | | public void SetLoadingSpinner(bool isActive) { loadingSpinner?.SetActive(isActive); } |
| | 55 | |
|
| | 56 | | protected new virtual void Awake() |
| | 57 | | { |
| 198 | 58 | | base.Awake(); |
| 198 | 59 | | thumbnail.sprite = null; |
| 198 | 60 | | warningPanel.SetActive(false); |
| 198 | 61 | | hideWarningPanel.SetActive(false); |
| | 62 | |
|
| 198 | 63 | | if (!EnvironmentSettings.RUNNING_TESTS) |
| 0 | 64 | | view = GetComponentInParent<AvatarEditorHUDView>(); |
| 198 | 65 | | } |
| | 66 | |
|
| | 67 | | protected override void OnClick() |
| | 68 | | { |
| 0 | 69 | | OnClicked?.Invoke(this); |
| 0 | 70 | | warningPanel.SetActive(false); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public virtual void Initialize(WearableItem w, bool isSelected, int amount) |
| | 74 | | { |
| 4068 | 75 | | wearableItem = w; |
| 4068 | 76 | | selected = isSelected; |
| 4068 | 77 | | amountContainer.gameObject.SetActive(amount > 1); |
| 4068 | 78 | | amountText.text = $"x{amount.ToString()}"; |
| | 79 | |
|
| 4068 | 80 | | if (gameObject.activeInHierarchy) |
| 108 | 81 | | GetThumbnail(); |
| 4068 | 82 | | } |
| | 83 | |
|
| | 84 | | public void OnPointerEnter(PointerEventData eventData) |
| | 85 | | { |
| 0 | 86 | | if (!ShowReplacementWarningPanel()) |
| 0 | 87 | | ShowHidingWarningPanel(); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | private bool ShowReplacementWarningPanel() |
| | 91 | | { |
| 0 | 92 | | if (getEquippedWearablesReplacedByFunc == null) return false; |
| 0 | 93 | | var shouldShow = getEquippedWearablesReplacedByFunc(wearableItem); |
| 0 | 94 | | warningPanel.SetActive(shouldShow); |
| 0 | 95 | | return shouldShow; |
| | 96 | | } |
| | 97 | |
|
| | 98 | | private bool ShowHidingWarningPanel() |
| | 99 | | { |
| 0 | 100 | | if (getEquippedWearablesHiddenBy == null) return false; |
| 0 | 101 | | var shouldShow = getEquippedWearablesHiddenBy(wearableItem); |
| 0 | 102 | | hideWarningPanel.SetActive(shouldShow); |
| 0 | 103 | | return shouldShow; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public void OnPointerExit(PointerEventData eventData) |
| | 107 | | { |
| 0 | 108 | | warningPanel.SetActive(false); |
| 0 | 109 | | hideWarningPanel.SetActive(false); |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | public void SetHideOtherWerablesToastStrategy(Func<WearableItem, bool> function) => |
| 0 | 113 | | getEquippedWearablesHiddenBy = function; |
| | 114 | |
|
| | 115 | | public void SetReplaceOtherWearablesToastStrategy(Func<WearableItem, bool> function) => |
| 0 | 116 | | getEquippedWearablesReplacedByFunc = function; |
| | 117 | |
|
| | 118 | | private void OnThumbnailReady(Asset_Texture texture) |
| | 119 | | { |
| 0 | 120 | | SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED); |
| | 121 | |
|
| 0 | 122 | | if (thumbnail.sprite != null) |
| 0 | 123 | | Destroy(thumbnail.sprite); |
| | 124 | |
|
| 0 | 125 | | thumbnail.sprite = ThumbnailsManager.CreateSpriteFromTexture(texture.texture); |
| | 126 | |
|
| 0 | 127 | | if (view != null) |
| | 128 | | { |
| 0 | 129 | | if (view.avatarEditorCanvas.enabled) |
| 0 | 130 | | AudioScriptableObjects.listItemAppear.Play(true); |
| | 131 | | } |
| 0 | 132 | | } |
| | 133 | |
|
| 396 | 134 | | private void OnEnable() { GetThumbnail(); } |
| | 135 | |
|
| | 136 | | protected virtual void OnDestroy() |
| | 137 | | { |
| 198 | 138 | | ForgetThumbnail(); |
| 198 | 139 | | OnClicked = null; |
| 198 | 140 | | } |
| | 141 | |
|
| 0 | 142 | | protected void CallOnSellClicked() { OnSellClicked?.Invoke(this); } |
| | 143 | |
|
| | 144 | | private void GetThumbnail() |
| | 145 | | { |
| 306 | 146 | | string url = wearableItem?.ComposeThumbnailUrl(); |
| | 147 | |
|
| 306 | 148 | | if (url == loadedThumbnailURL) |
| | 149 | | { |
| 108 | 150 | | SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED); |
| 108 | 151 | | return; |
| | 152 | | } |
| | 153 | |
|
| 198 | 154 | | if (wearableItem == null || string.IsNullOrEmpty(url)) |
| | 155 | | { |
| 8 | 156 | | SetLoadingAnimationTrigger(LOADING_ANIMATOR_TRIGGER_LOADED); |
| 8 | 157 | | return; |
| | 158 | | } |
| | 159 | |
|
| 190 | 160 | | loadedThumbnailURL = url; |
| 190 | 161 | | var newLoadedThumbnailPromise = ThumbnailsManager.GetThumbnail(url, OnThumbnailReady); |
| 190 | 162 | | ThumbnailsManager.ForgetThumbnail(loadedThumbnailPromise); |
| 190 | 163 | | loadedThumbnailPromise = newLoadedThumbnailPromise; |
| 190 | 164 | | } |
| | 165 | |
|
| | 166 | | private void SetLoadingAnimationTrigger(int id) |
| | 167 | | { |
| 116 | 168 | | if (!loadingAnimator.isInitialized || loadingAnimator.runtimeAnimatorController == null) |
| 0 | 169 | | return; |
| | 170 | |
|
| 116 | 171 | | loadingAnimator.SetTrigger(id); |
| 116 | 172 | | } |
| | 173 | |
|
| | 174 | | private void ForgetThumbnail() |
| | 175 | | { |
| 198 | 176 | | ThumbnailsManager.ForgetThumbnail(loadedThumbnailPromise); |
| 198 | 177 | | loadedThumbnailURL = null; |
| 198 | 178 | | loadedThumbnailPromise = null; |
| 198 | 179 | | } |
| | 180 | | } |