| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.EmotesCustomization |
| | 7 | | { |
| | 8 | | public class EmotesCustomizationComponentView : BaseComponentView, IEmotesCustomizationComponentView |
| | 9 | | { |
| | 10 | | internal const string EMOTE_CARDS_POOL_NAME = "EmotesCustomization_EmoteCardsPool"; |
| | 11 | | internal const int EMOTE_CARDS_POOL_PREWARM = 40; |
| | 12 | | internal const int DEFAULT_SELECTED_SLOT = 1; |
| | 13 | |
|
| | 14 | | [Header("Assets References")] |
| | 15 | | [SerializeField] internal EmoteCardComponentView emoteCardPrefab; |
| | 16 | |
|
| | 17 | | [Header("Prefab References")] |
| | 18 | | [SerializeField] internal EmoteSlotSelectorComponentView emoteSlotSelector; |
| | 19 | | [SerializeField] internal GridContainerComponentView emotesGrid; |
| | 20 | | [SerializeField] internal NFTItemInfo emoteInfoPanel; |
| | 21 | |
|
| | 22 | | public event Action<string, int> onEmoteEquipped; |
| | 23 | | public event Action<string, int> onEmoteUnequipped; |
| | 24 | | public event Action<string> onSellEmoteClicked; |
| | 25 | | public event Action<string, int> onSlotSelected; |
| | 26 | |
|
| | 27 | | internal Pool emoteCardsPool; |
| | 28 | |
|
| 0 | 29 | | public bool isActive => gameObject.activeInHierarchy; |
| 48 | 30 | | public Transform viewTransform => transform; |
| 0 | 31 | | public int selectedSlot => emoteSlotSelector.selectedSlot; |
| 1402 | 32 | | public List<EmoteSlotCardComponentView> currentSlots => emoteSlotSelector.GetAllSlots(); |
| 0 | 33 | | public EmoteCardComponentView selectedCard { get; private set; } |
| | 34 | |
|
| | 35 | | public override void Awake() |
| | 36 | | { |
| 73 | 37 | | base.Awake(); |
| | 38 | |
|
| 73 | 39 | | emoteSlotSelector.onSlotSelected += OnSlotSelected; |
| 73 | 40 | | emoteInfoPanel.closeButton.onClick.AddListener(() => SetEmoteInfoPanelActive(false)); |
| | 41 | |
|
| 73 | 42 | | ConfigureEmotesPool(); |
| 73 | 43 | | } |
| | 44 | |
|
| | 45 | | public override void Start() |
| | 46 | | { |
| 0 | 47 | | base.Start(); |
| | 48 | |
|
| 0 | 49 | | emoteSlotSelector.SelectSlot(DEFAULT_SELECTED_SLOT); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | public override void RefreshControl() |
| | 53 | | { |
| 0 | 54 | | emoteSlotSelector.RefreshControl(); |
| 0 | 55 | | emotesGrid.RefreshControl(); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public override void Dispose() |
| | 59 | | { |
| 98 | 60 | | CleanEmotes(); |
| | 61 | |
|
| 98 | 62 | | emoteSlotSelector.onSlotSelected -= OnSlotSelected; |
| 98 | 63 | | emoteInfoPanel.closeButton.onClick.RemoveAllListeners(); |
| 98 | 64 | | emoteInfoPanel.sellButton.onClick.RemoveAllListeners(); |
| | 65 | |
|
| 98 | 66 | | base.Dispose(); |
| 98 | 67 | | } |
| | 68 | |
|
| | 69 | | public void CleanEmotes() |
| | 70 | | { |
| 99 | 71 | | emotesGrid.ExtractItems(); |
| 99 | 72 | | emoteCardsPool.ReleaseAll(); |
| 99 | 73 | | } |
| | 74 | |
|
| | 75 | | public EmoteCardComponentView AddEmote(EmoteCardComponentModel emote) |
| | 76 | | { |
| 930 | 77 | | EmoteCardComponentView emoteGO = InstantiateAndConfigureEmoteCard(emote); |
| 930 | 78 | | emotesGrid.AddItem(emoteGO); |
| | 79 | |
|
| 930 | 80 | | return emoteGO; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | public void RemoveEmote(string emoteId) |
| | 84 | | { |
| 0 | 85 | | EmoteCardComponentView emoteToRemove = GetEmoteCardById(emoteId); |
| 0 | 86 | | if (emoteToRemove != null) |
| 0 | 87 | | emotesGrid.RemoveItem(emoteToRemove); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public void EquipEmote(string emoteId, string emoteName, int slotNumber, bool selectSlotAfterEquip = true, bool |
| | 91 | | { |
| 4 | 92 | | if (string.IsNullOrEmpty(emoteId)) |
| 0 | 93 | | return; |
| | 94 | |
|
| 4 | 95 | | EmoteCardComponentView emoteCardsToUpdate = GetEmoteCardById(emoteId); |
| 4 | 96 | | if (emoteCardsToUpdate != null && emoteCardsToUpdate.model.assignedSlot == slotNumber) |
| 0 | 97 | | return; |
| | 98 | |
|
| 4 | 99 | | List<EmoteCardComponentView> currentEmoteCards = GetAllEmoteCards(); |
| 16 | 100 | | foreach (var existingEmoteCard in currentEmoteCards) |
| | 101 | | { |
| 4 | 102 | | if (existingEmoteCard.model.assignedSlot == slotNumber) |
| 0 | 103 | | existingEmoteCard.UnassignSlot(); |
| | 104 | |
|
| 4 | 105 | | if (existingEmoteCard.model.id == emoteId) |
| | 106 | | { |
| 4 | 107 | | existingEmoteCard.AssignSlot(slotNumber); |
| 4 | 108 | | emoteSlotSelector.AssignEmoteIntoSlot( |
| | 109 | | slotNumber, |
| | 110 | | emoteId, |
| | 111 | | emoteName, |
| | 112 | | existingEmoteCard.model.pictureSprite, |
| | 113 | | existingEmoteCard.model.pictureUri, |
| | 114 | | existingEmoteCard.model.rarity); |
| | 115 | |
|
| 4 | 116 | | if (selectSlotAfterEquip) |
| 4 | 117 | | emoteSlotSelector.SelectSlot(slotNumber); |
| | 118 | | } |
| | 119 | |
|
| 4 | 120 | | existingEmoteCard.SetEmoteAsAssignedInSelectedSlot(existingEmoteCard.model.assignedSlot == selectedSlot) |
| | 121 | | } |
| | 122 | |
|
| 4 | 123 | | SetEmoteInfoPanelActive(false); |
| | 124 | |
|
| 4 | 125 | | if (notifyEvent) |
| 4 | 126 | | onEmoteEquipped?.Invoke(emoteId, slotNumber); |
| 4 | 127 | | } |
| | 128 | |
|
| | 129 | | public void UnequipEmote(string emoteId, int slotNumber, bool notifyEvent = true) |
| | 130 | | { |
| 1404 | 131 | | if (string.IsNullOrEmpty(emoteId)) |
| 1400 | 132 | | return; |
| | 133 | |
|
| 4 | 134 | | EmoteCardComponentView emoteCardsToUpdate = GetEmoteCardById(emoteId); |
| 4 | 135 | | if (emoteCardsToUpdate != null) |
| | 136 | | { |
| 4 | 137 | | emoteCardsToUpdate.AssignSlot(-1); |
| 4 | 138 | | emoteCardsToUpdate.SetEmoteAsAssignedInSelectedSlot(false); |
| | 139 | | } |
| | 140 | |
|
| 4 | 141 | | emoteSlotSelector.AssignEmoteIntoSlot( |
| | 142 | | slotNumber, |
| | 143 | | string.Empty, |
| | 144 | | string.Empty, |
| | 145 | | null, |
| | 146 | | null, |
| | 147 | | string.Empty); |
| | 148 | |
|
| 4 | 149 | | SetEmoteInfoPanelActive(false); |
| | 150 | |
|
| 4 | 151 | | if (notifyEvent) |
| 4 | 152 | | onEmoteUnequipped?.Invoke(emoteId, slotNumber); |
| 4 | 153 | | } |
| | 154 | |
|
| | 155 | | public void OpenEmoteInfoPanel(EmoteCardComponentModel emoteModel, Color backgroundColor, Transform anchorTransf |
| | 156 | | { |
| 1 | 157 | | emoteInfoPanel.SetModel(NFTItemInfo.Model.FromEmoteItem(emoteModel)); |
| 1 | 158 | | emoteInfoPanel.SetBackgroundColor(backgroundColor); |
| 1 | 159 | | emoteInfoPanel.SetRarityName(emoteModel.rarity); |
| 1 | 160 | | SetEmoteInfoPanelActive(true); |
| 1 | 161 | | emoteInfoPanel.transform.SetParent(anchorTransform); |
| 1 | 162 | | emoteInfoPanel.transform.localPosition = Vector3.zero; |
| 1 | 163 | | emoteInfoPanel.sellButton.onClick.RemoveAllListeners(); |
| 1 | 164 | | emoteInfoPanel.sellButton.onClick.AddListener(() => onSellEmoteClicked?.Invoke(emoteModel.id)); |
| 1 | 165 | | } |
| | 166 | |
|
| 38 | 167 | | public void SetEmoteInfoPanelActive(bool isActive) { emoteInfoPanel.SetActive(isActive); } |
| | 168 | |
|
| 37894 | 169 | | public EmoteCardComponentView GetEmoteCardById(string emoteId) { return GetAllEmoteCards().FirstOrDefault(x => x |
| | 170 | |
|
| 92 | 171 | | public void SetActive(bool isActive) { gameObject.SetActive(isActive); } |
| | 172 | |
|
| 9122 | 173 | | public EmoteSlotCardComponentView GetSlot(int slotNumber) { return currentSlots.FirstOrDefault(x => x.model.slot |
| | 174 | |
|
| | 175 | | internal void ClickOnEmote(string emoteId, string emoteName, int slotNumber, bool isAssignedInSelectedSlot) |
| | 176 | | { |
| 2 | 177 | | if (!isAssignedInSelectedSlot) |
| 1 | 178 | | EquipEmote(emoteId, emoteName, slotNumber); |
| | 179 | | else |
| 1 | 180 | | UnequipEmote(emoteId, selectedSlot); |
| | 181 | |
|
| 2 | 182 | | SetEmoteInfoPanelActive(false); |
| 2 | 183 | | } |
| | 184 | |
|
| 6432 | 185 | | internal void OnEmoteSelected(string emoteId) { selectedCard = GetEmoteCardById(emoteId); } |
| | 186 | |
|
| | 187 | | internal void ConfigureEmotesPool() |
| | 188 | | { |
| 73 | 189 | | emoteCardsPool = PoolManager.i.GetPool(EMOTE_CARDS_POOL_NAME); |
| 73 | 190 | | if (emoteCardsPool == null) |
| | 191 | | { |
| 49 | 192 | | emoteCardsPool = PoolManager.i.AddPool( |
| | 193 | | EMOTE_CARDS_POOL_NAME, |
| | 194 | | GameObject.Instantiate(emoteCardPrefab).gameObject, |
| | 195 | | maxPrewarmCount: EMOTE_CARDS_POOL_PREWARM, |
| | 196 | | isPersistent: true); |
| | 197 | |
|
| 49 | 198 | | emoteCardsPool.ForcePrewarm(); |
| | 199 | | } |
| 73 | 200 | | } |
| | 201 | |
|
| | 202 | | internal EmoteCardComponentView InstantiateAndConfigureEmoteCard(EmoteCardComponentModel emotesInfo) |
| | 203 | | { |
| 931 | 204 | | EmoteCardComponentView emoteGO = emoteCardsPool.Get().gameObject.GetComponent<EmoteCardComponentView>(); |
| 931 | 205 | | emoteGO.Configure(emotesInfo); |
| 931 | 206 | | emoteGO.onMainClick.RemoveAllListeners(); |
| 931 | 207 | | emoteGO.onMainClick.AddListener(() => ClickOnEmote(emoteGO.model.id, emoteGO.model.name, selectedSlot, emote |
| 931 | 208 | | emoteGO.onInfoClick.RemoveAllListeners(); |
| 931 | 209 | | emoteGO.onInfoClick.AddListener(() => OpenEmoteInfoPanel( |
| | 210 | | emoteGO.model, |
| | 211 | | emoteGO.rarityMark.gameObject.activeSelf ? emoteGO.rarityMark.color : Color.grey, |
| | 212 | | emoteGO.emoteInfoAnchor)); |
| 931 | 213 | | emoteGO.onEmoteSelected -= OnEmoteSelected; |
| 931 | 214 | | emoteGO.onEmoteSelected += OnEmoteSelected; |
| | 215 | |
|
| 931 | 216 | | return emoteGO; |
| | 217 | | } |
| | 218 | |
|
| | 219 | | internal void OnSlotSelected(int slotNumber, string emoteId) |
| | 220 | | { |
| 6 | 221 | | List<EmoteCardComponentView> currentEmoteCards = GetAllEmoteCards(); |
| 24 | 222 | | foreach (var existingEmoteCard in currentEmoteCards) |
| | 223 | | { |
| 6 | 224 | | existingEmoteCard.SetEmoteAsAssignedInSelectedSlot(existingEmoteCard.model.assignedSlot == slotNumber); |
| | 225 | | } |
| | 226 | |
|
| 6 | 227 | | SetEmoteInfoPanelActive(false); |
| 6 | 228 | | onSlotSelected?.Invoke(emoteId, slotNumber); |
| 2 | 229 | | } |
| | 230 | |
|
| | 231 | | internal List<EmoteCardComponentView> GetAllEmoteCards() |
| | 232 | | { |
| 3237 | 233 | | return emotesGrid |
| | 234 | | .GetItems() |
| 34680 | 235 | | .Select(x => x as EmoteCardComponentView) |
| | 236 | | .ToList(); |
| | 237 | | } |
| | 238 | |
|
| | 239 | | internal static IEmotesCustomizationComponentView Create() |
| | 240 | | { |
| 48 | 241 | | EmotesCustomizationComponentView emotesCustomizationComponentView = Instantiate(Resources.Load<GameObject>(" |
| 48 | 242 | | emotesCustomizationComponentView.name = "_EmotesCustomizationSection"; |
| | 243 | |
|
| 48 | 244 | | return emotesCustomizationComponentView; |
| | 245 | | } |
| | 246 | | } |
| | 247 | | } |