Scriptum/Assets/Scripts/REFACTORING/Models/Panel/Slot/ItemSlot.cs

159 lines
3.6 KiB
C#
Raw Normal View History

2022-05-29 21:54:28 +02:00
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
2022-05-29 21:54:28 +02:00
{
[Header("UI Stuff to change")]
[SerializeField] private TextMeshProUGUI slotItemNumberText;
[SerializeField] private Image itemImage;
2022-11-27 21:28:55 +01:00
public DraggablePanelController _PanelController;
2022-05-29 21:54:28 +02:00
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 {
2022-05-29 21:54:28 +02:00
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;
2022-05-29 21:54:28 +02:00
/* 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)
2022-05-29 21:54:28 +02:00
{
this._PanelController = _PanelController;
2022-05-29 21:54:28 +02:00
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
2022-05-29 21:54:28 +02:00
{
Item = _item;
ChangeEnableImageView(true);
countStackableItems +=1;
UpdateItemNumberHolder();
}
2022-11-06 21:34:17 +01:00
public void ResetSlot()
{
Item = null;
ChangeEnableImageView(false);
countStackableItems = 0;
UpdateItemNumberHolder();
}
2022-05-29 21:54:28 +02:00
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;
}
2022-11-06 21:34:17 +01:00
#region Events methods
2022-05-29 21:54:28 +02:00
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);
}
2022-11-06 21:34:17 +01:00
#endregion
2022-05-29 21:54:28 +02:00
}