using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using Sirenix.OdinInspector; using KairoEngine.UI; namespace KairoEngine.Inventory { // Todo: Fix droping items on the ground public class DragAndDropItem : MonoBehaviour { public GraphicRaycaster m_Raycaster; public EventSystem m_EventSystem; public Image itemCursorImage; private PointerEventData m_PointerEventData; private ItemSlotUi previousItemSlot; private bool isDragging = false; private ItemRef itemRef; private List results; void Update() { if (MouseInputUIBlocker.BlockedByUI == true) { ItemSlotUi targetItemSlot = RaycastSlot(); if(targetItemSlot != null) { // Start Draging item from itemslot if(!isDragging && Input.GetButtonDown("Fire1") && targetItemSlot.draggable) { isDragging = true; previousItemSlot = targetItemSlot; itemRef = previousItemSlot.itemRef; previousItemSlot.parentItemContainer.RemoveItem(itemRef); itemCursorImage.sprite = itemRef.item.icon; itemCursorImage.gameObject.SetActive(true); itemCursorImage.transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0); } // Release item on itemslot if(isDragging && Input.GetButtonUp("Fire1")) { isDragging = false; itemCursorImage.gameObject.SetActive(false); if(targetItemSlot.itemRef == null && targetItemSlot.dropable) { targetItemSlot.parentItemContainer.AddItem(itemRef); } else { previousItemSlot.parentItemContainer.AddItem(itemRef); } } } else { // Check ui raycast results if(results.Count > 0) { // Item droped somewhere else in the ui if(isDragging && Input.GetButtonUp("Fire1")) { isDragging = false; previousItemSlot.parentItemContainer.AddItem(itemRef); itemCursorImage.gameObject.SetActive(false); } } else { if(isDragging && Input.GetButtonUp("Fire1")) { isDragging = false; itemCursorImage.gameObject.SetActive(false); // ! Fix when dependencies are solved //ICommand dropItemCommand = new DropItemCommand(previousItemSlot.parentItemContainer, itemRef); //CommandInvoker.AddCommand(dropItemCommand); } } } } // Release item on ground // Update Dragged item's icon image position if (isDragging && Input.GetButton("Fire1")) { itemCursorImage.transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0); } } private ItemSlotUi RaycastSlot() { //Set up the new Pointer Event m_PointerEventData = new PointerEventData(m_EventSystem); //Set the Pointer Event Position to that of the mouse position m_PointerEventData.position = Input.mousePosition; //Create a list of Raycast Results results = new List(); //Raycast using the Graphics Raycaster and mouse click position m_Raycaster.Raycast(m_PointerEventData, results); //For every result returned, output the name of the GameObject on the Canvas hit by the Ray foreach (RaycastResult result in results) { ItemSlotUi slot = result.gameObject.GetComponent(); if (slot != null) { // Hovering a item slot if(slot.itemRef == null) { //Debug.Log("Hovering empty item slot " + slot.name); } else { //Debug.Log("Hovering item " + slot.itemRef.item.name); } return slot; } } return null; } } }