< Summary

Class:DragAndDropSceneObjectController
Assembly:BuildModeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/GodMode/DragAndDropSceneObjectController.cs
Covered lines:30
Uncovered lines:10
Coverable lines:40
Total lines:107
Line coverage:75% (30 of 40)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Initialize(...)0%110100%
Dispose()0%12300%
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    {
 043        sceneCatalogController.OnCatalogItemStartDrag -= AdapterStartDragging;
 044        dragAndDropSceneObjectView.OnDrop -= Drop;
 045        if (catalogItemCopy != null && catalogItemCopy.gameObject != null )
 046            GameObject.Destroy(catalogItemCopy.gameObject);
 047    }
 48
 49    public void Drop()
 50    {
 151        CatalogItemDropped();
 152        OnDrop?.Invoke();
 153    }
 54
 55    public void CatalogItemDropped()
 56    {
 257        if (catalogItemCopy == null)
 158            return;
 59
 60        // If an item has been dropped in the view , we assign it as itemDropped and wait for the OnEndDrag to process t
 161        CatalogItem catalogItem = catalogItemAdapterDragged.GetContent();
 162        itemDroped = catalogItem;
 163    }
 64
 065    public CatalogItemAdapter GetLastAdapterDragged() { return catalogItemAdapterDragged; }
 66
 67    internal void AdapterStartDragging(CatalogItem catalogItemClicked, CatalogItemAdapter adapter)
 68    {
 69        // We create a copy of the adapter that has been dragging to move with the mouse as feedback
 470        var catalogItemAdapterDraggedGameObject = GameObject.Instantiate(adapter.gameObject, dragAndDropSceneObjectView.
 471        catalogItemCopy = catalogItemAdapterDraggedGameObject.GetComponent<CatalogItemAdapter>();
 72
 473        RectTransform adapterRT = adapter.GetComponent<RectTransform>();
 474        catalogItemCopy.SetContent(adapter.GetContent());
 475        catalogItemCopy.EnableDragMode(adapterRT.sizeDelta);
 76
 77        // However, since we have starting the drag event in the original adapter,
 78        // We need to track the drag event in the original and apply the event to the copy
 479        adapter.OnAdapterDrag += OnDrag;
 480        adapter.OnAdapterEndDrag += OnEndDrag;
 481        catalogItemAdapterDragged = adapter;
 482        OnStopInput?.Invoke();
 083    }
 84
 085    internal void OnDrag(PointerEventData data) {  MoveCopyAdapterToPosition(data.position); }
 86
 487    internal void MoveCopyAdapterToPosition(Vector3 position) { catalogItemCopy.gameObject.transform.position = position
 88
 89    internal void OnEndDrag(PointerEventData data)
 90    {
 191        OnResumeInput?.Invoke();
 192        if (catalogItemAdapterDragged != null)
 93        {
 194            catalogItemAdapterDragged.OnAdapterDrag -= OnDrag;
 195            catalogItemAdapterDragged.OnAdapterEndDrag -= OnEndDrag;
 96        }
 197        GameObject.Destroy(catalogItemCopy.gameObject);
 98
 99        // Note(Adrian): If a item has been dropped in the "drop view" we process it here since this event complete the 
 100        // If we don't wait for the full flow to finish, the OnCatalogItemDropped could refresh the catalog breaking the
 1101        if (itemDroped != null)
 102        {
 0103            OnCatalogItemDropped?.Invoke(itemDroped);
 0104            itemDroped = null;
 105        }
 1106    }
 107}