9fa400f9a7
Simple code refaactor
130 lines
3.1 KiB
C#
130 lines
3.1 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, ISlot, IBeginDragHandler, IEndDragHandler, IDragHandler, IDropHandler
|
|
{
|
|
[Header("UI Stuff to change")]
|
|
[SerializeField] private TextMeshProUGUI slotItemNumberText;
|
|
[SerializeField] private Image itemImage;
|
|
|
|
public BasePanelController _PanelController;
|
|
|
|
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 event Action<ItemSlot> OnBeginDragEvent;
|
|
public event Action<ItemSlot> OnEndDragEvent;
|
|
public event Action<ItemSlot> OnDragEvent;
|
|
public event Action<ItemSlot> OnDropEvent;
|
|
|
|
public void SetupSlot(int _number, Item _item, BasePanelController _PanelController)
|
|
{
|
|
this._PanelController = _PanelController;
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|