< 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:48
Coverable lines:185
Total lines:404
Line coverage:74% (137 of 185)
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%660100%
RemoveEmote(...)0%2100%
OnAnimationAdded(...)0%3.192033.33%
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
 2275368            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();
 583076            for (int i = 0; i < ownedEmotes.Length; i++)
 77            {
 282378                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
 282381                this.ownedEmotes[emote.id] = emote;
 82            }
 583083            foreach (WearableItem emote in this.ownedEmotes.Values)
 84            {
 282385                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;
 397096            foreach (string emoteId in this.ownedEmotes.Keys)
 97            {
 192298                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        {
 2823157            var emoteId = emote.id;
 2823158            if (!emote.IsEmote() || emotesCustomizationDataStore.currentLoadedEmotes.Contains(emoteId))
 1333159                return;
 160
 1490161            emotesCustomizationDataStore.currentLoadedEmotes.Add(emoteId);
 1490162            emotesDataStore.emotesOnUse.IncreaseRefCount((WearableLiterals.BodyShapes.FEMALE, emoteId));
 1490163            emotesDataStore.emotesOnUse.IncreaseRefCount((WearableLiterals.BodyShapes.MALE, emoteId));
 164
 1490165            if (!emote.ShowInBackpack())
 576166                return;
 167
 914168            EmoteCardComponentModel emoteToAdd = ParseWearableItemIntoEmoteCardModel(emote);
 914169            EmoteCardComponentView newEmote = view.AddEmote(emoteToAdd);
 170
 914171            if (newEmote != null)
 912172                newEmote.SetAsLoading(true);
 173
 914174            if (!emotesInLoadingState.ContainsKey(emoteId))
 914175                emotesInLoadingState.Add(emoteId, newEmote);
 176
 914177            RefreshEmoteLoadingState(emoteId);
 914178        }
 179
 180        internal void RemoveEmote(string emoteId)
 181        {
 0182            emotesDataStore.emotesOnUse.DecreaseRefCount((WearableLiterals.BodyShapes.FEMALE, emoteId));
 0183            emotesDataStore.emotesOnUse.DecreaseRefCount((WearableLiterals.BodyShapes.MALE, emoteId));
 0184            emotesCustomizationDataStore.currentLoadedEmotes.Remove(emoteId);
 0185            view.RemoveEmote(emoteId);
 0186            UpdateEmoteSlots();
 0187        }
 188
 189        internal void OnAnimationAdded((string bodyshapeId, string emoteId) values, EmoteClipData emoteClipData)
 190        {
 1191            if(emoteClipData.clip != null)
 1192                RefreshEmoteLoadingState(values.emoteId);
 193            else
 194            {
 0195                var emoteId = values.emoteId;
 0196                RemoveEmote(emoteId);
 0197                Debug.LogError("Emote " + emoteId + " was not found in emotes data store");
 198            }
 0199        }
 200
 201        internal void RefreshEmoteLoadingState(string emoteId)
 202        {
 2838203            if (emotesDataStore.animations.ContainsKey((bodyShapeId, emoteId)))
 204            {
 2205                emotesInLoadingState.TryGetValue(emoteId, out EmoteCardComponentView emote);
 2206                if (emote != null)
 207                {
 1208                    emote.SetAsLoading(false);
 1209                    emotesInLoadingState.Remove(emoteId);
 210                }
 211            }
 2838212        }
 213
 214        internal EmoteCardComponentModel ParseWearableItemIntoEmoteCardModel(WearableItem wearable)
 215        {
 915216            return new EmoteCardComponentModel
 217            {
 218                id = wearable.id,
 219                name = wearable.GetName(),
 220                description = wearable.description,
 221                pictureUri = wearable.ComposeThumbnailUrl(),
 222                pictureSprite = wearable.thumbnailSprite,
 223                isAssignedInSelectedSlot = false,
 224                isSelected = false,
 225                assignedSlot = -1,
 226                rarity = wearable.rarity,
 227                isInL2 = wearable.IsInL2(),
 228                isLoading = false,
 229                isCollectible = wearable.IsCollectible()
 230            };
 231        }
 232
 233        internal void UpdateEmoteSlots()
 234        {
 3434235            for (int i = 0; i < emotesCustomizationDataStore.unsavedEquippedEmotes.Count(); i++)
 236            {
 1558237                if (i > NUMBER_OF_SLOTS)
 238                    break;
 239
 1558240                if (emotesCustomizationDataStore.unsavedEquippedEmotes[i] == null) //empty slot
 241                {
 1550242                    EmoteSlotCardComponentView existingEmoteIntoSlot = view.GetSlot(i);
 1550243                    if (existingEmoteIntoSlot != null)
 1400244                        view.UnequipEmote(existingEmoteIntoSlot.model.emoteId, i, false);
 245
 1400246                    continue;
 247                }
 248
 8249                ownedEmotes.TryGetValue(emotesCustomizationDataStore.unsavedEquippedEmotes[i].id, out WearableItem emote
 8250                if (emoteItem != null && emotesCustomizationDataStore.currentLoadedEmotes.Contains(emoteItem.id))
 2251                    view.EquipEmote(emoteItem.id, emoteItem.GetName(), i, false, false);
 252            }
 159253        }
 254
 255        internal void StoreEquippedEmotes()
 256        {
 2257            List<EquippedEmoteData> newEquippedEmotesList = new List<EquippedEmoteData> { null, null, null, null, null, 
 258
 2259            if (view.currentSlots != null)
 260            {
 0261                foreach (EmoteSlotCardComponentView slot in view.currentSlots)
 262                {
 0263                    if (!string.IsNullOrEmpty(slot.model.emoteId))
 0264                        newEquippedEmotesList[slot.model.slotNumber] = new EquippedEmoteData
 265                        {
 266                            id = slot.model.emoteId,
 267                            cachedThumbnail = slot.model.pictureSprite
 268                        };
 269                }
 270            }
 271
 2272            emotesCustomizationDataStore.unsavedEquippedEmotes.Set(newEquippedEmotesList);
 2273        }
 274
 275        internal void OnEmoteEquipped(string emoteId, int slotNumber)
 276        {
 1277            StoreEquippedEmotes();
 1278            onEmotePreviewed?.Invoke(emoteId);
 1279            onEmoteEquipped?.Invoke(emoteId);
 1280        }
 281
 282        internal void OnEmoteUnequipped(string emoteId, int slotNumber)
 283        {
 1284            StoreEquippedEmotes();
 1285            onEmoteUnequipped?.Invoke(emoteId);
 1286        }
 287
 2288        internal void OnSellEmoteClicked(string emoteId) { onEmoteSell?.Invoke(emoteId); }
 289
 2290        internal void OnSlotSelected(string emoteId, int slotNumber) { onEmotePreviewed?.Invoke(emoteId); }
 291
 292        internal void ConfigureShortcuts()
 293        {
 62294            equipInputAction = Resources.Load<InputAction_Hold>("DefaultConfirmAction");
 62295            equipInputAction.OnFinished += OnEquipInputActionTriggered;
 296
 297            // showInfoInputAction = Resources.Load<InputAction_Hold>("ZoomIn");
 298            // showInfoInputAction.OnFinished += OnShowInfoInputActionTriggered;
 299
 62300            shortcut0InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut0");
 62301            shortcut0InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 302
 62303            shortcut1InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut1");
 62304            shortcut1InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 305
 62306            shortcut2InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut2");
 62307            shortcut2InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 308
 62309            shortcut3InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut3");
 62310            shortcut3InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 311
 62312            shortcut4InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut4");
 62313            shortcut4InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 314
 62315            shortcut5InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut5");
 62316            shortcut5InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 317
 62318            shortcut6InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut6");
 62319            shortcut6InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 320
 62321            shortcut7InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut7");
 62322            shortcut7InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 323
 62324            shortcut8InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut8");
 62325            shortcut8InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 326
 62327            shortcut9InputAction = Resources.Load<InputAction_Trigger>("ToggleShortcut9");
 62328            shortcut9InputAction.OnTriggered += OnNumericShortcutInputActionTriggered;
 62329        }
 330
 331        internal void OnEquipInputActionTriggered(DCLAction_Hold action)
 332        {
 0333            if (!isEmotesCustomizationSectionOpen || view.selectedCard == null || view.selectedCard.model.isLoading)
 0334                return;
 335
 0336            if (!view.selectedCard.model.isAssignedInSelectedSlot)
 337            {
 0338                view.EquipEmote(
 339                    view.selectedCard.model.id,
 340                    view.selectedCard.model.name,
 341                    view.selectedSlot);
 342            }
 343            else
 344            {
 0345                view.UnequipEmote(
 346                    view.selectedCard.model.id,
 347                    view.selectedSlot);
 348            }
 0349        }
 350
 351        internal void OnShowInfoInputActionTriggered(DCLAction_Hold action)
 352        {
 0353            if (!isEmotesCustomizationSectionOpen || view.selectedCard == null || !view.selectedCard.model.isCollectible
 0354                return;
 355
 0356            view.OpenEmoteInfoPanel(
 357                view.selectedCard.model,
 358                view.selectedCard.rarityMark.gameObject.activeSelf ? view.selectedCard.rarityMark.color : Color.grey,
 359                view.selectedCard.emoteInfoAnchor);
 0360        }
 361
 362        internal void OnNumericShortcutInputActionTriggered(DCLAction_Trigger action)
 363        {
 0364            if (!isEmotesCustomizationSectionOpen || view.selectedCard == null || view.selectedCard.model.isLoading)
 0365                return;
 366
 367            switch (action)
 368            {
 369                case DCLAction_Trigger.ToggleShortcut0:
 0370                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 0);
 0371                    break;
 372                case DCLAction_Trigger.ToggleShortcut1:
 0373                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 1);
 0374                    break;
 375                case DCLAction_Trigger.ToggleShortcut2:
 0376                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 2);
 0377                    break;
 378                case DCLAction_Trigger.ToggleShortcut3:
 0379                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 3);
 0380                    break;
 381                case DCLAction_Trigger.ToggleShortcut4:
 0382                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 4);
 0383                    break;
 384                case DCLAction_Trigger.ToggleShortcut5:
 0385                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 5);
 0386                    break;
 387                case DCLAction_Trigger.ToggleShortcut6:
 0388                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 6);
 0389                    break;
 390                case DCLAction_Trigger.ToggleShortcut7:
 0391                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 7);
 0392                    break;
 393                case DCLAction_Trigger.ToggleShortcut8:
 0394                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 8);
 0395                    break;
 396                case DCLAction_Trigger.ToggleShortcut9:
 0397                    view.EquipEmote(view.selectedCard.model.id, view.selectedCard.model.name, 9);
 398                    break;
 399            }
 0400        }
 401
 48402        internal virtual IEmotesCustomizationComponentView CreateView() => EmotesCustomizationComponentView.Create();
 403    }
 404}