< Summary

Class:QuickBarController
Assembly:BuildModeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/Common/QuickBarController.cs
Covered lines:47
Uncovered lines:25
Coverable lines:72
Total lines:165
Line coverage:65.2% (47 of 72)
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%5.015092.86%
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/DCLPlugins/BuilderInWorld/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
 1535    internal CatalogItem[] quickBarShortcutsCatalogItems = new CatalogItem[AMOUNT_OF_QUICK_SLOTS];
 1536    internal AssetPromise_Texture[] quickBarShortcutsThumbnailPromises = new AssetPromise_Texture[AMOUNT_OF_QUICK_SLOTS]
 1537    internal int lastIndexDroped = -1;
 38
 39    public void Initialize(IQuickBarView quickBarView, IDragAndDropSceneObjectController dragAndDropController)
 40    {
 1441        this.quickBarView = quickBarView;
 1442        this.dragAndDropController = dragAndDropController;
 43
 1444        quickBarView.OnQuickBarObjectSelected += OnQuickBarObjectSelected;
 1445        quickBarView.OnSetIndexToDrop += SetIndexToDrop;
 1446        quickBarView.OnSceneObjectDroppedFromQuickBar += SceneObjectDroppedFromQuickBar;
 1447        quickBarView.OnSceneObjectDroppedFromCatalog += SceneObjectDroppedFromCatalog;
 1448        quickBarView.OnQuickBarInputTriggered += QuickBarInput;
 1449        dragAndDropController.OnStopInput += CancelDragging;
 1450    }
 51
 52    public void Dispose()
 53    {
 1454        if (quickBarView != null)
 55        {
 1356            quickBarView.OnQuickBarObjectSelected -= OnQuickBarObjectSelected;
 1357            quickBarView.OnSetIndexToDrop -= SetIndexToDrop;
 1358            quickBarView.OnSceneObjectDroppedFromQuickBar -= SceneObjectDroppedFromQuickBar;
 1359            quickBarView.OnSceneObjectDroppedFromCatalog -= SceneObjectDroppedFromCatalog;
 1360            quickBarView.OnQuickBarInputTriggered -= QuickBarInput;
 61        }
 1462        if(dragAndDropController != null)
 1363            dragAndDropController.OnStopInput -= CancelDragging;
 64
 28065        foreach (AssetPromise_Texture loadedThumbnailPromise in quickBarShortcutsThumbnailPromises)
 66        {
 12667            if (loadedThumbnailPromise == null)
 68                continue;
 69
 070            ClearThumbnailPromise(loadedThumbnailPromise);
 71        }
 1472    }
 73
 074    public int GetSlotsCount() { return AMOUNT_OF_QUICK_SLOTS; }
 75
 76    public CatalogItem QuickBarObjectSelected(int index)
 77    {
 1278        if (quickBarShortcutsCatalogItems.Length > index && quickBarShortcutsCatalogItems[index] != null)
 79        {
 1280            OnCatalogItemSelected?.Invoke(quickBarShortcutsCatalogItems[index]);
 1281            return quickBarShortcutsCatalogItems[index];
 82        }
 83
 084        return null;
 85    }
 86
 087    private void OnQuickBarObjectSelected(int obj) { QuickBarObjectSelected(obj); }
 88
 089    public void SetIndexToDrop(int index) { lastIndexDroped = index; }
 90
 91    public void SceneObjectDroppedFromQuickBar(int fromQuickBarIndex, int toQuickBarIndex, Texture texture)
 92    {
 193        SetQuickBarShortcut(quickBarShortcutsCatalogItems[fromQuickBarIndex], toQuickBarIndex, texture);
 194        MoveQuickbarThumbnailPromise(fromQuickBarIndex, toQuickBarIndex);
 195        RemoveQuickBarShortcut(fromQuickBarIndex);
 196    }
 97
 98    private void MoveQuickbarThumbnailPromise(int fromQuickBarIndex, int toQuickBarIndex)
 99    {
 1100        quickBarShortcutsThumbnailPromises[toQuickBarIndex] = quickBarShortcutsThumbnailPromises[fromQuickBarIndex];
 1101        quickBarShortcutsThumbnailPromises[fromQuickBarIndex] = null;
 1102    }
 103
 104    public void SceneObjectDroppedFromCatalog(BaseEventData data)
 105    {
 1106        CatalogItemAdapter adapter = dragAndDropController.GetLastAdapterDragged();
 107
 1108        if (adapter != null)
 0109            SetCatalogItemToShortcut(adapter.GetContent());
 1110    }
 111
 112    private void SetCatalogItemToShortcut(CatalogItem catalogItem)
 113    {
 0114        if (catalogItem == null)
 0115            return;
 116
 0117        var url = catalogItem.GetThumbnailUrl();
 118
 0119        if (string.IsNullOrEmpty(url))
 0120            return;
 121
 0122        ClearThumbnailPromise(quickBarShortcutsThumbnailPromises[lastIndexDroped]);
 0123        quickBarShortcutsThumbnailPromises[lastIndexDroped] = new AssetPromise_Texture(url);
 124
 0125        quickBarShortcutsThumbnailPromises[lastIndexDroped].OnSuccessEvent += x =>
 126        {
 0127            SetQuickBarShortcut(catalogItem, lastIndexDroped, x.texture);
 0128        };
 129
 0130        quickBarShortcutsThumbnailPromises[lastIndexDroped].OnFailEvent += (x, error) =>
 131        {
 0132            Debug.Log($"Error downloading: {url}, Exception: {error}");
 0133        };
 134
 0135        AssetPromiseKeeper_Texture.i.Keep(quickBarShortcutsThumbnailPromises[lastIndexDroped]);
 0136    }
 137
 138    private void ClearThumbnailPromise(AssetPromise_Texture thumbnailToClear)
 139    {
 1140        if (thumbnailToClear != null)
 141        {
 0142            thumbnailToClear.ClearEvents();
 0143            AssetPromiseKeeper_Texture.i.Forget(thumbnailToClear);
 0144            thumbnailToClear = null;
 145        }
 1146    }
 147
 148    private void RemoveQuickBarShortcut(int index)
 149    {
 1150        quickBarShortcutsCatalogItems[index] = null;
 1151        quickBarView.SetShortcutAsEmpty(index);
 1152        ClearThumbnailPromise(quickBarShortcutsThumbnailPromises[index]);
 1153    }
 154
 155    public void SetQuickBarShortcut(CatalogItem catalogItem, int index, Texture texture)
 156    {
 13157        quickBarShortcutsCatalogItems[index] = catalogItem;
 13158        quickBarView.SetTextureToShortcut(index, texture);
 13159        OnCatalogItemAssigned?.Invoke(catalogItem);
 0160    }
 161
 6162    public void QuickBarInput(int quickBarSlot) { OnQuickBarShortcutSelected?.Invoke(quickBarSlot); }
 163
 2164    public void CancelDragging() { quickBarView.CancelCurrentDragging(); }
 165}