// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Kinect.Toolkit
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using Microsoft.Kinect.Toolkit.Properties;
///
/// View model for the KinectSensorChooser.
///
public class KinectSensorChooserUIViewModel : DependencyObject
{
[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "ReadOnlyDependencyProperty requires private static field to be initialized prior to the public static field")]
private static readonly DependencyPropertyKey MessagePropertyKey = DependencyProperty.RegisterReadOnly(
"Message", typeof(string), typeof(KinectSensorChooserUIViewModel), new PropertyMetadata(string.Empty));
[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "ReadOnlyDependencyProperty requires private static field to be initialized prior to the public static field")]
private static readonly DependencyPropertyKey MoreInformationPropertyKey = DependencyProperty.RegisterReadOnly(
"MoreInformation", typeof(string), typeof(KinectSensorChooserUIViewModel), new PropertyMetadata(string.Empty));
[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "ReadOnlyDependencyProperty requires private static field to be initialized prior to the public static field")]
private static readonly DependencyPropertyKey MoreInformationUriPropertyKey =
DependencyProperty.RegisterReadOnly(
"MoreInformationUri", typeof(Uri), typeof(KinectSensorChooserUIViewModel), new PropertyMetadata(null));
[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "ReadOnlyDependencyProperty requires private static field to be initialized prior to the public static field")]
private static readonly DependencyPropertyKey MoreInformationVisibilityPropertyKey =
DependencyProperty.RegisterReadOnly(
"MoreInformationVisibility",
typeof(Visibility),
typeof(KinectSensorChooserUIViewModel),
new PropertyMetadata(Visibility.Collapsed));
///
/// Set this to true when the application is listining for audio input from the sensor.
/// UI will show a microphone icon.
///
public static readonly DependencyProperty IsListeningProperty = DependencyProperty.Register(
"IsListening",
typeof(bool),
typeof(KinectSensorChooserUIViewModel),
new PropertyMetadata(false, (o, args) => ((KinectSensorChooserUIViewModel)o).IsListeningChanged()));
///
/// The KinectSensorChooser whose status we are displaying.
///
public static readonly DependencyProperty KinectSensorChooserProperty = DependencyProperty.Register(
"KinectSensorChooser",
typeof(KinectSensorChooser),
typeof(KinectSensorChooserUIViewModel),
new PropertyMetadata(
null,
(o, args) => ((KinectSensorChooserUIViewModel)o).OnKinectKinectSensorChooserChanged((KinectSensorChooser)args.NewValue)));
///
/// The current ChooserStatus of our KinectSensorChooser
///
public static readonly DependencyProperty StatusProperty = DependencyProperty.Register(
"Status",
typeof(ChooserStatus),
typeof(KinectSensorChooserUIViewModel),
new PropertyMetadata(ChooserStatus.None, (o, args) => ((KinectSensorChooserUIViewModel)o).OnStatusChanged()));
///
/// The state we want the VisualStateManager in the UI to be in.
///
public static readonly DependencyProperty VisualStateProperty = DependencyProperty.Register(
"VisualState", typeof(string), typeof(KinectSensorChooserUIViewModel), new PropertyMetadata(null));
///
/// The short message to display.
///
public static readonly DependencyProperty MessageProperty = MessagePropertyKey.DependencyProperty;
///
/// The more descriptive message to display.
///
public static readonly DependencyProperty MoreInformationProperty = MoreInformationPropertyKey.DependencyProperty;
///
/// Uri for more information on the state we are in.
///
public static readonly DependencyProperty MoreInformationUriProperty = MoreInformationUriPropertyKey.DependencyProperty;
///
/// The visibility we want for the MoreInformation Uri.
///
public static readonly DependencyProperty MoreInformationVisibilityProperty = MoreInformationVisibilityPropertyKey.DependencyProperty;
private RelayCommand retryCommand;
///
/// Set this to true when the application is listining for audio input from the sensor.
/// UI will show a microphone icon.
///
public bool IsListening
{
get
{
return (bool)this.GetValue(IsListeningProperty);
}
set
{
this.SetValue(IsListeningProperty, value);
}
}
///
/// The KinectSensorChooser whose status we are displaying.
///
public KinectSensorChooser KinectSensorChooser
{
get
{
return (KinectSensorChooser)this.GetValue(KinectSensorChooserProperty);
}
set
{
this.SetValue(KinectSensorChooserProperty, value);
}
}
///
/// The short message to display.
///
public string Message
{
get
{
return (string)this.GetValue(MessageProperty);
}
private set
{
this.SetValue(MessagePropertyKey, value);
}
}
///
/// The more descriptive message to display.
///
public string MoreInformation
{
get
{
return (string)this.GetValue(MoreInformationProperty);
}
private set
{
this.SetValue(MoreInformationPropertyKey, value);
}
}
///
/// Uri for more information on the state we are in.
///
public Uri MoreInformationUri
{
get
{
return (Uri)this.GetValue(MoreInformationUriProperty);
}
private set
{
this.SetValue(MoreInformationUriPropertyKey, value);
}
}
///
/// The visibility we want for the MoreInformation Uri.
///
public Visibility MoreInformationVisibility
{
get
{
return (Visibility)this.GetValue(MoreInformationVisibilityProperty);
}
private set
{
this.SetValue(MoreInformationVisibilityPropertyKey, value);
}
}
///
/// Command to retry getting a sensor.
///
public ICommand RetryCommand
{
get
{
if (this.retryCommand == null)
{
this.retryCommand = new RelayCommand(this.Retry, this.CanRetry);
}
return this.retryCommand;
}
}
///
/// The current ChooserStatus of our KinectSensorChooser
///
public ChooserStatus Status
{
get
{
return (ChooserStatus)this.GetValue(StatusProperty);
}
set
{
this.SetValue(StatusProperty, value);
}
}
///
/// The state we want the VisualStateManager in the UI to be in.
///
public string VisualState
{
get
{
return (string)this.GetValue(VisualStateProperty);
}
set
{
this.SetValue(VisualStateProperty, value);
}
}
///
/// Determines if the retry command is available
///
/// true if retry is valid, false otherwise
private bool CanRetry()
{
// You can retry if another app was using the sensor the last
// time we tried. You can also retry if the only problem was
// that there were no sensors available since this app may
// have released one.
return (0 != (this.Status & ChooserStatus.SensorConflict)) || (this.Status == ChooserStatus.NoAvailableSensors);
}
private void IsListeningChanged()
{
this.UpdateState();
}
private void OnKinectKinectSensorChooserChanged(KinectSensorChooser newValue)
{
if (newValue != null)
{
var statusBinding = new Binding("Status") { Source = newValue };
BindingOperations.SetBinding(this, StatusProperty, statusBinding);
}
else
{
BindingOperations.ClearBinding(this, StatusProperty);
}
}
private void OnStatusChanged()
{
this.UpdateState();
}
private void Retry()
{
if (this.KinectSensorChooser != null)
{
this.KinectSensorChooser.TryResolveConflict();
}
}
private void UpdateState()
{
string newVisualState;
string message;
string moreInfo = null;
Uri moreInfoUri = null;
if ((this.Status & ChooserStatus.SensorStarted) != 0)
{
if (this.IsListening)
{
newVisualState = "AllSetListening";
message = Resources.MessageAllSetListening;
}
else
{
newVisualState = "AllSetNotListening";
message = Resources.MessageAllSet;
}
}
else if ((this.Status & ChooserStatus.SensorInitializing) != 0)
{
newVisualState = "Initializing";
message = Resources.MessageInitializing;
}
else if ((this.Status & ChooserStatus.SensorConflict) != 0)
{
newVisualState = "Error";
message = Resources.MessageConflict;
moreInfo = Resources.MoreInformationConflict;
moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239812");
}
else if ((this.Status & ChooserStatus.SensorNotGenuine) != 0)
{
newVisualState = "Error";
message = Resources.MessageNotGenuine;
moreInfo = Resources.MoreInformationNotGenuine;
moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239813");
}
else if ((this.Status & ChooserStatus.SensorNotSupported) != 0)
{
newVisualState = "Error";
message = Resources.MessageNotSupported;
moreInfo = Resources.MoreInformationNotSupported;
moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239814");
}
else if ((this.Status & ChooserStatus.SensorError) != 0)
{
newVisualState = "Error";
message = Resources.MessageError;
moreInfo = Resources.MoreInformationError;
moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239817");
}
else if ((this.Status & ChooserStatus.SensorInsufficientBandwidth) != 0)
{
newVisualState = "Error";
message = Resources.MessageInsufficientBandwidth;
moreInfo = Resources.MoreInformationInsufficientBandwidth;
moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239818");
}
else if ((this.Status & ChooserStatus.SensorNotPowered) != 0)
{
newVisualState = "Error";
message = Resources.MessageNotPowered;
moreInfo = Resources.MoreInformationNotPowered;
moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239819");
}
else if ((this.Status & ChooserStatus.NoAvailableSensors) != 0)
{
newVisualState = "NoAvailableSensors";
message = Resources.MessageNoAvailableSensors;
moreInfo = Resources.MoreInformationNoAvailableSensors;
moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239815");
}
else
{
newVisualState = "Stopped";
message = Resources.MessageNoAvailableSensors;
moreInfo = Resources.MoreInformationNoAvailableSensors;
moreInfoUri = new Uri("http://go.microsoft.com/fwlink/?LinkID=239815");
}
this.Message = message;
this.MoreInformation = moreInfo;
this.MoreInformationUri = moreInfoUri;
this.MoreInformationVisibility = moreInfoUri == null ? Visibility.Collapsed : Visibility.Visible;
if (this.retryCommand != null)
{
this.retryCommand.InvokeCanExecuteChanged();
}
this.VisualState = newVisualState;
}
}
}