Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / UI / Inventory / InventoryButton.cs
  1. using System.Reflection;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. public class InventoryButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
  7. {
  8. [SerializeField] private GameObject buttonOptions;
  9. public int slotIndex;
  10. private float pressStartTime;
  11. private bool isHolding, isBeingDragged, isOverlapping;
  12. private float pressDuration;
  13. private InventoryManager inventoryManager;
  14. [SerializeField] private float holdThreshold = 0.01f;
  15. private Vector3 originalPosition;
  16. private InventoryButton overlappingButton;
  17. [SerializeField] private InventorySlot data;
  18. [SerializeField] private PlayerInventoryUI playerInventoryUI;
  19. private void Start()
  20. {
  21. originalPosition = transform.position;
  22. inventoryManager = GameManager.Instance.PlayerController.GetComponentInChildren<InventoryManager>();
  23. }
  24. public void OnPointerDown(PointerEventData eventData)
  25. {
  26. if (eventData.button == PointerEventData.InputButton.Left)
  27. {
  28. if (!data.IsEmpty())
  29. {
  30. pressStartTime = Time.time;
  31. isHolding = false;
  32. isBeingDragged = false;
  33. }
  34. }
  35. else if (eventData.button == PointerEventData.InputButton.Right)
  36. {
  37. if (!data.IsEmpty())
  38. {
  39. data.Owner.SplitSlot(data.SlotIndex);
  40. }
  41. }
  42. }
  43. public void OnPointerUp(PointerEventData eventData)
  44. {
  45. if (eventData.button == PointerEventData.InputButton.Left)
  46. {
  47. if (isHolding)
  48. {
  49. OnHoldRelease();
  50. }
  51. else
  52. {
  53. OnSingleClick();
  54. }
  55. isHolding = false;
  56. isBeingDragged = false;
  57. transform.position = originalPosition; // Keep original position
  58. }
  59. }
  60. private void Update()
  61. {
  62. if (Input.GetMouseButton(0) && !isBeingDragged)
  63. {
  64. if (!data.IsEmpty())
  65. {
  66. pressDuration = Time.time - pressStartTime;
  67. if (!isHolding && pressDuration >= holdThreshold)
  68. {
  69. isHolding = true;
  70. OnHoldPress();
  71. }
  72. }
  73. }
  74. }
  75. private void OnSingleClick()
  76. {
  77. if (data.Item != null)
  78. {
  79. //if (playerInventoryUI.currentButtonOptions == null)
  80. //{
  81. // playerInventoryUI.currentButtonOptions = Instantiate(buttonOptions, transform);
  82. // playerInventoryUI.currentButtonOptions.transform.position = transform.position;
  83. //}
  84. //else
  85. //{
  86. // playerInventoryUI.currentButtonOptions = null;
  87. // playerInventoryUI.currentButtonOptions = Instantiate(buttonOptions, transform);
  88. // playerInventoryUI.currentButtonOptions.transform.position = transform.position;
  89. //}
  90. }
  91. }
  92. private void OnHoldPress()
  93. {
  94. isBeingDragged = true;
  95. }
  96. public void OnDrag(PointerEventData eventData)
  97. {
  98. if (isBeingDragged)
  99. {
  100. transform.position = Input.mousePosition;
  101. }
  102. }
  103. public void ChangeData(InventorySlot d)
  104. {
  105. data = d;
  106. Image[] images = gameObject.GetComponentsInChildren<Image>();
  107. TextMeshProUGUI tmp = gameObject.GetComponentInChildren<TextMeshProUGUI>();
  108. foreach (Image image in images)
  109. {
  110. if (image.gameObject.transform.childCount == 0)
  111. {
  112. if (data.Item != null)
  113. {
  114. image.color = Color.white;
  115. image.sprite = data.Item.ItemImage;
  116. tmp.text = data.Amount > 1 ? d.Amount.ToString() : string.Empty;
  117. }
  118. else
  119. {
  120. image.color = Color.clear; // Reset to transparent
  121. image.sprite = null;
  122. tmp.text = string.Empty; // Clear text
  123. }
  124. }
  125. }
  126. }
  127. private void OnHoldRelease()
  128. {
  129. if (isOverlapping && overlappingButton != null)
  130. {
  131. if (!data.Owner.TryMergeSlots(data, overlappingButton.data))
  132. {
  133. // Perform the swap in the inventory data
  134. inventoryManager.SwapInventorySlot(data, overlappingButton.data);
  135. }
  136. isOverlapping = false;
  137. overlappingButton = null;
  138. }
  139. transform.position = originalPosition; // Return to original position visually
  140. }
  141. private void OnTriggerEnter2D(Collider2D collision)
  142. {
  143. // Check if the current button is being dragged and the collision is with another button
  144. if (isBeingDragged && collision.TryGetComponent(out InventoryButton button) && button != this)
  145. {
  146. overlappingButton = button;
  147. isOverlapping = true;
  148. Debug.Log($"Started overlapping with button at index {button.slotIndex}");
  149. }
  150. }
  151. private void OnTriggerExit2D(Collider2D collision)
  152. {
  153. if (collision.TryGetComponent(out InventoryButton button) && button == overlappingButton)
  154. {
  155. overlappingButton = null;
  156. isOverlapping = false;
  157. }
  158. }
  159. }