5 using System.Windows.Controls;
 
    6 using System.Windows.Data;
 
    8 using SiliconStudio.Core.Mathematics;
 
   10 namespace SiliconStudio.Presentation.Controls
 
   14         private bool interlock;
 
   19         public static readonly DependencyProperty VectorProperty = DependencyProperty.Register(
"Vector", typeof(
Vector4), typeof(
Vector4Editor), 
new FrameworkPropertyMetadata(
default(
Vector4), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVectorPropertyChanged, null, 
false, UpdateSourceTrigger.Explicit));
 
   24         public static readonly DependencyProperty XProperty = DependencyProperty.Register(
"X", typeof(
float), typeof(
Vector4Editor), 
new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCartesianPropertyChanged));
 
   29         public static readonly DependencyProperty YProperty = DependencyProperty.Register(
"Y", typeof(
float), typeof(
Vector4Editor), 
new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCartesianPropertyChanged));
 
   34         public static readonly DependencyProperty ZProperty = DependencyProperty.Register(
"Z", typeof(
float), typeof(
Vector4Editor), 
new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCartesianPropertyChanged));
 
   39         public static readonly DependencyProperty WProperty = DependencyProperty.Register(
"W", typeof(
float), typeof(
Vector4Editor), 
new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCartesianPropertyChanged));
 
   44         public static readonly DependencyProperty LengthProperty = DependencyProperty.Register(
"Length", typeof(
float), typeof(
Vector4Editor), 
new FrameworkPropertyMetadata(0.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPolarPropertyChanged, CoerceLengthValue));
 
   49         public Vector4 Vector { 
get { 
return (
Vector4)GetValue(VectorProperty); } set { SetValue(VectorProperty, value); } }
 
   54         public float X { 
get { 
return (
float)GetValue(XProperty); } set { SetValue(XProperty, value); } }
 
   59         public float Y { 
get { 
return (
float)GetValue(YProperty); } set { SetValue(YProperty, value); } }
 
   64         public float Z { 
get { 
return (
float)GetValue(ZProperty); } set { SetValue(ZProperty, value); } }
 
   69         public float W { 
get { 
return (
float)GetValue(WProperty); } set { SetValue(WProperty, value); } }
 
   74         public float Length { 
get { 
return (
float)GetValue(LengthProperty); } set { SetValue(LengthProperty, value); } }
 
   79         private void OnVectorValueChanged()
 
   84                 SetCurrentValue(XProperty, Vector.X);
 
   85                 SetCurrentValue(YProperty, Vector.Y);
 
   86                 SetCurrentValue(ZProperty, Vector.Z);
 
   87                 SetCurrentValue(WProperty, Vector.W);
 
   88                 SetCurrentValue(LengthProperty, Vector.Length());
 
   92             UpdateBinding(VectorProperty);
 
   99         private void OnCartesianValueChanged(DependencyPropertyChangedEventArgs e)
 
  104                 if (e.Property == XProperty)
 
  105                     Vector = 
new Vector4((
float)e.NewValue, Vector.Y, Vector.Z, Vector.W);
 
  106                 else if (e.Property == YProperty)
 
  107                     Vector = 
new Vector4(Vector.X, (
float)e.NewValue, Vector.Z, Vector.W);
 
  108                 else if (e.Property == ZProperty)
 
  109                     Vector = 
new Vector4(Vector.X, Vector.Y, (
float)e.NewValue, Vector.W);
 
  110                 else if (e.Property == WProperty)
 
  111                     Vector = 
new Vector4(Vector.X, Vector.Y, Vector.Z, (
float)e.NewValue);
 
  113                     throw new ArgumentException(
"Property unsupported by method OnCartesianPropertyChanged.");
 
  115                 SetCurrentValue(LengthProperty, Vector.Length());
 
  119             UpdateBinding(e.Property);
 
  126         private void OnLengthValueChanged(DependencyPropertyChangedEventArgs e)
 
  135                 SetCurrentValue(XProperty, 
Vector.X);
 
  136                 SetCurrentValue(YProperty, 
Vector.Y);
 
  137                 SetCurrentValue(ZProperty, 
Vector.Z);
 
  138                 SetCurrentValue(WProperty, 
Vector.W);
 
  142             UpdateBinding(e.Property);
 
  149         private void UpdateBinding(DependencyProperty dependencyProperty)
 
  151             BindingExpression expression = GetBindingExpression(dependencyProperty);
 
  152             if (expression != null)
 
  153                 expression.UpdateSource();
 
  161         private static void OnVectorPropertyChanged(
DependencyObject sender, DependencyPropertyChangedEventArgs e)
 
  163             var editor = (Vector4Editor)sender;
 
  164             editor.OnVectorValueChanged();
 
  173         private static void OnCartesianPropertyChanged(
DependencyObject sender, DependencyPropertyChangedEventArgs e)
 
  175             var editor = (Vector4Editor)sender;
 
  176             editor.OnCartesianValueChanged(e);
 
  184         private static void OnPolarPropertyChanged(
DependencyObject sender, DependencyPropertyChangedEventArgs e)
 
  186             var editor = (Vector4Editor)sender;
 
  187             editor.OnLengthValueChanged(e);
 
  193         private static object CoerceLengthValue(
DependencyObject sender, 
object baseValue)
 
  195             return Math.Max(0.0f, (float)baseValue);
 
Represents a four dimensional mathematical vector.