Kinect GUI Framework for XNA Games
- Posted: Oct 09, 2012 at 6:00 AM
- 5,629 Views
Looks like we're having a Walt Smith double-header with a second project from him in so many days...
This example will show you how to build a simple GUI framework for the Kinect to use with XNA applications. XNA doesn't have a native set of GUI widgets like WPF does, so if you need a button or a scrollbox, you have to build your own. I have done this several times and I finally created a reusable framework to save myself time on projects.
This example assumes that you have a Kinect for Windows and the SDK version 1.5 for the Kinect. It also assumes that you have installed the XNA game studio 4.0 or higher.
Building the Sample
If you have the prerequisites, you should be able to build and run the example with no problem provided that your Kinect device is hooked up and running on your PC.
The solution is divided into two major parts, the KSGui framework project, and an XNA game example project that use the KSGui framework. There is a utility project also in the solution, but it is just a hodgepodge of freeware libraries that I lifted from various websites.
First let us look at the KSGui framework. Every good framework starts with a contract (or interface as we say in .NET land), and so the KSGui has IKSComponent. This interface contains the essential properties and methods that are needed for a component to be a KSWidget. Below is the source code for this interface.
It's a pretty straightforward interface for anyone familiar with XNA games. Notice that Update() is not required by the interface. You will see that some of our components don't need Update(), so I left it out of the interface. You can put it back if you like symmetry more than pragmatism.
The base class for the KSGui framework is KSWidget, and of course it is based on the IKSComponent interface. I will not post all of the source code for KSWidget here, but there are a couple of interesting parts to this class. For instance, since we are dealing with a Kinect as the input device, we don't have any method for a person to click anything in the GUI. So we implement a sort of hover time for widgets. If the user hovers over a widget for some set time, we raise events. That code looks like this:
Project Information URL: http://code.msdn.microsoft.com/Kinect-GUI-Framework-for-496b9806
Project Download URL: http://code.msdn.microsoft.com/Kinect-GUI-Framework-for-496b9806
Project Source URL: http://code.msdn.microsoft.com/Kinect-GUI-Framework-for-496b9806