< Summary

Class:QuickBarView
Assembly:BuildModeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BuildModeHUD/Scripts/Common/QuickBarView.cs
Covered lines:74
Uncovered lines:32
Coverable lines:106
Total lines:244
Line coverage:69.8% (74 of 106)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
QuickBarView()0%110100%
Create()0%110100%
Awake()0%330100%
OnDestroy()0%330100%
QuickBarObjectSelected(...)0%220100%
SetIndexToBeginDrag(...)0%220100%
SetIndexToDrop(...)0%220100%
SceneObjectDroppedFromQuickBar(...)0%220100%
SceneObjectDroppedFromCatalog(...)0%220100%
SetTextureToShortcut(...)0%5.024060%
SetShortcutAsEmpty(...)0%6200%
OnQuickBar1InputTriggedered(...)0%2100%
OnQuickBar2InputTriggedered(...)0%2100%
OnQuickBar3InputTriggedered(...)0%2100%
OnQuickBar4InputTriggedered(...)0%2100%
OnQuickBar5InputTriggedered(...)0%2100%
OnQuickBar6InputTriggedered(...)0%2100%
OnQuickBar7InputTriggedered(...)0%2100%
OnQuickBar8InputTriggedered(...)0%2100%
OnQuickBar9InputTriggedered(...)0%2100%
OnQuickBarInputTriggedered(...)0%220100%
CreateSlotToDrag()0%4.054085.71%
BeginDragSlot(...)0%3.033085.71%
DragSlot(...)0%3.143075%
EndDragSlot(...)0%3.043083.33%
CancelCurrentDragging()0%220100%

File(s)

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

#LineLine coverage
 1using System;
 2using UnityEngine;
 3using UnityEngine.EventSystems;
 4using UnityEngine.UI;
 5
 6public interface IQuickBarView
 7{
 8    event Action<int> OnQuickBarInputTriggered;
 9    event Action<int> OnQuickBarObjectSelected;
 10    event Action<int, int, Texture> OnSceneObjectDroppedFromQuickBar;
 11    event Action<BaseEventData> OnSceneObjectDroppedFromCatalog;
 12    event Action<int> OnSetIndexToBeginDrag;
 13    event Action<int> OnSetIndexToDrop;
 14
 15    void OnQuickBarInputTriggedered(int index);
 16    void QuickBarObjectSelected(int index);
 17    void SceneObjectDroppedFromQuickBar(int fromIndex, int toIndex, Texture texture);
 18    void SceneObjectDroppedFromCatalog(BaseEventData data);
 19    void SetIndexToBeginDrag(int index);
 20    void SetIndexToDrop(int index);
 21    void SetTextureToShortcut(int shortcutIndex, Texture texture);
 22    void SetShortcutAsEmpty(int shortcutIndex);
 23    void BeginDragSlot(int triggerIndex);
 24    void DragSlot(BaseEventData eventData, int triggerIndex);
 25    void EndDragSlot(int triggerIndex);
 26    void CancelCurrentDragging();
 27
 28}
 29
 30public class QuickBarView : MonoBehaviour, IQuickBarView
 31{
 32    private const string VIEW_PATH = "Common/QuickBarView";
 33
 34    public event Action<int> OnQuickBarObjectSelected;
 35    public event Action<int> OnSetIndexToBeginDrag;
 36    public event Action<int> OnSetIndexToDrop;
 37    public event Action<int, int, Texture> OnSceneObjectDroppedFromQuickBar;
 38    public event Action<BaseEventData> OnSceneObjectDroppedFromCatalog;
 39    public event Action<int> OnQuickBarInputTriggered;
 40
 41    [SerializeField] internal Canvas generalCanvas;
 42    [SerializeField] internal QuickBarSlot[] shortcutsImgs;
 43    [SerializeField] internal Button[] shortcutsButtons;
 44    [SerializeField] internal EventTrigger[] shortcutsEventTriggers;
 45    [SerializeField] internal InputAction_Trigger quickBar1InputAction;
 46    [SerializeField] internal InputAction_Trigger quickBar2InputAction;
 47    [SerializeField] internal InputAction_Trigger quickBar3InputAction;
 48    [SerializeField] internal InputAction_Trigger quickBar4InputAction;
 49    [SerializeField] internal InputAction_Trigger quickBar5InputAction;
 50    [SerializeField] internal InputAction_Trigger quickBar6InputAction;
 51    [SerializeField] internal InputAction_Trigger quickBar7InputAction;
 52    [SerializeField] internal InputAction_Trigger quickBar8InputAction;
 53    [SerializeField] internal InputAction_Trigger quickBar9InputAction;
 54
 8555    internal int lastIndexToBeginDrag = -1;
 56    internal QuickBarSlot draggedSlot = null;
 57
 58    internal static QuickBarView Create()
 59    {
 1160        var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<QuickBarView>();
 1161        view.gameObject.name = "_QuickBarView";
 62
 1163        return view;
 64    }
 65
 66    private void Awake()
 67    {
 5668        CreateSlotToDrag();
 69
 112070        for (int i = 0; i < shortcutsButtons.Length; i++)
 71        {
 50472            int buttonIndex = i;
 50473            shortcutsButtons[buttonIndex].onClick.AddListener(() => QuickBarObjectSelected(buttonIndex));
 74        }
 75
 112076        for (int i = 0; i < shortcutsEventTriggers.Length; i++)
 77        {
 50478            int triggerIndex = i;
 79
 50480            BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.BeginDrag, (eventData)
 81            {
 082                BeginDragSlot(triggerIndex);
 083            });
 84
 50485            BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.Drag, (eventData) =>
 86            {
 087                DragSlot(eventData, triggerIndex);
 088            });
 89
 50490            BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.EndDrag, (eventData) =
 91            {
 092                EndDragSlot(triggerIndex);
 093            });
 94
 50495            BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.Drop, (eventData) =>
 96            {
 097                SetIndexToDrop(triggerIndex);
 98
 099                if (lastIndexToBeginDrag != -1)
 100                {
 0101                    SceneObjectDroppedFromQuickBar(lastIndexToBeginDrag, triggerIndex, shortcutsImgs[lastIndexToBeginDra
 0102                    CancelCurrentDragging();
 0103                }
 104                else
 105                {
 0106                    SceneObjectDroppedFromCatalog(eventData);
 107                }
 0108            });
 109        }
 110
 56111        quickBar1InputAction.OnTriggered += OnQuickBar1InputTriggedered;
 56112        quickBar2InputAction.OnTriggered += OnQuickBar2InputTriggedered;
 56113        quickBar3InputAction.OnTriggered += OnQuickBar3InputTriggedered;
 56114        quickBar4InputAction.OnTriggered += OnQuickBar4InputTriggedered;
 56115        quickBar5InputAction.OnTriggered += OnQuickBar5InputTriggedered;
 56116        quickBar6InputAction.OnTriggered += OnQuickBar6InputTriggedered;
 56117        quickBar7InputAction.OnTriggered += OnQuickBar7InputTriggedered;
 56118        quickBar8InputAction.OnTriggered += OnQuickBar8InputTriggedered;
 56119        quickBar9InputAction.OnTriggered += OnQuickBar9InputTriggedered;
 56120    }
 121
 122    private void OnDestroy()
 123    {
 1120124        for (int i = 0; i < shortcutsButtons.Length; i++)
 125        {
 504126            int buttonIndex = i;
 504127            shortcutsButtons[buttonIndex].onClick.RemoveAllListeners();
 128        }
 129
 1120130        for (int i = 0; i < shortcutsEventTriggers.Length; i++)
 131        {
 504132            int triggerIndex = i;
 504133            BIWUtils.RemoveEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.Drop);
 134        }
 135
 56136        quickBar1InputAction.OnTriggered -= OnQuickBar1InputTriggedered;
 56137        quickBar2InputAction.OnTriggered -= OnQuickBar2InputTriggedered;
 56138        quickBar3InputAction.OnTriggered -= OnQuickBar3InputTriggedered;
 56139        quickBar4InputAction.OnTriggered -= OnQuickBar4InputTriggedered;
 56140        quickBar5InputAction.OnTriggered -= OnQuickBar5InputTriggedered;
 56141        quickBar6InputAction.OnTriggered -= OnQuickBar6InputTriggedered;
 56142        quickBar7InputAction.OnTriggered -= OnQuickBar7InputTriggedered;
 56143        quickBar8InputAction.OnTriggered -= OnQuickBar8InputTriggedered;
 56144        quickBar9InputAction.OnTriggered -= OnQuickBar9InputTriggedered;
 56145    }
 146
 4147    public void QuickBarObjectSelected(int index) { OnQuickBarObjectSelected?.Invoke(index); }
 148
 3149    public void SetIndexToBeginDrag(int index) { OnSetIndexToBeginDrag?.Invoke(index); }
 150
 2151    public void SetIndexToDrop(int index) { OnSetIndexToDrop?.Invoke(index); }
 152
 2153    public void SceneObjectDroppedFromQuickBar(int fromIndex, int toIndex, Texture texture) { OnSceneObjectDroppedFromQu
 154
 2155    public void SceneObjectDroppedFromCatalog(BaseEventData data) { OnSceneObjectDroppedFromCatalog?.Invoke(data); }
 156
 157    public void SetTextureToShortcut(int shortcutIndex, Texture texture)
 158    {
 9159        if (shortcutIndex >= shortcutsImgs.Length)
 0160            return;
 161
 9162        if (shortcutsImgs[shortcutIndex] != null && texture != null)
 0163            shortcutsImgs[shortcutIndex].SetTexture(texture);
 9164    }
 165
 166    public void SetShortcutAsEmpty(int shortcutIndex)
 167    {
 0168        if (shortcutIndex >= shortcutsImgs.Length)
 0169            return;
 170
 0171        shortcutsImgs[shortcutIndex].SetEmpty();
 0172    }
 173
 0174    private void OnQuickBar1InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(0); }
 175
 0176    private void OnQuickBar2InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(1); }
 177
 0178    private void OnQuickBar3InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(2); }
 179
 0180    private void OnQuickBar4InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(3); }
 181
 0182    private void OnQuickBar5InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(4); }
 183
 0184    private void OnQuickBar6InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(5); }
 185
 0186    private void OnQuickBar7InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(6); }
 187
 0188    private void OnQuickBar8InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(7); }
 189
 0190    private void OnQuickBar9InputTriggedered(DCLAction_Trigger action) { OnQuickBarInputTriggedered(8); }
 191
 2192    public void OnQuickBarInputTriggedered(int index) { OnQuickBarInputTriggered?.Invoke(index); }
 193
 194    internal void CreateSlotToDrag()
 195    {
 56196        if (shortcutsImgs.Length == 0)
 0197            return;
 198
 56199        draggedSlot = Instantiate(shortcutsImgs[0], generalCanvas != null ? generalCanvas.transform : null);
 56200        draggedSlot.EnableDragMode();
 56201        draggedSlot.SetEmpty();
 56202        draggedSlot.SetActive(false);
 56203    }
 204
 205    public void BeginDragSlot(int triggerIndex)
 206    {
 1207        if (draggedSlot == null || shortcutsImgs[triggerIndex].isEmpty)
 0208            return;
 209
 1210        lastIndexToBeginDrag = triggerIndex;
 1211        SetIndexToBeginDrag(triggerIndex);
 1212        draggedSlot.SetActive(true);
 1213        draggedSlot.SetTexture(shortcutsImgs[triggerIndex].image.texture);
 1214    }
 215
 216    public void DragSlot(BaseEventData eventData, int triggerIndex)
 217    {
 1218        if (draggedSlot == null || shortcutsImgs[triggerIndex].isEmpty)
 0219            return;
 220
 1221        draggedSlot.slotTransform.position = ((PointerEventData)eventData).position;
 1222    }
 223
 224    public void EndDragSlot(int triggerIndex)
 225    {
 1226        if (draggedSlot == null || shortcutsImgs[triggerIndex].isEmpty)
 0227            return;
 228
 1229        draggedSlot.SetEmpty();
 1230        draggedSlot.SetActive(false);
 1231        QuickBarObjectSelected(triggerIndex);
 1232    }
 233
 234    public void CancelCurrentDragging()
 235    {
 1236        lastIndexToBeginDrag = -1;
 237
 1238        if (draggedSlot != null)
 239        {
 1240            draggedSlot.SetEmpty();
 1241            draggedSlot.SetActive(false);
 242        }
 1243    }
 244}