-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
321 lines (266 loc) · 12.4 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
321 lines (266 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//-----------------------------------------------------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// <Description>
// This program detects a set of discrete gestures for a single, tracked person.
// If no person is tracked, the gesture detector will be paused.
// Note: This sample uses polling to get new frames from the Kinect sensor at 60 fps; for event notification, please see the 'DiscreteGestureBasics-WPF' sample.
// </Description>
//-------------------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Samples.Kinect.ContinuousGestureBasics
{
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using Microsoft.Kinect;
/// <summary>
/// Interaction logic for the MainWindow
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged, IDisposable
{
/// <summary> Active Kinect sensor </summary>
private KinectSensor kinectSensor = null;
/// <summary> Array for the bodies (Kinect can track up to 6 people simultaneously) </summary>
private Body[] bodies = null;
/// <summary> Index of the active body (first tracked person in the body array) </summary>
private int activeBodyIndex = 0;
/// <summary> Reader for body frames </summary>
private BodyFrameReader bodyFrameReader = null;
/// <summary> Current kinect status text to display </summary>
private string statusText = null;
/// <summary> KinectBodyView object which handles drawing the active body to a view box in the UI </summary>
private KinectBodyView kinectBodyView = null;
/// <summary> Gesture detector which will be tied to the active body (closest skeleton to the sensor) </summary>
private GestureDetector gestureDetector = null;
/// <summary> GestureResultView for displaying gesture results associated with the tracked person in the UI </summary>
private GestureResultView gestureResultView = null;
/// <summary> SpaceView for displaying spaceship position and rotation, which are related to gesture detection results </summary>
// private SpaceView spaceView = null;
/// <summary> Timer for updating Kinect frames and space images at 60 fps </summary>
private DispatcherTimer dispatcherTimer = null;
/// <summary>
/// Initializes a new instance of the MainWindow class
/// </summary>
public MainWindow()
{
// initialize the MainWindow
this.InitializeComponent();
// only one sensor is currently supported
this.kinectSensor = KinectSensor.GetDefault();
// open the sensor
this.kinectSensor.Open();
// set the initial status text
this.UpdateKinectStatusText();
// open the reader for the body frames
this.bodyFrameReader = this.kinectSensor.BodyFrameSource.OpenReader();
// initialize the BodyViewer object for displaying tracked bodies in the UI
this.kinectBodyView = new KinectBodyView(this.kinectSensor);
// initialize the GestureDetector object
this.gestureResultView = new GestureResultView(false, false, false);
this.gestureDetector = new GestureDetector(this.kinectSensor, this.gestureResultView);
// set data context objects for display in UI
this.DataContext = this;
this.kinectBodyViewbox.DataContext = this.kinectBodyView;
this.gestureResultGrid.DataContext = this.gestureResultView;
}
/// <summary>
/// INotifyPropertyChangedPropertyChanged event to allow window controls to bind to changeable data
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Gets the current Kinect sensor status text to display in UI
/// </summary>
public string StatusText
{
get
{
return this.statusText;
}
private set
{
if (this.statusText != value)
{
this.statusText = value;
this.NotifyPropertyChanged();
}
}
}
/// <summary>
/// Disposes all unmanaged resources for the class
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes the GestureDetector object
/// </summary>
/// <param name="disposing">True if Dispose was called directly, false if the GC handles the disposing</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (this.gestureDetector != null)
{
this.gestureDetector.Dispose();
this.gestureDetector = null;
}
}
}
/// <summary>
/// Polls for new Kinect frames and updates moving objects in the spaceView
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void DispatcherTimer_Tick(object sender, EventArgs e)
{
this.UpdateKinectStatusText();
this.UpdateKinectFrameData();
}
/// <summary>
/// Starts the dispatcher timer to check for new Kinect frames and update objects in space @60fps
/// Note: We are using a dispatcher timer to demonstrate usage of the VGB polling APIs,
/// please see the 'DiscreteGestureBasics-WPF' sample for event notification.
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void MainWindow_Loaded(object sender, EventArgs e)
{
// set the UI to render at 60fps
CompositionTarget.Rendering += this.DispatcherTimer_Tick;
// set the game timer to run at 60fps
this.dispatcherTimer = new DispatcherTimer();
this.dispatcherTimer.Tick += this.DispatcherTimer_Tick;
this.dispatcherTimer.Interval = TimeSpan.FromSeconds(1 / 60);
this.dispatcherTimer.Start();
}
/// <summary>
/// Execute shutdown tasks
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void MainWindow_Closing(object sender, CancelEventArgs e)
{
if (this.bodyFrameReader != null)
{
// BodyFrameReader is IDisposable
this.bodyFrameReader.Dispose();
this.bodyFrameReader = null;
}
if (this.gestureDetector != null)
{
// The GestureDetector contains disposable members (VisualGestureBuilderFrameSource and VisualGestureBuilderFrameReader)
this.gestureDetector.Dispose();
this.gestureDetector = null;
}
if (this.kinectSensor != null)
{
this.kinectSensor.Close();
this.kinectSensor = null;
}
}
/// <summary>
/// Gets the first body in the bodies array that is currently tracked by the Kinect sensor
/// </summary>
/// <returns>Index of first tracked body, or -1 if no body is tracked</returns>
private int GetActiveBodyIndex()
{
int activeBodyIndex = -1;
int maxBodies = this.kinectSensor.BodyFrameSource.BodyCount;
for (int i = 0; i < maxBodies; ++i)
{
// find the first tracked body and verify it has hands tracking enabled (by default, Kinect will only track handstate for 2 people)
if (this.bodies[i].IsTracked && (this.bodies[i].HandRightState != HandState.NotTracked || this.bodies[i].HandLeftState != HandState.NotTracked))
{
activeBodyIndex = i;
break;
}
}
return activeBodyIndex;
}
/// <summary>
/// Retrieves the latest body frame data from the sensor and updates the associated gesture detector object
/// </summary>
private void UpdateKinectFrameData()
{
bool dataReceived = false;
using (var bodyFrame = this.bodyFrameReader.AcquireLatestFrame())
{
if (bodyFrame != null)
{
if (this.bodies == null)
{
// creates an array of 6 bodies, which is the max number of bodies that Kinect can track simultaneously
this.bodies = new Body[bodyFrame.BodyCount];
}
// The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
// As long as those body objects are not disposed and not set to null in the array,
// those body objects will be re-used.
bodyFrame.GetAndRefreshBodyData(this.bodies);
if (!this.bodies[this.activeBodyIndex].IsTracked)
{
// we lost tracking of the active body, so update to the first tracked body in the array
int bodyIndex = this.GetActiveBodyIndex();
if (bodyIndex > 0)
{
this.activeBodyIndex = bodyIndex;
}
}
dataReceived = true;
}
}
if (dataReceived)
{
Body activeBody = this.bodies[this.activeBodyIndex];
// visualize the new body data
this.kinectBodyView.UpdateBodyData(activeBody);
// visualize the new gesture data
if (activeBody.TrackingId != this.gestureDetector.TrackingId)
{
// if the tracking ID changed, update the detector with the new value
this.gestureDetector.TrackingId = activeBody.TrackingId;
}
if (this.gestureDetector.TrackingId == 0)
{
// the active body is not tracked, pause the detector and update the UI
this.gestureDetector.IsPaused = true;
// this.gestureDetector.ClosedHandState = false;
this.gestureResultView.UpdateGestureResult(false, false, false);
}
else
{
// the active body is tracked, unpause the detector
this.gestureDetector.IsPaused = false;
// get the latest gesture frame from the sensor and updates the UI with the results
this.gestureDetector.UpdateGestureData();
}
}
}
/// <summary>
/// Updates the StatusText with the latest sensor state information
/// </summary>
private void UpdateKinectStatusText()
{
// reset the status text
this.StatusText = this.kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText
: Properties.Resources.NoSensorStatusText;
}
/// <summary>
/// Notifies UI that a property has changed
/// </summary>
/// <param name="propertyName">Name of property that has changed</param>
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}