234 lines
5.6 KiB
C#
234 lines
5.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace UFS3.Old
|
|
{
|
|
public class UI_Item : MonoBehaviour, IBeginDragHandler, IEventSystemHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
|
|
{
|
|
private static UI_Item CurrentDragingItem;
|
|
|
|
public Image itemIcon;
|
|
|
|
private Transform originalParent;
|
|
|
|
private Canvas canvas;
|
|
|
|
private Vector3 dragStartPos;
|
|
|
|
private RectTransform rectTransform;
|
|
|
|
private BaseItemData itemData;
|
|
|
|
private Vector2Int gridPos;
|
|
|
|
public UI_ItemTooltip itemTooltip;
|
|
|
|
[SerializeField]
|
|
private ItemDataVariable currentRod;
|
|
|
|
[SerializeField]
|
|
private GameObject equipedTextObject;
|
|
|
|
[SerializeField]
|
|
private ScriptableEventItemData onRodChange;
|
|
|
|
[SerializeField]
|
|
private ScriptableEventItemData onItemPlaced;
|
|
|
|
[SerializeField]
|
|
private ScriptableEventItemData onItemCantPlaced;
|
|
|
|
[SerializeField]
|
|
private UI_ItemSockets gearSockets;
|
|
|
|
private GameObject touchedCell;
|
|
|
|
public void Initialize(BaseItemData itemData, Vector2Int gridPos)
|
|
{
|
|
rectTransform = GetComponent<RectTransform>();
|
|
itemData.GridPosition = gridPos;
|
|
this.gridPos = gridPos;
|
|
this.itemData = itemData;
|
|
itemIcon.sprite = itemData.Icon;
|
|
rectTransform.sizeDelta = new Vector2(itemData.Width * 100, itemData.Height * 100);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
rectTransform = GetComponent<RectTransform>();
|
|
canvas = GetComponentInParent<Canvas>();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
onRodChange.OnRaised += OnItemEquip_OnRaised;
|
|
onItemPlaced.OnRaised += OnItemPlaced_OnRaised;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
onRodChange.OnRaised -= OnItemEquip_OnRaised;
|
|
onItemPlaced.OnRaised -= OnItemPlaced_OnRaised;
|
|
}
|
|
|
|
private void OnItemPlaced_OnRaised(BaseItemData obj)
|
|
{
|
|
if (itemData == obj)
|
|
{
|
|
Object.Destroy(base.gameObject);
|
|
}
|
|
}
|
|
|
|
private void OnItemEquip_OnRaised(BaseItemData obj)
|
|
{
|
|
equipedTextObject.SetActive(value: false);
|
|
}
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
if (!(CurrentDragingItem != null))
|
|
{
|
|
CurrentDragingItem = this;
|
|
dragStartPos = base.transform.position;
|
|
originalParent = base.transform.parent;
|
|
itemIcon.raycastTarget = false;
|
|
UI_InventoryCase.Instance.ClearItemCells(gridPos, itemData.Size);
|
|
}
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
itemTooltip.SetOff();
|
|
if (CurrentDragingItem != this)
|
|
{
|
|
return;
|
|
}
|
|
base.transform.position = eventData.position;
|
|
Vector3[] array = new Vector3[4];
|
|
rectTransform.GetWorldCorners(array);
|
|
Vector3 vector = array[1];
|
|
PointerEventData eventData2 = new PointerEventData(EventSystem.current)
|
|
{
|
|
position = vector
|
|
};
|
|
List<RaycastResult> list = new List<RaycastResult>();
|
|
EventSystem.current.RaycastAll(eventData2, list);
|
|
touchedCell = null;
|
|
if (list.Count <= 0)
|
|
{
|
|
return;
|
|
}
|
|
foreach (RaycastResult item in list)
|
|
{
|
|
if (item.gameObject.CompareTag("ItemCell"))
|
|
{
|
|
touchedCell = item.gameObject;
|
|
Debug.Log("results: " + item.gameObject.name);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
if (CurrentDragingItem != this)
|
|
{
|
|
return;
|
|
}
|
|
bool flag = false;
|
|
base.transform.SetParent(originalParent);
|
|
itemIcon.raycastTarget = true;
|
|
base.transform.position = dragStartPos;
|
|
if (touchedCell != null)
|
|
{
|
|
UI_ItemCell component = touchedCell.GetComponent<UI_ItemCell>();
|
|
flag = UI_InventoryCase.Instance.TryPutItem(component.GridPosition, itemData.Size);
|
|
if (flag)
|
|
{
|
|
RectTransform rectTransform = (RectTransform)base.transform;
|
|
rectTransform.anchoredPosition = Vector2.zero;
|
|
rectTransform.anchoredPosition += new Vector2((float)component.GridPosition.x * 100f, (float)component.GridPosition.y * -100f);
|
|
rectTransform.anchoredPosition3D = new Vector3(rectTransform.anchoredPosition.x, rectTransform.anchoredPosition.y, 0f);
|
|
gridPos = component.GridPosition;
|
|
}
|
|
}
|
|
if (!flag)
|
|
{
|
|
UI_InventoryCase.Instance.TryPutItem(gridPos, itemData.Size);
|
|
}
|
|
CurrentDragingItem = null;
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
itemTooltip.SetOff();
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
itemTooltip.DrawItemData(itemData);
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
if (eventData.button == PointerEventData.InputButton.Right)
|
|
{
|
|
if (equipedTextObject.activeSelf)
|
|
{
|
|
currentRod.Value = null;
|
|
onRodChange.Raise(null);
|
|
}
|
|
else if (itemData is RodData)
|
|
{
|
|
currentRod.Value = itemData;
|
|
onRodChange.Raise(itemData);
|
|
equipedTextObject.SetActive(value: true);
|
|
}
|
|
else if ((bool)currentRod.Value && itemData is ReelData)
|
|
{
|
|
AttachHandler();
|
|
}
|
|
else if ((bool)currentRod.Value && itemData is LineData)
|
|
{
|
|
AttachHandler();
|
|
}
|
|
else if ((bool)currentRod.Value && itemData is LureData)
|
|
{
|
|
AttachHandler();
|
|
}
|
|
else
|
|
{
|
|
itemData.Use();
|
|
}
|
|
}
|
|
void AttachHandler()
|
|
{
|
|
RodData rodData = (RodData)currentRod.Value;
|
|
if (!rodData.AttachGear(itemData))
|
|
{
|
|
onItemCantPlaced.Raise(itemData);
|
|
}
|
|
else
|
|
{
|
|
UI_Item[] array = Object.FindObjectsByType<UI_Item>(FindObjectsSortMode.None);
|
|
foreach (UI_Item uI_Item in array)
|
|
{
|
|
if (uI_Item.itemData == rodData)
|
|
{
|
|
uI_Item.SocketItem(itemData);
|
|
}
|
|
}
|
|
UI_InventoryCase.Instance.RemoveItem(itemData);
|
|
Object.Destroy(base.gameObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SocketItem(BaseItemData itemData)
|
|
{
|
|
gearSockets.AddToSocket(itemData);
|
|
}
|
|
}
|
|
}
|