< Summary

Class:DCL.EmotesCustomization.EmotesCustomizationComponentController
Assembly:AvatarEditorHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/EmotesCustomization/EmotesCustomizationComponentController.cs
Covered lines:136
Uncovered lines:45
Coverable lines:181
Total lines:391
Line coverage:75.1% (136 of 181)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EmotesCustomizationComponentController()0%110100%
Initialize(...)0%110100%
SetEmotes(...)0%4.034088.24%
SetEquippedBodyShape(...)0%3.023087.5%
RestoreEmoteSlots()0%110100%
Dispose()0%110100%
OnEquippedEmotesSet(...)0%110100%
ConfigureView()0%110100%
IsStarMenuOpenChanged(...)0%110100%
OnAvatarEditorVisibleChanged(...)0%110100%
AddEmote(...)0%5.015092.86%
RemoveEmote(...)0%2100%
OnAnimationAdded(...)0%110100%
RefreshEmoteLoadingState(...)0%330100%
ParseWearableItemIntoEmoteCardModel(...)0%110100%
UpdateEmoteSlots()0%770100%
StoreEquippedEmotes()0%6.744044.44%
OnEmoteEquipped(...)0%330100%
OnEmoteUnequipped(...)0%220100%
OnSellEmoteClicked(...)0%220100%
OnSlotSelected(...)0%220100%
ConfigureShortcuts()0%110100%
OnEquipInputActionTriggered(...)0%30500%
OnShowInfoInputActionTriggered(...)0%42600%
OnNumericShortcutInputActionTriggered(...)0%2101400%
CreateView()0%110100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using DCL.Emotes;
 5using UnityEngine;
 6
 7namespace DCL.EmotesCustomization
 8{
 9    public class EmotesCustomizationComponentController : IEmotesCustomizationComponentController
 10    {
 11        internal const int NUMBER_OF_SLOTS = 10;
 12
 013        internal bool isEmotesCustomizationSectionOpen => exploreV2DataStore.isOpen.Get() && view.isActive;
 14
 15        internal IEmotesCustomizationComponentView view;
 16        internal InputAction_Hold equipInputAction;
 17        internal InputAction_Hold showInfoInputAction;
 18        internal InputAction_Trigger shortcut0InputAction;
 19        internal InputAction_Trigger shortcut1InputAction;
 20        internal InputAction_Trigger shortcut2InputAction;
 21        internal InputAction_Trigger shortcut3InputAction;
 22        internal InputAction_Trigger shortcut4InputAction;
 23        internal InputAction_Trigger shortcut5InputAction;
 24        internal InputAction_Trigger shortcut6InputAction;
 25        internal InputAction_Trigger shortcut7InputAction;
 26        internal InputAction_Trigger shortcut8InputAction;
 27        internal InputAction_Trigger shortcut9InputAction;
 6228        internal BaseDictionary<string, EmoteCardComponentView> emotesInLoadingState = new BaseDictionary<string, EmoteC
 29
 30        internal DataStore_EmotesCustomization emotesCustomizationDataStore;
 31        internal DataStore_Emotes emotesDataStore;
 32        internal DataStore_ExploreV2 exploreV2DataStore;
 33        internal DataStore_HUDs hudsDataStore;
 6234        internal Dictionary<string, WearableItem> ownedEmotes = new Dictionary<string, WearableItem>();
 35        internal string bodyShapeId;
 36
 37        public event Action<string> onEmotePreviewed;
 38        public event Action<string> onEmoteEquipped;
 39        public event Action<string> onEmoteUnequipped;
 40        public event Action<string> onEmoteSell;
 41
 42        public IEmotesCustomizationComponentView Initialize(
 43            DataStore_EmotesCustomization emotesCustomizationDataStore,
 44            DataStore_Emotes emotesDataStore,
 45            DataStore_ExploreV2 exploreV2DataStore,
 46            DataStore_HUDs hudsDataStore)
 47        {
 6248            this.emotesCustomizationDataStore = emotesCustomizationDataStore;
 6249            this.emotesDataStore = emotesDataStore;
 6250            this.exploreV2DataStore = exploreV2DataStore;
 6251            this.hudsDataStore = hudsDataStore;
 52
 6253            IEmotesCustomizationComponentView view = ConfigureView();
 6254            ConfigureShortcuts();
 55
 6256            emotesCustomizationDataStore.equippedEmotes.OnSet += OnEquippedEmotesSet;
 6257            OnEquippedEmotesSet(emotesCustomizationDataStore.equippedEmotes.Get());
 58
 6259            emotesDataStore.animations.OnAdded -= OnAnimationAdded;
 6260            emotesDataStore.animations.OnAdded += OnAnimationAdded;
 61
 6262            return view;
 63        }
 64
 65        public void SetEmotes(WearableItem[] ownedEmotes)
 66        {
 67            //Find loaded emotes that are not contained in emotesToSet
 4968            List<string> idsToRemove = emotesCustomizationDataStore.currentLoadedEmotes.Get().Where(x => ownedEmotes.All
 69
 9870            foreach (string emoteId in idsToRemove)
 71            {
 072                RemoveEmote(emoteId);
 73            }
 74
 4975            this.ownedEmotes.Clear();
 192676            for (int i = 0; i < ownedEmotes.Length; i++)
 77            {
 91478                WearableItem emote = ownedEmotes[i];
 79                //emotes owned multiple times will be received as separated entities, overriding by emote.id removes tha
 80                //if we want to show the amount of ocurrences we can modify this
 91481                this.ownedEmotes[emote.id] = emote;
 82            }
 192683            foreach (WearableItem emote in this.ownedEmotes.Values)
 84            {
 91485                AddEmote(emote);
 86            }
 4987            UpdateEmoteSlots();
 4988        }
 89
 90        public void SetEquippedBodyShape(string bodyShapeId)
 91        {
 5492            if (bodyShapeId == this.bodyShapeId)
 093                return;
 94
 5495            this.bodyShapeId = bodyShapeId;
 212296            foreach (string emoteId in this.ownedEmotes.Keys)
 97            {
 100798                RefreshEmoteLoadingState(emoteId);
 99            }
 54100        }
 101
 102        public void RestoreEmoteSlots()
 103        {
 1104            emotesCustomizationDataStore.unsavedEquippedEmotes.Set(emotesCustomizationDataStore.equippedEmotes.Get());
 1105            UpdateEmoteSlots();
 1106        }
 107
 108        public void Dispose()
 109        {
 14110            view.onEmoteEquipped -= OnEmoteEquipped;
 14111            view.onEmoteUnequipped -= OnEmoteUnequipped;
 14112            view.onSellEmoteClicked -= OnSellEmoteClicked;
 14113            view.onSlotSelected -= OnSlotSelected;
 14114            exploreV2DataStore.isOpen.OnChange -= IsStarMenuOpenChanged;
 14115            hudsDataStore.avatarEditorVisible.OnChange -= OnAvatarEditorVisibleChanged;
 14116            emotesCustomizationDataStore.equippedEmotes.OnSet -= OnEquippedEmotesSet;
 14117            emotesDataStore.animations.OnAdded -= OnAnimationAdded;
 14118            equipInputAction.OnFinished -= OnEquipInputActionTriggered;
 14119            showInfoInputAction.OnFinished -= OnShowInfoInputActionTriggered;
 14120            shortcut0InputAction.OnTriggered -= OnNumericShortcutInputActionTriggered;
 14121            shortcut1InputAction.OnTriggered -= OnNumericShortcutInputActionTriggered;
 14122            shortcut2InputAction.OnTriggered -= OnNumericShortcutInputActionTriggered;
 14123            shortcut3InputAction.OnTriggered -= OnNumericShortcutInputActionTriggered;
 14124            shortcut4InputAction.OnTriggered -= OnNumericShortcutInputActionTriggered;
 14125            shortcut5InputAction.OnTriggered -= OnNumericShortcutInputActionTriggered;
 14126            shortcut6InputAction.OnTriggered -= OnNumericShortcutInputActionTriggered;
 14127            shortcut7InputAction.OnTriggered -= OnNumericShortcutInputActionTriggered;
 14128            shortcut8InputAction.OnTriggered -= OnNumericShortcutInputActionTriggered;
 14129            shortcut9InputAction.OnTriggered -= OnNumericShortcutInputActionTriggered;
 14130        }
 131
 132        internal void OnEquippedEmotesSet(IEnumerable<EquippedEmoteData> equippedEmotes)
 133        {
 65134            emotesCustomizationDataStore.unsavedEquippedEmotes.Set(equippedEmotes);
 65135            UpdateEmoteSlots();
 65136        }
 137
 138        internal IEmotesCustomizationComponentView ConfigureView()
 139        {
 62140            view = CreateView();
 62141            view.onEmoteEquipped += OnEmoteEquipped;
 62142            view.onEmoteUnequipped += OnEmoteUnequipped;
 62143            view.onSellEmoteClicked += OnSellEmoteClicked;
 62144            view.onSlotSelected += OnSlotSelected;
 62145            exploreV2DataStore.isOpen.OnChange += IsStarMenuOpenChanged;
 62146            hudsDataStore.avatarEditorVisible.OnChange += OnAvatarEditorVisibleChanged;
 147
 62148            return view;
 149        }
 150
 2151        internal void IsStarMenuOpenChanged(bool currentIsOpen, bool previousIsOpen) { view.SetEmoteInfoPanelActive(fals
 152
 92153        internal void OnAvatarEditorVisibleChanged(bool current, bool previous) { view.SetActive(current); }
 154
 155        internal void AddEmote(WearableItem emote)
 156        {
 914157            var emoteId = emote.id;
 914158            if (!emote.IsEmote() || emotesCustomizationDataStore.currentLoadedEmotes.Contains(emoteId))
 0159                return;
 160
 914161            emotesCustomizationDataStore.currentLoadedEmotes.Add(emoteId);
 914162            emotesDataStore.emotesOnUse.IncreaseRefCount((WearableLiterals.BodyShapes.FEMALE, emoteId));
 914163            emotesDataStore.emotesOnUse.IncreaseRefCount((WearableLiterals.BodyShapes.MALE, emoteId));
 914164            EmoteCardComponentModel emoteToAdd = ParseWearableItemIntoEmoteCardModel(emote);
 914165            EmoteCardComponentView newEmote = view.AddEmote(emoteToAdd);
 166
 914167            if (newEmote != null)
 912168                newEmote.SetAsLoading(true);
 169
 914170            if (!emotesInLoadingState.ContainsKey(emoteId))
 914171                emotesInLoadingState.Add(emoteId, newEmote);
 172
 914173            RefreshEmoteLoadingState(emoteId);
 914174        }
 175
 176        internal void RemoveEmote(string emoteId)
 177        {
 0178            emotesDataStore.emotesOnUse.DecreaseRefCount((WearableLiterals.BodyShapes.FEMALE, emoteId));
 0179            emotesDataStore.emotesOnUse.DecreaseRefCount((WearableLiterals.BodyShapes.MALE, emoteId));
 0180            emotesCustomizationDataStore.currentLoadedEmotes.Remove(emoteId);
 0181            view.RemoveEmote(emoteId);
 0182            UpdateEmoteSlots();
 0183        }
 184
 185
 2186        internal void OnAnimationAdded((string bodyshapeId, string emoteId) values, AnimationClip animationClip) { Refre
 187
 188        internal void RefreshEmoteLoadingState(string emoteId)
 189        {
 1923190            if (emotesDataStore.animations.ContainsKey((bodyShapeId, emoteId)))
 191            {
 2192                emotesInLoadingState.TryGetValue(emoteId, out EmoteCardComponentView emote);
 2193                if (emote != null)
 194                {
 1195                    emote.SetAsLoading(false);
 1196                    emotesInLoadingState.Remove(emoteId);
 197                }
 198            }
 1923199        }
 200
 201        internal EmoteCardComponentModel ParseWearableItemIntoEmoteCardModel(WearableItem wearable)
 202        {
 915203            return new EmoteCardComponentModel
 204            {
 205                id = wearable.id,
 206                name = wearable.GetName(),
 207                description = wearable.description,
 208                pictureUri = wearable.ComposeThumbnailUrl(),
 209                pictureSprite = wearable.thumbnailSprite,
 210                isAssignedInSelectedSlot = false,
 211                isSelected = false,
 212                assignedSlot = -1,
 213                rarity = wearable.rarity,
 214                isInL2 = wearable.IsInL2(),
 215                isLoading = false,
 216                isCollectible = wearable.IsCollectible()
 217            };
 218        }
 219
 220        internal void UpdateEmoteSlots()
 221        {
 2488222            for (int i = 0; i < emotesCustomizationDataStore.unsavedEquippedEmotes.Count(); i++)
 223            {
 1128224                if (i > NUMBER_OF_SLOTS)
 225                    break;
 226
 1128227                if (emotesCustomizationDataStore.unsavedEquippedEmotes[i] == null) //empty slot
 228                {
 1120229                    EmoteSlotCardComponentView existingEmoteIntoSlot = view.GetSlot(i);
 1120230                    if (existingEmoteIntoSlot != null)
 970231                        view.UnequipEmote(existingEmoteIntoSlot.model.emoteId, i, false);
 232
 970233                    continue;
 234                }
 235
 8236                ownedEmotes.TryGetValue(emotesCustomizationDataStore.unsavedEquippedEmotes[i].id, out WearableItem emote
 8237                if (emoteItem != null && emotesCustomizationDataStore.currentLoadedEmotes.Contains(emoteItem.id))
 2238                    view.EquipEmote(emoteItem.id, emoteItem.GetName(), i, false, false);
 239            }
 116240        }
 241
 242        internal void StoreEquippedEmotes()
 243        {
 2244            List<EquippedEmoteData> newEquippedEmotesList = new List<EquippedEmoteData> { null, null, null, null, null, 
 245
 2246            if (view.currentSlots != null)
 247            {
 0248                foreach (EmoteSlotCardComponentView slot in view.currentSlots)
 249                {
 0250                    if (!string.IsNullOrEmpty(slot.model.emoteId))
 0251                        newEquippedEmotesList[slot.model.slotNumber] = new EquippedEmoteData
 252                        {
 253                            id = slot.model.emoteId,
 254                            cachedThumbnail = slot.model.pictureSprite
 255                        };
 256                }
 257            }
 258
 2259            emotesCustomizationDataStore.unsavedEquippedEmotes.Set(newEquippedEmotesList);
 2260        }
 261
 262        internal void OnEmoteEquipped(string emoteId, int slotNumber)
 263        {
 1264            StoreEquippedEmotes();
 1265            onEmotePreviewed?.Invoke(emoteId);
 1266            onEmoteEquipped?.Invoke(emoteId);
 1267        }
 268
 269        internal void OnEmoteUnequipped(string emoteId, int slotNumber)
 270        {
 1271            StoreEquippedEmotes();
 1272            onEmoteUnequipped?.Invoke(emoteId);
 1273        }
 274
 2275        internal void OnSellEmoteClicked(string emoteId) { onEmoteSell?.Invoke(emoteId); }
 276
 2277        internal void OnSlotSelected(string emoteId, int slotNumber) { onEmotePreviewed?.Invoke(emoteId); }
 278
 279        internal void ConfigureShortcuts()
 280        {
 62281            equipInputAction = Resources.Load<InputAction_Hold>("DefaultConfirmAction");
 62282            equipInputAction.OnFinished += OnEquipInputActionTriggered;
 283
 62284            showInfoInputAction = Resources.Load<InputAction_Hold>("ZoomIn");
 62285            showInfoInputAction.OnFinished += OnShowInfoInputActionTriggered;
 286
 62287            shortcut0InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut0");
 62288            shortcut0InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 289
 62290            shortcut1InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut1");
 62291            shortcut1InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 292
 62293            shortcut2InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut2");
 62294            shortcut2InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 295
 62296            shortcut3InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut3");
 62297            shortcut3InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 298
 62299            shortcut4InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut4");
 62300            shortcut4InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 301
 62302            shortcut5InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut5");
 62303            shortcut5InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 304
 62305            shortcut6InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut6");
 62306            shortcut6InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 307
 62308            shortcut7InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut7");
 62309            shortcut7InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 310
 62311            shortcut8InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut8");
 62312            shortcut8InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 313
 62314            shortcut9InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut9");
 62315            shortcut9InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 62316        }
 317
 318        internal void OnEquipInputActionTriggered(DCLAction_Hold action)
 319        {
 0320            if (!isEmotesCustomizationSectionOpen || view.selectedCard == null || view.selectedCard.model.isLoading)
 0321                return;
 322
 0323            if (!view.selectedCard.model.isAssignedInSelectedSlot)
 324            {
 0325                view.EquipEmote(
 326                    view.selectedCard.model.id,
 327                    view.selectedCard.model.name,
 328                    view.selectedSlot);
 329            }
 330            else
 331            {
 0332                view.UnequipEmote(
 333                    view.selectedCard.model.id,
 334                    view.selectedSlot);
 335            }
 0336        }
 337
 338        internal void OnShowInfoInputActionTriggered(DCLAction_Hold action)
 339        {
 0340            if (!isEmotesCustomizationSectionOpen || view.selectedCard == null || !view.selectedCard.model.isCollectible
 0341                return;
 342
 0343            view.OpenEmoteInfoPanel(
 344                view.selectedCard.model,
 345                view.selectedCard.rarityMark.gameObject.activeSelf ? view.selectedCard.rarityMark.color : Color.grey,
 346                view.selectedCard.emoteInfoAnchor);
 0347        }
 348
 349        internal void OnNumericShortcutInputActionTriggered(DCLAction_Trigger action)
 350        {
 0351            if (!isEmotesCustomizationSectionOpen || view.selectedCard == null || view.selectedCard.model.isLoading)
 0352                return;
 353
 354            switch (action)
 355            {
 356                case DCLAction_Trigger.ToggleShortcut0:
 0357                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 0);
 0358                    break;
 359                case DCLAction_Trigger.ToggleShortcut1:
 0360                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 1);
 0361                    break;
 362                case DCLAction_Trigger.ToggleShortcut2:
 0363                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 2);
 0364                    break;
 365                case DCLAction_Trigger.ToggleShortcut3:
 0366                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 3);
 0367                    break;
 368                case DCLAction_Trigger.ToggleShortcut4:
 0369                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 4);
 0370                    break;
 371                case DCLAction_Trigger.ToggleShortcut5:
 0372                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 5);
 0373                    break;
 374                case DCLAction_Trigger.ToggleShortcut6:
 0375                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 6);
 0376                    break;
 377                case DCLAction_Trigger.ToggleShortcut7:
 0378                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 7);
 0379                    break;
 380                case DCLAction_Trigger.ToggleShortcut8:
 0381                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 8);
 0382                    break;
 383                case DCLAction_Trigger.ToggleShortcut9:
 0384                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 9);
 385                    break;
 386            }
 0387        }
 388
 48389        internal virtual IEmotesCustomizationComponentView CreateView() => EmotesCustomizationComponentView.Create();
 390    }
 391}