source: other-projects/playing-in-the-street/summer-2013/trunk/Microsoft.Kinect.Toolkit/KinectSensorChooserUIViewModel.cs@ 28895

Last change on this file since 28895 was 28895, checked in by davidb, 10 years ago

Base Kinect Project

File size: 14.8 KB
Line 
1// --------------------------------------------------------------------------------------------------------------------
2// <copyright file="KinectSensorChooserUIViewModel.cs" company="Microsoft">
3// Copyright (c) Microsoft Corporation. All rights reserved.
4// </copyright>
5// --------------------------------------------------------------------------------------------------------------------
6
7namespace Microsoft.Kinect.Toolkit
8{
9 using System;
10 using System.Diagnostics.CodeAnalysis;
11 using System.Windows;
12 using System.Windows.Data;
13 using System.Windows.Input;
14 using Microsoft.Kinect.Toolkit.Properties;
15
16 /// <summary>
17 /// View model for the KinectSensorChooser.
18 /// </summary>
19 public class KinectSensorChooserUIViewModel : DependencyObject
20 {
21 [SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "ReadOnlyDependencyProperty requires private static field to be initialized prior to the public static field")]
22 private static readonly DependencyPropertyKey MessagePropertyKey = DependencyProperty.RegisterReadOnly(
23 "Message", typeof(string), typeof(KinectSensorChooserUIViewModel), new PropertyMetadata(string.Empty));
24
25 [SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "ReadOnlyDependencyProperty requires private static field to be initialized prior to the public static field")]
26 private static readonly DependencyPropertyKey MoreInformationPropertyKey = DependencyProperty.RegisterReadOnly(
27 "MoreInformation", typeof(string), typeof(KinectSensorChooserUIViewModel), new PropertyMetadata(string.Empty));
28
29 [SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "ReadOnlyDependencyProperty requires private static field to be initialized prior to the public static field")]
30 private static readonly DependencyPropertyKey MoreInformationUriPropertyKey =
31 DependencyProperty.RegisterReadOnly(
32 "MoreInformationUri", typeof(Uri), typeof(KinectSensorChooserUIViewModel), new PropertyMetadata(null));
33
34 [SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "ReadOnlyDependencyProperty requires private static field to be initialized prior to the public static field")]
35 private static readonly DependencyPropertyKey MoreInformationVisibilityPropertyKey =
36 DependencyProperty.RegisterReadOnly(
37 "MoreInformationVisibility",
38 typeof(Visibility),
39 typeof(KinectSensorChooserUIViewModel),
40 new PropertyMetadata(Visibility.Collapsed));
41
42 /// <summary>
43 /// Set this to true when the application is listining for audio input from the sensor.
44 /// UI will show a microphone icon.
45 /// </summary>
46 public static readonly DependencyProperty IsListeningProperty = DependencyProperty.Register(
47 "IsListening",
48 typeof(bool),
49 typeof(KinectSensorChooserUIViewModel),
50 new PropertyMetadata(false, (o, args) => ((KinectSensorChooserUIViewModel)o).IsListeningChanged()));
51
52 /// <summary>
53 /// The KinectSensorChooser whose status we are displaying.
54 /// </summary>
55 public static readonly DependencyProperty KinectSensorChooserProperty = DependencyProperty.Register(
56 "KinectSensorChooser",
57 typeof(KinectSensorChooser),
58 typeof(KinectSensorChooserUIViewModel),
59 new PropertyMetadata(
60 null,
61 (o, args) => ((KinectSensorChooserUIViewModel)o).OnKinectKinectSensorChooserChanged((KinectSensorChooser)args.NewValue)));
62
63 /// <summary>
64 /// The current ChooserStatus of our KinectSensorChooser
65 /// </summary>
66 public static readonly DependencyProperty StatusProperty = DependencyProperty.Register(
67 "Status",
68 typeof(ChooserStatus),
69 typeof(KinectSensorChooserUIViewModel),
70 new PropertyMetadata(ChooserStatus.None, (o, args) => ((KinectSensorChooserUIViewModel)o).OnStatusChanged()));
71
72 /// <summary>
73 /// The state we want the VisualStateManager in the UI to be in.
74 /// </summary>
75 public static readonly DependencyProperty VisualStateProperty = DependencyProperty.Register(
76 "VisualState", typeof(string), typeof(KinectSensorChooserUIViewModel), new PropertyMetadata(null));
77
78 /// <summary>
79 /// The short message to display.
80 /// </summary>
81 public static readonly DependencyProperty MessageProperty = MessagePropertyKey.DependencyProperty;
82
83 /// <summary>
84 /// The more descriptive message to display.
85 /// </summary>
86 public static readonly DependencyProperty MoreInformationProperty = MoreInformationPropertyKey.DependencyProperty;
87
88 /// <summary>
89 /// Uri for more information on the state we are in.
90 /// </summary>
91 public static readonly DependencyProperty MoreInformationUriProperty = MoreInformationUriPropertyKey.DependencyProperty;
92
93 /// <summary>
94 /// The visibility we want for the MoreInformation Uri.
95 /// </summary>
96 public static readonly DependencyProperty MoreInformationVisibilityProperty = MoreInformationVisibilityPropertyKey.DependencyProperty;
97
98 private RelayCommand retryCommand;
99
100 /// <summary>
101 /// Set this to true when the application is listining for audio input from the sensor.
102 /// UI will show a microphone icon.
103 /// </summary>
104 public bool IsListening
105 {
106 get
107 {
108 return (bool)this.GetValue(IsListeningProperty);
109 }
110
111 set
112 {
113 this.SetValue(IsListeningProperty, value);
114 }
115 }
116
117 /// <summary>
118 /// The KinectSensorChooser whose status we are displaying.
119 /// </summary>
120 public KinectSensorChooser KinectSensorChooser
121 {
122 get
123 {
124 return (KinectSensorChooser)this.GetValue(KinectSensorChooserProperty);
125 }
126
127 set
128 {
129 this.SetValue(KinectSensorChooserProperty, value);
130 }
131 }
132
133 /// <summary>
134 /// The short message to display.
135 /// </summary>
136 public string Message
137 {
138 get
139 {
140 return (string)this.GetValue(MessageProperty);
141 }
142
143 private set
144 {
145 this.SetValue(MessagePropertyKey, value);
146 }
147 }
148
149 /// <summary>
150 /// The more descriptive message to display.
151 /// </summary>
152 public string MoreInformation
153 {
154 get
155 {
156 return (string)this.GetValue(MoreInformationProperty);
157 }
158
159 private set
160 {
161 this.SetValue(MoreInformationPropertyKey, value);
162 }
163 }
164
165 /// <summary>
166 /// Uri for more information on the state we are in.
167 /// </summary>
168 public Uri MoreInformationUri
169 {
170 get
171 {
172 return (Uri)this.GetValue(MoreInformationUriProperty);
173 }
174
175 private set
176 {
177 this.SetValue(MoreInformationUriPropertyKey, value);
178 }
179 }
180
181 /// <summary>
182 /// The visibility we want for the MoreInformation Uri.
183 /// </summary>
184 public Visibility MoreInformationVisibility
185 {
186 get
187 {
188 return (Visibility)this.GetValue(MoreInformationVisibilityProperty);
189 }
190
191 private set
192 {
193 this.SetValue(MoreInformationVisibilityPropertyKey, value);
194 }
195 }
196
197 /// <summary>
198 /// Command to retry getting a sensor.
199 /// </summary>
200 public ICommand RetryCommand
201 {
202 get
203 {
204 if (this.retryCommand == null)
205 {
206 this.retryCommand = new RelayCommand(this.Retry, this.CanRetry);
207 }
208
209 return this.retryCommand;
210 }
211 }
212
213 /// <summary>
214 /// The current ChooserStatus of our KinectSensorChooser
215 /// </summary>
216 public ChooserStatus Status
217 {
218 get
219 {
220 return (ChooserStatus)this.GetValue(StatusProperty);
221 }
222
223 set
224 {
225 this.SetValue(StatusProperty, value);
226 }
227 }
228
229 /// <summary>
230 /// The state we want the VisualStateManager in the UI to be in.
231 /// </summary>
232 public string VisualState
233 {
234 get
235 {
236 return (string)this.GetValue(VisualStateProperty);
237 }
238
239 set
240 {
241 this.SetValue(VisualStateProperty, value);
242 }
243 }
244
245 /// <summary>
246 /// Determines if the retry command is available
247 /// </summary>
248 /// <returns>true if retry is valid, false otherwise</returns>
249 private bool CanRetry()
250 {
251 // You can retry if another app was using the sensor the last
252 // time we tried. You can also retry if the only problem was
253 // that there were no sensors available since this app may
254 // have released one.
255 return (0 != (this.Status & ChooserStatus.SensorConflict)) || (this.Status == ChooserStatus.NoAvailableSensors);
256 }
257
258 private void IsListeningChanged()
259 {
260 this.UpdateState();
261 }
262
263 private void OnKinectKinectSensorChooserChanged(KinectSensorChooser newValue)
264 {
265 if (newValue != null)
266 {
267 var statusBinding = new Binding("Status") { Source = newValue };
268 BindingOperations.SetBinding(this, StatusProperty, statusBinding);
269 }
270 else
271 {
272 BindingOperations.ClearBinding(this, StatusProperty);
273 }
274 }
275
276 private void OnStatusChanged()
277 {
278 this.UpdateState();
279 }
280
281 private void Retry()
282 {
283 if (this.KinectSensorChooser != null)
284 {
285 this.KinectSensorChooser.TryResolveConflict();
286 }
287 }
288
289 private void UpdateState()
290 {
291 string newVisualState;
292 string message;
293 string moreInfo = null;
294 Uri moreInfoUri = null;
295
296 if ((this.Status & ChooserStatus.SensorStarted) != 0)
297 {
298 if (this.IsListening)
299 {
300 newVisualState = "AllSetListening";
301 message = Resources.MessageAllSetListening;
302 }
303 else
304 {
305 newVisualState = "AllSetNotListening";
306 message = Resources.MessageAllSet;
307 }
308 }
309 else if ((this.Status & ChooserStatus.SensorInitializing) != 0)
310 {
311 newVisualState = "Initializing";
312 message = Resources.MessageInitializing;
313 }
314 else if ((this.Status & ChooserStatus.SensorConflict) != 0)
315 {
316 newVisualState = "Error";
317 message = Resources.MessageConflict;
318 moreInfo = Resources.MoreInformationConflict;
319 moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239812");
320 }
321 else if ((this.Status & ChooserStatus.SensorNotGenuine) != 0)
322 {
323 newVisualState = "Error";
324 message = Resources.MessageNotGenuine;
325 moreInfo = Resources.MoreInformationNotGenuine;
326 moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239813");
327 }
328 else if ((this.Status & ChooserStatus.SensorNotSupported) != 0)
329 {
330 newVisualState = "Error";
331 message = Resources.MessageNotSupported;
332 moreInfo = Resources.MoreInformationNotSupported;
333 moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239814");
334 }
335 else if ((this.Status & ChooserStatus.SensorError) != 0)
336 {
337 newVisualState = "Error";
338 message = Resources.MessageError;
339 moreInfo = Resources.MoreInformationError;
340 moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239817");
341 }
342 else if ((this.Status & ChooserStatus.SensorInsufficientBandwidth) != 0)
343 {
344 newVisualState = "Error";
345 message = Resources.MessageInsufficientBandwidth;
346 moreInfo = Resources.MoreInformationInsufficientBandwidth;
347 moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239818");
348 }
349 else if ((this.Status & ChooserStatus.SensorNotPowered) != 0)
350 {
351 newVisualState = "Error";
352 message = Resources.MessageNotPowered;
353 moreInfo = Resources.MoreInformationNotPowered;
354 moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239819");
355 }
356 else if ((this.Status & ChooserStatus.NoAvailableSensors) != 0)
357 {
358 newVisualState = "NoAvailableSensors";
359 message = Resources.MessageNoAvailableSensors;
360 moreInfo = Resources.MoreInformationNoAvailableSensors;
361 moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239815");
362 }
363 else
364 {
365 newVisualState = "Stopped";
366 message = Resources.MessageNoAvailableSensors;
367 moreInfo = Resources.MoreInformationNoAvailableSensors;
368 moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239815");
369 }
370
371 this.Message = message;
372 this.MoreInformation = moreInfo;
373 this.MoreInformationUri = moreInfoUri;
374 this.MoreInformationVisibility = moreInfoUri == null ? Visibility.Collapsed : Visibility.Visible;
375 if (this.retryCommand != null)
376 {
377 this.retryCommand.InvokeCanExecuteChanged();
378 }
379
380 this.VisualState = newVisualState;
381 }
382 }
383}
Note: See TracBrowser for help on using the repository browser.