Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Vector3Editor.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Windows;
5 using System.Windows.Controls;
6 using System.Windows.Data;
7 
8 using SiliconStudio.Core.Mathematics;
9 
10 namespace SiliconStudio.Presentation.Controls
11 {
12  public class Vector3Editor : Control
13  {
14  private bool interlock;
15 
16  /// <summary>
17  /// Identifies the <see cref="Vector"/> dependency property.
18  /// </summary>
19  public static readonly DependencyProperty VectorProperty = DependencyProperty.Register("Vector", typeof(Vector3), typeof(Vector3Editor), new FrameworkPropertyMetadata(default(Vector3), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVectorPropertyChanged, null, false, UpdateSourceTrigger.Explicit));
20 
21  /// <summary>
22  /// Identifies the <see cref="X"/> dependency property.
23  /// </summary>
24  public static readonly DependencyProperty XProperty = DependencyProperty.Register("X", typeof(float), typeof(Vector3Editor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCartesianPropertyChanged));
25 
26  /// <summary>
27  /// Identifies the <see cref="Y"/> dependency property.
28  /// </summary>
29  public static readonly DependencyProperty YProperty = DependencyProperty.Register("Y", typeof(float), typeof(Vector3Editor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCartesianPropertyChanged));
30 
31  /// <summary>
32  /// Identifies the <see cref="Z"/> dependency property.
33  /// </summary>
34  public static readonly DependencyProperty ZProperty = DependencyProperty.Register("Z", typeof(float), typeof(Vector3Editor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCartesianPropertyChanged));
35 
36  /// <summary>
37  /// Identifies the <see cref="Length"/> dependency property.
38  /// </summary>
39  public static readonly DependencyProperty LengthProperty = DependencyProperty.Register("Length", typeof(float), typeof(Vector3Editor), new FrameworkPropertyMetadata(0.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPolarPropertyChanged, CoerceLengthValue));
40 
41  /// <summary>
42  /// The <see cref="Vector3"/> associated to this control.
43  /// </summary>
44  public Vector3 Vector { get { return (Vector3)GetValue(VectorProperty); } set { SetValue(VectorProperty, value); } }
45 
46  /// <summary>
47  /// The X component (in Cartesian coordinate system) of the <see cref="Vector3"/> associated to this control.
48  /// </summary>
49  public float X { get { return (float)GetValue(XProperty); } set { SetValue(XProperty, value); } }
50 
51  /// <summary>
52  /// The Y component (in Cartesian coordinate system) of the <see cref="Vector3"/> associated to this control.
53  /// </summary>
54  public float Y { get { return (float)GetValue(YProperty); } set { SetValue(YProperty, value); } }
55 
56  /// <summary>
57  /// The Y component (in Cartesian coordinate system) of the <see cref="Vector3"/> associated to this control.
58  /// </summary>
59  public float Z { get { return (float)GetValue(ZProperty); } set { SetValue(ZProperty, value); } }
60 
61  /// <summary>
62  /// The length (in polar coordinate system) of the <see cref="Vector3"/> associated to this control.
63  /// </summary>
64  public float Length { get { return (float)GetValue(LengthProperty); } set { SetValue(LengthProperty, value); } }
65 
66  /// <summary>
67  /// Raised when the <see cref="Vector"/> property is modified.
68  /// </summary>
69  private void OnVectorValueChanged()
70  {
71  if (!interlock)
72  {
73  interlock = true;
74  SetCurrentValue(XProperty, Vector.X);
75  SetCurrentValue(YProperty, Vector.Y);
76  SetCurrentValue(ZProperty, Vector.Z);
77  SetCurrentValue(LengthProperty, Vector.Length());
78  interlock = false;
79  }
80 
81  UpdateBinding(VectorProperty);
82  }
83 
84  /// <summary>
85  /// Raised when the <see cref="X"/>, <see cref="Y"/> or <see cref="Z"/> properties are modified.
86  /// </summary>
87  /// <param name="e">The dependency property that has changed.</param>
88  private void OnCartesianValueChanged(DependencyPropertyChangedEventArgs e)
89  {
90  if (!interlock)
91  {
92  interlock = true;
93  if (e.Property == XProperty)
94  Vector = new Vector3((float)e.NewValue, Vector.Y, Vector.Z);
95  else if (e.Property == YProperty)
96  Vector = new Vector3(Vector.X, (float)e.NewValue, Vector.Z);
97  else if (e.Property == ZProperty)
98  Vector = new Vector3(Vector.X, Vector.Y, (float)e.NewValue);
99  else
100  throw new ArgumentException("Property unsupported by method OnCartesianPropertyChanged.");
101 
102  SetCurrentValue(LengthProperty, Vector.Length());
103  interlock = false;
104  }
105 
106  UpdateBinding(e.Property);
107  }
108 
109  /// <summary>
110  /// Raised when the <see cref="Length"/> property is modified.
111  /// </summary>
112  /// <param name="e">The dependency property that has changed.</param>
113  private void OnLengthValueChanged(DependencyPropertyChangedEventArgs e)
114  {
115  if (!interlock)
116  {
117  interlock = true;
118  var vector = Vector;
119  vector.Normalize();
120  vector *= Length;
121  Vector = vector;
122  SetCurrentValue(XProperty, Vector.X);
123  SetCurrentValue(YProperty, Vector.Y);
124  SetCurrentValue(ZProperty, Vector.Z);
125  interlock = false;
126  }
127 
128  UpdateBinding(e.Property);
129  }
130 
131  /// <summary>
132  /// Updates the binding of the given dependency property.
133  /// </summary>
134  /// <param name="dependencyProperty">The dependency property.</param>
135  private void UpdateBinding(DependencyProperty dependencyProperty)
136  {
137  BindingExpression expression = GetBindingExpression(dependencyProperty);
138  if (expression != null)
139  expression.UpdateSource();
140  }
141 
142  /// <summary>
143  /// Raised by <see cref="VectorProperty"/> when the <see cref="Vector"/> dependency property is modified.
144  /// </summary>
145  /// <param name="sender">The dependency object where the event handler is attached.</param>
146  /// <param name="e">The event data.</param>
147  private static void OnVectorPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
148  {
149  var editor = (Vector3Editor)sender;
150  editor.OnVectorValueChanged();
151  }
152 
153  /// <summary>
154  /// Raised by <see cref="XProperty"/>, <see cref="YProperty"/> or <see cref="ZProperty"/> when respectively
155  /// the <see cref="X"/>, <see cref="Y"/> or <see cref="Z"/> dependency property is modified.
156  /// </summary>
157  /// <param name="sender">The dependency object where the event handler is attached.</param>
158  /// <param name="e">The event data.</param>
159  private static void OnCartesianPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
160  {
161  var editor = (Vector3Editor)sender;
162  editor.OnCartesianValueChanged(e);
163  }
164 
165  /// <summary>
166  /// Raised by <see cref="LengthProperty"/> when the <see cref="Length"/> dependency property is modified.
167  /// </summary>
168  /// <param name="sender">The dependency object where the event handler is attached.</param>
169  /// <param name="e">The event data.</param>
170  private static void OnPolarPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
171  {
172  var editor = (Vector3Editor)sender;
173  editor.OnLengthValueChanged(e);
174  }
175 
176  /// <summary>
177  /// Coerce the value of the Length so it is always positive
178  /// </summary>
179  private static object CoerceLengthValue(DependencyObject sender, object baseValue)
180  {
181  return Math.Max(0.0f, (float)baseValue);
182  }
183  }
184 }
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
SiliconStudio.Core.Mathematics.Vector3 Vector3