DragAndDropItem.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.UI;
  8. namespace KairoEngine.Inventory
  9. {
  10. // Todo: Fix droping items on the ground
  11. public class DragAndDropItem : MonoBehaviour
  12. {
  13. public GraphicRaycaster m_Raycaster;
  14. public EventSystem m_EventSystem;
  15. public Image itemCursorImage;
  16. private PointerEventData m_PointerEventData;
  17. private ItemSlotUi previousItemSlot;
  18. private bool isDragging = false;
  19. private ItemRef itemRef;
  20. private List<RaycastResult> results;
  21. void Update()
  22. {
  23. if (MouseInputUIBlocker.BlockedByUI == true)
  24. {
  25. ItemSlotUi targetItemSlot = RaycastSlot();
  26. if(targetItemSlot != null)
  27. {
  28. // Start Draging item from itemslot
  29. if(!isDragging && Input.GetButtonDown("Fire1") && targetItemSlot.draggable)
  30. {
  31. isDragging = true;
  32. previousItemSlot = targetItemSlot;
  33. itemRef = previousItemSlot.itemRef;
  34. previousItemSlot.parentItemContainer.RemoveItem(itemRef);
  35. itemCursorImage.sprite = itemRef.item.icon;
  36. itemCursorImage.gameObject.SetActive(true);
  37. itemCursorImage.transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
  38. }
  39. // Release item on itemslot
  40. if(isDragging && Input.GetButtonUp("Fire1"))
  41. {
  42. isDragging = false;
  43. itemCursorImage.gameObject.SetActive(false);
  44. if(targetItemSlot.itemRef == null && targetItemSlot.dropable)
  45. {
  46. targetItemSlot.parentItemContainer.AddItem(itemRef);
  47. }
  48. else
  49. {
  50. previousItemSlot.parentItemContainer.AddItem(itemRef);
  51. }
  52. }
  53. }
  54. else
  55. {
  56. // Check ui raycast results
  57. if(results.Count > 0)
  58. {
  59. // Item droped somewhere else in the ui
  60. if(isDragging && Input.GetButtonUp("Fire1"))
  61. {
  62. isDragging = false;
  63. previousItemSlot.parentItemContainer.AddItem(itemRef);
  64. itemCursorImage.gameObject.SetActive(false);
  65. }
  66. }
  67. else
  68. {
  69. if(isDragging && Input.GetButtonUp("Fire1"))
  70. {
  71. isDragging = false;
  72. itemCursorImage.gameObject.SetActive(false);
  73. // ! Fix when dependencies are solved
  74. //ICommand dropItemCommand = new DropItemCommand(previousItemSlot.parentItemContainer, itemRef);
  75. //CommandInvoker.AddCommand(dropItemCommand);
  76. }
  77. }
  78. }
  79. }
  80. // Release item on ground
  81. // Update Dragged item's icon image position
  82. if (isDragging && Input.GetButton("Fire1"))
  83. {
  84. itemCursorImage.transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
  85. }
  86. }
  87. private ItemSlotUi RaycastSlot()
  88. {
  89. //Set up the new Pointer Event
  90. m_PointerEventData = new PointerEventData(m_EventSystem);
  91. //Set the Pointer Event Position to that of the mouse position
  92. m_PointerEventData.position = Input.mousePosition;
  93. //Create a list of Raycast Results
  94. results = new List<RaycastResult>();
  95. //Raycast using the Graphics Raycaster and mouse click position
  96. m_Raycaster.Raycast(m_PointerEventData, results);
  97. //For every result returned, output the name of the GameObject on the Canvas hit by the Ray
  98. foreach (RaycastResult result in results)
  99. {
  100. ItemSlotUi slot = result.gameObject.GetComponent<ItemSlotUi>();
  101. if (slot != null)
  102. {
  103. // Hovering a item slot
  104. if(slot.itemRef == null)
  105. {
  106. //Debug.Log("Hovering empty item slot " + slot.name);
  107. }
  108. else
  109. {
  110. //Debug.Log("Hovering item " + slot.itemRef.item.name);
  111. }
  112. return slot;
  113. }
  114. }
  115. return null;
  116. }
  117. }
  118. }