mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
ADD UI Event
ADD UI Event
This commit is contained in:
117
Assets/TEngine/Runtime/UI/Extend/DragHandler.cs
Normal file
117
Assets/TEngine/Runtime/UI/Extend/DragHandler.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
public class DragHandler : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
|
||||
{
|
||||
private OnDragEvent _onDrag;
|
||||
private OnDragEvent _onBeginDrag;
|
||||
private OnDragEvent _onEndDrag;
|
||||
private bool _isOnDrag = false;
|
||||
private RectTransform _rect;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_rect = (transform.parent) ? transform.parent.GetComponent<RectTransform>() : null;
|
||||
}
|
||||
|
||||
public OnDragEvent onDrag
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_onDrag == null)
|
||||
{
|
||||
_onDrag = new OnDragEvent();
|
||||
}
|
||||
return _onDrag;
|
||||
}
|
||||
}
|
||||
|
||||
public OnDragEvent onBeginDrag
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_onBeginDrag == null)
|
||||
{
|
||||
_onBeginDrag = new OnDragEvent();
|
||||
}
|
||||
return _onBeginDrag;
|
||||
}
|
||||
}
|
||||
|
||||
public OnDragEvent onEndDrag
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_onEndDrag == null)
|
||||
{
|
||||
_onEndDrag = new OnDragEvent();
|
||||
}
|
||||
return _onEndDrag;
|
||||
}
|
||||
}
|
||||
|
||||
public static DragHandler Get(GameObject go)
|
||||
{
|
||||
DragHandler listener = go.GetComponent<DragHandler>();
|
||||
if (listener == null) listener = go.AddComponent<DragHandler>();
|
||||
return listener;
|
||||
}
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
if (eventData.button != PointerEventData.InputButton.Left) return;
|
||||
_isOnDrag = true;
|
||||
if (onDrag != null)
|
||||
{
|
||||
Vector3 position = new Vector3(eventData.position.x, eventData.position.y, 0);
|
||||
if (_rect)
|
||||
{
|
||||
RectTransformUtility.ScreenPointToWorldPointInRectangle(_rect, eventData.position, eventData.pressEventCamera, out position);
|
||||
position.z = 0;
|
||||
}
|
||||
onDrag.Invoke(position, eventData);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
if (eventData.button != PointerEventData.InputButton.Left) return;
|
||||
_isOnDrag = true;
|
||||
if (onBeginDrag != null)
|
||||
{
|
||||
Vector3 position = new Vector3(eventData.position.x, eventData.position.y, 0);
|
||||
if (_rect)
|
||||
{
|
||||
RectTransformUtility.ScreenPointToWorldPointInRectangle(_rect, eventData.position, eventData.pressEventCamera, out position);
|
||||
//position.z = eventData.pressEventCamera.transform.position.z;
|
||||
position.z = 0;
|
||||
}
|
||||
onBeginDrag.Invoke(position, eventData);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnEndDrag(PointerEventData eventData)
|
||||
{
|
||||
if (eventData.button != PointerEventData.InputButton.Left || !_isOnDrag) return;
|
||||
if (onEndDrag != null)
|
||||
{
|
||||
Vector3 position = new Vector3(eventData.position.x, eventData.position.y, 0);
|
||||
if (_rect)
|
||||
{
|
||||
RectTransformUtility.ScreenPointToWorldPointInRectangle(_rect, eventData.position, eventData.pressEventCamera, out position);
|
||||
position.z = 0;
|
||||
}
|
||||
onEndDrag.Invoke(position, eventData);
|
||||
}
|
||||
_isOnDrag = false;
|
||||
}
|
||||
}
|
||||
|
||||
public class OnDragEvent : UnityEvent<Vector3, PointerEventData>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/UI/Extend/DragHandler.cs.meta
Normal file
11
Assets/TEngine/Runtime/UI/Extend/DragHandler.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 716cdc0097e73004bb9ce44a46f4eef7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
154
Assets/TEngine/Runtime/UI/Extend/PointerLongPress.cs
Normal file
154
Assets/TEngine/Runtime/UI/Extend/PointerLongPress.cs
Normal file
@@ -0,0 +1,154 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
public class PointerLongPress : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler, IPointerClickHandler
|
||||
{
|
||||
public float durationThreshold = 1.0f; //触发长按的时间阈值
|
||||
public float startSpeed = 3; //开始速度
|
||||
public float addSpeed = 2; //加速度
|
||||
public float maxValue = 25; //最大值
|
||||
public float callIntervalTime = 0.05f; //调用间隔时间,时间为0只触发一次长按点击回调
|
||||
|
||||
private bool _isPointerDown = false;
|
||||
private bool _longPressTriggered = false;
|
||||
private bool _isGreaterMaxValue = false; //是否已经大于最大值
|
||||
private float _curTime = 0;
|
||||
private float _curCallTime = 0;
|
||||
|
||||
private OnLongPressEvent _onLongPress;
|
||||
private UnityEvent _onClick;
|
||||
|
||||
public UnityEvent onClick
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_onClick == null)
|
||||
{
|
||||
_onClick = new UnityEvent();
|
||||
}
|
||||
return _onClick;
|
||||
}
|
||||
}
|
||||
|
||||
public OnLongPressEvent onLongPress
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_onLongPress == null)
|
||||
{
|
||||
_onLongPress = new OnLongPressEvent();
|
||||
}
|
||||
return _onLongPress;
|
||||
}
|
||||
}
|
||||
|
||||
public static PointerLongPress Get(GameObject go)
|
||||
{
|
||||
PointerLongPress listener = go.GetComponent<PointerLongPress>();
|
||||
if (listener == null) listener = go.AddComponent<PointerLongPress>();
|
||||
return listener;
|
||||
}
|
||||
|
||||
public void SetLongPressParam(float durationThreshold, float startSpeed, float maxValue, float addSpeed, float callIntervalTime)
|
||||
{
|
||||
this.durationThreshold = durationThreshold;
|
||||
this.startSpeed = startSpeed;
|
||||
this.maxValue = maxValue;
|
||||
this.addSpeed = addSpeed;
|
||||
this.callIntervalTime = callIntervalTime;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_isPointerDown)
|
||||
{
|
||||
if (!_longPressTriggered)
|
||||
{
|
||||
_curTime += Time.deltaTime;
|
||||
if (_curTime >= durationThreshold)
|
||||
{
|
||||
_longPressTriggered = true;
|
||||
_isGreaterMaxValue = false;
|
||||
_curTime = 0;
|
||||
_curCallTime = 0;
|
||||
if (callIntervalTime <= 0)
|
||||
{
|
||||
onLongPress?.Invoke(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (callIntervalTime > 0)
|
||||
{
|
||||
_curTime += Time.deltaTime;
|
||||
_curCallTime += Time.deltaTime;
|
||||
if (_curCallTime >= callIntervalTime)
|
||||
{
|
||||
float value = 0;
|
||||
if (_isGreaterMaxValue)
|
||||
{
|
||||
value = maxValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
float curSpeed = (startSpeed + startSpeed + addSpeed * _curTime) * 0.5f;
|
||||
value = curSpeed * _curTime;
|
||||
if (value >= maxValue)
|
||||
{
|
||||
_isGreaterMaxValue = true;
|
||||
value = maxValue;
|
||||
}
|
||||
}
|
||||
|
||||
//Core.ULogger.LogInfo("value:" + value + ",长按持续时间:" + _curTime);
|
||||
_curCallTime = 0;
|
||||
onLongPress?.Invoke(Mathf.FloorToInt(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_isPointerDown = false;
|
||||
_longPressTriggered = false;
|
||||
_curTime = 0;
|
||||
}
|
||||
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
if (eventData.button != PointerEventData.InputButton.Left) return;
|
||||
_curTime = 0;
|
||||
_isPointerDown = true;
|
||||
_longPressTriggered = false;
|
||||
}
|
||||
|
||||
public void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
if (eventData.button != PointerEventData.InputButton.Left) return;
|
||||
_isPointerDown = false;
|
||||
}
|
||||
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
if (eventData.button != PointerEventData.InputButton.Left) return;
|
||||
_isPointerDown = false;
|
||||
}
|
||||
|
||||
public void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
if (eventData.button != PointerEventData.InputButton.Left) return;
|
||||
if (!_longPressTriggered)
|
||||
{
|
||||
onClick.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class OnLongPressEvent : UnityEvent<int>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
11
Assets/TEngine/Runtime/UI/Extend/PointerLongPress.cs.meta
Normal file
11
Assets/TEngine/Runtime/UI/Extend/PointerLongPress.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1db7650851b8e0347a105f9136510166
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user