Detecting multiple gesture types using Monogame in Windows 8

Gestures in this environment are hard. Ridiculously hard. There is a ReadGesture function, but it doesn’t work. Let’s imagine that you want to detect the following gestures:

Tap
Double Tap
Drag

Not exactly complex. We’ll need to know some basic information about each gesture:

Tap: where have we tapped
Double tap: where have we double tapped (and that the two taps are in the same location)
Drag: the start and end positions of the drag.

Let’s have a look at some code:

     
TouchPanel.EnabledGestures =
GestureType.FreeDrag | GestureType.DragComplete | GestureType.DoubleTap | GestureType.Tap;

while (TouchPanel.IsGestureAvailable)
{
    GestureSample gs = TouchPanel.ReadGesture();
    switch (gs.GestureType)
    {
        case GestureType.FreeDrag:
            System.Diagnostics.Debug.WriteLine(string.Format("FreeDrag: {0}", gs.Position));
            break;

        case GestureType.DragComplete: 
            System.Diagnostics.Debug.WriteLine(string.Format("DragComplete: {0}", gs.Position));

            break;

        case GestureType.DoubleTap:
            System.Diagnostics.Debug.WriteLine("DoubleTap");

            break;

        case GestureType.Tap:
            System.Diagnostics.Debug.WriteLine("Tap");

            break;

    }
}

So, let’s run this and see what we get. We certainly get all the gestures. However, there are the following issues:

    1. The DragComplete doesn’t provide any information at all, other than the fact that the drag isn’t taking place anymore

    2. The drag works in bursts, so you get many drag events for a single drag

    3. The Tap event fires, but a Double Tap doesn’t replace the Tap, so you get both.

There’s a bit of rolling-your-own here. Let’s start with the drag, because it’s actually the easiest problem to solve. We know when the use finishes dragging, so a simple variable to say that they are dragging and where they started will solve most of this problem:

    private Vector2? dragStart = null;

Why is it nullable? Well, this variable stores two separate states: whether the user is dragging, and where they started.

Next, we need to know where they were last (if anyone from Microsoft ever reads this, please feel free to give a detailed description in the comments why this hoop jumping is necessary!):

    private Vector2? lastPosition = null;

Then we need to store and check these variables; here’s the new section of the switch statement:

    case GestureType.FreeDrag:
                                System.Diagnostics.Debug.WriteLine(string.Format("FreeDrag: {0}", gs.Position));
                        
        if (!dragStart.HasValue)
        {
            dragStart = gs.Position;
        }
        
        lastPosition = gs.Position;
            break;

       case GestureType.DragComplete:                    

           System.Diagnostics.Debug.WriteLine(string.Format("DragComplete: {0}", gs.Position));
                       
           if (dragStart.HasValue)
           {
               var gsDragDelta = lastPosition - dragStart.Value;
               if (gsDragDelta.Value.LengthSquared() > DRAG_TOLERANCE)
               {
                   if (gsDragDelta.Value.X != 0 || gsDragDelta.Value.Y != 0)
                   {
                       dragStart = null;
                       // Do stuff on drag complete
                       break;

That handles the drag. Next, the tap. Handling the Tap is easy, but what if, as I did, you want to handle Tap to do one thing, and Double Tap to do another? This fried my brain for a while; then something occurred to me. What if you delay the action:

    
    case GestureType.DoubleTap:    
        dblTap = true;
        // Do stuff on Double tap                  
        System.Diagnostics.Debug.WriteLine("DoubleTap");
        Task.Delay(100).ContinueWith((args) => { dblTap = false; });
        break;

    case GestureType.Tap:
        Task.Delay(100).ContinueWith((args) =>
        {
            if (!dblTap)
            {
                // DO stuff on Tap                
                System.Diagnostics.Debug.WriteLine("Tap");
            }
        });
        break;

I imagine the delay needs to be experimented with. 100ms seems to work for me.

As usual, if you have any suggestions, comments, or ways to do this better then please leave a comment.

2 thoughts on “Detecting multiple gesture types using Monogame in Windows 8

  1. Pingback: Detecting Keyboard Gestures Using Monogame | Arrested Developing

Leave a Reply

Your email address will not be published. Required fields are marked *