|
@@ -0,0 +1,239 @@
|
|
|
+using System;
|
|
|
+using System.Collections;
|
|
|
+using System.Collections.Generic;
|
|
|
+using Fort23.UTool;
|
|
|
+using UnityEngine;
|
|
|
+using UnityEngine.EventSystems;
|
|
|
+
|
|
|
+public class LongPressDragBtn : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler,
|
|
|
+ IPointerClickHandler, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerEnterHandler
|
|
|
+{
|
|
|
+ private float _time;
|
|
|
+
|
|
|
+ public float startTime = 0.5f;
|
|
|
+
|
|
|
+ public float intervalTime = 0.05f;
|
|
|
+
|
|
|
+ public Action onClick;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 连按
|
|
|
+ /// </summary>
|
|
|
+ public Action longPress;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 长按结束
|
|
|
+ /// </summary>
|
|
|
+ public Action longPressEnd;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 是否按下状态
|
|
|
+ /// </summary>
|
|
|
+ [HideInInspector] public bool isPressd;
|
|
|
+
|
|
|
+ private bool _isTriggerLongPress;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 是否触发了长按
|
|
|
+ /// </summary>
|
|
|
+ public bool IsTriggerLongPress => _isTriggerLongPress;
|
|
|
+
|
|
|
+ // 拖拽相关变量
|
|
|
+ private bool _isDragging;
|
|
|
+ private Vector2 _dragStartPosition;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 拖拽阈值(像素),超过这个距离才认为是拖拽
|
|
|
+ /// </summary>
|
|
|
+ public float dragThreshold = 10f;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 拖拽开始事件
|
|
|
+ /// </summary>
|
|
|
+ public Action<PointerEventData> onDragStart;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 拖拽进行中事件
|
|
|
+ /// </summary>
|
|
|
+ public Action<PointerEventData> onDrag;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 拖拽结束事件
|
|
|
+ /// </summary>
|
|
|
+ public Action<PointerEventData> onDragEnd;
|
|
|
+
|
|
|
+ public Action<PointerEventData> onPointerEnter;
|
|
|
+
|
|
|
+ public Action<PointerEventData> onPointerExit;
|
|
|
+
|
|
|
+ void OnPress(bool pressed)
|
|
|
+ {
|
|
|
+ if (pressed) // 按下
|
|
|
+ {
|
|
|
+ isPressd = true;
|
|
|
+ _isDragging = false; // 重置拖拽状态
|
|
|
+ }
|
|
|
+ else // 松开或离开
|
|
|
+ {
|
|
|
+ if (isPressd)
|
|
|
+ {
|
|
|
+ if (_isTriggerLongPress && !_isDragging) // 触发了长按且不是拖拽,执行 longPressEnd
|
|
|
+ {
|
|
|
+ longPressEnd?.Invoke();
|
|
|
+ _isTriggerLongPress = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ isPressd = false;
|
|
|
+ _time = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Update()
|
|
|
+ {
|
|
|
+ // 如果正在拖拽,不执行长按逻辑
|
|
|
+ if (_isDragging) return;
|
|
|
+
|
|
|
+ if (isPressd && !_isTriggerLongPress)
|
|
|
+ {
|
|
|
+ _time += Time.deltaTime;
|
|
|
+ if (_time >= startTime)
|
|
|
+ {
|
|
|
+ _isTriggerLongPress = true;
|
|
|
+ _time -= intervalTime;
|
|
|
+ longPress?.Invoke();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (isPressd && _isTriggerLongPress)
|
|
|
+ {
|
|
|
+ _time += Time.deltaTime;
|
|
|
+ if (_time >= intervalTime)
|
|
|
+ {
|
|
|
+ _time -= intervalTime;
|
|
|
+ longPress?.Invoke();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 终止
|
|
|
+ /// </summary>
|
|
|
+ public void Terminated()
|
|
|
+ {
|
|
|
+ if (isPressd)
|
|
|
+ {
|
|
|
+ isPressd = false;
|
|
|
+ if (_isTriggerLongPress)
|
|
|
+ {
|
|
|
+ longPressEnd?.Invoke();
|
|
|
+ }
|
|
|
+
|
|
|
+ _isTriggerLongPress = false;
|
|
|
+ _time = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ _isDragging = false; // 同时终止拖拽状态
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnPointerDown(PointerEventData eventData)
|
|
|
+ {
|
|
|
+ _dragStartPosition = eventData.position;
|
|
|
+ OnPress(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnPointerUp(PointerEventData eventData)
|
|
|
+ {
|
|
|
+ // 如果正在拖拽,先触发拖拽结束
|
|
|
+ if (_isDragging)
|
|
|
+ {
|
|
|
+ OnEndDrag(eventData);
|
|
|
+ }
|
|
|
+
|
|
|
+ OnPress(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnPointerExit(PointerEventData eventData)
|
|
|
+ {
|
|
|
+ onPointerExit?.Invoke(eventData);
|
|
|
+ // 如果正在拖拽,不处理退出事件(拖拽过程中允许离开按钮区域)
|
|
|
+ if (!_isDragging)
|
|
|
+ {
|
|
|
+ OnPress(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnPointerEnter(PointerEventData eventData)
|
|
|
+ {
|
|
|
+ onPointerEnter?.Invoke(eventData);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnPointerClick(PointerEventData eventData)
|
|
|
+ {
|
|
|
+ // 如果发生了拖拽,不触发点击事件
|
|
|
+ if (_isDragging || _isTriggerLongPress) return;
|
|
|
+
|
|
|
+ onClick?.Invoke();
|
|
|
+ isPressd = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ========== 拖拽相关方法 ==========
|
|
|
+
|
|
|
+ public void OnBeginDrag(PointerEventData eventData)
|
|
|
+ {
|
|
|
+ // 检查是否达到拖拽阈值
|
|
|
+ if (Vector2.Distance(eventData.position, _dragStartPosition) >= dragThreshold && !_isDragging)
|
|
|
+ {
|
|
|
+ _isDragging = true;
|
|
|
+ // 开始拖拽时终止长按逻辑
|
|
|
+ if (_isTriggerLongPress)
|
|
|
+ {
|
|
|
+ longPressEnd?.Invoke();
|
|
|
+ _isTriggerLongPress = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ onDragStart?.Invoke(eventData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnDrag(PointerEventData eventData)
|
|
|
+ {
|
|
|
+ if (_isDragging)
|
|
|
+ {
|
|
|
+ onDrag?.Invoke(eventData);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // 如果还没有正式开始拖拽,检查是否达到阈值
|
|
|
+ if (Vector2.Distance(eventData.position, _dragStartPosition) >= dragThreshold)
|
|
|
+ {
|
|
|
+ OnBeginDrag(eventData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnEndDrag(PointerEventData eventData)
|
|
|
+ {
|
|
|
+ if (_isDragging)
|
|
|
+ {
|
|
|
+ onDragEnd?.Invoke(eventData);
|
|
|
+ _isDragging = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 检查是否正在拖拽
|
|
|
+ /// </summary>
|
|
|
+ public bool IsDragging => _isDragging;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 强制结束拖拽
|
|
|
+ /// </summary>
|
|
|
+ public void ForceEndDrag()
|
|
|
+ {
|
|
|
+ if (_isDragging)
|
|
|
+ {
|
|
|
+ _isDragging = false;
|
|
|
+ onDragEnd?.Invoke(null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|