< Summary

Class:QuickBarController
Assembly:BuildModeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuildModeHUD/Scripts/Common/QuickBarController.cs
Covered lines:45
Uncovered lines:25
Coverable lines:70
Total lines:161
Line coverage:64.2% (45 of 70)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
QuickBarController()0%110100%
Initialize(...)0%110100%
Dispose()0%3.013091.67%
GetSlotsCount()0%2100%
QuickBarObjectSelected(...)0%4.254075%
OnQuickBarObjectSelected(...)0%2100%
SetIndexToDrop(...)0%2100%
SceneObjectDroppedFromQuickBar(...)0%110100%
MoveQuickbarThumbnailPromise(...)0%110100%
SceneObjectDroppedFromCatalog(...)0%2.062075%
SetCatalogItemToShortcut(...)0%12300%
ClearThumbnailPromise(...)0%2.862040%
RemoveQuickBarShortcut(...)0%110100%
SetQuickBarShortcut(...)0%2.062075%
QuickBarInput(...)0%220100%
CancelDragging()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuildModeHUD/Scripts/Common/QuickBarController.cs

#LineLine coverage
 1using DCL;
 2using System;
 3using UnityEngine;
 4using UnityEngine.EventSystems;
 5
 6public interface IQuickBarController
 7{
 8    event Action<int> OnQuickBarShortcutSelected;
 9    event Action<CatalogItem> OnCatalogItemSelected;
 10    event Action<CatalogItem> OnCatalogItemAssigned;
 11
 12    void Initialize(IQuickBarView view, IDragAndDropSceneObjectController dragAndDropSceneObjectController);
 13    void Dispose();
 14    int GetSlotsCount();
 15    CatalogItem QuickBarObjectSelected(int index);
 16    void SetIndexToDrop(int index);
 17    void SceneObjectDroppedFromQuickBar(int fromQuickBarIndex, int toQuickBarIndex, Texture texture);
 18    void SceneObjectDroppedFromCatalog(BaseEventData data);
 19    void SetQuickBarShortcut(CatalogItem catalogItem, int index, Texture texture);
 20    void QuickBarInput(int quickBarSlot);
 21    void CancelDragging();
 22}
 23
 24public class QuickBarController : IQuickBarController
 25{
 26    const int AMOUNT_OF_QUICK_SLOTS = 9;
 27
 28    public event Action<int> OnQuickBarShortcutSelected;
 29    public event Action<CatalogItem> OnCatalogItemSelected;
 30    public event Action<CatalogItem> OnCatalogItemAssigned;
 31
 32    internal IQuickBarView quickBarView;
 33    internal IDragAndDropSceneObjectController dragAndDropController;
 34
 4135    internal CatalogItem[] quickBarShortcutsCatalogItems = new CatalogItem[AMOUNT_OF_QUICK_SLOTS];
 4136    internal AssetPromise_Texture[] quickBarShortcutsThumbnailPromises = new AssetPromise_Texture[AMOUNT_OF_QUICK_SLOTS]
 4137    internal int lastIndexDroped = -1;
 38
 39    public void Initialize(IQuickBarView quickBarView, IDragAndDropSceneObjectController dragAndDropController)
 40    {
 4041        this.quickBarView = quickBarView;
 4042        this.dragAndDropController = dragAndDropController;
 43
 4044        quickBarView.OnQuickBarObjectSelected += OnQuickBarObjectSelected;
 4045        quickBarView.OnSetIndexToDrop += SetIndexToDrop;
 4046        quickBarView.OnSceneObjectDroppedFromQuickBar += SceneObjectDroppedFromQuickBar;
 4047        quickBarView.OnSceneObjectDroppedFromCatalog += SceneObjectDroppedFromCatalog;
 4048        quickBarView.OnQuickBarInputTriggered += QuickBarInput;
 4049        dragAndDropController.OnStopInput += CancelDragging;
 4050    }
 51
 52    public void Dispose()
 53    {
 3954        quickBarView.OnQuickBarObjectSelected -= OnQuickBarObjectSelected;
 3955        quickBarView.OnSetIndexToDrop -= SetIndexToDrop;
 3956        quickBarView.OnSceneObjectDroppedFromQuickBar -= SceneObjectDroppedFromQuickBar;
 3957        quickBarView.OnSceneObjectDroppedFromCatalog -= SceneObjectDroppedFromCatalog;
 3958        quickBarView.OnQuickBarInputTriggered -= QuickBarInput;
 3959        dragAndDropController.OnStopInput -= CancelDragging;
 60
 78061        foreach (AssetPromise_Texture loadedThumbnailPromise in quickBarShortcutsThumbnailPromises)
 62        {
 35163            if (loadedThumbnailPromise == null)
 64                continue;
 65
 066            ClearThumbnailPromise(loadedThumbnailPromise);
 67        }
 3968    }
 69
 070    public int GetSlotsCount() { return AMOUNT_OF_QUICK_SLOTS; }
 71
 72    public CatalogItem QuickBarObjectSelected(int index)
 73    {
 1274        if (quickBarShortcutsCatalogItems.Length > index && quickBarShortcutsCatalogItems[index] != null)
 75        {
 1276            OnCatalogItemSelected?.Invoke(quickBarShortcutsCatalogItems[index]);
 1277            return quickBarShortcutsCatalogItems[index];
 78        }
 79
 080        return null;
 81    }
 82
 083    private void OnQuickBarObjectSelected(int obj) { QuickBarObjectSelected(obj); }
 84
 085    public void SetIndexToDrop(int index) { lastIndexDroped = index; }
 86
 87    public void SceneObjectDroppedFromQuickBar(int fromQuickBarIndex, int toQuickBarIndex, Texture texture)
 88    {
 189        SetQuickBarShortcut(quickBarShortcutsCatalogItems[fromQuickBarIndex], toQuickBarIndex, texture);
 190        MoveQuickbarThumbnailPromise(fromQuickBarIndex, toQuickBarIndex);
 191        RemoveQuickBarShortcut(fromQuickBarIndex);
 192    }
 93
 94    private void MoveQuickbarThumbnailPromise(int fromQuickBarIndex, int toQuickBarIndex)
 95    {
 196        quickBarShortcutsThumbnailPromises[toQuickBarIndex] = quickBarShortcutsThumbnailPromises[fromQuickBarIndex];
 197        quickBarShortcutsThumbnailPromises[fromQuickBarIndex] = null;
 198    }
 99
 100    public void SceneObjectDroppedFromCatalog(BaseEventData data)
 101    {
 1102        CatalogItemAdapter adapter = dragAndDropController.GetLastAdapterDragged();
 103
 1104        if (adapter != null)
 0105            SetCatalogItemToShortcut(adapter.GetContent());
 1106    }
 107
 108    private void SetCatalogItemToShortcut(CatalogItem catalogItem)
 109    {
 0110        if (catalogItem == null)
 0111            return;
 112
 0113        var url = catalogItem.GetThumbnailUrl();
 114
 0115        if (string.IsNullOrEmpty(url))
 0116            return;
 117
 0118        ClearThumbnailPromise(quickBarShortcutsThumbnailPromises[lastIndexDroped]);
 0119        quickBarShortcutsThumbnailPromises[lastIndexDroped] = new AssetPromise_Texture(url);
 120
 0121        quickBarShortcutsThumbnailPromises[lastIndexDroped].OnSuccessEvent += x =>
 122        {
 0123            SetQuickBarShortcut(catalogItem, lastIndexDroped, x.texture);
 0124        };
 125
 0126        quickBarShortcutsThumbnailPromises[lastIndexDroped].OnFailEvent += x =>
 127        {
 0128            Debug.Log($"Error downloading: {url}");
 0129        };
 130
 0131        AssetPromiseKeeper_Texture.i.Keep(quickBarShortcutsThumbnailPromises[lastIndexDroped]);
 0132    }
 133
 134    private void ClearThumbnailPromise(AssetPromise_Texture thumbnailToClear)
 135    {
 1136        if (thumbnailToClear != null)
 137        {
 0138            thumbnailToClear.ClearEvents();
 0139            AssetPromiseKeeper_Texture.i.Forget(thumbnailToClear);
 0140            thumbnailToClear = null;
 141        }
 1142    }
 143
 144    private void RemoveQuickBarShortcut(int index)
 145    {
 1146        quickBarShortcutsCatalogItems[index] = null;
 1147        quickBarView.SetShortcutAsEmpty(index);
 1148        ClearThumbnailPromise(quickBarShortcutsThumbnailPromises[index]);
 1149    }
 150
 151    public void SetQuickBarShortcut(CatalogItem catalogItem, int index, Texture texture)
 152    {
 13153        quickBarShortcutsCatalogItems[index] = catalogItem;
 13154        quickBarView.SetTextureToShortcut(index, texture);
 13155        OnCatalogItemAssigned?.Invoke(catalogItem);
 0156    }
 157
 6158    public void QuickBarInput(int quickBarSlot) { OnQuickBarShortcutSelected?.Invoke(quickBarSlot); }
 159
 2160    public void CancelDragging() { quickBarView.CancelCurrentDragging(); }
 161}