< Summary

Class:DragAndDropSceneObjectController
Assembly:BuildModeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/GodMode/DragAndDropSceneObjectController.cs
Covered lines:34
Uncovered lines:8
Coverable lines:42
Total lines:111
Line coverage:80.9% (34 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Initialize(...)0%110100%
Dispose()0%6.975057.14%
Drop()0%220100%
CatalogItemDropped()0%220100%
GetLastAdapterDragged()0%2100%
AdapterStartDragging(...)0%22090%
OnDrag(...)0%2100%
MoveCopyAdapterToPosition(...)0%110100%
OnEndDrag(...)0%5.275077.78%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/GodMode/DragAndDropSceneObjectController.cs

#LineLine coverage
 1using System;
 2using UnityEngine;
 3using UnityEngine.EventSystems;
 4
 5public interface IDragAndDropSceneObjectController
 6{
 7    event Action<CatalogItem> OnCatalogItemDropped;
 8    event Action OnDrop;
 9    event Action OnResumeInput;
 10    event Action OnStopInput;
 11
 12    void Initialize(ISceneCatalogController catalogController, IDragAndDropSceneObjectView dragAndDropSceneObjectView);
 13    void Dispose();
 14    void Drop();
 15    CatalogItemAdapter GetLastAdapterDragged();
 16}
 17
 18public class DragAndDropSceneObjectController : IDragAndDropSceneObjectController
 19{
 20    public event Action OnResumeInput;
 21    public event Action OnStopInput;
 22    public event Action<CatalogItem> OnCatalogItemDropped;
 23    public event Action OnDrop;
 24
 25    private ISceneCatalogController sceneCatalogController;
 26
 27    private CatalogItemAdapter catalogItemAdapterDragged;
 28    internal CatalogItemAdapter catalogItemCopy;
 29    internal CatalogItem itemDroped;
 30    private IDragAndDropSceneObjectView dragAndDropSceneObjectView;
 31
 32    public void Initialize(ISceneCatalogController sceneCatalogController, IDragAndDropSceneObjectView dragAndDropSceneO
 33    {
 534        this.sceneCatalogController = sceneCatalogController;
 535        sceneCatalogController.OnCatalogItemStartDrag += AdapterStartDragging;
 36
 537        this.dragAndDropSceneObjectView = dragAndDropSceneObjectView;
 538        this.dragAndDropSceneObjectView.OnDrop += Drop;
 539    }
 40
 41    public void Dispose()
 42    {
 143        if(sceneCatalogController != null)
 044            sceneCatalogController.OnCatalogItemStartDrag -= AdapterStartDragging;
 45
 146        if(dragAndDropSceneObjectView != null)
 047            dragAndDropSceneObjectView.OnDrop -= Drop;
 48
 149        if (catalogItemCopy != null && catalogItemCopy.gameObject != null )
 050            GameObject.Destroy(catalogItemCopy.gameObject);
 151    }
 52
 53    public void Drop()
 54    {
 155        CatalogItemDropped();
 156        OnDrop?.Invoke();
 157    }
 58
 59    public void CatalogItemDropped()
 60    {
 261        if (catalogItemCopy == null)
 162            return;
 63
 64        // If an item has been dropped in the view , we assign it as itemDropped and wait for the OnEndDrag to process t
 165        CatalogItem catalogItem = catalogItemAdapterDragged.GetContent();
 166        itemDroped = catalogItem;
 167    }
 68
 069    public CatalogItemAdapter GetLastAdapterDragged() { return catalogItemAdapterDragged; }
 70
 71    internal void AdapterStartDragging(CatalogItem catalogItemClicked, CatalogItemAdapter adapter)
 72    {
 73        // We create a copy of the adapter that has been dragging to move with the mouse as feedback
 474        var catalogItemAdapterDraggedGameObject = GameObject.Instantiate(adapter.gameObject, dragAndDropSceneObjectView.
 475        catalogItemCopy = catalogItemAdapterDraggedGameObject.GetComponent<CatalogItemAdapter>();
 76
 477        RectTransform adapterRT = adapter.GetComponent<RectTransform>();
 478        catalogItemCopy.SetContent(adapter.GetContent());
 479        catalogItemCopy.EnableDragMode(adapterRT.sizeDelta);
 80
 81        // However, since we have starting the drag event in the original adapter,
 82        // We need to track the drag event in the original and apply the event to the copy
 483        adapter.OnAdapterDrag += OnDrag;
 484        adapter.OnAdapterEndDrag += OnEndDrag;
 485        catalogItemAdapterDragged = adapter;
 486        OnStopInput?.Invoke();
 087    }
 88
 089    internal void OnDrag(PointerEventData data) {  MoveCopyAdapterToPosition(data.position); }
 90
 491    internal void MoveCopyAdapterToPosition(Vector3 position) { catalogItemCopy.gameObject.transform.position = position
 92
 93    internal void OnEndDrag(PointerEventData data)
 94    {
 195        OnResumeInput?.Invoke();
 196        if (catalogItemAdapterDragged != null)
 97        {
 198            catalogItemAdapterDragged.OnAdapterDrag -= OnDrag;
 199            catalogItemAdapterDragged.OnAdapterEndDrag -= OnEndDrag;
 100        }
 1101        GameObject.Destroy(catalogItemCopy.gameObject);
 102
 103        // Note(Adrian): If a item has been dropped in the "drop view" we process it here since this event complete the 
 104        // If we don't wait for the full flow to finish, the OnCatalogItemDropped could refresh the catalog breaking the
 1105        if (itemDroped != null)
 106        {
 0107            OnCatalogItemDropped?.Invoke(itemDroped);
 0108            itemDroped = null;
 109        }
 1110    }
 111}