-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
155 lines (135 loc) · 10.8 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
//------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace Microsoft.Samples.Kinect.KinectExplorer
{
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using Microsoft.Kinect;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
#region Private state
private readonly KinectSensorItems sensorItems;
#endregion Private state
#region ctor & Window events
public MainWindow()
{
InitializeComponent();
this.sensorItems = new KinectSensorItems();
this.kinectSensors.ItemsSource = this.sensorItems;
}
private void WindowLoaded(object sender, EventArgs e)
{
this.KinectStart();
}
private void WindowClosed(object sender, EventArgs e)
{
this.KinectStop();
}
#endregion ctor & Window events
#region Multi-Kinect discovery + setup
private void KinectStart()
{
// listen to any status change for Kinects.
KinectSensor.KinectSensors.StatusChanged += this.KinectsStatusChanged;
// show status for each sensor that is found now.
foreach (KinectSensor kinect in KinectSensor.KinectSensors)
{
this.ShowStatus(kinect, kinect.Status);
}
}
private void KinectStop()
{
foreach (KinectSensorItem sensorItem in this.sensorItems)
{
var kinectWindow = sensorItem.Window;
if (kinectWindow != null)
{
kinectWindow.Kinect = null;
kinectWindow.Close();
}
}
this.sensorItems.Clear();
}
private void ShowStatus(KinectSensor kinectSensor, KinectStatus kinectStatus)
{
sensorStatusChanges.Text += kinectSensor.DeviceConnectionId + " " + kinectStatus + "\n";
KinectSensorItem sensorItem;
this.sensorItems.SensorLookup.TryGetValue(kinectSensor, out sensorItem);
switch (kinectStatus)
{
case KinectStatus.Disconnected:
case KinectStatus.NotPowered:
if (sensorItem != null)
{
this.sensorItems.Remove(sensorItem);
if (sensorItem.Window != null)
{
sensorItem.Window.Close();
sensorItem.Window = null;
}
}
break;
default:
if (sensorItem == null)
{
sensorItem = new KinectSensorItem
{
Window = null,
Sensor = kinectSensor,
Status = kinectSensor.Status
};
this.sensorItems.Add(sensorItem);
}
// show a window, if one isn't already shown, unless we are initializing
if (sensorItem.Window == null && kinectStatus != KinectStatus.Initializing)
{
var kinectWindow = new KinectWindow { Kinect = kinectSensor };
kinectWindow.Show();
sensorItem.Window = kinectWindow;
}
if (sensorItem.Window != null)
{
sensorItem.Window.StatusChanged();
}
sensorItem.Status = kinectStatus;
break;
}
}
private void KinectsStatusChanged(object sender, StatusChangedEventArgs e)
{
this.ShowStatus(e.Sensor, e.Status);
}
#endregion Multi-Kinect discovery + setup
#region UI event handlers
private void ShowMoreInfo(object sender, RoutedEventArgs e)
{
Hyperlink hyperlink = e.OriginalSource as Hyperlink;
if (hyperlink != null)
{
// Careful - ensure that this NavigateUri comes from a trusted source, as in this sample, before launching a process using it.
Process.Start(new ProcessStartInfo(hyperlink.NavigateUri.ToString()));
}
e.Handled = true;
}
// if you mousedown with a control key pressed down, it will try to begin execution again...as if it was a fresh startup.
// likely not useful to include in the tool, once we fix bugs.
private void GridMouseDown(object sender, MouseEventArgs e)
{
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
{
this.KinectStop();
this.KinectStart();
}
}
#endregion UI event handlers
}
}