短按改长按
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using AibisDream.Framework;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
namespace AibisDream
|
||||
{
|
||||
@@ -16,6 +16,11 @@ namespace AibisDream
|
||||
|
||||
[SerializeField] private int idleLayerId;
|
||||
[SerializeField] private int idleLayerOrder;
|
||||
|
||||
[Header("长按设置")]
|
||||
[SerializeField] private float longPressDuration = 0.65f;
|
||||
[SerializeField] private float longPressScale = 1.1f;
|
||||
[SerializeField] private float shakeIntensity = 0.1f;
|
||||
#endregion
|
||||
|
||||
// 拖拽状态
|
||||
@@ -32,6 +37,13 @@ namespace AibisDream
|
||||
}
|
||||
}
|
||||
private SnapResult _snapResult;
|
||||
|
||||
// 长按相关状态
|
||||
private bool _isPressing;
|
||||
private bool _isLongPressTriggered;
|
||||
private Coroutine _longPressCoroutine;
|
||||
private Vector3 _originalScale;
|
||||
private Vector3 _originalPosition;
|
||||
|
||||
// 组件引用
|
||||
private BlockShape _blockShape;
|
||||
@@ -56,14 +68,23 @@ namespace AibisDream
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_eventTrigger.UnRegister(EventTriggerType.PointerDown, OnPointerDown);
|
||||
_eventTrigger.UnRegister(EventTriggerType.PointerUp, OnPointerUp);
|
||||
_eventTrigger.UnRegister(EventTriggerType.PointerClick, OnPointerClick);
|
||||
|
||||
// 清理协程
|
||||
if (_longPressCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_longPressCoroutine);
|
||||
_longPressCoroutine = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void RegisterEvent()
|
||||
{
|
||||
_eventTrigger.Register(EventTriggerType.PointerDown, OnPointerDown);
|
||||
_eventTrigger.Register(EventTriggerType.PointerUp, OnPointerUp);
|
||||
_eventTrigger.Register(EventTriggerType.PointerClick, OnPointerClick);
|
||||
// _eventTrigger.Register(EventTriggerType.PointerDown, OnPointerDown);
|
||||
// _eventTrigger.Register(EventTriggerType.PointerUp, OnPointerUp);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
@@ -74,6 +95,38 @@ namespace AibisDream
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPointerDown(BaseEventData eventData)
|
||||
{
|
||||
if (eventData is not PointerEventData { button: PointerEventData.InputButton.Left })
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isDraggable) return;
|
||||
|
||||
// 只有在静止状态才能开始长按
|
||||
if (CurrentState == DragState.Idle || CurrentState == DragState.Placed)
|
||||
{
|
||||
_isPressing = true;
|
||||
_isLongPressTriggered = false;
|
||||
_longPressCoroutine = StartCoroutine(LongPressCoroutine());
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPointerUp(BaseEventData eventData)
|
||||
{
|
||||
if (eventData is not PointerEventData { button: PointerEventData.InputButton.Left })
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果还在长按过程中(未完成),则取消
|
||||
if (_isPressing && !_isLongPressTriggered)
|
||||
{
|
||||
CancelLongPress();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPointerClick(BaseEventData eventData)
|
||||
{
|
||||
if (eventData is not PointerEventData { button: PointerEventData.InputButton.Left })
|
||||
@@ -81,13 +134,8 @@ namespace AibisDream
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果当前物体不在拖拽中则进入拖拽逻辑
|
||||
if (CurrentState == DragState.Idle || CurrentState == DragState.Placed)
|
||||
{
|
||||
StartDrag();
|
||||
}
|
||||
// 如果当前物体在拖拽中则取消拖拽
|
||||
else
|
||||
// 只有在拖拽中点击才结束拖拽
|
||||
if (CurrentState == DragState.Dragging && _isLongPressTriggered)
|
||||
{
|
||||
EndDrag();
|
||||
}
|
||||
@@ -108,6 +156,8 @@ namespace AibisDream
|
||||
|
||||
public void EndDrag()
|
||||
{
|
||||
// 清理长按状态
|
||||
CancelLongPress();
|
||||
|
||||
// 先吸附到回收槽
|
||||
var recycled = BlockPuzzleSystem.Instance.TrySnapToRecycleSlot(_blockShape);
|
||||
@@ -133,8 +183,11 @@ namespace AibisDream
|
||||
_eventTrigger.OnPointerUp(null);
|
||||
OnDragEnd?.Invoke(_blockShape, CurrentState == DragState.Placed);
|
||||
}
|
||||
|
||||
// TODO: 都没吸附到,则恢复原位
|
||||
else
|
||||
{
|
||||
// 都没吸附到,恢复原状态和缩放
|
||||
transform.localScale = Vector3.one;
|
||||
}
|
||||
|
||||
TargetGrid.GridVisualizer.UpdateAllCellsVisual();
|
||||
}
|
||||
@@ -157,6 +210,7 @@ namespace AibisDream
|
||||
|
||||
OnDragUpdate?.Invoke(_blockShape);
|
||||
}
|
||||
|
||||
private void HandleStateChange(DragState newState)
|
||||
{
|
||||
var sortingGroup = _blockShape?.SortingGroup;
|
||||
@@ -182,6 +236,57 @@ namespace AibisDream
|
||||
return new Vector3(mouseWorldPos.x, mouseWorldPos.y, 0);
|
||||
}
|
||||
|
||||
private IEnumerator LongPressCoroutine()
|
||||
{
|
||||
float elapsed = 0f;
|
||||
_originalScale = transform.localScale;
|
||||
_originalPosition = transform.localPosition;
|
||||
|
||||
while (elapsed < longPressDuration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
float progress = elapsed / longPressDuration;
|
||||
|
||||
// 抖动效果(轻微)
|
||||
float shake = Mathf.Sin(elapsed * 30f) * shakeIntensity * progress;
|
||||
transform.localPosition = _originalPosition + Vector3.right * shake;
|
||||
|
||||
// 放大效果(缓慢 EaseOut)
|
||||
float scale = Mathf.Lerp(1f, longPressScale, EaseOutQuad(progress));
|
||||
transform.localScale = _originalScale * scale;
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// 完成长按,开始拖拽
|
||||
_isLongPressTriggered = true;
|
||||
StartDrag();
|
||||
}
|
||||
|
||||
private float EaseOutQuad(float t)
|
||||
{
|
||||
return t * (2f - t);
|
||||
}
|
||||
|
||||
private void CancelLongPress()
|
||||
{
|
||||
if (_longPressCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_longPressCoroutine);
|
||||
_longPressCoroutine = null;
|
||||
}
|
||||
|
||||
// 恢复原始状态
|
||||
if (_isPressing && !_isLongPressTriggered)
|
||||
{
|
||||
transform.localScale = _originalScale;
|
||||
transform.localPosition = _originalPosition;
|
||||
}
|
||||
|
||||
_isPressing = false;
|
||||
_isLongPressTriggered = false;
|
||||
}
|
||||
|
||||
// 实现IInteraction接口
|
||||
public bool IsActive => true;
|
||||
public bool IsAvailable => true;
|
||||
|
||||
Reference in New Issue
Block a user