Miro
Miro

Reputation: 1806

Kinect Custom Cursor

I need to make a cursor in Kinect, but I don't know where to start. I need to make it using WPF and C#. Cursor should be in shape of hand and when I hover over element the "loading" circle should appear, and when it "loads" it should fire click event on hovered element. I'm sure that your're all familiar with this.

It would be of great help if someone could write me some directions oh how to accomplish this.

Upvotes: 1

Views: 2268

Answers (2)

astreal
astreal

Reputation: 3513

Here is a nice solution using the official SDK, but even if you're not, it can be very (!) helpful (it was for me):

You should take a look at the (free) code available here Beginning Kinect Programming with the Microsoft Kinect SDK sample code you click on "Source Code/Downloads" and what's is going to interest you in the sample is the Chapter 6 (name of the folder).

Basically they're using a static class KinectInput that allow to raise new event like KinectCursorEnterEvent, there is a cursor manager KinectCursorManager which does almost everything get the hand position/update the cursor ... and They use an adorner to put the cursor, with the FrameworkElement you want as a cursor.

They implement the HoverButton you're talking about. It fires the click event after a timer elapsed (timer launched when the KinectCursorEnterEvent occured). The solution they propose is elegant, and allow an easy implementation of nice controls. You can easily modify the code to handle the two cursor (that's the value-added of the Kinect, isn't it?)

having an Enumeration CursorSide:

public enum CursorSide
{
    Left,
    Right
}

and modifyong only the KinectCursorManager having a Dictionary of capacity 2, and the enumeration being the Key, and having a pointer on the elemtn under the cursor for each hand:

    private Dictionary<CursorSide, CursorAdorner> _cursorAdorner;
    private UIElement _lastElementOverRightHand;
    private UIElement _lastElementOverLeftHand;

But before you have to remove the part of the code that does the selection of the primaryHand (basically the hand closest to the Kinect).

I hope this can help somebody :-]

The book is very interesting, you can buy it for a few bucks.

Upvotes: 1

juergen d
juergen d

Reputation: 204766

Here is a code snippet that might help you:

using Microsoft.Research.Kinect.Nui;

Runtime nui = Runtime.Kinects[0];
nui.Initialize(RuntimeOptions.UseSkeletalTracking);
nui.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(nui_SkeletonFrameReady);

void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
    SkeletonFrame sf = e.SkeletonFrame;
    SkeletonData d = (from s in sf.Skeletons
                      where s.TrackingState == SkeletonTrackingState.Tracked
                      select s).FirstOrDefault();

     if (d != null)
     {
          SetHandPosition(imageCursor, d.Joints[JointID.HandLeft]);
     }
}

void SetHandPosition(FrameworkElement e, Joint joint)
{
    Joint scaledJoint = Coding4Fun.Kinect.Wpf.SkeletalExtensions.ScaleTo(joint, 600, 400, 0.75f, 0.75f);

    Canvas.SetLeft(e, scaledJoint.Position.X);
    Canvas.SetTop(e, scaledJoint.Position.Y);
} 

If you want your cursor to be different hovering on an element then just go to the elements properties and set a cursor for that element. In Visual Studio you can choose a cursor in the elements properties.

To make a click on a hover over an element you have to implement the MouseEnter event

private void button1_MouseEnter(object sender, MouseEventArgs e)
{
....        
}

Here are some infos about it:

button1.PerformClick() in wpf

Upvotes: 3

Related Questions