< 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:134
Uncovered lines:45
Coverable lines:179
Total lines:390
Line coverage:74.8% (134 of 179)
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        internal string bodyShapeId;
 13
 14        internal DataStore_EmotesCustomization emotesCustomizationDataStore;
 15        internal DataStore_Emotes emotesDataStore;
 6216        internal BaseDictionary<string, EmoteCardComponentView> emotesInLoadingState = new BaseDictionary<string, EmoteC
 17        internal InputAction_Hold equipInputAction;
 18        internal DataStore_ExploreV2 exploreV2DataStore;
 19        internal DataStore_HUDs hudsDataStore;
 6220        internal Dictionary<string, WearableItem> ownedEmotes = new Dictionary<string, WearableItem>();
 21        // internal InputAction_Hold showInfoInputAction;
 22        internal InputAction_Trigger shortcut0InputAction;
 23        internal InputAction_Trigger shortcut1InputAction;
 24        internal InputAction_Trigger shortcut2InputAction;
 25        internal InputAction_Trigger shortcut3InputAction;
 26        internal InputAction_Trigger shortcut4InputAction;
 27        internal InputAction_Trigger shortcut5InputAction;
 28        internal InputAction_Trigger shortcut6InputAction;
 29        internal InputAction_Trigger shortcut7InputAction;
 30        internal InputAction_Trigger shortcut8InputAction;
 31        internal InputAction_Trigger shortcut9InputAction;
 32
 33        internal IEmotesCustomizationComponentView view;
 34
 035        internal bool isEmotesCustomizationSectionOpen => exploreV2DataStore.isOpen.Get() && view.isActive;
 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;
 119            // 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
 2185        internal void OnAnimationAdded((string bodyshapeId, string emoteId) values, EmoteClipData emoteClipData) { Refre
 186
 187        internal void RefreshEmoteLoadingState(string emoteId)
 188        {
 2094189            if (emotesDataStore.animations.ContainsKey((bodyShapeId, emoteId)))
 190            {
 2191                emotesInLoadingState.TryGetValue(emoteId, out EmoteCardComponentView emote);
 2192                if (emote != null)
 193                {
 1194                    emote.SetAsLoading(false);
 1195                    emotesInLoadingState.Remove(emoteId);
 196                }
 197            }
 2094198        }
 199
 200        internal EmoteCardComponentModel ParseWearableItemIntoEmoteCardModel(WearableItem wearable)
 201        {
 915202            return new EmoteCardComponentModel
 203            {
 204                id = wearable.id,
 205                name = wearable.GetName(),
 206                description = wearable.description,
 207                pictureUri = wearable.ComposeThumbnailUrl(),
 208                pictureSprite = wearable.thumbnailSprite,
 209                isAssignedInSelectedSlot = false,
 210                isSelected = false,
 211                assignedSlot = -1,
 212                rarity = wearable.rarity,
 213                isInL2 = wearable.IsInL2(),
 214                isLoading = false,
 215                isCollectible = wearable.IsCollectible()
 216            };
 217        }
 218
 219        internal void UpdateEmoteSlots()
 220        {
 3434221            for (int i = 0; i < emotesCustomizationDataStore.unsavedEquippedEmotes.Count(); i++)
 222            {
 1558223                if (i > NUMBER_OF_SLOTS)
 224                    break;
 225
 1558226                if (emotesCustomizationDataStore.unsavedEquippedEmotes[i] == null) //empty slot
 227                {
 1550228                    EmoteSlotCardComponentView existingEmoteIntoSlot = view.GetSlot(i);
 1550229                    if (existingEmoteIntoSlot != null)
 1400230                        view.UnequipEmote(existingEmoteIntoSlot.model.emoteId, i, false);
 231
 1400232                    continue;
 233                }
 234
 8235                ownedEmotes.TryGetValue(emotesCustomizationDataStore.unsavedEquippedEmotes[i].id, out WearableItem emote
 8236                if (emoteItem != null && emotesCustomizationDataStore.currentLoadedEmotes.Contains(emoteItem.id))
 2237                    view.EquipEmote(emoteItem.id, emoteItem.GetName(), i, false, false);
 238            }
 159239        }
 240
 241        internal void StoreEquippedEmotes()
 242        {
 2243            List<EquippedEmoteData> newEquippedEmotesList = new List<EquippedEmoteData> { null, null, null, null, null, 
 244
 2245            if (view.currentSlots != null)
 246            {
 0247                foreach (EmoteSlotCardComponentView slot in view.currentSlots)
 248                {
 0249                    if (!string.IsNullOrEmpty(slot.model.emoteId))
 0250                        newEquippedEmotesList[slot.model.slotNumber] = new EquippedEmoteData
 251                        {
 252                            id = slot.model.emoteId,
 253                            cachedThumbnail = slot.model.pictureSprite
 254                        };
 255                }
 256            }
 257
 2258            emotesCustomizationDataStore.unsavedEquippedEmotes.Set(newEquippedEmotesList);
 2259        }
 260
 261        internal void OnEmoteEquipped(string emoteId, int slotNumber)
 262        {
 1263            StoreEquippedEmotes();
 1264            onEmotePreviewed?.Invoke(emoteId);
 1265            onEmoteEquipped?.Invoke(emoteId);
 1266        }
 267
 268        internal void OnEmoteUnequipped(string emoteId, int slotNumber)
 269        {
 1270            StoreEquippedEmotes();
 1271            onEmoteUnequipped?.Invoke(emoteId);
 1272        }
 273
 2274        internal void OnSellEmoteClicked(string emoteId) { onEmoteSell?.Invoke(emoteId); }
 275
 2276        internal void OnSlotSelected(string emoteId, int slotNumber) { onEmotePreviewed?.Invoke(emoteId); }
 277
 278        internal void ConfigureShortcuts()
 279        {
 62280            equipInputAction = Resources.Load<InputAction_Hold>("DefaultConfirmAction");
 62281            equipInputAction.OnFinished += OnEquipInputActionTriggered;
 282
 283            // showInfoInputAction = Resources.Load<InputAction_Hold>("ZoomIn");
 284            // showInfoInputAction.OnFinished += OnShowInfoInputActionTriggered;
 285
 62286            shortcut0InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut0");
 62287            shortcut0InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 288
 62289            shortcut1InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut1");
 62290            shortcut1InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 291
 62292            shortcut2InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut2");
 62293            shortcut2InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 294
 62295            shortcut3InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut3");
 62296            shortcut3InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 297
 62298            shortcut4InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut4");
 62299            shortcut4InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 300
 62301            shortcut5InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut5");
 62302            shortcut5InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 303
 62304            shortcut6InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut6");
 62305            shortcut6InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 306
 62307            shortcut7InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut7");
 62308            shortcut7InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 309
 62310            shortcut8InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut8");
 62311            shortcut8InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 312
 62313            shortcut9InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut9");
 62314            shortcut9InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 62315        }
 316
 317        internal void OnEquipInputActionTriggered(DCLAction_Hold action)
 318        {
 0319            if (!isEmotesCustomizationSectionOpen || view.selectedCard == null || view.selectedCard.model.isLoading)
 0320                return;
 321
 0322            if (!view.selectedCard.model.isAssignedInSelectedSlot)
 323            {
 0324                view.EquipEmote(
 325                    view.selectedCard.model.id,
 326                    view.selectedCard.model.name,
 327                    view.selectedSlot);
 0328            }
 329            else
 330            {
 0331                view.UnequipEmote(
 332                    view.selectedCard.model.id,
 333                    view.selectedSlot);
 334            }
 0335        }
 336
 337        internal void OnShowInfoInputActionTriggered(DCLAction_Hold action)
 338        {
 0339            if (!isEmotesCustomizationSectionOpen || view.selectedCard == null || !view.selectedCard.model.isCollectible
 0340                return;
 341
 0342            view.OpenEmoteInfoPanel(
 343                view.selectedCard.model,
 344                view.selectedCard.rarityMark.gameObject.activeSelf ? view.selectedCard.rarityMark.color : Color.grey,
 345                view.selectedCard.emoteInfoAnchor);
 0346        }
 347
 348        internal void OnNumericShortcutInputActionTriggered(DCLAction_Trigger action)
 349        {
 0350            if (!isEmotesCustomizationSectionOpen || view.selectedCard == null || view.selectedCard.model.isLoading)
 0351                return;
 352
 353            switch (action)
 354            {
 355                case DCLAction_Trigger.ToggleShortcut0:
 0356                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 0);
 0357                    break;
 358                case DCLAction_Trigger.ToggleShortcut1:
 0359                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 1);
 0360                    break;
 361                case DCLAction_Trigger.ToggleShortcut2:
 0362                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 2);
 0363                    break;
 364                case DCLAction_Trigger.ToggleShortcut3:
 0365                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 3);
 0366                    break;
 367                case DCLAction_Trigger.ToggleShortcut4:
 0368                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 4);
 0369                    break;
 370                case DCLAction_Trigger.ToggleShortcut5:
 0371                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 5);
 0372                    break;
 373                case DCLAction_Trigger.ToggleShortcut6:
 0374                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 6);
 0375                    break;
 376                case DCLAction_Trigger.ToggleShortcut7:
 0377                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 7);
 0378                    break;
 379                case DCLAction_Trigger.ToggleShortcut8:
 0380                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 8);
 0381                    break;
 382                case DCLAction_Trigger.ToggleShortcut9:
 0383                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 9);
 384                    break;
 385            }
 0386        }
 387
 48388        internal virtual IEmotesCustomizationComponentView CreateView() => EmotesCustomizationComponentView.Create();
 389    }
 390}