ThreeKingdomsSlingshot/Assets/Scripts/OutGame/UI/TapDrag.cs

70 lines
2.1 KiB
C#

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace TKS.OutGame
{
/// <summary>탭 + 끌기 겸용 — 가로로 끌면 끌기 콜백 · 세로로 끌면 부모 ScrollRect 로 넘긴다(목록 스크롤 유지).</summary>
public class TapDrag : MonoBehaviour, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public Action OnTap;
public Action<PointerEventData> OnDragStart;
public Action<PointerEventData> OnDragMove;
public Action<PointerEventData> OnDragEnd;
/// <summary>true = 어느 방향이든 끌기(편성 자리) · false = 가로로 끌 때만(목록 카드).</summary>
public bool AnyDirection;
ScrollRect scroll;
bool forwarding;
bool dragging;
void Awake()
{
scroll = GetComponentInParent<ScrollRect>();
}
public void OnPointerClick(PointerEventData e)
{
if (e.dragging || dragging || forwarding) return;
if (OnTap != null) OnTap();
}
public void OnBeginDrag(PointerEventData e)
{
bool horizontal = Mathf.Abs(e.delta.x) > Mathf.Abs(e.delta.y);
if (OnDragStart != null && (AnyDirection || horizontal))
{
dragging = true;
OnDragStart(e);
return;
}
if (scroll != null)
{
forwarding = true;
scroll.OnBeginDrag(e);
}
}
public void OnDrag(PointerEventData e)
{
if (dragging)
{
if (OnDragMove != null) OnDragMove(e);
}
else if (forwarding && scroll != null) scroll.OnDrag(e);
}
public void OnEndDrag(PointerEventData e)
{
if (dragging)
{
dragging = false;
if (OnDragEnd != null) OnDragEnd(e);
}
else if (forwarding && scroll != null) scroll.OnEndDrag(e);
forwarding = false;
}
}
}