Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / UI / Inventory / InventoryButton.cs
using System.Reflection;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class InventoryButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
    [SerializeField] private GameObject buttonOptions;
    public int slotIndex;

    private float pressStartTime;
    private bool isHolding, isBeingDragged, isOverlapping;

    private float pressDuration;
    private InventoryManager inventoryManager;

    [SerializeField] private float holdThreshold = 0.01f;

    private Vector3 originalPosition;
    private InventoryButton overlappingButton;

    [SerializeField] private InventorySlot data;
    [SerializeField] private PlayerInventoryUI playerInventoryUI;

    private void Start()
    {
        originalPosition = transform.position;
        inventoryManager = GameManager.Instance.PlayerController.GetComponentInChildren<InventoryManager>();
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            if (!data.IsEmpty())
            {
                pressStartTime = Time.time;
                isHolding = false;
                isBeingDragged = false;
            }
        }

        else if (eventData.button == PointerEventData.InputButton.Right)
        {
            if (!data.IsEmpty())
            {
                data.Owner.SplitSlot(data.SlotIndex);
            }

        }
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            if (isHolding)
            {
                OnHoldRelease();
            }
            else
            {
                OnSingleClick();
            }

            isHolding = false;
            isBeingDragged = false;
            transform.position = originalPosition; // Keep original position
        }
    }

    private void Update()
    {
        if (Input.GetMouseButton(0) && !isBeingDragged)
        {
            if (!data.IsEmpty())
            {
                pressDuration = Time.time - pressStartTime;

                if (!isHolding && pressDuration >= holdThreshold)
                {
                    isHolding = true;
                    OnHoldPress();
                }
            }
        }
    }

    private void OnSingleClick()
    {
        if (data.Item != null)
        {
            //if (playerInventoryUI.currentButtonOptions == null)
            //{
            //    playerInventoryUI.currentButtonOptions = Instantiate(buttonOptions, transform);
            //    playerInventoryUI.currentButtonOptions.transform.position = transform.position;
            //}

            //else
            //{
            //    playerInventoryUI.currentButtonOptions = null;
            //    playerInventoryUI.currentButtonOptions = Instantiate(buttonOptions, transform);
            //    playerInventoryUI.currentButtonOptions.transform.position = transform.position;
            //}
        }
    }

    private void OnHoldPress()
    {
        isBeingDragged = true;
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (isBeingDragged)
        {
            transform.position = Input.mousePosition;
        }
    }

    public void ChangeData(InventorySlot d)
    {
        data = d;

        Image[] images = gameObject.GetComponentsInChildren<Image>();
        TextMeshProUGUI tmp = gameObject.GetComponentInChildren<TextMeshProUGUI>();

        foreach (Image image in images)
        {
            if (image.gameObject.transform.childCount == 0)
            {
                if (data.Item != null)
                {
                    image.color = Color.white;
                    image.sprite = data.Item.ItemImage;
                    tmp.text = data.Amount > 1 ? d.Amount.ToString() : string.Empty;
                }
                else
                {
                    image.color = Color.clear; // Reset to transparent
                    image.sprite = null;
                    tmp.text = string.Empty;  // Clear text
                }
            }
        }
    }

    private void OnHoldRelease()
    {
        if (isOverlapping && overlappingButton != null)
        {

            if (!data.Owner.TryMergeSlots(data, overlappingButton.data))
            {
                // Perform the swap in the inventory data
                inventoryManager.SwapInventorySlot(data, overlappingButton.data);
            }


            isOverlapping = false;
            overlappingButton = null;
        }

        transform.position = originalPosition; // Return to original position visually
    }


    private void OnTriggerEnter2D(Collider2D collision)
    {
        // Check if the current button is being dragged and the collision is with another button
        if (isBeingDragged && collision.TryGetComponent(out InventoryButton button) && button != this)
        {
            overlappingButton = button;
            isOverlapping = true;
            Debug.Log($"Started overlapping with button at index {button.slotIndex}");
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.TryGetComponent(out InventoryButton button) && button == overlappingButton)
        {
            overlappingButton = null;
            isOverlapping = false;
        }
    }
}