diff --git a/Sources/KinectUtils/BaseMapping.cs b/Sources/KinectUtils/BaseMapping.cs index a37b1a4..c335df1 100644 --- a/Sources/KinectUtils/BaseMapping.cs +++ b/Sources/KinectUtils/BaseMapping.cs @@ -7,31 +7,80 @@ using System.Threading.Tasks; namespace KinectUtils { - class BaseMapping + public abstract class BaseMapping { - public void SubscribeToStartGesture(BaseGesture gesture) + protected bool running = false; + public event EventHandler> OnMapping; + + protected BaseMapping(BaseGesture startGesture, BaseGesture endGesture) + { + SubscribeToStartGesture(startGesture); + SubscribeToEndGesture(endGesture); + // Assuming GestureManager has an event that provides processed body data + GestureManager.ProcessedBodyFrameArrived += OnProcessedBodyFrameArrived; + } + + protected abstract T Mapping(Body body); + + protected bool TestMapping(Body body, out T output) + { + output = default(T); + if (running) + { + output = Mapping(body); + return true; + } + return false; + } + + protected void SubscribeToStartGesture(BaseGesture gesture) { - throw new NotImplementedException(); + gesture.GestureRecognized += (sender, e) => { if (!running) running = true; }; } - public void SubscribeToEndGesture(BaseGesture gesture) + protected void SubscribeToEndGesture(BaseGesture gesture) { - throw new NotImplementedException(); + gesture.GestureRecognized += (sender, e) => { if (running) running = false; }; } - public void SubscribeToToggleGesture(BaseGesture gesture) + // Assuming your GestureManager provides a similar event + protected void OnProcessedBodyFrameArrived(object sender, ProcessedBodyFrameEventArgs e) { - throw new NotImplementedException(); + foreach (var body in e.Bodies) + { + if (body.IsTracked) + { + T output; + if (TestMapping(body, out output)) + { + OnMapping?.Invoke(this, new OnMappingEventArgs(output)); + } + } + } } + } + + public class OnMappingEventArgs : EventArgs + { + public T MappedValue { get; private set; } - protected T Mapping(Body body) + public OnMappingEventArgs(T mappedValue) { - throw new NotImplementedException(); + MappedValue = mappedValue; } + } + + // Custom EventArgs to be used within your application logic + // This is not from the Kinect SDK, but rather a custom class to pass along processed body data + public class ProcessedBodyFrameEventArgs : EventArgs + { + public Body[] Bodies { get; private set; } + public Body ClosestBody { get; private set; } - public bool TestMapping(Body body, out T output) + public ProcessedBodyFrameEventArgs(Body[] bodies, Body closestBody) { - throw new NotImplementedException(); + Bodies = bodies; + ClosestBody = closestBody; } } } diff --git a/Sources/KinectUtils/GestureManager.cs b/Sources/KinectUtils/GestureManager.cs index d4761cf..1541fb3 100644 --- a/Sources/KinectUtils/GestureManager.cs +++ b/Sources/KinectUtils/GestureManager.cs @@ -14,7 +14,12 @@ namespace KinectUtils { private static BodyFrameReader bodyFrameReader; private static Body[] bodies; + static event EventHandler GestureRecognized; + public static event EventHandler BodyFrameArrived; + public static event EventHandler ProcessedBodyFrameArrived; + + static KinectManager KinectManager { get; set; } static List KnownGestures { get; set; } = new List(); @@ -62,19 +67,24 @@ namespace KinectUtils bodies = new Body[bodyframe.BodyCount]; } bodyframe.GetAndRefreshBodyData(bodies); - if (bodies != null) + + Body closestBody = bodies.Where(b => b.IsTracked) + .OrderBy(b => b.Joints[JointType.SpineBase].Position.Z) + .FirstOrDefault(); + + ProcessedBodyFrameArrived?.Invoke(sender, new ProcessedBodyFrameEventArgs(bodies, closestBody)); + + foreach (var body in bodies) { - foreach (var body in bodies) + if (body.IsTracked) { - if (body.IsTracked) + foreach (var gesture in KnownGestures) { - foreach (var gesture in KnownGestures) - { - gesture.TestGesture(body); - } + gesture.TestGesture(body); } } } + } } } diff --git a/Sources/LibMyGesturesBank/MyGesturesBank.csproj b/Sources/LibMyGesturesBank/MyGesturesBank.csproj index 3113c9f..3cea259 100644 --- a/Sources/LibMyGesturesBank/MyGesturesBank.csproj +++ b/Sources/LibMyGesturesBank/MyGesturesBank.csproj @@ -42,6 +42,7 @@ +