You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.3 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using Lib;
using Microsoft.Kinect;
namespace KinectUtils
{
public static class GestureManager
{
static private BodyFrameReader bodyFrameReader;
static private Body[] bodies;
static event EventHandler GestureRecognized;
static KinectManager KinectManager { get; set; }
static List<BaseGesture> KnownGestures { get; set; }
public static void AddGestures(IGestureFactory factory)
{
var gestures = factory.CreateGestures();
foreach (var gesture in gestures)
{
AddGesture(gesture);
}
}
public static void AddGesture(BaseGesture gesture)
{
if (!KnownGestures.Contains(gesture))
{
KnownGestures.Add(gesture);
gesture.GestureRecognized += (sender, e) => GestureRecognized?.Invoke(sender, e);
}
}
public static void RemoveGesture(BaseGesture gesture)
{
if (KnownGestures.Contains(gesture))
{
KnownGestures.Remove(gesture);
gesture.GestureRecognized -= (sender, e) => GestureRecognized?.Invoke(sender, e);
}
}
static public void StartAcquiringFrames(KinectManager kinectManager)
{
bodyFrameReader = kinectManager.Sensor.BodyFrameSource.OpenReader();
bodyFrameReader.FrameArrived += Reader_BodyFrameArrived;
}
static private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
using (var bodyframe = e.FrameReference.AcquireFrame())
{
if (bodyframe != null)
{
bodyframe.GetAndRefreshBodyData(bodies);
foreach (var body in bodies)
{
if (body.IsTracked)
{
foreach(var gesture in KnownGestures)
{
gesture.TestGesture(body);
}
}
}
}
}
}
}
}