High Performance Touch Interface #wpdev #wp7dev

Every now and again i come across developer questions like why is manipulation delta slow etc. Let me tell you why.

UIElement Silverlight for #windowsphone exposes a few events like

http://msdn.microsoft.com/en-us/library/system.windows.uielement(v=vs.95).aspx

Public eventSupported by Silverlight for Windows Phone ManipulationCompleted Occurs when a manipulation and inertia on the UIElement is complete.
Public eventSupported by Silverlight for Windows Phone ManipulationDelta Occurs when the input device changes position during a manipulation.
Public eventSupported by Silverlight for Windows Phone ManipulationStarted Occurs when an input device begins a manipulation on the UIElement.

These are high level touch interfaces and there is a significant overhead in reporting (and hence delay etc).

If you are say drawing on a bitmap or canvas, you dont want a min delta change before event is fired. For that lets look at low-level interface exposed in Silverlight.

http://msdn.microsoft.com/en-us/library/system.windows.input.touch(v=vs.95).aspx

Public eventStatic memberSupported by Silverlight for Windows Phone FrameReported Occurs when the input system promotes a Windows 7 touch message for Silverlight.

Touh.FrameReported is a low level method and has little overhead and is very very precise. Let me give you a code sample

Touch.FrameReported += Touch_FrameReported;

you can do above in Loaded event. Here’s the implementation of the Touch_FrameReported handler. WorkArea is Canvas in this. I have also used this in conjugation with WritableBitmap

private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
    try
    {
        // Determine if finger / mouse is down
        point = e.GetPrimaryTouchPoint(this.workArea);
        
        if (point.Position.X < 0 || point.Position.Y < 0)
        return;
        
        if (point.Position.X > this.workArea.Width || point.Position.Y > this.workArea.Height)
            return;
        
        if (this.lbLetter.SelectedIndex == -1)
            return;
        
        switch (point.Action)
        {
            case TouchAction.Down:
                draw = true;
                old_point = point;
                goto default;
        
            case TouchAction.Up:
                draw = false;
                break;
        
            default:
                Draw();
                break;
        }
    }
    catch
    {
        MessageBox.Show("Application encountered error processing last request.");
    }
}

I hope this is useful to #windowsphone developers out there.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s