Touch Form for Windows Mobile

When we take a look at Windows Mobile applications, we see more and more applications that need the ability to work with touch. Yes finger touch and not the touch by Stylus. The TouchFlo 3D software from HTC is a good example of such an application.

So I got an idea. A .NET Compact Framework TouchForm that recognizes some gestures.

TouchForm with Gesture Recognition

Yes I was thinking about very basic gesture recognition. I only want to recognize the following gestures:

  • Drag to North
  • Drag to East
  • Drag to South
  • Drag to West

But what is important though, is the fact that the gesture needs to be recognized. The gesture doesn’t have to be perfect is my opinion.

My idea was making use of Mathematics I learned a long time ago. Making use of the Unit Circle. By translating the start point of the dragging to (0,0) making the end point always having a position in the Unit Circle. The next step was the calculation of the angle inside the Unit Circle. We could basically use the following translation:

  • East (0 or 360 degrees)
  • North (90 degrees)
  • West (180 degrees)
  • South (270 degrees)

But of course I wanted to calculate everything instead of using a translation table. It took me some time to completely understand the mathematics again (I haven’t used this Mathematics for almost 10 years). Using the combination of both Sinus and Cosines to determine the real angle based on coordinates.

After the angle was calculated I want to ensure that the users gesture is also recognized when their dragging isn’t exact 90 degrees for example but might be 85 degrees or 95 degrees as well.

You can see a resulting example in this Silverlight Video. This example is just counting the gestures and showing the amount in labels. The center label is used for unrecognized gestures.

You can easily use this code. Just make sure you inherit your own form from TouchForm instead of the normal Form. Beside that you can attach a listener to the GestureRecognized event.

public partial class MyTouchForm : MM.Touch.TouchForm

{

    private int amountOfEast;

    private int amountOfNorth;

    private int amountOfNotRecognized;

    private int amountOfSouth;

    private int amountOfWest;

 

    public MyTouchForm()

    {

        InitializeComponent();

        GestureRecognized += MyTouchForm_GestureRecognized;

    }

 

    private void MyTouchForm_GestureRecognized(object sender, GestureRecognizedEventArgs e)

    {

        if (e.Gesture == MM.Touch.GestureType.NotRecognized)

            amountOfNotRecognized++;

        else if (e.Gesture == MM.Touch.GestureType.North)

            amountOfNorth++;

        else if (e.Gesture == MM.Touch.GestureType.East)

            amountOfEast++;

        else if (e.Gesture == MM.Touch.GestureType.South)

            amountOfSouth++;

        else if (e.Gesture == MM.Touch.GestureType.West)

            amountOfWest++;

 

        northLabel.Text = amountOfNorth.ToString();

        eastLabel.Text = amountOfEast.ToString();

        southLabel.Text = amountOfSouth.ToString();

        westLabel.Text = amountOfWest.ToString();

        notRecognizedLabel.Text = amountOfNotRecognized.ToString();

    }

}

You can download the source code here. Please let me know if you’re using this library in your project.

Gravatar