< 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:137
Uncovered lines:45
Coverable lines:182
Total lines:392
Line coverage:75.2% (137 of 182)
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%550100%
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
 907968            List<string> idsToRemove = emotesCustomizationDataStore.currentLoadedEmotes.Get().Where(x => ownedEmotes.All
 69
 18470            foreach (string emoteId in idsToRemove)
 71            {
 072                RemoveEmote(emoteId);
 73            }
 74
 9275            this.ownedEmotes.Clear();
 364676            for (int i = 0; i < ownedEmotes.Length; i++)
 77            {
 173178                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
 173181                this.ownedEmotes[emote.id] = emote;
 82            }
 364683            foreach (WearableItem emote in this.ownedEmotes.Values)
 84            {
 173185                AddEmote(emote);
 86            }
 9287            UpdateEmoteSlots();
 9288        }
 89
 90        public void SetEquippedBodyShape(string bodyShapeId)
 91        {
 6392            if (bodyShapeId == this.bodyShapeId)
 093                return;
 94
 6395            this.bodyShapeId = bodyShapeId;
 248296            foreach (string emoteId in this.ownedEmotes.Keys)
 97            {
 117898                RefreshEmoteLoadingState(emoteId);
 99            }
 63100        }
 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        {
 1731157            var emoteId = emote.id;
 1731158            if (!emote.IsEmote() || emotesCustomizationDataStore.currentLoadedEmotes.Contains(emoteId))
 817159                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
 186        internal void OnAnimationAdded((string bodyshapeId, string emoteId) values, EmoteClipData emoteClipData)
 2187        { RefreshEmoteLoadingState(values.emoteId); }
 188
 189        internal void RefreshEmoteLoadingState(string emoteId)
 190        {
 2094191            if (emotesDataStore.animations.ContainsKey((bodyShapeId, emoteId)))
 192            {
 2193                emotesInLoadingState.TryGetValue(emoteId, out EmoteCardComponentView emote);
 2194                if (emote != null)
 195                {
 1196                    emote.SetAsLoading(false);
 1197                    emotesInLoadingState.Remove(emoteId);
 198                }
 199            }
 2094200        }
 201
 202        internal EmoteCardComponentModel ParseWearableItemIntoEmoteCardModel(WearableItem wearable)
 203        {
 915204            return new EmoteCardComponentModel
 205            {
 206                id = wearable.id,
 207                name = wearable.GetName(),
 208                description = wearable.description,
 209                pictureUri = wearable.ComposeThumbnailUrl(),
 210                pictureSprite = wearable.thumbnailSprite,
 211                isAssignedInSelectedSlot = false,
 212                isSelected = false,
 213                assignedSlot = -1,
 214                rarity = wearable.rarity,
 215                isInL2 = wearable.IsInL2(),
 216                isLoading = false,
 217                isCollectible = wearable.IsCollectible()
 218            };
 219        }
 220
 221        internal void UpdateEmoteSlots()
 222        {
 3434223            for (int i = 0; i < emotesCustomizationDataStore.unsavedEquippedEmotes.Count(); i++)
 224            {
 1558225                if (i > NUMBER_OF_SLOTS)
 226                    break;
 227
 1558228                if (emotesCustomizationDataStore.unsavedEquippedEmotes[i] == null) //empty slot
 229                {
 1550230                    EmoteSlotCardComponentView existingEmoteIntoSlot = view.GetSlot(i);
 1550231                    if (existingEmoteIntoSlot != null)
 1400232                        view.UnequipEmote(existingEmoteIntoSlot.model.emoteId, i, false);
 233
 1400234                    continue;
 235                }
 236
 8237                ownedEmotes.TryGetValue(emotesCustomizationDataStore.unsavedEquippedEmotes[i].id, out WearableItem emote
 8238                if (emoteItem != null && emotesCustomizationDataStore.currentLoadedEmotes.Contains(emoteItem.id))
 2239                    view.EquipEmote(emoteItem.id, emoteItem.GetName(), i, false, false);
 240            }
 159241        }
 242
 243        internal void StoreEquippedEmotes()
 244        {
 2245            List<EquippedEmoteData> newEquippedEmotesList = new List<EquippedEmoteData> { null, null, null, null, null, 
 246
 2247            if (view.currentSlots != null)
 248            {
 0249                foreach (EmoteSlotCardComponentView slot in view.currentSlots)
 250                {
 0251                    if (!string.IsNullOrEmpty(slot.model.emoteId))
 0252                        newEquippedEmotesList[slot.model.slotNumber] = new EquippedEmoteData
 253                        {
 254                            id = slot.model.emoteId,
 255                            cachedThumbnail = slot.model.pictureSprite
 256                        };
 257                }
 258            }
 259
 2260            emotesCustomizationDataStore.unsavedEquippedEmotes.Set(newEquippedEmotesList);
 2261        }
 262
 263        internal void OnEmoteEquipped(string emoteId, int slotNumber)
 264        {
 1265            StoreEquippedEmotes();
 1266            onEmotePreviewed?.Invoke(emoteId);
 1267            onEmoteEquipped?.Invoke(emoteId);
 1268        }
 269
 270        internal void OnEmoteUnequipped(string emoteId, int slotNumber)
 271        {
 1272            StoreEquippedEmotes();
 1273            onEmoteUnequipped?.Invoke(emoteId);
 1274        }
 275
 2276        internal void OnSellEmoteClicked(string emoteId) { onEmoteSell?.Invoke(emoteId); }
 277
 2278        internal void OnSlotSelected(string emoteId, int slotNumber) { onEmotePreviewed?.Invoke(emoteId); }
 279
 280        internal void ConfigureShortcuts()
 281        {
 62282            equipInputAction = Resources.Load<InputAction_Hold>("DefaultConfirmAction");
 62283            equipInputAction.OnFinished += OnEquipInputActionTriggered;
 284
 62285            showInfoInputAction = Resources.Load<InputAction_Hold>("ZoomIn");
 62286            showInfoInputAction.OnFinished += OnShowInfoInputActionTriggered;
 287
 62288            shortcut0InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut0");
 62289            shortcut0InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 290
 62291            shortcut1InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut1");
 62292            shortcut1InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 293
 62294            shortcut2InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut2");
 62295            shortcut2InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 296
 62297            shortcut3InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut3");
 62298            shortcut3InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 299
 62300            shortcut4InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut4");
 62301            shortcut4InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 302
 62303            shortcut5InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut5");
 62304            shortcut5InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 305
 62306            shortcut6InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut6");
 62307            shortcut6InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 308
 62309            shortcut7InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut7");
 62310            shortcut7InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 311
 62312            shortcut8InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut8");
 62313            shortcut8InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 314
 62315            shortcut9InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut9");
 62316            shortcut9InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 62317        }
 318
 319        internal void OnEquipInputActionTriggered(DCLAction_Hold action)
 320        {
 0321            if (!isEmotesCustomizationSectionOpen || view.selectedCard == null || view.selectedCard.model.isLoading)
 0322                return;
 323
 0324            if (!view.selectedCard.model.isAssignedInSelectedSlot)
 325            {
 0326                view.EquipEmote(
 327                    view.selectedCard.model.id,
 328                    view.selectedCard.model.name,
 329                    view.selectedSlot);
 0330            }
 331            else
 332            {
 0333                view.UnequipEmote(
 334                    view.selectedCard.model.id,
 335                    view.selectedSlot);
 336            }
 0337        }
 338
 339        internal void OnShowInfoInputActionTriggered(DCLAction_Hold action)
 340        {
 0341            if (!isEmotesCustomizationSectionOpen || view.selectedCard == null || !view.selectedCard.model.isCollectible
 0342                return;
 343
 0344            view.OpenEmoteInfoPanel(
 345                view.selectedCard.model,
 346                view.selectedCard.rarityMark.gameObject.activeSelf ? view.selectedCard.rarityMark.color : Color.grey,
 347                view.selectedCard.emoteInfoAnchor);
 0348        }
 349
 350        internal void OnNumericShortcutInputActionTriggered(DCLAction_Trigger action)
 351        {
 0352            if (!isEmotesCustomizationSectionOpen || view.selectedCard == null || view.selectedCard.model.isLoading)
 0353                return;
 354
 355            switch (action)
 356            {
 357                case DCLAction_Trigger.ToggleShortcut0:
 0358                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 0);
 0359                    break;
 360                case DCLAction_Trigger.ToggleShortcut1:
 0361                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 1);
 0362                    break;
 363                case DCLAction_Trigger.ToggleShortcut2:
 0364                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 2);
 0365                    break;
 366                case DCLAction_Trigger.ToggleShortcut3:
 0367                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 3);
 0368                    break;
 369                case DCLAction_Trigger.ToggleShortcut4:
 0370                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 4);
 0371                    break;
 372                case DCLAction_Trigger.ToggleShortcut5:
 0373                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 5);
 0374                    break;
 375                case DCLAction_Trigger.ToggleShortcut6:
 0376                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 6);
 0377                    break;
 378                case DCLAction_Trigger.ToggleShortcut7:
 0379                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 7);
 0380                    break;
 381                case DCLAction_Trigger.ToggleShortcut8:
 0382                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 8);
 0383                    break;
 384                case DCLAction_Trigger.ToggleShortcut9:
 0385                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 9);
 386                    break;
 387            }
 0388        }
 389
 48390        internal virtual IEmotesCustomizationComponentView CreateView() => EmotesCustomizationComponentView.Create();
 391    }
 392}