Skip to content

Instantly share code, notes, and snippets.

@MarcusKohnert
Created November 10, 2014 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcusKohnert/51dc6e457f24db19771f to your computer and use it in GitHub Desktop.
Save MarcusKohnert/51dc6e457f24db19771f to your computer and use it in GitHub Desktop.
Drag & Drop in Detail - MainWindow.cs
using FluentKinect;
using Kinect.Reactive;
using Microsoft.Kinect;
using Microsoft.Kinect.Toolkit.Interaction;
using System;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace DragAndDrop
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private TextBlock lastTouched;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Task.Factory.StartNew(() => this.KinectConnect());
}
private async void KinectConnect()
{
this.Kinect = await KinectConnector.GetKinect();
this.Kinect.KickStart(true);
this.OnPropertyChanged("Kinect");
var subscr = this.Kinect
.GetUserInfoObservable(new InteractionClient(this))
.SelectMany(_ => _.Select(__ => __.HandPointers.Where(___ => ___.IsActive)))
.Where(_ => _.FirstOrDefault() != null)
.Select(_ => _.First())
.ObserveOnDispatcher()
.Subscribe(_ =>
{
var p = new Point(_.X * this.kinectRegion.ActualWidth, _.Y * this.kinectRegion.ActualHeight);
if (_.HandEventType == InteractionHandEventType.Grip)
{
var elem = this.kinectRegion.InputHitTest(p) as TextBlock;
if (elem != null)
{
this.lastTouched = elem;
}
}
else if (_.HandEventType == InteractionHandEventType.GripRelease)
{
this.lastTouched = null;
}
else
{
if (this.lastTouched == null) return;
Canvas.SetLeft(this.lastTouched, p.X - this.lastTouched.ActualWidth / 2);
Canvas.SetTop(this.lastTouched, p.Y - this.lastTouched.ActualHeight / 2);
}
});
}
public KinectSensor Kinect { get; private set; }
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void OnPropertyChanged(string propName)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment