NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/UI/UIButtonEx.cs

281 lines
7.1 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
[RequireComponent(typeof(Button))]
public class UIButtonEx: Button
{
private Button m_button;
private enum DragState
{
None,
Dragging,
End
}
private bool _isPressed;
private float _pressedTime;
private bool _isCalled = false;
private bool _isLongPressing;
private bool _upDragCalled;
private bool _outDragCalled;
2023-08-22 19:08:45 +08:00
private DragState _curDragState;
private Vector2 _mousePos;
private Vector2 _lastFrameMousePos;
private Vector2 _startDragPos;
2024-01-04 19:31:16 +08:00
private bool _isDraggingUp;
2023-10-19 15:00:19 +08:00
private const float MAX_PRESSED_TIME = 0.65f;
2023-08-22 19:08:45 +08:00
private const float MIN_PRESSED_TIME = 0.2f;
2024-01-04 19:31:16 +08:00
private const float MIN_DRAG_DISTANCE = 10f;
2023-08-22 19:08:45 +08:00
private const float MIN_UP_DRAG_DIS = 40f;
public delegate void OnLongPressCall();
public delegate void OnPointUpCall();
public delegate void OnStartDrag();
2024-01-04 19:31:16 +08:00
public delegate void OnDraging();
2023-08-22 19:08:45 +08:00
public delegate void OnEndDrag();
public delegate void OnUpDrag(); //向上拖动
public delegate void OnDragOut(); //向外拖动
2023-08-22 19:08:45 +08:00
public delegate void OnClick();
private OnLongPressCall _OnLongPress;
private OnPointUpCall _OnPointUp; //长按后抬起
private OnStartDrag _OnStartDrag;
2024-01-04 19:31:16 +08:00
private OnDraging _OnDrag;
2023-08-22 19:08:45 +08:00
private OnEndDrag _OnEndDrag;
private OnUpDrag _OnUpDrag;
private OnDragOut _OnDragOut;
2023-08-22 19:08:45 +08:00
private OnClick _OnClick;
2024-07-02 16:21:13 +08:00
public OnUpDrag UpDrag
{
get { return _OnUpDrag; }
}
2023-08-22 19:08:45 +08:00
//测试代码
private int _count = 0;
2024-06-03 19:52:28 +08:00
protected override void Awake()
2023-08-22 19:08:45 +08:00
{
2024-06-03 19:52:28 +08:00
base.Awake();
2023-08-22 19:08:45 +08:00
m_button = GetComponent<Button>();
}
void Update()
{
if (_isPressed)
{
_mousePos = InputManager.Instance.GetTouchPosition();
_pressedTime += Time.deltaTime;
//判断是否进入拖拽
var distance = Vector2.Distance(_startDragPos, _mousePos);
2024-01-04 19:31:16 +08:00
2023-08-22 19:08:45 +08:00
if (!_isLongPressing && distance > MIN_DRAG_DISTANCE && _curDragState == DragState.None)
{
_EnterDrag();
}
if (_IsDragging() && _lastFrameMousePos != _mousePos)
{
_OnDrag?.Invoke();
if (!_upDragCalled)
{
if(Math.Abs(_startDragPos.y - _mousePos.y) > MIN_UP_DRAG_DIS)
{
_OnUpDrag?.Invoke();
_upDragCalled = true;
}
}
if (!_outDragCalled)
2023-08-22 19:08:45 +08:00
{
if (Vector2.Distance(_startDragPos, _mousePos) > MIN_UP_DRAG_DIS)
{
_OnDragOut?.Invoke();
_outDragCalled = true;
}
2023-08-22 19:08:45 +08:00
}
}
if (_pressedTime > MAX_PRESSED_TIME && !_isCalled && !_IsDragging())
{
_isCalled = true;
_isLongPressing = true;
_OnLongPress?.Invoke();
}
_lastFrameMousePos = _mousePos;
2024-01-04 19:31:16 +08:00
//如果是其他组件的拖动导致提前调用了OnPointerUp
if (_isDraggingUp)
{
if (!_upDragCalled)
{
if(Math.Abs(_startDragPos.y - _mousePos.y) > MIN_DRAG_DISTANCE)
{
_OnUpDrag?.Invoke();
_upDragCalled = true;
}
}
_isDraggingUp = false;
_isPressed = false;
}
2023-08-22 19:08:45 +08:00
}
}
private bool _IsDragging()
{
return _curDragState == DragState.Dragging && !_isLongPressing;
}
private void _EnterDrag()
{
_curDragState = DragState.Dragging;
_upDragCalled = false;
_outDragCalled = false;
2023-08-22 19:08:45 +08:00
_OnStartDrag?.Invoke();
}
private void _ExitDrag()
{
_curDragState = DragState.None;
_upDragCalled = false;
_outDragCalled = false;
2023-08-22 19:08:45 +08:00
_OnEndDrag?.Invoke();
}
//按键放下和抬起
public override void OnPointerDown(PointerEventData data)
{
_pressedTime = 0f;
_isPressed = true;
_startDragPos = InputManager.Instance.GetTouchPosition();
_isLongPressing = false;
2024-01-04 19:31:16 +08:00
_upDragCalled = false;
2023-08-22 19:08:45 +08:00
}
public override void OnPointerUp(PointerEventData data)
{
2023-10-19 15:00:19 +08:00
if ((_pressedTime > MIN_PRESSED_TIME && _pressedTime < MAX_PRESSED_TIME || _IsDragging()) && !_isLongPressing)
2023-08-22 19:08:45 +08:00
{
_OnPointUp?.Invoke();
}
else if(_pressedTime > 0)
{
_OnClick?.Invoke();
}
2024-01-04 19:31:16 +08:00
if (!data.dragging)
2023-08-22 19:08:45 +08:00
{
2024-01-04 19:31:16 +08:00
if (_IsDragging())
{
_ExitDrag();
}
_pressedTime = 0f;
_isPressed = false;
_isCalled = false;
_isLongPressing = false;
_upDragCalled = false;
_outDragCalled = false;
_isDraggingUp = false;
2023-08-22 19:08:45 +08:00
}
2024-01-04 19:31:16 +08:00
else
{
_pressedTime = 0f;
//_isPressed = false;
_isCalled = false;
_isLongPressing = false;
//_upDragCalled = false;
_outDragCalled = false;
_isDraggingUp = true;
}
// _pressedTime = 0f;
// _isPressed = false;
// _isCalled = false;
// _isLongPressing = false;
// _upDragCalled = false;
// _outDragCalled = false;
2023-08-22 19:08:45 +08:00
}
//添加事件监听回调,外部调用接口
public void AddLongPressListener(OnLongPressCall func)
{
_OnLongPress += func;
}
public void AddPointUpListener(OnPointUpCall func)
{
_OnPointUp += func;
}
public void AddUpDragListener(OnUpDrag func)
{
_OnUpDrag += func;
}
public void AddOutDragListener(OnDragOut func)
{
_OnDragOut += func;
}
public void AddStartDragListener(OnStartDrag func)
{
_OnStartDrag += func;
}
2024-01-04 19:31:16 +08:00
public void AddOnDragListener(OnDraging func)
{
_OnDrag += func;
}
public void AddEndDragListener(OnEndDrag func)
{
_OnEndDrag += func;
}
2023-08-22 19:08:45 +08:00
public void AddClickListener(OnClick func)
{
_OnClick += func;
}
//移除事件绑定接口,外部调用
public void RemoveAllListener()
{
if (m_button)
{
m_button.onClick.RemoveAllListeners();
}
// Delegate[] onLongPressList = OnLongPress.GetInvocationList();
// Delegate[] onPointUpList = OnPointUp.GetInvocationList();
// foreach (var deleg in onLongPressList)
// {
// OnLongPress -= deleg as OnLongPressCall;
// }
// foreach (var deleg in onPointUpList)
// {
// OnPointUp -= deleg as OnPointUpCall;
// }
}
public void RemoveListener<T>(T call)
{
2023-10-19 15:00:19 +08:00
if (call is OnLongPressCall pressCall)
2023-08-22 19:08:45 +08:00
{
2023-10-19 15:00:19 +08:00
_OnLongPress -= pressCall;
2023-08-22 19:08:45 +08:00
}
}
}