137 lines
3.2 KiB
C#
137 lines
3.2 KiB
C#
![]() |
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using TMPro;
|
||
|
using UnityEngine.UI;
|
||
|
using UnityEngine.EventSystems;
|
||
|
using System; // for Action type
|
||
|
|
||
|
public class ItemSlot : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler, IDropHandler
|
||
|
{
|
||
|
[Header("UI Stuff to change")]
|
||
|
[SerializeField] private TextMeshProUGUI slotItemNumberText;
|
||
|
[SerializeField] private Image itemImage;
|
||
|
|
||
|
public ChestPanelController chestPanelController;
|
||
|
|
||
|
public event Action<ItemSlot> OnBeginDragEvent;
|
||
|
public event Action<ItemSlot> OnEndDragEvent;
|
||
|
public event Action<ItemSlot> OnDragEvent;
|
||
|
public event Action<ItemSlot> OnDropEvent;
|
||
|
|
||
|
private Color normalColor = Color.white;
|
||
|
private Color disabledColor = new Color(1, 1, 1, 0); // transparency = 0
|
||
|
|
||
|
[Header("Slot variable")]
|
||
|
[SerializeField]
|
||
|
protected int _number;
|
||
|
public int Number {
|
||
|
get { return _number; }
|
||
|
set {
|
||
|
_number = value;
|
||
|
}
|
||
|
}
|
||
|
[SerializeField]
|
||
|
protected Item _item;
|
||
|
public Item Item {
|
||
|
get { return _item; }
|
||
|
set {
|
||
|
_item = value;
|
||
|
|
||
|
if(_item == null) {
|
||
|
itemImage.color = disabledColor;
|
||
|
} else {
|
||
|
itemImage.sprite = _item.Image;
|
||
|
itemImage.color = normalColor;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
public int countStackableItems = 0;
|
||
|
|
||
|
|
||
|
|
||
|
public void SetupInventorySlot(int _number, Item _item, ChestPanelController _chestPanelController)
|
||
|
{
|
||
|
chestPanelController = _chestPanelController;
|
||
|
|
||
|
Number = _number;
|
||
|
|
||
|
if(_item)
|
||
|
{
|
||
|
SetItem(_item);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void SetItem(Item _item) // dont change slot numer after assign new item, numbers are bind during object init
|
||
|
{
|
||
|
Item = _item;
|
||
|
|
||
|
ChangeEnableImageView(true);
|
||
|
|
||
|
countStackableItems +=1;
|
||
|
|
||
|
UpdateItemNumberHolder();
|
||
|
}
|
||
|
|
||
|
|
||
|
private void ChangeEnableImageView(bool _flag)
|
||
|
{
|
||
|
itemImage.color = _flag ? normalColor : disabledColor; //!slotImage.enabled;
|
||
|
}
|
||
|
|
||
|
private void ChangeNumberHeldView(bool _flag)
|
||
|
{
|
||
|
slotItemNumberText.enabled = _flag;
|
||
|
}
|
||
|
|
||
|
private void UpdateItemNumberHolder()
|
||
|
{
|
||
|
slotItemNumberText.text = "" + countStackableItems;
|
||
|
}
|
||
|
|
||
|
// Start is called before the first frame update
|
||
|
void Start()
|
||
|
{
|
||
|
if(!Item)
|
||
|
{
|
||
|
ChangeEnableImageView(false);
|
||
|
ChangeNumberHeldView(false);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Update is called once per frame
|
||
|
void Update()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public virtual bool CanReceiveItem(Item item)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// Events methods
|
||
|
public void OnBeginDrag(PointerEventData eventData)
|
||
|
{
|
||
|
if(OnBeginDragEvent != null)
|
||
|
OnBeginDragEvent(this);
|
||
|
}
|
||
|
|
||
|
public void OnEndDrag(PointerEventData eventData)
|
||
|
{
|
||
|
if(OnEndDragEvent != null)
|
||
|
OnEndDragEvent(this);
|
||
|
}
|
||
|
|
||
|
public void OnDrag(PointerEventData eventData)
|
||
|
{
|
||
|
if(OnDragEvent != null)
|
||
|
OnDragEvent(this);
|
||
|
}
|
||
|
|
||
|
public void OnDrop(PointerEventData eventData)
|
||
|
{
|
||
|
if(OnDropEvent != null)
|
||
|
OnDropEvent(this);
|
||
|
}
|
||
|
}
|