2022-11-06 21:34:17 +01:00
|
|
|
|
using System.Collections.Generic;
|
2022-11-19 17:02:31 +01:00
|
|
|
|
using System.Linq;
|
2022-11-06 21:34:17 +01:00
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
2022-12-19 03:34:45 +01:00
|
|
|
|
using UnityEngine.EventSystems;
|
2022-11-06 21:34:17 +01:00
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
2022-11-24 03:03:30 +01:00
|
|
|
|
public abstract class DraggablePanelController : PanelController<IndexValuePair<int, EquippableItem>>, DraggablePanelInterface
|
2022-11-06 21:34:17 +01:00
|
|
|
|
{
|
2022-12-22 03:38:00 +01:00
|
|
|
|
protected PanelTypeEnum Type = PanelTypeEnum.None;
|
|
|
|
|
|
2022-12-19 03:34:45 +01:00
|
|
|
|
public bool AllowToUseItemInPanel = true;
|
|
|
|
|
|
2022-11-06 21:34:17 +01:00
|
|
|
|
[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;
|
2022-12-19 03:34:45 +01:00
|
|
|
|
slot.OnPointerClickEvent += MouseClick;
|
2022-11-06 21:34:17 +01:00
|
|
|
|
|
|
|
|
|
return slot;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 03:34:45 +01:00
|
|
|
|
#region drag & drop handler
|
|
|
|
|
public virtual void BeginDrag(ItemSlot itemSlot)
|
2022-11-06 21:34:17 +01:00
|
|
|
|
{
|
|
|
|
|
if (itemSlot.Item != null)
|
|
|
|
|
{
|
|
|
|
|
DraggedSlotController.Instance.CreateDraggedSlot(itemSlot, _panelContent.transform.position);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 03:34:45 +01:00
|
|
|
|
public virtual void Drag(ItemSlot itemSlot)
|
2022-11-06 21:34:17 +01:00
|
|
|
|
{
|
|
|
|
|
DraggedSlotController.Instance.UpdatePosition(Input.mousePosition);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 17:02:31 +01:00
|
|
|
|
public virtual void EndDrag(ItemSlot itemSlot)
|
2022-11-06 21:34:17 +01:00
|
|
|
|
{
|
|
|
|
|
if (!DraggedSlotController.Instance.IsDragged()) // if there was nothing dragged - ignore event
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
DraggedSlotController.Instance.RemoveDraggedSlot();
|
2022-11-19 17:02:31 +01:00
|
|
|
|
|
|
|
|
|
// Rebuild/apply UiManager content (list of items) base on slots values after its updating
|
2022-11-06 21:34:17 +01:00
|
|
|
|
for (int i = 0; i < ChildBoxList.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (ChildBoxList[i].Item != null)
|
|
|
|
|
{
|
2022-11-24 03:03:30 +01:00
|
|
|
|
UiManager.Add(new IndexValuePair<int, EquippableItem>(i, ChildBoxList[i].Item));
|
2022-11-06 21:34:17 +01:00
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
2022-11-19 17:02:31 +01:00
|
|
|
|
//draggedSlot.Item = (EquippableItem)new Item("test", "test desc", 1, null, null);
|
2022-11-06 21:34:17 +01:00
|
|
|
|
|
2022-11-24 03:03:30 +01:00
|
|
|
|
EquippableItem tmpDraggedItem = draggedSlot.Item == null ? null : new EquippableItem(draggedSlot.Item); // InventoryUIManager.Instance.DraggedSlot.Item; // remember temporary currently dragged item
|
|
|
|
|
UiManager.Add(new IndexValuePair<int, EquippableItem>(dropItemSlot.Number, tmpDraggedItem));
|
2022-11-27 21:28:55 +01:00
|
|
|
|
|
2022-11-19 17:02:31 +01:00
|
|
|
|
// !!! remember item from dropped position to future operation !!!
|
|
|
|
|
if (dropItem)
|
|
|
|
|
DraggedSlotController.Instance.UpdateItem(new EquippableItem(dropItem));
|
|
|
|
|
else
|
|
|
|
|
DraggedSlotController.Instance.UpdateItem(null);
|
2022-11-06 21:34:17 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-19 03:34:45 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public void MouseClick(ItemSlot itemSlot, PointerEventData eventData)
|
|
|
|
|
{
|
|
|
|
|
if (eventData.button == PointerEventData.InputButton.Right && eventData.clickCount == 1)
|
|
|
|
|
{
|
|
|
|
|
SingleRightMouseClick(itemSlot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (eventData.button == PointerEventData.InputButton.Right && eventData.clickCount == 2)
|
|
|
|
|
{
|
|
|
|
|
DoubleRightMouseClick(itemSlot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (eventData.button == PointerEventData.InputButton.Left && eventData.clickCount == 1)
|
|
|
|
|
{
|
|
|
|
|
SingleLeftMouseClick(itemSlot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (eventData.button == PointerEventData.InputButton.Left && eventData.clickCount == 2)
|
|
|
|
|
{
|
|
|
|
|
DoubleLeftMouseClick(itemSlot);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region mouse handler
|
|
|
|
|
public virtual void SingleRightMouseClick(ItemSlot itemSlot)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void DoubleRightMouseClick(ItemSlot itemSlot)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public virtual void SingleLeftMouseClick(ItemSlot itemSlot)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public virtual void DoubleLeftMouseClick(ItemSlot itemSlot)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2022-11-06 21:34:17 +01:00
|
|
|
|
|
|
|
|
|
public ISlot GetDraggedSlot()
|
|
|
|
|
{
|
|
|
|
|
return DraggedSlotController.Instance.DraggedSlot;
|
|
|
|
|
}
|
|
|
|
|
}
|