2022-10-19 13:51:46 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
using System;
|
|
|
|
using static SimpleInventorySystem.GridUI;
|
|
|
|
|
|
|
|
namespace SimpleInventorySystem
|
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
public class SlotUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
|
|
|
public int width;
|
|
|
|
public int height;
|
|
|
|
public bool useImageAsSlotHolder;
|
|
|
|
public Image image;
|
|
|
|
public GridUI grid;
|
|
|
|
public GridUI.Cell cell;
|
2022-10-29 19:56:00 +00:00
|
|
|
public bool CellInUse;
|
2022-10-19 13:51:46 +00:00
|
|
|
public ItemUI itemUI;
|
|
|
|
[HideInInspector] public GroupSlotUI groupSlot;
|
|
|
|
private AppendUI appendUI;
|
2022-10-29 19:56:00 +00:00
|
|
|
[SerializeField] public int cellX;
|
|
|
|
[SerializeField] public int cellY;
|
|
|
|
public bool EquipSlot;
|
|
|
|
public List<SlotUI> LinkedSlots = new List<SlotUI>();
|
|
|
|
private LinkedSlotUI linkedSlotUI;
|
|
|
|
public bool PointerIsHoveredOver;
|
|
|
|
private bool droppedStackOntoEmptySlot;
|
2022-10-19 13:51:46 +00:00
|
|
|
|
|
|
|
internal ItemUI GetItemUI()
|
|
|
|
{
|
|
|
|
return itemUI;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float imageScale = 1f;
|
|
|
|
public List<ItemTags> AllowedItemTags;
|
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
/*public void OnDrop(PointerEventData eventData)
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
|
|
|
//
|
|
|
|
//("OnDrop");
|
|
|
|
if (eventData.pointerDrag != null && AllowedDraggable(eventData.pointerDrag))
|
|
|
|
{
|
|
|
|
DropOntoSlot(eventData.pointerDrag);
|
|
|
|
}
|
2022-10-29 19:56:00 +00:00
|
|
|
}*/
|
2022-10-19 13:51:46 +00:00
|
|
|
|
|
|
|
public bool AllowedDraggable(GameObject gameObject)
|
|
|
|
{
|
|
|
|
return gameObject.GetComponent<ItemUI>() != null;
|
|
|
|
}
|
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
public bool IsLinkedSlot()
|
|
|
|
{
|
|
|
|
return linkedSlotUI != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool DropOntoSlot(GameObject item, bool dropStack = true)
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
|
|
|
ItemUI itemDrop = item.GetComponent<ItemUI>();
|
2022-10-29 19:56:00 +00:00
|
|
|
|
|
|
|
if (IsLinkedSlot())
|
|
|
|
{
|
|
|
|
GameObject newItemDrop = Instantiate(item);
|
|
|
|
itemDrop = newItemDrop.GetComponent<ItemUI>();
|
|
|
|
itemDrop.IsLinkedSlotClone = true;
|
|
|
|
}
|
|
|
|
|
2022-10-19 13:51:46 +00:00
|
|
|
List<Cell> occupiedCells = new List<Cell>();
|
2022-10-29 19:56:00 +00:00
|
|
|
|
|
|
|
if (!itemDrop.IsLinkedSlotClone && ((HasStack() && itemUI != null && itemUI.itemPrefab == itemDrop.itemPrefab) || !dropStack))
|
|
|
|
{
|
|
|
|
Debug.Log("Handling Stack");
|
|
|
|
// TODO: HANDLE STACKING HERE
|
|
|
|
int maxStackDrop = 1;
|
|
|
|
if (dropStack)
|
|
|
|
{
|
|
|
|
maxStackDrop = itemDrop.Count;
|
|
|
|
if (maxStackDrop > itemUI.StackLimit - itemUI.Count)
|
|
|
|
{
|
|
|
|
maxStackDrop = itemDrop.Count - itemUI.Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(itemUI != null)
|
|
|
|
{
|
|
|
|
if (itemUI.AddToStack(maxStackDrop))
|
|
|
|
{
|
|
|
|
Debug.Log("Add to stack");
|
|
|
|
itemDrop.RemoveFromStack(maxStackDrop);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GameObject GO = Instantiate(item, transform, false);
|
|
|
|
GO.transform.localPosition = new Vector3(0, 0, 0);
|
|
|
|
ItemUI oldItemDrop = itemDrop;
|
|
|
|
itemDrop = GO.GetComponent<ItemUI>();
|
|
|
|
if (itemDrop.AddToStack(maxStackDrop))
|
|
|
|
{
|
|
|
|
oldItemDrop.RemoveFromStack(maxStackDrop);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dropCheck = (grid != null && grid.CanMoveInCells(itemDrop, this, occupiedCells) || (grid == null && !cell.inUse));
|
2022-10-19 13:51:46 +00:00
|
|
|
|
|
|
|
ItemTags itemTag = itemDrop.ItemTag;
|
|
|
|
|
|
|
|
bool allowed = AllowedItemTags == null || AllowedItemTags.Count == 0 || AllowedItemTags.Contains(ItemTags.Any) || AllowedItemTags.Contains(itemTag);
|
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
/*Debug.Log("GridUI: DropOntoSlot(): " + gameObject.name + ": " + "dropCheck: " + dropCheck);
|
|
|
|
Debug.Log("GridUI: DropOntoSlot(): " + gameObject.name + ": " + "allowed: " + allowed);*/
|
2022-10-19 13:51:46 +00:00
|
|
|
|
|
|
|
if (dropCheck && allowed)
|
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
if (occupiedCells != null && occupiedCells.Count > 1)
|
|
|
|
{
|
|
|
|
//Debug.Log("SlotUI: " + occupiedCells.Count);
|
|
|
|
itemDrop.SetSlots(occupiedCells);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.Log("In Use");
|
|
|
|
cell.inUse = true;
|
|
|
|
CellInUse = cell.inUse;
|
|
|
|
}
|
2022-10-19 13:51:46 +00:00
|
|
|
|
|
|
|
Transform parent = transform;
|
|
|
|
if (useImageAsSlotHolder && image != null)
|
|
|
|
{
|
|
|
|
parent = image.gameObject.transform;
|
|
|
|
}
|
|
|
|
item.GetComponent<Transform>().SetParent(parent);
|
|
|
|
item.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
|
|
|
|
//item.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
|
|
|
|
itemDrop.SetImageScale(imageScale);
|
|
|
|
itemDrop.SetSlot(this);
|
|
|
|
this.itemUI = itemDrop;
|
2022-10-29 19:56:00 +00:00
|
|
|
|
|
|
|
itemUI.Drop();
|
|
|
|
|
|
|
|
if (itemUI.Stackable)
|
|
|
|
{
|
|
|
|
droppedStackOntoEmptySlot = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsLinkedSlot())
|
|
|
|
{
|
|
|
|
foreach (SlotUI ls in LinkedSlots)
|
|
|
|
{
|
|
|
|
if (ls == this)
|
|
|
|
{
|
|
|
|
Debug.LogWarning("No self-referencing for this array ;)");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//ls.DropOntoSlot(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grid != null)
|
|
|
|
{
|
|
|
|
grid.HideSlotsOfItem(itemDrop);
|
|
|
|
}
|
2022-10-19 13:51:46 +00:00
|
|
|
HideImage();
|
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
if (groupSlot != null)
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
|
|
|
groupSlot.OnItemDropped();
|
|
|
|
}
|
|
|
|
if (appendUI != null)
|
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
appendUI.AppendUIToTransform(itemUI);
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (itemDrop.previousSlot != this)
|
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
// TODO: See why this is outputting at beginning, probably from test objects in Loot
|
|
|
|
//Debug.LogWarning("SlotUI: Failed to drop. dropCheck: " + dropCheck + ", allowed: " + allowed);
|
2022-10-19 13:51:46 +00:00
|
|
|
// return item to previous parent
|
|
|
|
itemDrop.ReturnToSlot();
|
|
|
|
return false;
|
|
|
|
//Debug.Log("Resetting parent");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
//Debug.LogWarning("SlotUI: Failed to drop. dropCheck: " + dropCheck + ", allowed: " + allowed);
|
2022-10-19 13:51:46 +00:00
|
|
|
Debug.LogError("SlotUI: Something wrong has happened.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
if (image == null)
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
|
|
|
Image[] images = GetComponentsInChildren<Image>(); // This will retrieve the first instance of an image in the FIRST child with the component
|
|
|
|
//Debug.Log(images.Length);
|
2022-10-29 19:56:00 +00:00
|
|
|
if (images.Length > 0)
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
if (images[0].transform == transform)
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
if (images.Length > 1)
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
|
|
|
image = images[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
image = images[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
if (grid == null)
|
2022-10-19 13:51:46 +00:00
|
|
|
grid = GetComponentInParent<GridUI>();
|
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
// Check again
|
|
|
|
if (grid == null)
|
|
|
|
{
|
|
|
|
cell = new Cell(0, 0, this, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TryGetComponent(out AppendUI aui))
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
|
|
|
appendUI = aui;
|
|
|
|
}
|
2022-10-29 19:56:00 +00:00
|
|
|
|
|
|
|
if (TryGetComponent(out LinkedSlotUI lui))
|
|
|
|
{
|
|
|
|
linkedSlotUI = lui;
|
|
|
|
}
|
|
|
|
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveDragDropItem()
|
|
|
|
{
|
|
|
|
ShowImage();
|
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
if (appendUI != null)
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
appendUI.RemoveUIFromTransform(itemUI);
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
foreach (SlotUI linked in LinkedSlots)
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
linked.RemoveDragDropItem();
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
itemUI = null;
|
|
|
|
|
|
|
|
if (cell != null)
|
2022-10-19 13:51:46 +00:00
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
cell.inUse = false;
|
|
|
|
CellInUse = cell.inUse;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupSlot != null)
|
|
|
|
{
|
|
|
|
groupSlot.OnItemRemoved();
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsSingleCellSlot()
|
|
|
|
{
|
|
|
|
return width == 1 && height == 1;
|
|
|
|
}
|
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
public bool HasStack()
|
|
|
|
{
|
|
|
|
return itemUI != null && itemUI.Stackable;
|
|
|
|
}
|
|
|
|
|
2022-10-19 13:51:46 +00:00
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2022-10-29 19:56:00 +00:00
|
|
|
if (PointerIsHoveredOver)
|
|
|
|
{
|
|
|
|
ItemUI droppedItem = InventorySystem.instance.DraggedItem;
|
|
|
|
if (droppedItem != null && !droppedItem.PickedUpOnFrame)
|
|
|
|
{
|
|
|
|
if (Input.GetMouseButtonDown(0))
|
|
|
|
{
|
|
|
|
bool callback = DropOntoSlot(droppedItem.gameObject);
|
|
|
|
if (callback && droppedItem.Stackable)
|
|
|
|
{
|
|
|
|
if (droppedItem.Count <= 0)
|
|
|
|
Destroy(droppedItem.gameObject);
|
|
|
|
else if (itemUI.Count < droppedItem.Count && !droppedStackOntoEmptySlot)
|
|
|
|
{
|
|
|
|
InventorySystem.instance.DraggedItem = droppedItem;
|
|
|
|
droppedStackOntoEmptySlot = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Input.GetMouseButtonDown(1) && droppedItem.Stackable)
|
|
|
|
{
|
|
|
|
Debug.Log("Right Click");
|
|
|
|
if(DropOntoSlot(droppedItem.gameObject, false))
|
|
|
|
{
|
|
|
|
if (droppedItem.Count <= 0)
|
|
|
|
Destroy(droppedItem.gameObject);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
InventorySystem.instance.DraggedItem = droppedItem;
|
|
|
|
droppedItem.transform.SetParent(InventorySystem.instance.inventoryUI.transform);
|
|
|
|
droppedStackOntoEmptySlot = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-19 13:51:46 +00:00
|
|
|
|
2022-10-29 19:56:00 +00:00
|
|
|
public void SetCell(Cell cell)
|
|
|
|
{
|
|
|
|
this.cell = cell;
|
|
|
|
cellX = this.cell.X;
|
|
|
|
cellY = this.cell.Y;
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal void HideImage()
|
|
|
|
{
|
|
|
|
if(image != null)
|
|
|
|
{
|
|
|
|
image.color = new Color(image.color.r, image.color.g, image.color.b, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
internal void ShowImage()
|
|
|
|
{
|
|
|
|
if (image != null)
|
|
|
|
{
|
|
|
|
image.color = new Color(image.color.r, image.color.g, image.color.b, 1);
|
|
|
|
}
|
|
|
|
}
|
2022-10-29 19:56:00 +00:00
|
|
|
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
|
|
{
|
|
|
|
ItemUI droppedItem = InventorySystem.instance.DraggedItem;
|
|
|
|
if (droppedItem != null && !droppedItem.PickedUpOnFrame)
|
|
|
|
{
|
|
|
|
DropOntoSlot(droppedItem.gameObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
|
|
{
|
|
|
|
PointerIsHoveredOver = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
|
|
{
|
|
|
|
PointerIsHoveredOver = true;
|
|
|
|
}
|
2022-10-19 13:51:46 +00:00
|
|
|
}
|
|
|
|
}
|