MOBILE

XNA Game Studio 4.0 : Multitouch Input For Windows Phones (part 2) - Displaying GestureSample Data

8/6/2012 5:50:44 PM

Reading Gestures from the TouchPanel

Gestures provide a richer and higher level abstraction than the raw state data you can access by reading the TouchPanel state. Gestures are performed by the user over time and require that the system continuously process user input to determine whether an enabled gesture has been performed. TouchPanel provides the capability to define which gestures should be tracked by using the EnabledGestures property and the GestureType enumeration flags. By default, the TouchPanel has no gestures enabled because gesture processing requires additional CPU processing. You should limit which gestures you enable to the minimum you currently need to keep the processing cost to a minimum.

Table 5 contains the enumeration values for GamePadType.

Table 5. GestureType Enumeration
NameDescription
NoneNo gestures should be processed.
TapA short touch at a single point on the screen.
DoubleTapTwo repeated taps on the screen near a single point.
HoldOne second touch at a single point on the screen.
HorizontalDragA touch that moves horizontally across the screen without lifting the finger.
VerticalDragA touch that moves vertically across the screen without lifting the finger.
FreeDragA touch that moves across the screen without lifting the finger.
PinchTwo touch points that are pull closer or further away in a pinching motion.
FlickA touch that is quickly moved in a flicking motion across the screen.
DragCompleteUsed to signal when one of the drag gestures is completed.
PinchCompleteUsed to signal when the pinch gesture is completed.

Enabling the gestures you want to respond to is a simple as combining multiple GestureType flags using the TouchPanel.EnabledGestures static property. The following lines of code enables two gestures:

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

The gesture system then continuously checks for the tap and free drag gestures while the game runs. In our game’s input logic code, you then need to check to see whether any gestures have been detected. This is done by calling the TouchPanel.IsGestureAvailable static property. This property returns true at least one gesture is ready to be processed. The TouchPanel.ReadGesture method is then used to read the next gesture in the queue and remove it from the queue.

The following code reads all of the available gestures from the TouchPanel.

// Loop while there are still gestures to read
while (TouchPanel.IsGestureAvailable)
{
    // Read the next gesture
    GestureSample gesture = TouchPanel.ReadGesture();

    // Use a switch statement to detemine which type of gesture has been recieved
    switch (gesture.GestureType)
    {
        case GestureType.Tap:
            // Process tap gesture
            break;
        case GestureType.FreeDrag:
            // Process free drag gesture
            break;
    }
}

					  

After each GestureSample is read, the game can use the gesture input to take some type of action. A GestureSample is a structure that holds all of the data relevant to a gesture. Using a switch statement is helpful to determine which type of gesture is used and to take different game actions based on the gesture.

Table 6 contains the properties exposed by the GestureSample structure.

Table 6. Properties of GestureSample
PropertyTypeDescription
GestureTypeGestureTypeThe type of gesture that occurred
PositionVector2First touch location of the gesture
Position2Vector2Second touch location of the gesture used in multiple point gestures like Pinch
DeltaVector2Delta information about the gesture such as the movement in a FreeDrag gesture
Delta2Vector2Second delta information used in multiple point gestures
TimestampTimespanSpan of time in which the gesture occurred

Depending on the type of gesture, the different property values of the GestureSample can be used when taking action within your game. For example, if you use the Flick gesture in your game to scroll the map, you might care about only the Delta property so you would know which direction the flick occurred in and with how much velocity. In other situations, you might care about the position of the gesture when using the DoubleTap gesture for selection. In that case, you would use the Position property.

Displaying GestureSample Data

Now that you have learned how to enable and read gestures let’s create a simple application that displays the last five gestures and all of their corresponding GestureSample properties.

First, you will need a List to store all of the processed gestures in so you can print them later when drawing. Add the following member variable to your game:

List<GestureSample> gestures = new List<GestureSample>();

Next, in your games Update method, add the following lines of code:

// Loop while there are still gestures to read
while (TouchPanel.IsGestureAvailable)
{
    // Add each to the stack
    gestures.Add(TouchPanel.ReadGesture());
}

This continuously reads all of the available gestures and adds them to the List for later processing.

Now in your games Draw method, add the following lines of code to loop over all of the gestures and write their values.

spriteBatch.Begin();
Vector2 textPosition = new Vector2(5, 5);

// Loop over all of the gestures and draw their information to the screen
for (int i = gestures.Count-1; i >= 0; i—)
{
    // Draw all of the properties of the gesture
    GestureSample gesture = gestures[i];
    spriteBatch.DrawString(spriteFont, "GestureType: " +
                           gesture.GestureType.ToString(), textPosition,
Color.White);
    textPosition.Y += 25;
    spriteBatch.DrawString(spriteFont, "Position: " +
                          gesture.Position.ToString(), textPosition, Color.White);
    textPosition.Y += 25;
    spriteBatch.DrawString(spriteFont, "Position2: " +
                          gesture.Position2.ToString(), textPosition, Color.White);
    textPosition.Y += 25;
    spriteBatch.DrawString(spriteFont, "Delta: " +
                           gesture.Delta.ToString(), textPosition, Color.White);
    textPosition.Y += 25;
    spriteBatch.DrawString(spriteFont, "Delta2: " +
                           gesture.Delta2.ToString(), textPosition, Color.White);
    textPosition.Y += 25;
    spriteBatch.DrawString(spriteFont, "Timestamp: " +
                           gesture.Timestamp.ToString(), textPosition,
                           Color.White);
    textPosition.Y += 40;
}
spriteBatch.End();

// Remove any gestures that wont print on the screen
if (gestures.Count > 5)
    gestures.RemoveRange(0, gestures.Count - 5);

					  

In the previous code, we loop over the List of GestureSample structures in reverse order and write out each of the properties. We then check the size of the list so that we can remove any gestures that would not be displayed on the screen.

Running the sample shows results similar to those seen in Figure 1.

Figure 1. Gesture sample that displays gesture properties

Other  
  •  Motorola Motoluxe
  •  Apple's Undiscovered Country : The future of the iPhone
  •  Apple's Undiscovered Country : The Future Of The Ipad
  •  Programming the iPhone : Standard Control Types (part 6) - Segmented Controls
  •  Programming the iPhone : Standard Control Types (part 5) - Search Bars
  •  Programming the iPhone : Standard Control Types (part 4) - Tables and Pickers
  •  Programming the iPhone : Standard Control Types (part 3) - Sliders
  •  Programming the iPhone : Standard Control Types (part 2) - Modal Buttons
  •  Programming the iPhone : Standard Control Types (part 1) - Buttons
  •  Sony Introduced 3 New Phones In Xperia Series: Tipo, Miro And Ion
  •  Samsung Galaxy SIII And HTC One X: A Detailed And Thorough Comparison
  •  In Control
  •  BlackBerry Virtual Keyboard: The Development Through Each Appliance
  •  3D Phone – Why Is LG Optimus 3D Max P725 The Best Choice, Still?
  •  XNA Game Studio 4.0 : Xbox 360 Gamepad (part 2) - Moving Sprites Based on Gamepad Input
  •  XNA Game Studio 4.0 : Xbox 360 Gamepad (part 1) - Reading Gamepad State
  •  XNA Game Studio 4.0 : Adding Interactivity with User Input - Precision Control of a Mouse
  •  Asus Transformer Pad TF300
  •  Mobile Phone Game Programming : Using Sprite Animation - Building the UFO Example Program
  •  Mobile Phone Game Programming : Using Sprite Animation - Achieving Smooth Animation with the GameCanvas Class
  •  
    Most View
    Windows Home Server : Admin Console Drive-By
    SQL Server 2005 : Report Definition and Design (part 1) - Data Sources, Report Layouts
    Lenovo IdeaPad S400 - Stylish And Affordable Laptop
    Application Security in Windows Vista
    Microsoft ASP.NET 4 : Repeated-Value Data Binding (part 1)
    iOS – Tax Time : TapTax, My Tax Return 2012, Income Tax Calculator, ITP
    Windows 7: Customizing Menus and the Control Panel (part 2) - Navigating and Customizing the Control Panel
    Migrating to Active Directory in Windows Server 2003 (part 2) - Moving from Windows 2000 Server
    Has Apple Lost It? (Part 1)
    Rise Of The Mobile Processors (Part 1)
    Top 10
    ADO.NET Programming : Microsoft SQL Server (part 4) - Working with Typed Data Sets
    ADO.NET Programming : Microsoft SQL Server (part 3) - Using Stored Procedures with DataSet Objects
    ADO.NET Programming : Microsoft SQL Server (part 2) - Using SQL Server Stored Procedures
    ADO.NET Programming : Microsoft SQL Server (part 1) - Connecting to SQL Server, Creating Command Objects
    Windows Phone 8 In-Depth Review (Part 6)
    Windows Phone 8 In-Depth Review (Part 5)
    Windows Phone 8 In-Depth Review (Part 4)
    Windows Phone 8 In-Depth Review (Part 3)
    Windows Phone 8 In-Depth Review (Part 2)
    Windows Phone 8 In-Depth Review (Part 1)