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 Pressing" )] public class LongPressingGestureRecognizer : AveragedGestureRecognizer { /// <summary> /// Event fired when the the gesture is recognized /// </summary> public event EventDelegate<LongPressingGestureRecognizer> OnLongPressing; /// <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 from its <see cref="AveragedGestureRecognizer.StartPosition">initial position</see> without making the gesture fail /// </summary> public float MoveTolerance = 5.0f; Vector2 delta = Vector2.zero; Vector2 lastPos = Vector2.zero; /// <summary> /// Amount of motion performed since the last update /// </summary> public Vector2 MoveDelta { get { return delta; } private set { delta = value; } } //protected override bool CanBegin( FingerGestures.IFingerList touches ) //{ // if(!base.CanBegin( touches )) // return false; // if(touches.GetAverageDistanceFromStart() < MoveTolerance) // return false; // return true; //} protected override void OnBegin( FingerGestures.IFingerList touches ) { Duration = 0.3f; Position = touches.GetAveragePosition(); StartPosition = Position; MoveDelta = Vector2.zero; lastPos = Position; } protected override GestureState OnActive( FingerGestures.IFingerList touches ) { if(touches.Count != RequiredFingerCount) { return GestureState.Failed; } Position = touches.GetAveragePosition(); MoveDelta = Position - lastPos; if(ElapsedTime >= Duration) { if(MoveDelta.sqrMagnitude > 0) { lastPos = Position; } RaiseOnLongPressing(); } return GestureState.InProgress; } protected void RaiseOnLongPressing() { if(OnLongPressing != null) { OnLongPressing( this ); } } }