| | 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 | | { |
| 0 | 43 | | sceneCatalogController.OnCatalogItemStartDrag -= AdapterStartDragging; |
| 0 | 44 | | dragAndDropSceneObjectView.OnDrop -= Drop; |
| 0 | 45 | | if (catalogItemCopy != null && catalogItemCopy.gameObject != null ) |
| 0 | 46 | | GameObject.Destroy(catalogItemCopy.gameObject); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public void Drop() |
| | 50 | | { |
| 1 | 51 | | CatalogItemDropped(); |
| 1 | 52 | | OnDrop?.Invoke(); |
| 1 | 53 | | } |
| | 54 | |
|
| | 55 | | public void CatalogItemDropped() |
| | 56 | | { |
| 2 | 57 | | if (catalogItemCopy == null) |
| 1 | 58 | | 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 |
| 1 | 61 | | CatalogItem catalogItem = catalogItemAdapterDragged.GetContent(); |
| 1 | 62 | | itemDroped = catalogItem; |
| 1 | 63 | | } |
| | 64 | |
|
| 0 | 65 | | 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 |
| 4 | 70 | | var catalogItemAdapterDraggedGameObject = GameObject.Instantiate(adapter.gameObject, dragAndDropSceneObjectView. |
| 4 | 71 | | catalogItemCopy = catalogItemAdapterDraggedGameObject.GetComponent<CatalogItemAdapter>(); |
| | 72 | |
|
| 4 | 73 | | RectTransform adapterRT = adapter.GetComponent<RectTransform>(); |
| 4 | 74 | | catalogItemCopy.SetContent(adapter.GetContent()); |
| 4 | 75 | | 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 |
| 4 | 79 | | adapter.OnAdapterDrag += OnDrag; |
| 4 | 80 | | adapter.OnAdapterEndDrag += OnEndDrag; |
| 4 | 81 | | catalogItemAdapterDragged = adapter; |
| 4 | 82 | | OnStopInput?.Invoke(); |
| 0 | 83 | | } |
| | 84 | |
|
| 0 | 85 | | internal void OnDrag(PointerEventData data) { MoveCopyAdapterToPosition(data.position); } |
| | 86 | |
|
| 4 | 87 | | internal void MoveCopyAdapterToPosition(Vector3 position) { catalogItemCopy.gameObject.transform.position = position |
| | 88 | |
|
| | 89 | | internal void OnEndDrag(PointerEventData data) |
| | 90 | | { |
| 1 | 91 | | OnResumeInput?.Invoke(); |
| 1 | 92 | | if (catalogItemAdapterDragged != null) |
| | 93 | | { |
| 1 | 94 | | catalogItemAdapterDragged.OnAdapterDrag -= OnDrag; |
| 1 | 95 | | catalogItemAdapterDragged.OnAdapterEndDrag -= OnEndDrag; |
| | 96 | | } |
| 1 | 97 | | 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 |
| 1 | 101 | | if (itemDroped != null) |
| | 102 | | { |
| 0 | 103 | | OnCatalogItemDropped?.Invoke(itemDroped); |
| 0 | 104 | | itemDroped = null; |
| | 105 | | } |
| 1 | 106 | | } |
| | 107 | | } |