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