118 lines
3.3 KiB
C#
118 lines
3.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace BF
|
|
{
|
|
public class CellDragEvent : UITouchEvent, IBeginDragHandler, IDragHandler, IEndDragHandler
|
|
{
|
|
private ScrollRect scrollRect;
|
|
[Tooltip("设置值为Drag**")]
|
|
public UITouchEventType slideType = UITouchEventType.Unknown;
|
|
|
|
private Vector2 touchStart = Vector2.zero;
|
|
private Vector2 touchOffest = Vector2.zero;
|
|
private bool isTriggerSlideEvent = false;
|
|
private UITouchEventType bindSlideType;
|
|
|
|
public void AddTouchEventListener(Action<int, float, float> func, int slideType)
|
|
{
|
|
luaFunc = func;
|
|
bindSlideType = (UITouchEventType) slideType;
|
|
}
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
if (!enable)
|
|
{
|
|
return;
|
|
}
|
|
touchStart = eventData.position;
|
|
scrollRect.OnBeginDrag(eventData);
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
if (!enable)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!isTriggerSlideEvent)
|
|
{
|
|
touchOffest = eventData.position - touchStart;
|
|
if (IsTargetDrection(touchOffest))
|
|
{
|
|
if (slideType == bindSlideType)
|
|
{
|
|
scrollRect.StopMovement();
|
|
luaFunc((int) slideType, eventData.position.x, eventData.position.y);
|
|
isTriggerSlideEvent = true;
|
|
}
|
|
else
|
|
{
|
|
luaFunc((int)UITouchEventType.Drag, eventData.position.x, eventData.position.y);
|
|
scrollRect.OnDrag(eventData);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
if (!enable)
|
|
{
|
|
return;
|
|
}
|
|
|
|
isTriggerSlideEvent = false;
|
|
scrollRect.OnEndDrag(eventData);
|
|
}
|
|
public bool IsTargetDrection(Vector2 offset)
|
|
{
|
|
|
|
var offsetAbsX = Mathf.Abs(offset.x);
|
|
var offsetAbsY = Mathf.Abs(offset.y);
|
|
|
|
if (offsetAbsX > EventSystem.current.pixelDragThreshold || offsetAbsY > EventSystem.current.pixelDragThreshold)
|
|
{
|
|
if (offsetAbsX > offsetAbsY)
|
|
{
|
|
if (offset.x > 0)
|
|
{
|
|
slideType = UITouchEventType.DragRight;
|
|
}
|
|
else
|
|
{
|
|
slideType = UITouchEventType.DragLeft;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (offset.y > 0)
|
|
{
|
|
slideType = UITouchEventType.DragUp;
|
|
}
|
|
else
|
|
{
|
|
slideType = UITouchEventType.DragDown;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void SetSrollRect(ScrollRect scroll)
|
|
{
|
|
scrollRect = scroll;
|
|
}
|
|
}
|
|
|
|
}
|