From 83f59b66a21efb47bd4add6e7d78042eabcdce87 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Wed, 25 May 2022 23:37:28 +0800 Subject: [PATCH] ADD UI Event ADD UI Event --- .../TEngine/Runtime/UI/Extend/DragHandler.cs | 117 +++++++++++++ .../Runtime/UI/Extend/DragHandler.cs.meta | 11 ++ .../Runtime/UI/Extend/PointerLongPress.cs | 154 ++++++++++++++++++ .../UI/Extend/PointerLongPress.cs.meta | 11 ++ 4 files changed, 293 insertions(+) create mode 100644 Assets/TEngine/Runtime/UI/Extend/DragHandler.cs create mode 100644 Assets/TEngine/Runtime/UI/Extend/DragHandler.cs.meta create mode 100644 Assets/TEngine/Runtime/UI/Extend/PointerLongPress.cs create mode 100644 Assets/TEngine/Runtime/UI/Extend/PointerLongPress.cs.meta diff --git a/Assets/TEngine/Runtime/UI/Extend/DragHandler.cs b/Assets/TEngine/Runtime/UI/Extend/DragHandler.cs new file mode 100644 index 00000000..8014b966 --- /dev/null +++ b/Assets/TEngine/Runtime/UI/Extend/DragHandler.cs @@ -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() : 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(); + if (listener == null) listener = go.AddComponent(); + 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 + { + + } +} diff --git a/Assets/TEngine/Runtime/UI/Extend/DragHandler.cs.meta b/Assets/TEngine/Runtime/UI/Extend/DragHandler.cs.meta new file mode 100644 index 00000000..57f58de8 --- /dev/null +++ b/Assets/TEngine/Runtime/UI/Extend/DragHandler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 716cdc0097e73004bb9ce44a46f4eef7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/UI/Extend/PointerLongPress.cs b/Assets/TEngine/Runtime/UI/Extend/PointerLongPress.cs new file mode 100644 index 00000000..2e0a9382 --- /dev/null +++ b/Assets/TEngine/Runtime/UI/Extend/PointerLongPress.cs @@ -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(); + if (listener == null) listener = go.AddComponent(); + 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 + { + + } +} diff --git a/Assets/TEngine/Runtime/UI/Extend/PointerLongPress.cs.meta b/Assets/TEngine/Runtime/UI/Extend/PointerLongPress.cs.meta new file mode 100644 index 00000000..ceb7624a --- /dev/null +++ b/Assets/TEngine/Runtime/UI/Extend/PointerLongPress.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1db7650851b8e0347a105f9136510166 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: