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
- {
-
- 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)
- {
-
- 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);
- }
-
- 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
- {
-
- if(results.Count > 0)
- {
-
- 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);
-
-
-
- }
- }
-
- }
- }
-
-
-
- if (isDragging && Input.GetButton("Fire1"))
- {
- itemCursorImage.transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
- }
- }
- private ItemSlotUi RaycastSlot()
- {
-
- m_PointerEventData = new PointerEventData(m_EventSystem);
-
- m_PointerEventData.position = Input.mousePosition;
-
- results = new List<RaycastResult>();
-
- m_Raycaster.Raycast(m_PointerEventData, results);
-
- foreach (RaycastResult result in results)
- {
- ItemSlotUi slot = result.gameObject.GetComponent<ItemSlotUi>();
- if (slot != null)
- {
-
- if(slot.itemRef == null)
- {
-
- }
- else
- {
-
- }
- return slot;
- }
- }
- return null;
- }
- }
- }
|