Files
Main/Assets/Launcher/ExternalLibs/FingerGestures/Components/LongPressGestureRecognizer.cs
2025-01-25 04:38:09 +08:00

57 lines
1.6 KiB
C#

using UnityEngine;
using System.Collections;
/// <summary>
/// Long-Press gesture: detects when the finger is held down without moving, for a specific duration
/// </summary>
[AddComponentMenu( "FingerGestures/Gesture Recognizers/Long Press" )]
public class LongPressGestureRecognizer : AveragedGestureRecognizer
{
/// <summary>
/// Event fired when the the gesture is recognized
/// </summary>
public event EventDelegate<LongPressGestureRecognizer> OnLongPress;
/// <summary>
/// How long the finger must stay down without moving in order to validate the gesture
/// </summary>
public float Duration = 0.3f;
/// <summary>
/// How far the finger is allowed to move around its starting position without breaking the gesture
/// </summary>
public float MoveTolerance = 5.0f;
protected override void OnBegin( FingerGestures.IFingerList touches )
{
Duration = 0.3f;
Position = touches.GetAveragePosition();
StartPosition = Position;
}
protected override GestureState OnActive( FingerGestures.IFingerList touches )
{
if( touches.Count != RequiredFingerCount )
return GestureState.Failed;
if( ElapsedTime >= Duration )
{
RaiseOnLongPress();
return GestureState.Recognized;
}
// check if we moved too far from initial position
if( touches.GetAverageDistanceFromStart() > MoveTolerance )
return GestureState.Failed;
return GestureState.InProgress;
}
protected void RaiseOnLongPress()
{
if( OnLongPress != null )
OnLongPress( this );
}
}