-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
1403 lines (1197 loc) · 56.9 KB
/
MainWindow.xaml.cs
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace Microsoft.Samples.Kinect.HackISUName
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;
using System.Runtime.InteropServices;
using System.Windows.Shapes;
using Microsoft.Samples.Kinect.HackISUName.Gestures;
using Microsoft.Speech.AudioFormat;
using Microsoft.Speech.Recognition;
using Microsoft.Samples.Kinect.SpeechBasics;
using System.Text;
using System.Threading;
/// <summary>
/// Interaction logic for MainWindow
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
/// <summary>
/// Radius of drawn hand circles
/// </summary>
private const double HandSize = 30;
/// <summary>
/// Thickness of drawn joint lines
/// </summary>
private const double JointThickness = 3;
/// <summary>
/// Thickness of clip edge rectangles
/// </summary>
private const double ClipBoundsThickness = 10;
/// <summary>
/// Constant for clamping Z values of camera space points from being negative
/// </summary>
private const float InferredZPositionClamp = 0.1f;
/// <summary>
/// Brush used for drawing hands that are currently tracked as closed
/// </summary>
private readonly Brush handClosedBrush = new SolidColorBrush(Color.FromArgb(128, 255, 0, 0));
/// <summary>
/// Brush used for drawing hands that are currently tracked as opened
/// </summary>
private readonly Brush handOpenBrush = new SolidColorBrush(Color.FromArgb(128, 0, 255, 0));
/// <summary>
/// Brush used for drawing hands that are currently tracked as in lasso (pointer) position
/// </summary>
private readonly Brush handLassoBrush = new SolidColorBrush(Color.FromArgb(128, 0, 0, 255));
/// <summary>
/// Brush used for drawing joints that are currently tracked
/// </summary>
private readonly Brush trackedJointBrush = new SolidColorBrush(Color.FromArgb(255, 68, 192, 68));
/// <summary>
/// Brush used for drawing joints that are currently inferred
/// </summary>
private readonly Brush inferredJointBrush = Brushes.Yellow;
/// <summary>
/// Pen used for drawing bones that are currently inferred
/// </summary>
private readonly Pen inferredBonePen = new Pen(Brushes.Gray, 1);
/// <summary>
/// Drawing group for body rendering output
/// </summary>
private DrawingGroup drawingGroup;
/// <summary>
/// Drawing image that we will display
/// </summary>
private DrawingImage imageSource;
/// <summary>
/// Active Kinect sensor
/// </summary>
private KinectSensor kinectSensor = null;
/// <summary>
/// Coordinate mapper to map one type of point to another
/// </summary>
private CoordinateMapper coordinateMapper = null;
/// <summary>
/// Reader for body frames
/// </summary>
private BodyFrameReader bodyFrameReader = null;
/// <summary>
/// Array for the bodies
/// </summary>
private Body[] bodies = null;
/// <summary>
/// definition of bones
/// </summary>
private List<Tuple<JointType, JointType>> bones;
/// <summary>
/// Width of display (depth space)
/// </summary>
private int displayWidth;
/// <summary>
/// Height of display (depth space)
/// </summary>
private int displayHeight;
/// <summary>
/// List of colors for each body tracked
/// </summary>
private List<Pen> bodyColors;
/// <summary>
/// Current status text to display
/// </summary>
private string statusText = null;
private IntPtr window;
private GestureListener knockGesture;
private GestureListener knockPullGesture;
private GestureListener slapGesture;
private GestureListener pokeGesture;
/// <summary>
/// Stream for 32b-16b conversion.
/// </summary>
private KinectAudioStream convertStream = null;
/// <summary>
/// Speech recognition engine using audio data from Kinect.
/// </summary>
private SpeechRecognitionEngine speechEngine = null;
private enum HandMouseStates
{
NONE,
CURSOR,
WINDOW_DRAG,
SCROLL_UP,
SCROLL_DOWN,
VOLUME_UP,
VOLUME_DOWN
}
private HandMouseStates HandMouseState = HandMouseStates.NONE;
/// <summary>
/// Initializes a new instance of the MainWindow class.
/// </summary>
public MainWindow()
{
// one sensor is currently supported
this.kinectSensor = KinectSensor.GetDefault();
// get the coordinate mapper
this.coordinateMapper = this.kinectSensor.CoordinateMapper;
// get the depth (display) extents
FrameDescription frameDescription = this.kinectSensor.DepthFrameSource.FrameDescription;
// get size of joint space
this.displayWidth = frameDescription.Width;
this.displayHeight = frameDescription.Height;
// open the reader for the body frames
this.bodyFrameReader = this.kinectSensor.BodyFrameSource.OpenReader();
// a bone defined as a line between two joints
this.bones = new List<Tuple<JointType, JointType>>();
// Torso
this.bones.Add(new Tuple<JointType, JointType>(JointType.Head, JointType.Neck));
this.bones.Add(new Tuple<JointType, JointType>(JointType.Neck, JointType.SpineShoulder));
this.bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.SpineMid));
this.bones.Add(new Tuple<JointType, JointType>(JointType.SpineMid, JointType.SpineBase));
this.bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderRight));
this.bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderLeft));
this.bones.Add(new Tuple<JointType, JointType>(JointType.SpineBase, JointType.HipRight));
this.bones.Add(new Tuple<JointType, JointType>(JointType.SpineBase, JointType.HipLeft));
// Right Arm
this.bones.Add(new Tuple<JointType, JointType>(JointType.ShoulderRight, JointType.ElbowRight));
this.bones.Add(new Tuple<JointType, JointType>(JointType.ElbowRight, JointType.WristRight));
this.bones.Add(new Tuple<JointType, JointType>(JointType.WristRight, JointType.HandRight));
this.bones.Add(new Tuple<JointType, JointType>(JointType.HandRight, JointType.HandTipRight));
this.bones.Add(new Tuple<JointType, JointType>(JointType.WristRight, JointType.ThumbRight));
// Left Arm
this.bones.Add(new Tuple<JointType, JointType>(JointType.ShoulderLeft, JointType.ElbowLeft));
this.bones.Add(new Tuple<JointType, JointType>(JointType.ElbowLeft, JointType.WristLeft));
this.bones.Add(new Tuple<JointType, JointType>(JointType.WristLeft, JointType.HandLeft));
this.bones.Add(new Tuple<JointType, JointType>(JointType.HandLeft, JointType.HandTipLeft));
this.bones.Add(new Tuple<JointType, JointType>(JointType.WristLeft, JointType.ThumbLeft));
// Right Leg
this.bones.Add(new Tuple<JointType, JointType>(JointType.HipRight, JointType.KneeRight));
this.bones.Add(new Tuple<JointType, JointType>(JointType.KneeRight, JointType.AnkleRight));
this.bones.Add(new Tuple<JointType, JointType>(JointType.AnkleRight, JointType.FootRight));
// Left Leg
this.bones.Add(new Tuple<JointType, JointType>(JointType.HipLeft, JointType.KneeLeft));
this.bones.Add(new Tuple<JointType, JointType>(JointType.KneeLeft, JointType.AnkleLeft));
this.bones.Add(new Tuple<JointType, JointType>(JointType.AnkleLeft, JointType.FootLeft));
// populate body colors, one for each BodyIndex
this.bodyColors = new List<Pen>();
this.bodyColors.Add(new Pen(Brushes.Red, 6));
this.bodyColors.Add(new Pen(Brushes.Orange, 6));
this.bodyColors.Add(new Pen(Brushes.Green, 6));
this.bodyColors.Add(new Pen(Brushes.Blue, 6));
this.bodyColors.Add(new Pen(Brushes.Indigo, 6));
this.bodyColors.Add(new Pen(Brushes.Violet, 6));
// set IsAvailableChanged event notifier
this.kinectSensor.IsAvailableChanged += this.Sensor_IsAvailableChanged;
// open the sensor
this.kinectSensor.Open();
// grab the audio stream
IReadOnlyList<AudioBeam> audioBeamList = this.kinectSensor.AudioSource.AudioBeams;
System.IO.Stream audioStream = audioBeamList[0].OpenInputStream();
// create the convert stream
this.convertStream = new KinectAudioStream(audioStream);
// set the status text
this.StatusText = this.kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText
: Properties.Resources.NoSensorStatusText;
RecognizerInfo ri = TryGetKinectRecognizer();
if (null != ri)
{
this.speechEngine = new SpeechRecognitionEngine(ri.Id);
//var directions = new Choices();
//directions.Add(new SemanticResultValue("hide", "hide"));
//directions.Add(new SemanticResultValue("minimize", "minimize"));
//directions.Add(new SemanticResultValue("maximize", "maximize"));
//directions.Add(new SemanticResultValue("snap left", "snap left"));
//directions.Add(new SemanticResultValue("snap right", "snap right"));
//directions.Add(new SemanticResultValue("front", "front"));
//directions.Add(new SemanticResultValue("drag", "drag"));
//directions.Add(new SemanticResultValue("get windows", "get windows"));
// Grammar for snapping
GrammarBuilder snap = new GrammarBuilder { Culture = ri.Culture };
// Any window
snap.Append(new Choices("snap"));
snap.Append(new Choices("Spotify", "Genie", "Chrome", "Media Player", "Visual Studio", "Github", "Eclipse", "Word", "Notepad"));
snap.Append(new Choices("left", "right", "down", "up"));
var g = new Grammar(snap);
this.speechEngine.LoadGrammar(g);
GrammarBuilder snap2 = new GrammarBuilder { Culture = ri.Culture };
snap2.Append(new Choices("snap"));
snap2.AppendWildcard();
snap2.Append(new Choices("left", "right", "down", "up"));
var g4 = new Grammar(snap2);
this.speechEngine.LoadGrammar(g4);
GrammarBuilder grab1 = new GrammarBuilder { Culture = ri.Culture };
grab1.Append(new Choices("grab"));
grab1.Append(new Choices("Spotify", "Genie", "Chrome", "Media Player", "Visual Studio", "Github", "Eclipse", "Word", "Notepad"));
var g1 = new Grammar(grab1);
this.speechEngine.LoadGrammar(g1);
GrammarBuilder drag1 = new GrammarBuilder { Culture = ri.Culture };
drag1.Append(new Choices("grab"));
drag1.Append(new Choices("Spotify", "Genie", "Chrome", "Media Player", "Visual Studio", "Github", "Eclipse", "Word", "Notepad"));
var d1 = new Grammar(drag1);
this.speechEngine.LoadGrammar(d1);
GrammarBuilder grab2 = new GrammarBuilder { Culture = ri.Culture };
// Any window
grab2.Append(new Choices("grab"));
var g2 = new Grammar(grab2);
this.speechEngine.LoadGrammar(g2);
GrammarBuilder dropit = new GrammarBuilder { Culture = ri.Culture };
// Any window
dropit.Append(new Choices("drop it"));
var drop = new Grammar(dropit);
this.speechEngine.LoadGrammar(drop);
GrammarBuilder mouse = new GrammarBuilder { Culture = ri.Culture };
// Any window
mouse.Append(new Choices("mouse mode"));
var mg = new Grammar(mouse);
this.speechEngine.LoadGrammar(mg);
GrammarBuilder click = new GrammarBuilder { Culture = ri.Culture };
click.Append(new Choices("click", "double click", "right click"));
var clickGram = new Grammar(click);
this.speechEngine.LoadGrammar(clickGram);
GrammarBuilder go = new GrammarBuilder { Culture = ri.Culture };
go.Append(new Choices("lets hack", "shut it down"));
var goGram = new Grammar(go);
this.speechEngine.LoadGrammar(goGram);
this.speechEngine.SpeechRecognized += this.SpeechRecognized;
this.speechEngine.SpeechRecognitionRejected += this.SpeechRejected;
// let the convertStream know speech is going active
this.convertStream.SpeechActive = true;
// For long recognition sessions (a few hours or more), it may be beneficial to turn off adaptation of the acoustic model.
// This will prevent recognition accuracy from degrading over time.
speechEngine.UpdateRecognizerSetting("AdaptationOn", 0);
this.speechEngine.SetInputToAudioStream(
this.convertStream, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
this.speechEngine.RecognizeAsync(RecognizeMode.Multiple);
}
else
{
this.StatusText = "No recognizer";
}
// Create the drawing group we'll use for drawing
this.drawingGroup = new DrawingGroup();
// Create an image source that we can use in our image control
this.imageSource = new DrawingImage(this.drawingGroup);
// use the window object as the view model in this simple example
this.DataContext = this;
// initialize the components (controls) of the window
this.InitializeComponent();
KnockSegment1 knockSegment1 = new KnockSegment1();
KnockSegment2 knockSegment2 = new KnockSegment2();
KnockSegment3 knockSegment3 = new KnockSegment3();
SlapSegment1 slapSegment1 = new SlapSegment1();
SlapSegment2 slapSegment2 = new SlapSegment2();
PokeSegment1 pokeSegment1 = new PokeSegment1();
PokeSegment2 pokeSegment2 = new PokeSegment2();
IGestureSegment[] knock = new IGestureSegment[]
{
knockSegment1,
knockSegment2
};
IGestureSegment[] knockPull = new IGestureSegment[]
{
knockSegment1,
knockSegment3
};
IGestureSegment[] slap = new IGestureSegment[]
{
slapSegment1,
slapSegment2
};
IGestureSegment[] poke = new IGestureSegment[]
{
pokeSegment1,
pokeSegment2
};
knockGesture = new GestureListener(knock);
knockGesture.GestureRecognized += Gesture_KnockRecognized;
knockPullGesture = new GestureListener(knockPull);
knockPullGesture.GestureRecognized += Gesture_KnockPullRecognized;
slapGesture = new GestureListener(slap);
slapGesture.GestureRecognized += Gesture_SlapRecognized;
pokeGesture = new GestureListener(poke);
pokeGesture.GestureRecognized += Gesture_PokeRecognized;
WindowDragStart dragSeg1 = new WindowDragStart();
WindowDragMove dragSeg2 = new WindowDragMove();
MouseMoveStart mouseSeg1 = new MouseMoveStart();
ScrollUpStart scrollUpSeg1 = new ScrollUpStart();
ScrollDownStart scrollUpSeg2 = new ScrollDownStart();
VolumeUpStart volumeUpStart = new VolumeUpStart();
VolumeDownStart volumeDownStart = new VolumeDownStart();
PausePlaySegment1 pausePlaySeg1 = new PausePlaySegment1();
PausePlaySegment2 pausePlaySeg2 = new PausePlaySegment2();
ShowAllStart showSeg1 = new ShowAllStart();
HideAllStart showSeg2 = new HideAllStart();
MouseMove mouseSeg2 = new MouseMove();
DragFinishedGesture dragFinished = new DragFinishedGesture();
VolumeFinishGesture volumeFinished = new VolumeFinishGesture();
ScrollFinishedGesture scrollFinished = new ScrollFinishedGesture();
MouseFinishedGesture mouseFinished = new MouseFinishedGesture();
IGestureSegment[] windowDrag = new IGestureSegment[]
{
dragSeg1,
dragSeg2
};
IGestureSegment[] mouseMove = new IGestureSegment[]
{
mouseSeg1,
mouseSeg2
};
IGestureSegment[] dragFinishedSequence = new IGestureSegment[]
{
dragFinished
};
IGestureSegment[] volumeFinishedSequence = new IGestureSegment[]
{
volumeFinished
};
IGestureSegment[] scrollFinishedSequence = new IGestureSegment[]
{
scrollFinished
};
IGestureSegment[] mouseFinishedSequence = new IGestureSegment[]
{
mouseFinished
};
IGestureSegment[] scrollUp = new IGestureSegment[]
{
scrollUpSeg1,
scrollUpSeg2
};
IGestureSegment[] scrollDown = new IGestureSegment[]
{
scrollUpSeg2,
scrollUpSeg1
};
IGestureSegment[] volumeUp = new IGestureSegment[]
{
volumeUpStart,
volumeDownStart
};
IGestureSegment[] volumeDown = new IGestureSegment[]
{
volumeDownStart,
volumeUpStart
};
IGestureSegment[] pausePlay = new IGestureSegment[]
{
pausePlaySeg1,
pausePlaySeg2
};
IGestureSegment[] bringUp = new IGestureSegment[]
{
showSeg1,
showSeg2
};
IGestureSegment[] bringDown = new IGestureSegment[]
{
showSeg2,
showSeg1
};
windowDragGesture = new GestureListener(windowDrag);
windowDragGesture.GestureRecognized += Gesture_DragMove;
windowDragGestureFinish = new GestureListener(dragFinishedSequence);
windowDragGestureFinish.GestureRecognized += Gesture_DragFinish;
mouseMoveGesture = new GestureListener(mouseMove);
mouseMoveGesture.GestureRecognized += Gesture_MouseMove;
mouseMoveGestureFinish = new GestureListener(mouseFinishedSequence);
mouseMoveGestureFinish.GestureRecognized += Gesture_MouseMoveFinish;
scrollUpGesture = new GestureListener(scrollUp);
scrollUpGesture.GestureRecognized += Gesture_ScrollUp;
scrollDownGesture = new GestureListener(scrollDown);
scrollDownGesture.GestureRecognized += Gesture_ScrollDown;
scrollGestureFinish = new GestureListener(scrollFinishedSequence);
scrollGestureFinish.GestureRecognized += Gesture_ScrollFinish;
volumeUpGesture = new GestureListener(volumeUp);
volumeUpGesture.GestureRecognized += Gesture_VolumeUp;
volumeDownGesture = new GestureListener(volumeDown);
volumeDownGesture.GestureRecognized += Gesture_VolumeDown;
volumeGestureFinish = new GestureListener(volumeFinishedSequence);
volumeGestureFinish.GestureRecognized += Gesture_VolumeFinish;
pausePlayGesture = new GestureListener(pausePlay);
pausePlayGesture.GestureRecognized += Gesture_PausePlay;
showAllGesture = new GestureListener(bringUp);
showAllGesture.GestureRecognized += Gesture_ShowAll;
hideAllGesture = new GestureListener(bringDown);
hideAllGesture.GestureRecognized += Gesture_HideAll;
}
private void Gesture_HideAll(object sender, EventArgs e)
{
Process[] processlist = Process.GetProcesses();
for (int i = 0; i < processlist.Length; i++)
{
if (!String.IsNullOrEmpty(processlist[i].MainWindowTitle))
{
window = Win32.GetForegroundWindow();
Win32.ShowWindow(window, Win32.SW_MINIMIZE);
Thread.Sleep(100);
}
}
}
private void Gesture_ShowAll(object sender, EventArgs e)
{
Process[] processlist = Process.GetProcesses();
for (int i = 0; i < processlist.Length; i++)
{
if (!String.IsNullOrEmpty(processlist[i].MainWindowTitle))
{
window = processlist[i].MainWindowHandle;
Win32.ShowWindow(window, Win32.SW_RESTORE);
Thread.Sleep(100);
}
}
}
private void Gesture_ScrollFinish(object sender, EventArgs e)
{
if (HandMouseState == HandMouseStates.SCROLL_DOWN || HandMouseState == HandMouseStates.SCROLL_UP)
{
HandMouseState = HandMouseStates.NONE;
Win32.SendMessage(window, Win32.WM_VSCROLL, (IntPtr)Win32.SB_ENDSCROLL, IntPtr.Zero);
}
}
private void Gesture_ScrollUp(object sender, EventArgs e)
{
HandMouseState = HandMouseStates.SCROLL_UP;
Console.WriteLine("Trying to scroll");
}
private void Gesture_ScrollDown(object sender, EventArgs e)
{
HandMouseState = HandMouseStates.SCROLL_DOWN;
Console.WriteLine("Trying to scroll down");
}
private void Gesture_VolumeFinish(object sender, EventArgs e)
{
if (HandMouseState == HandMouseStates.VOLUME_DOWN || HandMouseState == HandMouseStates.VOLUME_UP)
{
HandMouseState = HandMouseStates.NONE;
Win32.keybd_event(Win32.VK_VOLUME_UP, 0, Win32.KEYEVENTF_KEYUP, 0);
Win32.keybd_event(Win32.VK_VOLUME_DOWN, 0, Win32.KEYEVENTF_KEYUP, 0);
}
}
private void Gesture_VolumeUp(object sender, EventArgs e)
{
HandMouseState = HandMouseStates.VOLUME_UP;
}
private void Gesture_VolumeDown(object sender, EventArgs e)
{
HandMouseState = HandMouseStates.VOLUME_DOWN;
}
private void Gesture_PausePlay(object sender, EventArgs e)
{
//In case user is using closed left hand to drag windows; conflicting commands or accidental clicks when right hand is cursor
if ((HandMouseState != HandMouseStates.WINDOW_DRAG || WindowDragData.dragHand != JointType.HandLeft) && HandMouseState != HandMouseStates.CURSOR)
{
Win32.keybd_event(Win32.VK_VOLUME_MUTE, 0, 0, 0);
}
}
public void Gesture_DragMove(object sender, EventArgs e)
{
HandMouseState = HandMouseStates.WINDOW_DRAG;
WindowDragData.resetOldHand = true;
window = Win32.GetForegroundWindow();
physWindow = new PhysWindow(window);
}
public void Gesture_DragFinish(object sender, EventArgs e)
{
if (HandMouseState == HandMouseStates.WINDOW_DRAG)
{
HandMouseState = HandMouseStates.NONE;
WindowDragData.checkForFling = true;
}
}
public void Gesture_MouseMove(object sender, EventArgs e)
{
HandMouseState = HandMouseStates.CURSOR;
MouseMoveData.resetOldHand = true;
}
public void Gesture_MouseMoveFinish(object sender, EventArgs e)
{
HandMouseState = HandMouseStates.NONE;
}
public void Gesture_KnockRecognized(object sender, EventArgs e)
{
Win32.mouse_event((int)Win32.MouseEventFlags.LeftDown, 0, 0, 0, 0);
}
public void Gesture_KnockPullRecognized(object sender, EventArgs e)
{
Win32.mouse_event((int)Win32.MouseEventFlags.LeftUp, 0, 0, 0, 0);
}
public void Gesture_SlapRecognized(object sender, EventArgs e)
{
Win32.mouse_event((int)Win32.MouseEventFlags.RightDown, 0, 0, 0, 0);
Win32.mouse_event((int)Win32.MouseEventFlags.RightUp, 0, 0, 0, 0);
}
public void Gesture_PokeRecognized(object sender, EventArgs e)
{
Win32.mouse_event((int)Win32.MouseEventFlags.LeftDown, 0, 0, 0, 0);
Win32.mouse_event((int)Win32.MouseEventFlags.LeftUp, 0, 0, 0, 0);
Win32.mouse_event((int)Win32.MouseEventFlags.LeftDown, 0, 0, 0, 0);
Win32.mouse_event((int)Win32.MouseEventFlags.LeftUp, 0, 0, 0, 0);
}
/// <summary>
/// INotifyPropertyChangedPropertyChanged event to allow window controls to bind to changeable data
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
private GestureListener windowDragGesture;
private GestureListener windowDragGestureFinish;
private GestureListener mouseMoveGesture;
private GestureListener mouseMoveGestureFinish;
private Dictionary<string, IntPtr> processNameMap;
private PhysWindow physWindow;
private GestureListener scrollUpGesture;
private GestureListener scrollGestureFinish;
private GestureListener scrollDownGesture;
private GestureListener volumeUpGesture;
private GestureListener volumeGestureFinish;
private GestureListener volumeDownGesture;
private GestureListener pausePlayGesture;
private GestureListener showAllGesture;
private GestureListener hideAllGesture;
/// <summary>
/// Gets the bitmap to display
/// </summary>
public ImageSource ImageSource
{
get
{
return this.imageSource;
}
}
/// <summary>
/// Gets or sets the current status text to display
/// </summary>
public string StatusText
{
get
{
return this.statusText;
}
set
{
if (this.statusText != value)
{
this.statusText = value;
// notify any bound elements that the text has changed
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("StatusText"));
}
}
}
}
/// <summary>
/// Execute start up tasks
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
if (this.bodyFrameReader != null)
{
this.bodyFrameReader.FrameArrived += this.Reader_FrameArrived;
}
}
/// <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.kinectSensor != null)
{
this.kinectSensor.Close();
this.kinectSensor = null;
}
}
/// <summary>
/// Handles the body frame data arriving from the sensor
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void Reader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
bool dataReceived = false;
using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
{
if (bodyFrame != null)
{
if (this.bodies == null)
{
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);
dataReceived = true;
}
}
if (dataReceived)
{
using (DrawingContext dc = this.drawingGroup.Open())
{
// Draw a transparent background to set the render size
dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));
int penIndex = 0;
Body body = null;
double zDist = double.MaxValue;
foreach (Body b in this.bodies)
{
Console.WriteLine(b.Joints[JointType.Head].Position.Z + " : " + zDist);
if (b.Joints[JointType.Head].Position.Z < zDist && b.Joints[JointType.Head].Position.Z != 0)
{
zDist = b.Joints[JointType.Head].Position.Z;
body = b;
}
}
Console.WriteLine("Body: " + body);
if (body != null)
{
Pen drawPen = this.bodyColors[penIndex++];
if (body.IsTracked)
{
Console.WriteLine(body.TrackingId);
this.DrawClippedEdges(body, dc);
IReadOnlyDictionary<JointType, Joint> joints = body.Joints;
// convert the joint points to depth (display) space
Dictionary<JointType, Point> jointPoints = new Dictionary<JointType, Point>();
Dictionary<JointType, float> jointZs = new Dictionary<JointType, float>();
foreach (JointType jointType in joints.Keys)
{
// sometimes the depth(Z) of an inferred joint may show as negative
// clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity)
CameraSpacePoint position = joints[jointType].Position;
if (position.Z < 0)
{
position.Z = InferredZPositionClamp;
}
DepthSpacePoint depthSpacePoint = this.coordinateMapper.MapCameraPointToDepthSpace(position);
jointPoints[jointType] = new Point(depthSpacePoint.X, depthSpacePoint.Y);
jointZs[jointType] = position.Z;
}
this.DrawBody(joints, jointPoints, dc, drawPen);
this.DrawHand(body.HandLeftState, jointPoints[JointType.HandLeft], dc);
this.DrawHand(body.HandRightState, jointPoints[JointType.HandRight], dc);
knockGesture.Update(body);
knockPullGesture.Update(body);
slapGesture.Update(body);
pokeGesture.Update(body);
windowDragGesture.Update(body);
windowDragGestureFinish.Update(body);
mouseMoveGesture.Update(body);
mouseMoveGestureFinish.Update(body);
scrollUpGesture.Update(body);
scrollDownGesture.Update(body);
scrollGestureFinish.Update(body);
volumeUpGesture.Update(body);
volumeDownGesture.Update(body);
volumeGestureFinish.Update(body);
pausePlayGesture.Update(body);
showAllGesture.Update(body);
hideAllGesture.Update(body);
handMouseBehavior(body, jointPoints[JointType.HandLeft], jointPoints[JointType.HandRight],jointZs[JointType.HandLeft],jointZs[JointType.HandRight]);
}
}
// prevent drawing outside of our render area
this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));
}
}
}
private int scrollCounter = 0;
private int volumeCounter = 0;
private void handMouseBehavior(Body body, Point leftHand, Point rightHand,float leftZ, float rightZ)
{
if (physWindow != null)
{
physWindow.update();
}
switch (HandMouseState)
{
case HandMouseStates.NONE:
if (WindowDragData.checkForFling)
{
checkForFling(body, leftHand, rightHand);
}
break;
case HandMouseStates.CURSOR:
Point mousePoint;
float zCoordinate;
double armLength = calculateArmLength(body);
if (MouseMoveData.dragHand == JointType.HandLeft)
{
MouseMoveData.signalHand = JointType.HandRight;
mousePoint = leftHand;
zCoordinate = leftZ;
}
else
{
MouseMoveData.signalHand = JointType.HandLeft;
mousePoint = rightHand;
zCoordinate = rightZ;
}
if (MouseMoveData.resetOldHand)
{
MouseMoveData.lastHandPoint = mousePoint;
MouseMoveData.lastHandZ = zCoordinate;
MouseMoveData.resetOldHand = false;
}
double dx = mousePoint.X - MouseMoveData.lastHandPoint.X;
double dy = mousePoint.Y - MouseMoveData.lastHandPoint.Y;
double dz = zCoordinate - MouseMoveData.lastHandZ;
MouseMoveData.lastHandPoint = mousePoint;
MouseMoveData.lastHandZ = zCoordinate;
{
double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
double multiplier = Math.Max(screenHeight / armLength, screenWidth / armLength) / 500;
dx *= multiplier;
dy *= multiplier;
Win32.POINT lpPoint;
Win32.GetCursorPos(out lpPoint);
Win32.SetCursorPos(lpPoint.X + (int)dx, lpPoint.Y + (int)dy);
}
break;
case HandMouseStates.WINDOW_DRAG:
handleWindowDragging(body, leftHand, rightHand);
break;
case HandMouseStates.SCROLL_UP:
scrollCounter++;
double maxSpeed = Math.Abs(body.Joints[JointType.ShoulderRight].Position.Y - body.Joints[JointType.HipRight].Position.Y);
double dist = Math.Min(maxSpeed, Math.Abs(body.Joints[JointType.ShoulderRight].Position.Y - body.Joints[JointType.HandRight].Position.Y));
int delay = (int)((1 - dist / maxSpeed) * 5) + 1;
if (scrollCounter % delay == 0)
{
Win32.SendMessage(window, Win32.WM_VSCROLL, (IntPtr)Win32.SB_LINEUP, IntPtr.Zero);
Console.WriteLine("Sending line up");
}
break;
case HandMouseStates.SCROLL_DOWN:
scrollCounter++;
double maxSpeed2 = Math.Abs(body.Joints[JointType.ShoulderRight].Position.Y - body.Joints[JointType.HipRight].Position.Y);
double dist2 = Math.Min(maxSpeed2, Math.Abs(body.Joints[JointType.ShoulderRight].Position.Y - body.Joints[JointType.HandRight].Position.Y));
int delay2 = (int)((1 - dist2 / maxSpeed2) * 5) + 1;
if (scrollCounter % delay2 == 0)
{
Win32.SendMessage(window, Win32.WM_VSCROLL, (IntPtr)Win32.SB_LINEDOWN, IntPtr.Zero);
Console.WriteLine("Sending line down");
}
break;
case HandMouseStates.VOLUME_UP:
volumeCounter++;
double maxSpeed3 = Math.Abs(body.Joints[JointType.ShoulderLeft].Position.Y - body.Joints[JointType.HipLeft].Position.Y);
double dist3 = Math.Min(maxSpeed3, Math.Abs(body.Joints[JointType.ShoulderLeft].Position.Y - body.Joints[JointType.HandLeft].Position.Y));
int delay3 = (int)((1 - dist3 / maxSpeed3) * 4) + 1;
if (scrollCounter % delay3 == 0)
{
Win32.keybd_event(Win32.VK_VOLUME_UP, 0, 0, 0);
}
break;
case HandMouseStates.VOLUME_DOWN:
volumeCounter++;
double maxSpeed4 = Math.Abs(body.Joints[JointType.ShoulderLeft].Position.Y - body.Joints[JointType.HipLeft].Position.Y);
double dist4 = Math.Min(maxSpeed4, Math.Abs(body.Joints[JointType.ShoulderLeft].Position.Y - body.Joints[JointType.HandLeft].Position.Y));
int delay4 = (int)((1 - dist4 / maxSpeed4) * 8) + 1;
if (scrollCounter % delay4 == 0)
{
Win32.keybd_event(Win32.VK_VOLUME_DOWN, 0, 0, 0);
}
break;
default:
break;
}
}
private void handleWindowDragging(Body body, Point leftHand, Point rightHand)
{
Point mousePoint;
double armLength = calculateArmLength(body);
if (WindowDragData.dragHand == JointType.HandLeft)
{
mousePoint = leftHand;
}
else
{
mousePoint = rightHand;
}
if (WindowDragData.resetOldHand)
{
WindowDragData.lastHandPoint = mousePoint;
WindowDragData.resetOldHand = false;
}
double dx = mousePoint.X - WindowDragData.lastHandPoint.X;
double dy = mousePoint.Y - WindowDragData.lastHandPoint.Y;
WindowDragData.lastHandPoint = mousePoint;