| | 1 | | using System; |
| | 2 | | using TMPro; |
| | 3 | | using UIComponents.Scripts.Components; |
| | 4 | | using UnityEngine; |
| | 5 | | using DG.Tweening; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | namespace DCL.Backpack |
| | 11 | | { |
| | 12 | | public class AvatarSlotComponentView : BaseComponentView<AvatarSlotComponentModel>, IAvatarSlotComponentView |
| | 13 | | { |
| | 14 | | private const float ANIMATION_TIME = 0.2f; |
| | 15 | | private const float SHAKE_ANIMATION_TIME = 0.75f; |
| | 16 | |
|
| | 17 | | [SerializeField] internal NftTypeColorSupportingSO typeColorSupporting; |
| | 18 | | [SerializeField] internal NftTypePreviewCameraFocusConfig previewCameraFocus; |
| | 19 | | [SerializeField] internal NftTypeIconSO typeIcons; |
| | 20 | | [SerializeField] internal RectTransform nftContainer; |
| | 21 | | [SerializeField] internal NftRarityBackgroundSO rarityBackgrounds; |
| | 22 | | [SerializeField] internal Image typeImage; |
| | 23 | | [SerializeField] internal ImageComponentView nftImage; |
| | 24 | | [SerializeField] internal Image backgroundRarityImage; |
| | 25 | | [SerializeField] internal Image focusedImage; |
| | 26 | | [SerializeField] internal Image selectedImage; |
| | 27 | | [SerializeField] private GameObject emptySlot; |
| | 28 | | [SerializeField] internal GameObject hiddenSlot; |
| | 29 | | [SerializeField] internal RectTransform tooltipContainer; |
| | 30 | | [SerializeField] internal TMP_Text tooltipCategoryText; |
| | 31 | | [SerializeField] internal TMP_Text tooltipHiddenText; |
| | 32 | | [SerializeField] internal Button button; |
| | 33 | | [SerializeField] internal Button unequipButton; |
| | 34 | | [SerializeField] internal Button overriddenHide; |
| | 35 | | [SerializeField] internal Button normalHide; |
| | 36 | |
|
| | 37 | | public event Action<AvatarSlotComponentModel, bool> OnSelectAvatarSlot; |
| | 38 | | public event Action<string> OnUnEquip; |
| | 39 | | public event Action<string> OnFocusHiddenBy; |
| | 40 | | public event Action<string, bool> OnHideUnhidePressed; |
| | 41 | |
|
| | 42 | | private bool isSelected = false; |
| | 43 | |
|
| 9 | 44 | | private readonly HashSet<string> hiddenByList = new HashSet<string>(); |
| | 45 | | private Vector2 nftContainerDefaultPosition; |
| | 46 | |
|
| | 47 | | public override void Awake() |
| | 48 | | { |
| 8 | 49 | | base.Awake(); |
| | 50 | |
|
| 8 | 51 | | overriddenHide.onClick.RemoveAllListeners(); |
| 8 | 52 | | overriddenHide.onClick.AddListener(()=> |
| | 53 | | { |
| 0 | 54 | | AudioScriptableObjects.hide.Play(true); |
| 0 | 55 | | OnHideUnhidePressed?.Invoke(model.category, false); |
| 0 | 56 | | SetForceRender(false); |
| 0 | 57 | | }); |
| 8 | 58 | | normalHide.onClick.RemoveAllListeners(); |
| 8 | 59 | | normalHide.onClick.AddListener(()=> |
| | 60 | | { |
| 0 | 61 | | AudioScriptableObjects.show.Play(true); |
| 0 | 62 | | OnHideUnhidePressed?.Invoke(model.category, true); |
| 0 | 63 | | SetForceRender(true); |
| 0 | 64 | | }); |
| 8 | 65 | | button.onClick.RemoveAllListeners(); |
| 8 | 66 | | button.onClick.AddListener(OnSlotClick); |
| 8 | 67 | | unequipButton.onClick.RemoveAllListeners(); |
| 8 | 68 | | unequipButton.onClick.AddListener(()=> |
| | 69 | | { |
| 0 | 70 | | OnUnEquip?.Invoke(model.wearableId); |
| 0 | 71 | | unequipButton.gameObject.SetActive(false); |
| 0 | 72 | | }); |
| 8 | 73 | | ResetSlot(); |
| 8 | 74 | | InitializeTooltipPositions(); |
| 8 | 75 | | } |
| | 76 | |
|
| | 77 | | private void InitializeTooltipPositions() |
| | 78 | | { |
| 8 | 79 | | tooltipContainer.gameObject.SetActive(true); |
| 8 | 80 | | tooltipContainer.gameObject.SetActive(false); |
| 8 | 81 | | nftContainerDefaultPosition = nftContainer.anchoredPosition; |
| 8 | 82 | | } |
| | 83 | |
|
| | 84 | | public void ResetSlot() |
| | 85 | | { |
| 8 | 86 | | SetRarity(null); |
| 8 | 87 | | SetNftImage(""); |
| 8 | 88 | | RefreshControl(); |
| 8 | 89 | | SetWearableId(""); |
| 8 | 90 | | SetHideList(Array.Empty<string>()); |
| 8 | 91 | | SetForceRender(false); |
| 8 | 92 | | } |
| | 93 | |
|
| | 94 | | public string[] GetHideList() => |
| 0 | 95 | | model.hidesList; |
| | 96 | |
|
| | 97 | | public override void RefreshControl() |
| | 98 | | { |
| 13 | 99 | | if (model == null) |
| 0 | 100 | | return; |
| | 101 | |
|
| 13 | 102 | | SetCategory(model.category); |
| 13 | 103 | | SetRarity(model.rarity); |
| 13 | 104 | | SetIsHidden(model.isHidden, model.hiddenBy); |
| 13 | 105 | | SetNftImage(model.imageUri); |
| 13 | 106 | | SetWearableId(model.wearableId); |
| 13 | 107 | | SetHideList(model.hidesList); |
| 13 | 108 | | } |
| | 109 | |
|
| | 110 | | public void SetHideList(string[] hideList) => |
| 21 | 111 | | model.hidesList = hideList; |
| | 112 | |
|
| | 113 | | public void SetForceRender(bool isOverridden) |
| | 114 | | { |
| 8 | 115 | | overriddenHide.gameObject.SetActive(isOverridden); |
| 8 | 116 | | normalHide.gameObject.SetActive(!isOverridden); |
| 8 | 117 | | } |
| | 118 | |
|
| | 119 | | public void SetIsHidden(bool isHidden, string hiddenBy) |
| | 120 | | { |
| 18 | 121 | | model.isHidden = isHidden; |
| | 122 | |
|
| 18 | 123 | | if (isHidden) |
| 4 | 124 | | hiddenByList.Add(hiddenBy); |
| | 125 | | else |
| 14 | 126 | | hiddenByList.Remove(hiddenBy); |
| | 127 | |
|
| 24 | 128 | | List<string> sortedList = hiddenByList.OrderBy(x => WearableItem.CATEGORIES_PRIORITY.IndexOf(x)).ToList(); |
| | 129 | |
|
| 18 | 130 | | if (sortedList.Count > 0) |
| | 131 | | { |
| 5 | 132 | | SetHideIconVisible(true); |
| 5 | 133 | | tooltipHiddenText.gameObject.SetActive(!string.IsNullOrEmpty(model.wearableId)); |
| 5 | 134 | | tooltipHiddenText.text = $"Hidden by {WearableItem.CATEGORIES_READABLE_MAPPING[sortedList[0]]}"; |
| 5 | 135 | | model.hiddenBy = sortedList[0]; |
| | 136 | | } |
| | 137 | | else |
| | 138 | | { |
| 13 | 139 | | SetHideIconVisible(false); |
| 13 | 140 | | tooltipHiddenText.gameObject.SetActive(false); |
| 13 | 141 | | model.hiddenBy = ""; |
| | 142 | | } |
| 13 | 143 | | } |
| | 144 | |
|
| | 145 | | public void SetCategory(string category) |
| | 146 | | { |
| 14 | 147 | | model.category = category; |
| 14 | 148 | | model.allowsColorChange = typeColorSupporting.IsColorSupportedByType(category); |
| 14 | 149 | | model.previewCameraFocus = previewCameraFocus.GetPreviewCameraFocus(category); |
| 14 | 150 | | typeImage.sprite = typeIcons.GetTypeImage(category); |
| 14 | 151 | | WearableItem.CATEGORIES_READABLE_MAPPING.TryGetValue(category, out string readableCategory); |
| 14 | 152 | | tooltipCategoryText.text = readableCategory; |
| 14 | 153 | | } |
| | 154 | |
|
| | 155 | | public void SetUnEquipAllowed(bool allowUnEquip) => |
| 0 | 156 | | model.unEquipAllowed = allowUnEquip; |
| | 157 | |
|
| | 158 | | public void SetNftImage(string imageUri) |
| | 159 | | { |
| 21 | 160 | | model.imageUri = imageUri; |
| 21 | 161 | | if (string.IsNullOrEmpty(imageUri)) |
| | 162 | | { |
| 21 | 163 | | nftImage.SetImage(Texture2D.grayTexture); |
| 21 | 164 | | nftImage.SetLoadingIndicatorVisible(false); |
| 21 | 165 | | emptySlot.SetActive(true); |
| 21 | 166 | | return; |
| | 167 | | } |
| | 168 | |
|
| 0 | 169 | | emptySlot.SetActive(false); |
| 0 | 170 | | nftImage.SetImage(imageUri); |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | public void SetRarity(string rarity) |
| | 174 | | { |
| 22 | 175 | | model.rarity = rarity; |
| 22 | 176 | | backgroundRarityImage.sprite = rarityBackgrounds.GetRarityImage(rarity); |
| 22 | 177 | | } |
| | 178 | |
|
| | 179 | | public void SetWearableId(string wearableId) => |
| 21 | 180 | | model.wearableId = wearableId; |
| | 181 | |
|
| | 182 | | public override void OnFocus() |
| | 183 | | { |
| 1 | 184 | | focusedImage.enabled = true; |
| 1 | 185 | | tooltipContainer.gameObject.SetActive(true); |
| | 186 | |
|
| 1 | 187 | | if (model.unEquipAllowed && !string.IsNullOrEmpty(model.imageUri)) |
| 0 | 188 | | unequipButton.gameObject.SetActive(true); |
| | 189 | |
|
| 1 | 190 | | if(model.isHidden) |
| 0 | 191 | | OnFocusHiddenBy?.Invoke(model.hiddenBy); |
| | 192 | |
|
| 1 | 193 | | ScaleUpAnimation(focusedImage.transform); |
| 1 | 194 | | } |
| | 195 | |
|
| | 196 | | public override void OnLoseFocus() |
| | 197 | | { |
| 9 | 198 | | focusedImage.enabled = false; |
| 9 | 199 | | tooltipContainer.gameObject.SetActive(false); |
| 9 | 200 | | unequipButton.gameObject.SetActive(false); |
| 9 | 201 | | } |
| | 202 | |
|
| | 203 | | public void OnSlotClick() |
| | 204 | | { |
| 0 | 205 | | if (!isSelected) |
| 0 | 206 | | Select(true); |
| | 207 | | else |
| 0 | 208 | | UnSelect(true); |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | public void UnSelect(bool notify) |
| | 212 | | { |
| 0 | 213 | | if (!isSelected) return; |
| 0 | 214 | | isSelected = false; |
| | 215 | |
|
| 0 | 216 | | ScaleDownAndResetAnimation(selectedImage); |
| | 217 | |
|
| 0 | 218 | | if (notify) |
| 0 | 219 | | OnSelectAvatarSlot?.Invoke(model, isSelected); |
| 0 | 220 | | } |
| | 221 | |
|
| | 222 | | public void OnPointerClickOnDifferentSlot() |
| | 223 | | { |
| 0 | 224 | | isSelected = false; |
| 0 | 225 | | selectedImage.enabled = false; |
| 0 | 226 | | } |
| | 227 | |
|
| | 228 | | private void ScaleUpAnimation(Transform targetTransform) |
| | 229 | | { |
| 1 | 230 | | targetTransform.transform.localScale = new Vector3(0, 0, 0); |
| 1 | 231 | | targetTransform.transform.DOScale(1, ANIMATION_TIME).SetEase(Ease.OutBack); |
| 1 | 232 | | } |
| | 233 | |
|
| | 234 | | private void ScaleDownAndResetAnimation(Image targetImage) |
| | 235 | | { |
| 0 | 236 | | targetImage.transform.DOScale(0, ANIMATION_TIME).SetEase(Ease.OutBack).OnComplete(() => |
| | 237 | | { |
| 0 | 238 | | targetImage.enabled = false; |
| 0 | 239 | | targetImage.transform.localScale = new Vector3(1, 1, 1); |
| 0 | 240 | | }); |
| 0 | 241 | | } |
| | 242 | |
|
| | 243 | | public void SetHideIconVisible(bool isVisible) => |
| 18 | 244 | | hiddenSlot.SetActive(isVisible && !string.IsNullOrEmpty(model.wearableId)); |
| | 245 | |
|
| | 246 | | public void Select(bool notify) |
| | 247 | | { |
| 0 | 248 | | if (isSelected) return; |
| | 249 | |
|
| 0 | 250 | | isSelected = true; |
| 0 | 251 | | selectedImage.enabled = true; |
| 0 | 252 | | ScaleUpAnimation(selectedImage.transform); |
| | 253 | |
|
| 0 | 254 | | if (notify) |
| 0 | 255 | | OnSelectAvatarSlot?.Invoke(model, isSelected); |
| 0 | 256 | | } |
| | 257 | |
|
| | 258 | | public void ShakeAnimation() => |
| 0 | 259 | | nftContainer.DOShakePosition(SHAKE_ANIMATION_TIME, 4).OnComplete(() => nftContainer.anchoredPosition = nftCo |
| | 260 | | } |
| | 261 | | } |