using UnityEngine; using System.Collections; /// /// Long-Press gesture: detects when the finger is held down without moving, for a specific duration /// [AddComponentMenu( "FingerGestures/Gesture Recognizers/Long Press" )] public class LongPressGestureRecognizer : AveragedGestureRecognizer { /// /// Event fired when the the gesture is recognized /// public event EventDelegate OnLongPress; /// /// How long the finger must stay down without moving in order to validate the gesture /// public float Duration = 0.3f; /// /// How far the finger is allowed to move around its starting position without breaking the gesture /// 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 ); } }