This is the code that is used at the pre-Hackathon training session on Thursday.

The code is explained using the “Flower” type of comments (don’t use this in production, it is used by me to illustrate many points in the code).

If you do not open the attached (scroll down to the bottom of the page to get the code).

Basically, with the Mango version of the WP7 O/S gestures are easily caught in programming by the use of the GestureSample enumerations that allow you to implement the test for the type of gesture such as tap, double tap, and so forth.

If you want, you can cut and paste any of the code as necessary.

 

Code Snippet
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input.Touch;
  5.  
  6. namespace Gestures
  7. {
  8.     public class Game1 : Microsoft.Xna.Framework.Game
  9.     {
  10.         /*******************************************************************************
  11.          * This space is often referred to as the "Class Level"
  12.          * ****************************************************************************/
  13.         GraphicsDeviceManager graphics;
  14.         SpriteBatch spriteBatch;
  15.         String msg = "Perform a Touch Motion";
  16.         Vector2 v_postionMsg;
  17.         SpriteFont WeirdoFont;
  18.  
  19.         /*******************************************************************************
  20.          * This is the end of the "Class Level"
  21.          ******************************************************************************/
  22.         public Game1()
  23.         {
  24.             /***************************************************************************
  25.              * Class Constructor area
  26.              **************************************************************************/
  27.             graphics = new GraphicsDeviceManager(this);
  28.             Content.RootDirectory = "Content";
  29.  
  30.             // Frame rate is 30 fps by default for Windows Phone.
  31.             TargetElapsedTime = TimeSpan.FromTicks(333333);
  32.             /**************************************************************************
  33.              * This is the end of the "Class Constructor"
  34.              *************************************************************************/
  35.         }
  36.  
  37.         protected override void Initialize()
  38.         {
  39.             /**********************************************************************************
  40.              * These are the touchpanel gestures or touchie feelies
  41.              *
  42.              * *******************************************************************************/
  43.             TouchPanel.EnabledGestures =
  44.                         GestureType.Tap | GestureType.DoubleTap
  45.                         | GestureType.Hold
  46.                         | GestureType.HorizontalDrag | GestureType.VerticalDrag
  47.                         | GestureType.FreeDrag | GestureType.DragComplete
  48.                         | GestureType.Pinch | GestureType.PinchComplete
  49.                         | GestureType.Flick;
  50.             /**********************************************************************************
  51.              * Just initializing the postion of the indication of the touch/gesture
  52.              * Could have used the "Zero" and had the indication at the top of the screen
  53.              * that would have looked at:
  54.              * v_postionMsg.Zero
  55.              *********************************************************************************/
  56.             v_postionMsg.X = 10;
  57.             v_postionMsg.Y = 420;
  58.             /*********************************************************************************
  59.              * base.Initialize(); is inserted by the default XNA Template
  60.              ********************************************************************************/
  61.             base.Initialize();
  62.         }
  63.  
  64.  
  65.         protected override void LoadContent()
  66.         {
  67.             /******************************************************************
  68.              * Create a new SpriteBatch, which can be used to draw textures.
  69.              * This is put in by default, normally I do not use an object class
  70.              * name with the first letter lower case because it is as
  71.              * descriptive as I would like.  You would make the choice as a
  72.              * developer.
  73.              * ***************************************************************/
  74.             spriteBatch = new SpriteBatch(GraphicsDevice);
  75.             WeirdoFont = Content.Load<SpriteFont>("WeirdoFont");
  76.             /*****************************************************************/
  77.         }
  78.         protected override void Update(GameTime gameTime)
  79.         {
  80.             /*****************************************************************
  81.              * The TouchType() method call was made using the "Extract Method"
  82.              * found in the refactor fly out when you right click on a one or
  83.              * more selected lines of code.
  84.              * **************************************************************/
  85.             TouchType();
  86.             /*****************************************************************
  87.              * base.Update(gameTime); is a line of code entered by the default
  88.              * template.
  89.              * **************************************************************/
  90.             base.Update(gameTime);
  91.             /****************************************************************/
  92.         }
  93.         protected override void Draw(GameTime gameTime)
  94.         {
  95.             /*****************************************************************
  96.              * The following line GraphicsDevice.Clear(Color.CornFlowerBlue)
  97.              * is entered by default, I changed the background to Color.White
  98.              * **************************************************************/
  99.             GraphicsDevice.Clear(Color.White);
  100.             /*****************************************************************
  101.              * spriteBatch.Begin(); starts the reading of the various
  102.              * spriteBatch s
  103.              * **************************************************************/
  104.             spriteBatch.Begin();
  105.             /*********************************************************************
  106.              * The following spriteBatch.Drawstring uses the WeirdoFont, followed
  107.              * by the related message, the vector that has the position of the
  108.              * message that will tell you what the touch/gesture was and then the
  109.              * color of the font.
  110.              ********************************************************************/
  111.             spriteBatch.DrawString(WeirdoFont, msg, v_postionMsg, Color.Black);
  112.             /*********************************************************************
  113.              * spriteBatch.End(); indicates the end of the drawing of the spritebatch
  114.              * items, there could be more than one line
  115.              * ******************************************************************/
  116.             spriteBatch.End();
  117.             /*********************************************************************
  118.              * base.Draw(gameTime); is entered by default by the default template
  119.              * ******************************************************************/
  120.             base.Draw(gameTime);
  121.         }
  122.  
  123.         /*************************************************************************
  124.          * This method was originally written in the Update method and extracted
  125.          * using the refactor flyout menu, "extract method"
  126.          * **********************************************************************/
  127.         private void TouchType()
  128.         {
  129.             /********************************************************************
  130.              * Check to make sure that the TouchPanel can accept gestures
  131.              *******************************************************************/
  132.             if (TouchPanel.IsGestureAvailable)
  133.             {
  134.                 /********************************************************************
  135.                  * GestureSample is an object that is contained in the .NET
  136.                  * Framework NameSpace Microsoft.Xna.Framework.Input.Touch
  137.                  * This is a helper enumeration that provides an easy way for
  138.                  * software designers to test which gestures has been used
  139.                  *******************************************************************/
  140.                 GestureSample gesture = TouchPanel.ReadGesture();
  141.                 /********************************************************************
  142.                  * The switch is also called a "case" in other languages.  What is
  143.                  * included in the paratheses after the switch is the "test" that
  144.                  * is performed by the software.
  145.                  *******************************************************************/
  146.                 switch (gesture.GestureType)
  147.                 {
  148.                     case GestureType.Hold:
  149.                         msg = "Hold Touch";
  150.                         break;
  151.                     case GestureType.Tap:
  152.                         msg = "Tap Touch";
  153.                         break;
  154.                     case GestureType.DoubleTap:
  155.                         msg = "Tap Double Touch";
  156.                         break;
  157.  
  158.                     case GestureType.HorizontalDrag:
  159.                         msg = "Drag Hort Touch";
  160.                         break;
  161.  
  162.                     case GestureType.VerticalDrag:
  163.                         msg = "Drag Vert Touch";
  164.                         break;
  165.  
  166.                     case GestureType.FreeDrag:
  167.                         msg = "Free Drag Touch";
  168.                         break;
  169.  
  170.                     case GestureType.DragComplete:
  171.                         msg = "Completed Drag Touch";
  172.                         break;
  173.  
  174.                     case GestureType.Flick:
  175.                         msg = "Flick Touch";
  176.                         break;
  177.  
  178.                     case GestureType.Pinch:
  179.                         msg = "Pinch Touch";
  180.                         break;
  181.  
  182.                     case GestureType.PinchComplete:
  183.                         msg = "Pinch Done Touch";
  184.                         break;
  185.                     /******************************************************/
  186.                 }
  187.                 /************************************************
  188.                  * This is how to detect the gesture position
  189.                  * v_postionMsg = gesture.Position;
  190.                  * and you would place the code here.
  191.                  ***********************************************/
  192.             }
  193.         }
  194.     }
  195. }