UCLA Material: Using Gestures
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
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input.Touch;
- namespace Gestures
- {
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- /*******************************************************************************
- * This space is often referred to as the "Class Level"
- * ****************************************************************************/
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- String msg = "Perform a Touch Motion";
- Vector2 v_postionMsg;
- SpriteFont WeirdoFont;
- /*******************************************************************************
- * This is the end of the "Class Level"
- ******************************************************************************/
- public Game1()
- {
- /***************************************************************************
- * Class Constructor area
- **************************************************************************/
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- // Frame rate is 30 fps by default for Windows Phone.
- TargetElapsedTime = TimeSpan.FromTicks(333333);
- /**************************************************************************
- * This is the end of the "Class Constructor"
- *************************************************************************/
- }
- protected override void Initialize()
- {
- /**********************************************************************************
- * These are the touchpanel gestures or touchie feelies
- *
- * *******************************************************************************/
- TouchPanel.EnabledGestures =
- GestureType.Tap | GestureType.DoubleTap
- | GestureType.Hold
- | GestureType.HorizontalDrag | GestureType.VerticalDrag
- | GestureType.FreeDrag | GestureType.DragComplete
- | GestureType.Pinch | GestureType.PinchComplete
- | GestureType.Flick;
- /**********************************************************************************
- * Just initializing the postion of the indication of the touch/gesture
- * Could have used the "Zero" and had the indication at the top of the screen
- * that would have looked at:
- * v_postionMsg.Zero
- *********************************************************************************/
- v_postionMsg.X = 10;
- v_postionMsg.Y = 420;
- /*********************************************************************************
- * base.Initialize(); is inserted by the default XNA Template
- ********************************************************************************/
- base.Initialize();
- }
- protected override void LoadContent()
- {
- /******************************************************************
- * Create a new SpriteBatch, which can be used to draw textures.
- * This is put in by default, normally I do not use an object class
- * name with the first letter lower case because it is as
- * descriptive as I would like. You would make the choice as a
- * developer.
- * ***************************************************************/
- spriteBatch = new SpriteBatch(GraphicsDevice);
- WeirdoFont = Content.Load<SpriteFont>("WeirdoFont");
- /*****************************************************************/
- }
- protected override void Update(GameTime gameTime)
- {
- /*****************************************************************
- * The TouchType() method call was made using the "Extract Method"
- * found in the refactor fly out when you right click on a one or
- * more selected lines of code.
- * **************************************************************/
- TouchType();
- /*****************************************************************
- * base.Update(gameTime); is a line of code entered by the default
- * template.
- * **************************************************************/
- base.Update(gameTime);
- /****************************************************************/
- }
- protected override void Draw(GameTime gameTime)
- {
- /*****************************************************************
- * The following line GraphicsDevice.Clear(Color.CornFlowerBlue)
- * is entered by default, I changed the background to Color.White
- * **************************************************************/
- GraphicsDevice.Clear(Color.White);
- /*****************************************************************
- * spriteBatch.Begin(); starts the reading of the various
- * spriteBatch s
- * **************************************************************/
- spriteBatch.Begin();
- /*********************************************************************
- * The following spriteBatch.Drawstring uses the WeirdoFont, followed
- * by the related message, the vector that has the position of the
- * message that will tell you what the touch/gesture was and then the
- * color of the font.
- ********************************************************************/
- spriteBatch.DrawString(WeirdoFont, msg, v_postionMsg, Color.Black);
- /*********************************************************************
- * spriteBatch.End(); indicates the end of the drawing of the spritebatch
- * items, there could be more than one line
- * ******************************************************************/
- spriteBatch.End();
- /*********************************************************************
- * base.Draw(gameTime); is entered by default by the default template
- * ******************************************************************/
- base.Draw(gameTime);
- }
- /*************************************************************************
- * This method was originally written in the Update method and extracted
- * using the refactor flyout menu, "extract method"
- * **********************************************************************/
- private void TouchType()
- {
- /********************************************************************
- * Check to make sure that the TouchPanel can accept gestures
- *******************************************************************/
- if (TouchPanel.IsGestureAvailable)
- {
- /********************************************************************
- * GestureSample is an object that is contained in the .NET
- * Framework NameSpace Microsoft.Xna.Framework.Input.Touch
- * This is a helper enumeration that provides an easy way for
- * software designers to test which gestures has been used
- *******************************************************************/
- GestureSample gesture = TouchPanel.ReadGesture();
- /********************************************************************
- * The switch is also called a "case" in other languages. What is
- * included in the paratheses after the switch is the "test" that
- * is performed by the software.
- *******************************************************************/
- switch (gesture.GestureType)
- {
- case GestureType.Hold:
- msg = "Hold Touch";
- break;
- case GestureType.Tap:
- msg = "Tap Touch";
- break;
- case GestureType.DoubleTap:
- msg = "Tap Double Touch";
- break;
- case GestureType.HorizontalDrag:
- msg = "Drag Hort Touch";
- break;
- case GestureType.VerticalDrag:
- msg = "Drag Vert Touch";
- break;
- case GestureType.FreeDrag:
- msg = "Free Drag Touch";
- break;
- case GestureType.DragComplete:
- msg = "Completed Drag Touch";
- break;
- case GestureType.Flick:
- msg = "Flick Touch";
- break;
- case GestureType.Pinch:
- msg = "Pinch Touch";
- break;
- case GestureType.PinchComplete:
- msg = "Pinch Done Touch";
- break;
- /******************************************************/
- }
- /************************************************
- * This is how to detect the gesture position
- * v_postionMsg = gesture.Position;
- * and you would place the code here.
- ***********************************************/
- }
- }
- }
- }