Touch Input

From XNAWiki
Jump to: navigation, search

Touch Input in XNA version 4.0 are available for the Windows Phone, Zune, and any PC with an appropriate touch sensitive screen. The input data is available via the TouchPanel class. Available information includes the location, number and state of touches on the device as low level information. Touches are represented by their Vector2 position. Higher-level information is also available as gestures. Gestures can represent multiple touches and contain positions, changes in position, and durations. Gestures can be categorized as common touch input activities such as Tap, Drag, or Flick via their GestureType property.

Example

The following code shows fields and initialization of an InputState class for touch screens.

public class InputState
    {
        TouchCollection touchLocations;
        TouchCollection prevTouchLocations;
        TouchPanelCapabilities touchPanelCapabilities;
        Vector2[] touchPositions;
        Vector2[] prevTouchPositions;
        GestureSample[] gestures;
 
        public InputState()
        {
            TouchPanel.EnabledGestures = GestureType.Flick; //many others available
 
            touchPanelCapabilities = TouchPanel.GetCapabilities();
            touchPositions = new Vector2[touchPanelCapabilities.MaximumTouchCount];
            prevTouchPositions = new Vector2[touchPanelCapabilities.MaximumTouchCount];
            gestures = new GestureSample[touchPanelCapabilities.MaximumTouchCount];
 
            for (int i = 0; i < touchPanelCapabilities.MaximumTouchCount; i++)
            {
                touchPositions[i] = new Vector2();
                prevTouchPositions[i] = new Vector2();
                gestures[i] = new GestureSample();
            }
        }
        ...
    }

This class's update method is written like this:

public void Update()
        {
            prevTouchLocations = touchLocations;
 
            for (int i = 0; i < touchLocations.Count; i++)
                prevTouchPositions[i] = touchPositions[i];
 
            touchLocations = TouchPanel.GetState();
 
            for (int i = 0; i < touchPanelCapabilities.MaximumTouchCount; i++)
            {
                if (i < touchLocations.Count)
                    touchPositions[i] = touchLocations[i].Position;
                else
                    touchPositions[i] = new Vector2(-1, -1); //off-screen point
 
                if (TouchPanel.IsGestureAvailable)
                    gestures[i] = TouchPanel.ReadGesture();
                else
                    gestures[i] = new GestureSample(); //empty gesturesample
            }            
        }

This class can be augmented with any number of methods to analyze touch activity. Comparing touch positions can yield information for motion. Polling for the existence of a gesture can trigger specific events.

See Also

For inputs on a particular portion of the screen, a convenient method may be to define a Touch Zone Interface.

Wrapper for Zune Input (Circa 2008) ZunePadState

External Links

Microsoft.Xna.Framework.Input.Touch Documentation

Creator's Club Touch Thumbsticks example