106 lines
3.2 KiB
C#
106 lines
3.2 KiB
C#
![]() |
using System.Collections.Generic;
|
|||
|
using UnityEditor;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
|
|||
|
public abstract class DraggablePanelController : PanelController<KeyValuePair<int, Item>>, DraggablePanelInterface
|
|||
|
{
|
|||
|
[SerializeField] protected new List<ISlot> ChildBoxList = new List<ISlot>();
|
|||
|
|
|||
|
protected static UIWarehouseManager _uiManager;
|
|||
|
|
|||
|
protected new UIWarehouseManager UiManager
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_uiManager == null) { return FetchUiManager() as UIWarehouseManager; }
|
|||
|
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public ISlot SetupDragAndDropToSlot(ISlot slot)
|
|||
|
{
|
|||
|
slot.OnBeginDragEvent += BeginDrag;
|
|||
|
slot.OnEndDragEvent += EndDrag;
|
|||
|
slot.OnDragEvent += Drag;
|
|||
|
slot.OnDropEvent += Drop;
|
|||
|
|
|||
|
return slot;
|
|||
|
}
|
|||
|
|
|||
|
public void BeginDrag(ItemSlot itemSlot)
|
|||
|
{
|
|||
|
if (itemSlot.Item != null)
|
|||
|
{
|
|||
|
DraggedSlotController.Instance.CreateDraggedSlot(itemSlot, _panelContent.transform.position);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void Drag(ItemSlot itemSlot)
|
|||
|
{
|
|||
|
DraggedSlotController.Instance.UpdatePosition(Input.mousePosition);
|
|||
|
}
|
|||
|
|
|||
|
public void EndDrag(ItemSlot itemSlot)
|
|||
|
{
|
|||
|
if (!DraggedSlotController.Instance.IsDragged()) // if there was nothing dragged - ignore event
|
|||
|
return;
|
|||
|
|
|||
|
DraggedSlotController.Instance.RemoveDraggedSlot();
|
|||
|
|
|||
|
// Apply list of items
|
|||
|
for (int i = 0; i < ChildBoxList.Count; i++)
|
|||
|
{
|
|||
|
if (ChildBoxList[i].Item != null)
|
|||
|
{
|
|||
|
UiManager.Add(new KeyValuePair<int, Item>(i, (EquippableItem)ChildBoxList[i].Item));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
UiManager.RemoveByPosition(i);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public virtual void Drop(ItemSlot dropItemSlot)
|
|||
|
{
|
|||
|
if (!DraggedSlotController.Instance.IsDragged()) // if there was nothing dragged - ignore event
|
|||
|
return;
|
|||
|
|
|||
|
var draggedSlot = GetDraggedSlot();
|
|||
|
|
|||
|
if (dropItemSlot.CanReceiveItem(draggedSlot.Item) && draggedSlot.CanReceiveItem(dropItemSlot.Item))
|
|||
|
{
|
|||
|
EquippableItem dragItem = draggedSlot.Item as EquippableItem;
|
|||
|
EquippableItem dropItem = dropItemSlot.Item as EquippableItem;
|
|||
|
|
|||
|
// TODO insert child criterials
|
|||
|
|
|||
|
Item _tmpDraggedItem = draggedSlot.Item; // InventoryUIManager.Instance.DraggedSlot.Item; // remember temporary currently dragged item
|
|||
|
|
|||
|
|
|||
|
/*
|
|||
|
* not sure why but if you change order of assign, something will ovberride dropItemSlot.Item vale accordint to draggedSlot.Item = dropItemSlot.Item; value
|
|||
|
* BUG - not sure where
|
|||
|
*/
|
|||
|
draggedSlot.Item = dropItemSlot.Item;
|
|||
|
UiManager.Add(new KeyValuePair<int, Item>(draggedSlot.Number, (EquippableItem)draggedSlot.Item));
|
|||
|
|
|||
|
dropItemSlot.Item = _tmpDraggedItem;
|
|||
|
UiManager.Add(new KeyValuePair<int, Item>(dropItemSlot.Number, (EquippableItem)dropItemSlot.Item));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//public ISlot GetDraggedSlotFromManager() => InventoryUIManager.Instance.DraggedSlot;
|
|||
|
public ISlot GetDraggedSlot()
|
|||
|
{
|
|||
|
return DraggedSlotController.Instance.DraggedSlot;
|
|||
|
}
|
|||
|
|
|||
|
public void SetItemOnPosition()
|
|||
|
{
|
|||
|
throw new System.NotImplementedException();
|
|||
|
}
|
|||
|
}
|