< Summary

Class:DCL.EmotesCustomization.EmotesCustomizationComponentView
Assembly:AvatarEditorHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/EmotesCustomization/EmotesCustomizationComponentView.cs
Covered lines:92
Uncovered lines:14
Coverable lines:106
Total lines:247
Line coverage:86.7% (92 of 106)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
Start()0%2100%
RefreshControl()0%2100%
Dispose()0%110100%
CleanEmotes()0%110100%
AddEmote(...)0%110100%
RemoveEmote(...)0%6200%
EquipEmote(...)0%10.2910085.71%
UnequipEmote(...)0%550100%
OpenEmoteInfoPanel(...)0%110100%
SetEmoteInfoPanelActive(...)0%110100%
GetEmoteCardById(...)0%110100%
SetActive(...)0%110100%
GetSlot(...)0%110100%
ClickOnEmote(...)0%220100%
OnEmoteSelected(...)0%110100%
ConfigureEmotesPool()0%220100%
InstantiateAndConfigureEmoteCard(...)0%110100%
OnSlotSelected(...)0%330100%
GetAllEmoteCards()0%220100%
Create()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/EmotesCustomization/EmotesCustomizationComponentView.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using UnityEngine;
 5
 6namespace 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
 029        public bool isActive => gameObject.activeInHierarchy;
 4830        public Transform viewTransform => transform;
 631        public int selectedSlot => emoteSlotSelector.selectedSlot;
 140232        public List<EmoteSlotCardComponentView> currentSlots => emoteSlotSelector.GetAllSlots();
 321833        public EmoteCardComponentView selectedCard { get; private set; }
 34
 35        public override void Awake()
 36        {
 7337            base.Awake();
 38
 7339            emoteSlotSelector.onSlotSelected += OnSlotSelected;
 7340            emoteInfoPanel.closeButton.onClick.AddListener(() => SetEmoteInfoPanelActive(false));
 41
 7342            ConfigureEmotesPool();
 7343        }
 44
 45        public override void Start()
 46        {
 047            base.Start();
 48
 049            emoteSlotSelector.SelectSlot(DEFAULT_SELECTED_SLOT);
 050        }
 51
 52        public override void RefreshControl()
 53        {
 054            emoteSlotSelector.RefreshControl();
 055            emotesGrid.RefreshControl();
 056        }
 57
 58        public override void Dispose()
 59        {
 9860            CleanEmotes();
 61
 9862            emoteSlotSelector.onSlotSelected -= OnSlotSelected;
 9863            emoteInfoPanel.closeButton.onClick.RemoveAllListeners();
 9864            emoteInfoPanel.sellButton.onClick.RemoveAllListeners();
 65
 9866            base.Dispose();
 9867        }
 68
 69        public void CleanEmotes()
 70        {
 9971            emotesGrid.ExtractItems();
 9972            emoteCardsPool.ReleaseAll();
 9973        }
 74
 75        public EmoteCardComponentView AddEmote(EmoteCardComponentModel emote)
 76        {
 93077            EmoteCardComponentView emoteGO = InstantiateAndConfigureEmoteCard(emote);
 93078            emotesGrid.AddItemWithResize(emoteGO);
 79
 93080            return emoteGO;
 81        }
 82
 83        public void RemoveEmote(string emoteId)
 84        {
 085            EmoteCardComponentView emoteToRemove = GetEmoteCardById(emoteId);
 086            if (emoteToRemove != null)
 087                emotesGrid.RemoveItem(emoteToRemove);
 088        }
 89
 90        public void EquipEmote(string emoteId, string emoteName, int slotNumber, bool selectSlotAfterEquip = true, bool 
 91        {
 492            if (string.IsNullOrEmpty(emoteId))
 093                return;
 94
 495            EmoteCardComponentView emoteCardsToUpdate = GetEmoteCardById(emoteId);
 496            if (emoteCardsToUpdate != null && emoteCardsToUpdate.model.assignedSlot == slotNumber)
 097                return;
 98
 499            List<EmoteCardComponentView> currentEmoteCards = GetAllEmoteCards();
 16100            foreach (var existingEmoteCard in currentEmoteCards)
 101            {
 4102                if (existingEmoteCard.model.assignedSlot == slotNumber)
 0103                    existingEmoteCard.UnassignSlot();
 104
 4105                if (existingEmoteCard.model.id == emoteId)
 106                {
 4107                    existingEmoteCard.AssignSlot(slotNumber);
 4108                    emoteSlotSelector.AssignEmoteIntoSlot(
 109                        slotNumber,
 110                        emoteId,
 111                        emoteName,
 112                        existingEmoteCard.model.pictureSprite,
 113                        existingEmoteCard.model.pictureUri,
 114                        existingEmoteCard.model.rarity);
 115
 4116                    if (selectSlotAfterEquip)
 4117                        emoteSlotSelector.SelectSlot(slotNumber);
 118                }
 119
 4120                existingEmoteCard.SetEmoteAsAssignedInSelectedSlot(existingEmoteCard.model.assignedSlot == selectedSlot)
 121            }
 122
 4123            SetEmoteInfoPanelActive(false);
 124
 4125            if (notifyEvent)
 4126                onEmoteEquipped?.Invoke(emoteId, slotNumber);
 4127        }
 128
 129        public void UnequipEmote(string emoteId, int slotNumber, bool notifyEvent = true)
 130        {
 1404131            if (string.IsNullOrEmpty(emoteId))
 1400132                return;
 133
 4134            EmoteCardComponentView emoteCardsToUpdate = GetEmoteCardById(emoteId);
 4135            if (emoteCardsToUpdate != null)
 136            {
 4137                emoteCardsToUpdate.AssignSlot(-1);
 4138                emoteCardsToUpdate.SetEmoteAsAssignedInSelectedSlot(false);
 139            }
 140
 4141            emoteSlotSelector.AssignEmoteIntoSlot(
 142                slotNumber,
 143                string.Empty,
 144                string.Empty,
 145                null,
 146                null,
 147                string.Empty);
 148
 4149            SetEmoteInfoPanelActive(false);
 150
 4151            if (notifyEvent)
 4152                onEmoteUnequipped?.Invoke(emoteId, slotNumber);
 4153        }
 154
 155        public void OpenEmoteInfoPanel(EmoteCardComponentModel emoteModel, Color backgroundColor, Transform anchorTransf
 156        {
 1157            emoteInfoPanel.SetModel(NFTItemInfo.Model.FromEmoteItem(emoteModel));
 1158            emoteInfoPanel.SetBackgroundColor(backgroundColor);
 1159            emoteInfoPanel.SetRarityName(emoteModel.rarity);
 1160            SetEmoteInfoPanelActive(true);
 1161            emoteInfoPanel.transform.SetParent(anchorTransform);
 1162            emoteInfoPanel.transform.localPosition = Vector3.zero;
 1163            emoteInfoPanel.sellButton.onClick.RemoveAllListeners();
 1164            emoteInfoPanel.sellButton.onClick.AddListener(() => onSellEmoteClicked?.Invoke(emoteModel.id));
 1165        }
 166
 38167        public void SetEmoteInfoPanelActive(bool isActive) { emoteInfoPanel.SetActive(isActive); }
 168
 37894169        public EmoteCardComponentView GetEmoteCardById(string emoteId) { return GetAllEmoteCards().FirstOrDefault(x => x
 170
 92171        public void SetActive(bool isActive) { gameObject.SetActive(isActive); }
 172
 9122173        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        {
 2177            if (!isAssignedInSelectedSlot)
 1178                EquipEmote(emoteId, emoteName, slotNumber);
 179            else
 1180                UnequipEmote(emoteId, selectedSlot);
 181
 2182            SetEmoteInfoPanelActive(false);
 2183        }
 184
 6432185        internal void OnEmoteSelected(string emoteId) { selectedCard = GetEmoteCardById(emoteId); }
 186
 187        internal void ConfigureEmotesPool()
 188        {
 73189            emoteCardsPool = PoolManager.i.GetPool(EMOTE_CARDS_POOL_NAME);
 73190            if (emoteCardsPool == null)
 191            {
 49192                emoteCardsPool = PoolManager.i.AddPool(
 193                    EMOTE_CARDS_POOL_NAME,
 194                    GameObject.Instantiate(emoteCardPrefab).gameObject,
 195                    maxPrewarmCount: EMOTE_CARDS_POOL_PREWARM,
 196                    isPersistent: true);
 197
 49198                emoteCardsPool.ForcePrewarm();
 199            }
 73200        }
 201
 202        internal EmoteCardComponentView InstantiateAndConfigureEmoteCard(EmoteCardComponentModel emotesInfo)
 203        {
 931204            EmoteCardComponentView emoteGO = emoteCardsPool.Get().gameObject.GetComponent<EmoteCardComponentView>();
 931205            emoteGO.Configure(emotesInfo);
 931206            emoteGO.onMainClick.RemoveAllListeners();
 931207            emoteGO.onMainClick.AddListener(() => ClickOnEmote(emoteGO.model.id, emoteGO.model.name, selectedSlot, emote
 931208            emoteGO.onInfoClick.RemoveAllListeners();
 931209            emoteGO.onInfoClick.AddListener(() => OpenEmoteInfoPanel(
 210                emoteGO.model,
 211                emoteGO.rarityMark.gameObject.activeSelf ? emoteGO.rarityMark.color : Color.grey,
 212                emoteGO.emoteInfoAnchor));
 931213            emoteGO.onEmoteSelected -= OnEmoteSelected;
 931214            emoteGO.onEmoteSelected += OnEmoteSelected;
 215
 931216            return emoteGO;
 217        }
 218
 219        internal void OnSlotSelected(int slotNumber, string emoteId)
 220        {
 6221            List<EmoteCardComponentView> currentEmoteCards = GetAllEmoteCards();
 24222            foreach (var existingEmoteCard in currentEmoteCards)
 223            {
 6224                existingEmoteCard.SetEmoteAsAssignedInSelectedSlot(existingEmoteCard.model.assignedSlot == slotNumber);
 225            }
 226
 6227            SetEmoteInfoPanelActive(false);
 6228            onSlotSelected?.Invoke(emoteId, slotNumber);
 2229        }
 230
 231        internal List<EmoteCardComponentView> GetAllEmoteCards()
 232        {
 3237233            return emotesGrid
 234                .GetItems()
 34680235                .Select(x => x as EmoteCardComponentView)
 236                .ToList();
 237        }
 238
 239        internal static IEmotesCustomizationComponentView Create()
 240        {
 48241            EmotesCustomizationComponentView emotesCustomizationComponentView = Instantiate(Resources.Load<GameObject>("
 48242            emotesCustomizationComponentView.name = "_EmotesCustomizationSection";
 243
 48244            return emotesCustomizationComponentView;
 245        }
 246    }
 247}