Mutually exclusive gestures, how to.

Random discussions.

Mutually exclusive gestures, how to.

Postby jorrit5477 » Sun Aug 21, 2016 8:53 pm

I have a setup with a Fullscreen Layer and a Camera Layer attached to the main camera, and a ScreenTransformGesture attached to this camera as well. To another GameObject I have a tap gesture and a custom gesture attached. Gestures are working correctly.

All gestures work with one touch point. What I would like to accomplish is that the ScreenTransformGesture can only begin when the custom gesture is not executing and vice versa. What would be the best way to mutually exclude gestures not operating on the same layer and not on the same object?
jorrit5477
 
Posts: 8
Joined: Sun Feb 08, 2015 1:58 pm

Re: Mutually exclusive gestures, how to.

Postby jorrit5477 » Wed Aug 31, 2016 11:51 am

OK, I figured it out. What I did was creating an implementation of IGestureDelegate and set this on the IGestureManager instance as the GlobaleGestureDelegate. This allows to get notified just before a Gesture begins in ShouldBegin(Gesture gesture). In this method I store the reference to the first exclusive gesture, and return false if this reference is not null.

To this implementation I added a NotifyGestureEnded(Gesture gesture) method, and ensured this method is called when an exclusive gesture completes/fails or otherwise ends. If the gesture passed is the set exclusive gesture the stored reference is set to null, so other exclusive gesture can begin again.

Code: Select all
using System;
using TouchScript;
using TouchScript.Gestures;
using UnityEngine;

public class ExclusiveGestureDelegate : MonoBehaviour, IGestureDelegate
{
   #region Private Fields

   private Gesture currentExclusiveGesture;

   #endregion

   #region Unity Methods

   private void OnEnable()
   {
      GestureManager.Instance.GlobalGestureDelegate = this;
   }

   private void OnDisable()
   {
      if (GestureManager.Instance != null)
      {
         GestureManager.Instance.GlobalGestureDelegate = null;
      }
   }

   #endregion

   /// <summary>
   ///
   /// </summary>
   /// <param name="gesture"></param>
   public void NotifyGestureEnded(Gesture gesture)
   {
      if (currentExclusiveGesture == gesture)
      {
         currentExclusiveGesture = null;
      }
   }

   #region IGestureDelegate

   /// <inheritdoc />
   public bool ShouldBegin(Gesture gesture)
   {
      if (!currentExclusiveGesture)
      {
         currentExclusiveGesture = gesture;
         return true;
      }

      return false;
   }

   /// <inheritdoc />
   public bool ShouldReceiveTouch(Gesture gesture, TouchPoint touch)
   {
      return true;
   }

   /// <inheritdoc />
   public bool ShouldRecognizeSimultaneously(Gesture first, Gesture second)
   {
      return true;
   }

   #endregion
}
jorrit5477
 
Posts: 8
Joined: Sun Feb 08, 2015 1:58 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest