< Summary

Class:QuickBarView
Assembly:BuildModeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/Common/QuickBarView.cs
Covered lines:76
Uncovered lines:32
Coverable lines:108
Total lines:248
Line coverage:70.3% (76 of 108)
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%440100%
QuickBarObjectSelected(...)0%220100%
SetIndexToBeginDrag(...)0%220100%
SetIndexToDrop(...)0%220100%
SceneObjectDroppedFromQuickBar(...)0%220100%
SceneObjectDroppedFromCatalog(...)0%220100%
SetTextureToShortcut(...)0%5.024060%
SetShortcutAsEmpty(...)0%6200%
OnQuickBar1InputTriggered(...)0%2100%
OnQuickBar2InputTriggered(...)0%2100%
OnQuickBar3InputTriggered(...)0%2100%
OnQuickBar4InputTriggered(...)0%2100%
OnQuickBar5InputTriggered(...)0%2100%
OnQuickBar6InputTriggered(...)0%2100%
OnQuickBar7InputTriggered(...)0%2100%
OnQuickBar8InputTriggered(...)0%2100%
OnQuickBar9InputTriggered(...)0%2100%
RaiseQuickBarInputTriggered(...)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/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/Common/QuickBarView.cs

#LineLine coverage
 1using System;
 2using UnityEngine;
 3using UnityEngine.EventSystems;
 4using UnityEngine.UI;
 5using Object = UnityEngine.Object;
 6
 7public interface IQuickBarView
 8{
 9    event Action<int> OnQuickBarInputTriggered;
 10    event Action<int> OnQuickBarObjectSelected;
 11    event Action<int, int, Texture> OnSceneObjectDroppedFromQuickBar;
 12    event Action<BaseEventData> OnSceneObjectDroppedFromCatalog;
 13    event Action<int> OnSetIndexToBeginDrag;
 14    event Action<int> OnSetIndexToDrop;
 15
 16    void RaiseQuickBarInputTriggered(int index);
 17    void QuickBarObjectSelected(int index);
 18    void SceneObjectDroppedFromQuickBar(int fromIndex, int toIndex, Texture texture);
 19    void SceneObjectDroppedFromCatalog(BaseEventData data);
 20    void SetIndexToBeginDrag(int index);
 21    void SetIndexToDrop(int index);
 22    void SetTextureToShortcut(int shortcutIndex, Texture texture);
 23    void SetShortcutAsEmpty(int shortcutIndex);
 24    void BeginDragSlot(int triggerIndex);
 25    void DragSlot(BaseEventData eventData, int triggerIndex);
 26    void EndDragSlot(int triggerIndex);
 27    void CancelCurrentDragging();
 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
 3255    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    {
 3068        CreateSlotToDrag();
 69
 60070        for (int i = 0; i < shortcutsButtons.Length; i++)
 71        {
 27072            int buttonIndex = i;
 27073            shortcutsButtons[buttonIndex].onClick.AddListener(() => QuickBarObjectSelected(buttonIndex));
 74        }
 75
 60076        for (int i = 0; i < shortcutsEventTriggers.Length; i++)
 77        {
 27078            int triggerIndex = i;
 79
 27080            BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.BeginDrag, (eventData)
 81            {
 082                BeginDragSlot(triggerIndex);
 083            });
 84
 27085            BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.Drag, (eventData) =>
 86            {
 087                DragSlot(eventData, triggerIndex);
 088            });
 89
 27090            BIWUtils.ConfigureEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.EndDrag, (eventData) =
 91            {
 092                EndDragSlot(triggerIndex);
 093            });
 94
 27095            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
 30111        quickBar1InputAction.OnTriggered += OnQuickBar1InputTriggered;
 30112        quickBar2InputAction.OnTriggered += OnQuickBar2InputTriggered;
 30113        quickBar3InputAction.OnTriggered += OnQuickBar3InputTriggered;
 30114        quickBar4InputAction.OnTriggered += OnQuickBar4InputTriggered;
 30115        quickBar5InputAction.OnTriggered += OnQuickBar5InputTriggered;
 30116        quickBar6InputAction.OnTriggered += OnQuickBar6InputTriggered;
 30117        quickBar7InputAction.OnTriggered += OnQuickBar7InputTriggered;
 30118        quickBar8InputAction.OnTriggered += OnQuickBar8InputTriggered;
 30119        quickBar9InputAction.OnTriggered += OnQuickBar9InputTriggered;
 30120    }
 121
 122    private void OnDestroy()
 123    {
 600124        for (int i = 0; i < shortcutsButtons.Length; i++)
 125        {
 270126            int buttonIndex = i;
 270127            shortcutsButtons[buttonIndex].onClick.RemoveAllListeners();
 128        }
 129
 600130        for (int i = 0; i < shortcutsEventTriggers.Length; i++)
 131        {
 270132            int triggerIndex = i;
 270133            BIWUtils.RemoveEventTrigger(shortcutsEventTriggers[triggerIndex], EventTriggerType.Drop);
 134        }
 135
 136        // TODO(Brian): We should use an array here
 30137        quickBar1InputAction.OnTriggered -= OnQuickBar1InputTriggered;
 30138        quickBar2InputAction.OnTriggered -= OnQuickBar2InputTriggered;
 30139        quickBar3InputAction.OnTriggered -= OnQuickBar3InputTriggered;
 30140        quickBar4InputAction.OnTriggered -= OnQuickBar4InputTriggered;
 30141        quickBar5InputAction.OnTriggered -= OnQuickBar5InputTriggered;
 30142        quickBar6InputAction.OnTriggered -= OnQuickBar6InputTriggered;
 30143        quickBar7InputAction.OnTriggered -= OnQuickBar7InputTriggered;
 30144        quickBar8InputAction.OnTriggered -= OnQuickBar8InputTriggered;
 30145        quickBar9InputAction.OnTriggered -= OnQuickBar9InputTriggered;
 146
 30147        if (draggedSlot != null)
 29148            Object.Destroy(draggedSlot.gameObject);
 30149    }
 150
 4151    public void QuickBarObjectSelected(int index) { OnQuickBarObjectSelected?.Invoke(index); }
 152
 3153    public void SetIndexToBeginDrag(int index) { OnSetIndexToBeginDrag?.Invoke(index); }
 154
 2155    public void SetIndexToDrop(int index) { OnSetIndexToDrop?.Invoke(index); }
 156
 2157    public void SceneObjectDroppedFromQuickBar(int fromIndex, int toIndex, Texture texture) { OnSceneObjectDroppedFromQu
 158
 2159    public void SceneObjectDroppedFromCatalog(BaseEventData data) { OnSceneObjectDroppedFromCatalog?.Invoke(data); }
 160
 161    public void SetTextureToShortcut(int shortcutIndex, Texture texture)
 162    {
 9163        if (shortcutIndex >= shortcutsImgs.Length)
 0164            return;
 165
 9166        if (shortcutsImgs[shortcutIndex] != null && texture != null)
 0167            shortcutsImgs[shortcutIndex].SetTexture(texture);
 9168    }
 169
 170    public void SetShortcutAsEmpty(int shortcutIndex)
 171    {
 0172        if (shortcutIndex >= shortcutsImgs.Length)
 0173            return;
 174
 0175        shortcutsImgs[shortcutIndex].SetEmpty();
 0176    }
 177
 0178    private void OnQuickBar1InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(0); }
 179
 0180    private void OnQuickBar2InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(1); }
 181
 0182    private void OnQuickBar3InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(2); }
 183
 0184    private void OnQuickBar4InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(3); }
 185
 0186    private void OnQuickBar5InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(4); }
 187
 0188    private void OnQuickBar6InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(5); }
 189
 0190    private void OnQuickBar7InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(6); }
 191
 0192    private void OnQuickBar8InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(7); }
 193
 0194    private void OnQuickBar9InputTriggered(DCLAction_Trigger action) { RaiseQuickBarInputTriggered(8); }
 195
 2196    public void RaiseQuickBarInputTriggered(int index) { OnQuickBarInputTriggered?.Invoke(index); }
 197
 198    internal void CreateSlotToDrag()
 199    {
 30200        if (shortcutsImgs.Length == 0)
 0201            return;
 202
 30203        draggedSlot = Instantiate(shortcutsImgs[0], generalCanvas != null ? generalCanvas.transform : null);
 30204        draggedSlot.EnableDragMode();
 30205        draggedSlot.SetEmpty();
 30206        draggedSlot.SetActive(false);
 30207    }
 208
 209    public void BeginDragSlot(int triggerIndex)
 210    {
 1211        if (draggedSlot == null || shortcutsImgs[triggerIndex].isEmpty)
 0212            return;
 213
 1214        lastIndexToBeginDrag = triggerIndex;
 1215        SetIndexToBeginDrag(triggerIndex);
 1216        draggedSlot.SetActive(true);
 1217        draggedSlot.SetTexture(shortcutsImgs[triggerIndex].image.texture);
 1218    }
 219
 220    public void DragSlot(BaseEventData eventData, int triggerIndex)
 221    {
 1222        if (draggedSlot == null || shortcutsImgs[triggerIndex].isEmpty)
 0223            return;
 224
 1225        draggedSlot.slotTransform.position = ((PointerEventData)eventData).position;
 1226    }
 227
 228    public void EndDragSlot(int triggerIndex)
 229    {
 1230        if (draggedSlot == null || shortcutsImgs[triggerIndex].isEmpty)
 0231            return;
 232
 1233        draggedSlot.SetEmpty();
 1234        draggedSlot.SetActive(false);
 1235        QuickBarObjectSelected(triggerIndex);
 1236    }
 237
 238    public void CancelCurrentDragging()
 239    {
 1240        lastIndexToBeginDrag = -1;
 241
 1242        if (draggedSlot != null)
 243        {
 1244            draggedSlot.SetEmpty();
 1245            draggedSlot.SetActive(false);
 246        }
 1247    }
 248}