| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.EventSystems; |
| | 4 | |
|
| | 5 | | public 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 | |
|
| | 18 | | public 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 | | { |
| 5 | 34 | | this.sceneCatalogController = sceneCatalogController; |
| 5 | 35 | | sceneCatalogController.OnCatalogItemStartDrag += AdapterStartDragging; |
| | 36 | |
|
| 5 | 37 | | this.dragAndDropSceneObjectView = dragAndDropSceneObjectView; |
| 5 | 38 | | this.dragAndDropSceneObjectView.OnDrop += Drop; |
| 5 | 39 | | } |
| | 40 | |
|
| | 41 | | public void Dispose() |
| | 42 | | { |
| 1 | 43 | | if(sceneCatalogController != null) |
| 0 | 44 | | sceneCatalogController.OnCatalogItemStartDrag -= AdapterStartDragging; |
| | 45 | |
|
| 1 | 46 | | if(dragAndDropSceneObjectView != null) |
| 0 | 47 | | dragAndDropSceneObjectView.OnDrop -= Drop; |
| | 48 | |
|
| 1 | 49 | | if (catalogItemCopy != null && catalogItemCopy.gameObject != null ) |
| 0 | 50 | | GameObject.Destroy(catalogItemCopy.gameObject); |
| 1 | 51 | | } |
| | 52 | |
|
| | 53 | | public void Drop() |
| | 54 | | { |
| 1 | 55 | | CatalogItemDropped(); |
| 1 | 56 | | OnDrop?.Invoke(); |
| 1 | 57 | | } |
| | 58 | |
|
| | 59 | | public void CatalogItemDropped() |
| | 60 | | { |
| 2 | 61 | | if (catalogItemCopy == null) |
| 1 | 62 | | 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 |
| 1 | 65 | | CatalogItem catalogItem = catalogItemAdapterDragged.GetContent(); |
| 1 | 66 | | itemDroped = catalogItem; |
| 1 | 67 | | } |
| | 68 | |
|
| 0 | 69 | | 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 |
| 4 | 74 | | var catalogItemAdapterDraggedGameObject = GameObject.Instantiate(adapter.gameObject, dragAndDropSceneObjectView. |
| 4 | 75 | | catalogItemCopy = catalogItemAdapterDraggedGameObject.GetComponent<CatalogItemAdapter>(); |
| | 76 | |
|
| 4 | 77 | | RectTransform adapterRT = adapter.GetComponent<RectTransform>(); |
| 4 | 78 | | catalogItemCopy.SetContent(adapter.GetContent()); |
| 4 | 79 | | 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 |
| 4 | 83 | | adapter.OnAdapterDrag += OnDrag; |
| 4 | 84 | | adapter.OnAdapterEndDrag += OnEndDrag; |
| 4 | 85 | | catalogItemAdapterDragged = adapter; |
| 4 | 86 | | OnStopInput?.Invoke(); |
| 0 | 87 | | } |
| | 88 | |
|
| 0 | 89 | | internal void OnDrag(PointerEventData data) { MoveCopyAdapterToPosition(data.position); } |
| | 90 | |
|
| 4 | 91 | | internal void MoveCopyAdapterToPosition(Vector3 position) { catalogItemCopy.gameObject.transform.position = position |
| | 92 | |
|
| | 93 | | internal void OnEndDrag(PointerEventData data) |
| | 94 | | { |
| 1 | 95 | | OnResumeInput?.Invoke(); |
| 1 | 96 | | if (catalogItemAdapterDragged != null) |
| | 97 | | { |
| 1 | 98 | | catalogItemAdapterDragged.OnAdapterDrag -= OnDrag; |
| 1 | 99 | | catalogItemAdapterDragged.OnAdapterEndDrag -= OnEndDrag; |
| | 100 | | } |
| 1 | 101 | | 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 |
| 1 | 105 | | if (itemDroped != null) |
| | 106 | | { |
| 0 | 107 | | OnCatalogItemDropped?.Invoke(itemDroped); |
| 0 | 108 | | itemDroped = null; |
| | 109 | | } |
| 1 | 110 | | } |
| | 111 | | } |