One of the cool features in Silverlight 3 is the Multi-touch capabilities. This feature allows Silverlight developers to create fun and interactive applications that can be used with standard inputs, such as mouse and keyboard, as well as with touch-enabled devices.
If you have seen Minority Report, you are already aware of the gesture-based technology. The user can interact with the device to drag objects around, flick to navigate, and pinch to adjust the size of an object. These concepts have become a standard feature on a variety of devices, including Microsoft Surface, Windows 7, and mobile phones.
Silverlight 3 provides the fundamentals of receiving touch events from the hardware to the application. By default, the touch events are fired from Windows 7 to the browser (IE / Firefox). If the application is not processing the touch events, these events are registered as standard mouse events.
Here are two sample applications that utilize Multi-touch concepts. You will need a touch-enabled device and Windows 7 to get the full experience.
In this two-part blog series, I'll show you the process that I went through in developing a Silverlight class library with Multi-touch gestures and events. This process was used in SharePoint Silverview to add Silverlight / Windows 7 touch functionality in a SharePoint environment. You can find the complete source code at http://code.msdn.microsoft.com/ssv.
Jesse Bishop, Program Manager on the Microsoft Silverlight team, wrote an amazing article about Multi-touch Gesture recognition in Silverlight 3. The article illustrates on how to register touch events and process standard gestures on individual elements. The primary concept is to have a single entity that reads the incoming events and determine which element that is being accessed. After reading Jesse's article, I got inspired to extend his concept with a variety of gestures and events that can be used in a standard Silverlight application.
Before you can develop or run Silverlight application with Multi-touch, you need the right equipment. I have been developing on the Dell SX2210T and the HP TouchSmart IQ586. The HP TouchSmart TX2 is also a good choice. One interesting thing that I learned was the HP TouchSmart IQ586 only had support for a single touch point. There is an updated driver to ensure that the monitor utilizes two touch points. You can check for this by viewing the Pen and Touch information under System Information. It is ideal to have 2 Touch Points, if you plan to support two-finger gestures such as Scale and Rotate. With the recent release of Windows 7, there may be drivers for existing hardware that will need to be updated.
The Touch framework has two primary concepts: the TouchPoint object and the FrameReported event.
The TouchPoint object stores data related to a single touch point. This data is passed from the hardware as the user interacts with the touch device. The number of touch points vary on the supported hardware. The first touch point that is fired to the application is called the primary touch point. The object has two important properties: Position and Action. The Action property stores the current action state, such as Up, Down, and Move. The Up and Down actions are similar to the mouse's MouseLeftButtonUp and MouseLeftButonDown events. The Move action is the transition action that is constantly called until the user releases the Touch Point, which also invokes the Up action.
The FrameReported event is an application-wide event that sends data from the hardware to the application. One thing to note is that this event is global to the application rather than an individual element. The application doesn't directly know the element that the event is processing. It is possible to retrieve this information using the VisualTreeHelper.GetPointsUnderCoordinates method. The TouchFrameEventArgs argument has methods of getting information about touch points. You can get the current touch points by calling the GetPrimaryTouchPoint and GetTouchPoint methods.
Now, let's start by creating a class library. The library will be useful for rapidly developing touch applications. We will only need two objects for the library: TouchElement control and TouchProcessor (explained in Jesse's article).
In Visual Studio 2008 (or Visual Studio 2010 Beta 2), create a new Silverlight Application. Right-click on the Solution in the Solution Explorer and select Add New Project. In the Project Dialog box, select Silverlight Class Library and name it TouchLibrary. Remove the default Class1.cs file.
We will define the TouchElement control as a content control to allow you to place any element, such as an image, movie clip, or UIElement, into the control. This is not a requirement for deploying Touch applications, but rather a personal choice for development. One useful scenario is having a couple of TouchElement controls with images to allow users to drag and scale images around the user interface.
Right-click on the TouchLibrary and select Add New Item. In the Add New Item Dialog box, select User Control and name it TouchElement. Replace the TouchElement.xaml content with the following:
Note that this is a Content Control rather than a UserControl. We have added TranslateTransform and ScaleTransform transforms to render the control's transformation based on the touch events.
Now it's time for some code action. In Jesse Bishop's article, he implements the concept using ITouchElement as an interface that describes the supported gestures and ManipulateElement as a UserControl that implements the ITouchElement interface. I decided to merge the code from his elements into the TouchElement content control. I highly recommend checking out his article if you want to learn the details of the concept.
We will implement the Translate and Scale gestures. The Translate gesture occurs when the user drags the element from one position to another. The Scale gesture occurs when the user pinch and scales using two touch points. There may be scenarios when the developer wants to create their own custom translate and scale events. For this reason, we will add support for default transforms as well as add custom events that will send information back to the application.
The TransformEventArgs class stores basic data for both Translate and Scale events.
The TransformHandler delegate is used for both events, with the TransformEventArgs object as the second parameter.
We will add three properties to the TouchElement class. The TouchEnabled property will be used in the TouchProcessor class to check which TouchElement objects need to be processed. The TouchDrag and TouchScale boolean properties enable the default touch behaviors for drag (translate) and scale events. By default, the properties are set to false to allow the user to create their own events.
The TouchDrag and TouchScale properties connect the default event handlers, which validates the input value and directly updates TouchElement's transform values.
The TranslateElement and ScaleElement methods calculate the appropriate transforms from the positions based on the touch events. After the value is calculated, the event is fired with the TransformEventArgs as a parameter. This will process the default transformation as well as any custom work that is connected to the event.
The TouchProcessor class calls the TouchPointReported method to process the touch event based on the TouchPoint's Action property. On the Down action, we collect information on the TouchPoint. If it is the initial touch point, then the position is later used for distance calculation. In the next blog post, I will go into additional functionality that can be added in the TouchDown method, such as a start time for timing touch event for other gestures. On the Touch Up action, we free up the current point. The TouchUp method can also be used to process any gestures, which will be detailed in the next blog post. The Touch Move is where the fun's at. After the Down action occurs, the Move action constantly runs until the Up action is called. If there is only a single touch point, the TranslateElement method is called. The ScaleElement method is called if there are multiple points. In Jesse's article, he also has Rotate functionality, which is processed with multiple points.
The TouchProcessor class manages the global touch functionality by connecting the Touch's FrameReported event. Jesse goes into detail about the TouchProcessor class in his article. Add the following class to the Class Library. The primary difference in this version is the RootElement property, which is used to connect to the current application's visual root. This property is required for the VisualTreeHelper.FindElementsInHostCoordinates method.
In the Silverlight Project, add TouchLibrary to the project's References. This will allow you to add the custom touch functionality to your project.
In MainPage.xaml, add the following namespace to connect to the new library.
Add the following to MainPage.xaml to create three TouchElements (two with color rectangles and one with an image). The image source has the project name pre-pended to ensure that it loads the image from the correct project. The TouchDrag and TouchScale properties are enabled to allow you to manipulate the items.
In MainPage.xaml.cs, add the following code to initialize the TouchProcessor object.
One thing to note is that the Touch functionality doesn't work in Windowless mode. By default, Windowless mode is set to false and it is a feature that should be used with caution. For more information on the limitations of Windowless mode, check out this article.
Compile and run the application. As mentioned before, you will need the required hardware to recognized the Touch events. You can drag the three items across the screen as well as scale them to different sizes. The item is also brought to the front of the screen after release the touch point.
The demo of this version can be found here.
Thanks for checking out this article. Stay tuned for my next blog post, where I introduce more customization for the TouchLibrary, including Gesture support, parented selection, and touch events in a ChildWindow.
Feel free to leave comments at http://blog.bayprince.com/2010/01/multi-touch-in-silverlight-3-2/