126 lines
3.6 KiB
C#
126 lines
3.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
/// <summary>
|
|
/// 打孔带在 UI 中的拖拽与点击交互。
|
|
/// </summary>
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
public class PunchTapeInteraction : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler,
|
|
IBeginDragHandler, IDragHandler, IEndDragHandler
|
|
{
|
|
private Canvas canvas;
|
|
[HideInInspector]
|
|
public bool isFavorited = false;
|
|
|
|
private RectTransform rectTransform;
|
|
|
|
private CursorMode cursorMode = CursorMode.Auto;
|
|
|
|
private bool isLocked = false;
|
|
private bool _favoriteDragActive;
|
|
|
|
private void Awake()
|
|
{
|
|
rectTransform = GetComponent<RectTransform>();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
canvas = GetComponentInParent<Canvas>();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (_favoriteDragActive)
|
|
{
|
|
_favoriteDragActive = false;
|
|
PunchTapeFavoritesDragEvents.NotifyDragEnded();
|
|
}
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
Cursor.SetCursor(null, Vector2.zero, cursorMode);
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
}
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
if (!isFavorited || isLocked) return;
|
|
|
|
transform.SetParent(canvas.transform, true);
|
|
transform.localScale = Vector3.one;
|
|
transform.SetAsLastSibling();
|
|
var cg = GetComponent<CanvasGroup>();
|
|
if (cg != null) cg.blocksRaycasts = false;
|
|
_favoriteDragActive = true;
|
|
PunchTapeFavoritesDragEvents.NotifyDragStarted();
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
if (!isFavorited || isLocked) return;
|
|
|
|
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
|
canvas.transform as RectTransform,
|
|
eventData.position,
|
|
eventData.pressEventCamera,
|
|
out Vector2 localPoint))
|
|
{
|
|
rectTransform.localPosition = new Vector3(localPoint.x, localPoint.y + 200f, 0f);
|
|
}
|
|
transform.rotation = Quaternion.Euler(0, 0, -90f);
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
if (!isFavorited || isLocked) return;
|
|
|
|
var cg = GetComponent<CanvasGroup>();
|
|
if (cg != null) cg.blocksRaycasts = true;
|
|
|
|
var slot = eventData.pointerEnter?
|
|
.GetComponentInParent<MemoryPunchTapeSlot>();
|
|
var item = GetComponent<PunchTapeItem>();
|
|
|
|
bool accepted = slot != null && slot.TryAcceptDrop(item);
|
|
|
|
if (_favoriteDragActive)
|
|
{
|
|
_favoriteDragActive = false;
|
|
PunchTapeFavoritesDragEvents.NotifyDragEnded();
|
|
}
|
|
|
|
if (accepted)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
else
|
|
{
|
|
var manager = PunchTapeManager.Instance;
|
|
if (manager != null && manager.FavoritesPanel != null)
|
|
manager.FavoritesPanel.ReturnToSlot(this);
|
|
}
|
|
}
|
|
|
|
public void SetIsFavorited(bool favorited)
|
|
{
|
|
isFavorited = favorited;
|
|
}
|
|
|
|
public void SetIsLock(bool locked)
|
|
{
|
|
isLocked = locked;
|
|
}
|
|
}
|
|
}
|