projectEli/Assets/SimpleInventorySystem/Scripts/UI/SlotUI.cs

188 lines
5.5 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
using static SimpleInventorySystem.GridUI;
namespace SimpleInventorySystem
{
public class SlotUI : MonoBehaviour, IDropHandler
{
public int width;
public int height;
public bool useImageAsSlotHolder;
public Image image;
public GridUI grid;
public GridUI.Cell cell;
public ItemUI itemUI;
[HideInInspector] public GroupSlotUI groupSlot;
private AppendUI appendUI;
internal ItemUI GetItemUI()
{
return itemUI;
}
public float imageScale = 1f;
public List<ItemTags> AllowedItemTags;
public void OnDrop(PointerEventData eventData)
{
//
//("OnDrop");
if (eventData.pointerDrag != null && AllowedDraggable(eventData.pointerDrag))
{
DropOntoSlot(eventData.pointerDrag);
}
}
public bool AllowedDraggable(GameObject gameObject)
{
return gameObject.GetComponent<ItemUI>() != null;
}
public bool DropOntoSlot(GameObject item)
{
ItemUI itemDrop = item.GetComponent<ItemUI>();
List<Cell> occupiedCells = new List<Cell>();
bool dropCheck = grid.CanMoveInCells(itemDrop, this, occupiedCells);
ItemTags itemTag = itemDrop.ItemTag;
bool allowed = AllowedItemTags == null || AllowedItemTags.Count == 0 || AllowedItemTags.Contains(ItemTags.Any) || AllowedItemTags.Contains(itemTag);
Debug.Log("GridUI: DropOntoSlot(): " + gameObject.name + ": " + "dropCheck: " + dropCheck);
Debug.Log("GridUI: DropOntoSlot(): " + gameObject.name + ": " + "allowed: " + allowed);
if (dropCheck && allowed)
{
Debug.Log("SlotUI: " + occupiedCells.Count);
itemDrop.SetSlots(occupiedCells);
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;
grid.HideSlotsOfItem(itemDrop);
HideImage();
if(groupSlot != null)
{
groupSlot.OnItemDropped();
}
if (appendUI != null)
{
appendUI.AppendUIToTransform();
}
return true;
}
else if (itemDrop.previousSlot != this)
{
Debug.LogWarning("SlotUI: Failed to drop. dropCheck: " + dropCheck + ", allowed: " + allowed);
// return item to previous parent
itemDrop.ReturnToSlot();
return false;
//Debug.Log("Resetting parent");
}
else
{
Debug.LogWarning("SlotUI: Failed to drop. dropCheck: " + dropCheck + ", allowed: " + allowed);
Debug.LogError("SlotUI: Something wrong has happened.");
return false;
}
}
void Awake()
{
if(image == null)
{
Image[] images = GetComponentsInChildren<Image>(); // This will retrieve the first instance of an image in the FIRST child with the component
//Debug.Log(images.Length);
if(images.Length > 0)
{
if(images[0].transform == transform)
{
if(images.Length > 1)
{
image = images[1];
}
}
else
{
image = images[0];
}
}
}
if(grid == null)
grid = GetComponentInParent<GridUI>();
if(TryGetComponent(out AppendUI aui))
{
appendUI = aui;
}
}
public void RemoveDragDropItem()
{
ShowImage();
if (cell != null)
{
cell.inUse = false;
}
if(groupSlot != null)
{
groupSlot.OnItemRemoved();
}
if (appendUI != null)
{
appendUI.RemoveUIFromTransform();
}
}
public bool IsSingleCellSlot()
{
return width == 1 && height == 1;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
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);
}
}
}
}