123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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<RaycastResult> 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<RaycastResult>();
- //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<ItemSlotUi>();
- 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;
- }
- }
- }
|