Scriptum/Assets/Scripts/REFACTORING/Models/Panel/Slot/ItemSlot.cs
2022-11-28 00:52:47 +01:00

159 lines
3.6 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 DraggablePanelController _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 EquippableItem _item;
public EquippableItem 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 ItemSlot() { }
public ItemSlot(int number, EquippableItem item = null)
{
Number = number;
Item = item;
}*/
/*
public ItemSlot(ItemSlot slot)
{
Number = slot.Number;
Item = slot.Item;
slotItemNumberText = slot.slotItemNumberText;
itemImage = slot.itemImage;
}*/
public void SetupSlot(int _number, EquippableItem _item, WarehousePanelController _PanelController)
{
this._PanelController = _PanelController;
Number = _number;
if(_item)
{
SetItem(_item);
}
}
public void SetItem(EquippableItem _item) // dont change slot numer after assign new item, numbers are bind during object init
{
Item = _item;
ChangeEnableImageView(true);
countStackableItems +=1;
UpdateItemNumberHolder();
}
public void ResetSlot()
{
Item = null;
ChangeEnableImageView(false);
countStackableItems = 0;
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;
}
#region 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);
}
#endregion
}