Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
MatrixEditor.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.Collections.Generic;
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 MatrixEditor : Control
13  {
14  private static readonly Dictionary<DependencyProperty, int> PropertyToIndex;
15  private bool interlock;
16 
17  /// <summary>
18  /// Identifies the <see cref="Matrix"/> dependency property.
19  /// </summary>
20  public static readonly DependencyProperty MatrixProperty = DependencyProperty.Register("Matrix", typeof(Matrix), typeof(MatrixEditor), new FrameworkPropertyMetadata(default(Matrix), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMatrixPropertyChanged, null, false, UpdateSourceTrigger.Explicit));
21 
22  /// <summary>
23  /// Identifies the <see cref="M11"/> dependency property.
24  /// </summary>
25  public static readonly DependencyProperty M11Property = DependencyProperty.Register("M11", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
26 
27  /// <summary>
28  /// Identifies the <see cref="M12"/> dependency property.
29  /// </summary>
30  public static readonly DependencyProperty M12Property = DependencyProperty.Register("M12", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
31 
32  /// <summary>
33  /// Identifies the <see cref="M13"/> dependency property.
34  /// </summary>
35  public static readonly DependencyProperty M13Property = DependencyProperty.Register("M13", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
36 
37  /// <summary>
38  /// Identifies the <see cref="M14"/> dependency property.
39  /// </summary>
40  public static readonly DependencyProperty M14Property = DependencyProperty.Register("M14", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
41 
42  /// <summary>
43  /// Identifies the <see cref="M21"/> dependency property.
44  /// </summary>
45  public static readonly DependencyProperty M21Property = DependencyProperty.Register("M21", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
46 
47  /// <summary>
48  /// Identifies the <see cref="M22"/> dependency property.
49  /// </summary>
50  public static readonly DependencyProperty M22Property = DependencyProperty.Register("M22", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
51 
52  /// <summary>
53  /// Identifies the <see cref="M23"/> dependency property.
54  /// </summary>
55  public static readonly DependencyProperty M23Property = DependencyProperty.Register("M23", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
56 
57  /// <summary>
58  /// Identifies the <see cref="M24"/> dependency property.
59  /// </summary>
60  public static readonly DependencyProperty M24Property = DependencyProperty.Register("M24", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
61 
62  /// <summary>
63  /// Identifies the <see cref="M31"/> dependency property.
64  /// </summary>
65  public static readonly DependencyProperty M31Property = DependencyProperty.Register("M31", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
66 
67  /// <summary>
68  /// Identifies the <see cref="M32"/> dependency property.
69  /// </summary>
70  public static readonly DependencyProperty M32Property = DependencyProperty.Register("M32", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
71 
72  /// <summary>
73  /// Identifies the <see cref="M33"/> dependency property.
74  /// </summary>
75  public static readonly DependencyProperty M33Property = DependencyProperty.Register("M33", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
76 
77  /// <summary>
78  /// Identifies the <see cref="M34"/> dependency property.
79  /// </summary>
80  public static readonly DependencyProperty M34Property = DependencyProperty.Register("M34", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
81 
82  /// <summary>
83  /// Identifies the <see cref="M41"/> dependency property.
84  /// </summary>
85  public static readonly DependencyProperty M41Property = DependencyProperty.Register("M41", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
86 
87  /// <summary>
88  /// Identifies the <see cref="M42"/> dependency property.
89  /// </summary>
90  public static readonly DependencyProperty M42Property = DependencyProperty.Register("M42", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
91 
92  /// <summary>
93  /// Identifies the <see cref="M43"/> dependency property.
94  /// </summary>
95  public static readonly DependencyProperty M43Property = DependencyProperty.Register("M43", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
96 
97  /// <summary>
98  /// Identifies the <see cref="M44"/> dependency property.
99  /// </summary>
100  public static readonly DependencyProperty M44Property = DependencyProperty.Register("M44", typeof(float), typeof(MatrixEditor), new FrameworkPropertyMetadata(.0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnElementValuePropertyChanged));
101 
102  static MatrixEditor()
103  {
104  PropertyToIndex = new Dictionary<DependencyProperty, int> {
105  { M11Property, 0 }, { M12Property, 1 }, { M13Property, 2 }, { M14Property, 3 },
106  { M21Property, 4 }, { M22Property, 5 }, { M23Property, 6 }, { M24Property, 7 },
107  { M31Property, 8 }, { M32Property, 9 }, { M33Property, 10 }, { M34Property, 11 },
108  { M41Property, 12 }, { M42Property, 13 }, { M43Property, 14 }, { M44Property, 15 },
109  };
110  }
111  /// <summary>
112  /// The <see cref="Matrix"/> associated to this control.
113  /// </summary>
114  public Matrix Matrix { get { return (Matrix)GetValue(MatrixProperty); } set { SetValue(MatrixProperty, value); } }
115 
116  /// <summary>
117  /// The value at the first column of the first row of the <see cref="Matrix"/> associated to this control.
118  /// </summary>
119  public float M11 { get { return (float)GetValue(M11Property); } set { SetValue(M11Property, value); } }
120 
121  /// <summary>
122  /// The value at the second column of the first row of the <see cref="Matrix"/> associated to this control.
123  /// </summary>
124  public float M12 { get { return (float)GetValue(M12Property); } set { SetValue(M12Property, value); } }
125 
126  /// <summary>
127  /// The value at the third column of the first row of the <see cref="Matrix"/> associated to this control.
128  /// </summary>
129  public float M13 { get { return (float)GetValue(M13Property); } set { SetValue(M13Property, value); } }
130 
131  /// <summary>
132  /// The value at the fourth column of the first row of the <see cref="Matrix"/> associated to this control.
133  /// </summary>
134  public float M14 { get { return (float)GetValue(M14Property); } set { SetValue(M14Property, value); } }
135 
136  /// <summary>
137  /// The value at the first column of the second row of the <see cref="Matrix"/> associated to this control.
138  /// </summary>
139  public float M21 { get { return (float)GetValue(M21Property); } set { SetValue(M21Property, value); } }
140 
141  /// <summary>
142  /// The value at the second column of the second row of the <see cref="Matrix"/> associated to this control.
143  /// </summary>
144  public float M22 { get { return (float)GetValue(M22Property); } set { SetValue(M22Property, value); } }
145 
146  /// <summary>
147  /// The value at the third column of the second row of the <see cref="Matrix"/> associated to this control.
148  /// </summary>
149  public float M23 { get { return (float)GetValue(M23Property); } set { SetValue(M23Property, value); } }
150 
151  /// <summary>
152  /// The value at the fourth column of the second row of the <see cref="Matrix"/> associated to this control.
153  /// </summary>
154  public float M24 { get { return (float)GetValue(M24Property); } set { SetValue(M24Property, value); } }
155 
156  /// <summary>
157  /// The value at the first column of the third row of the <see cref="Matrix"/> associated to this control.
158  /// </summary>
159  public float M31 { get { return (float)GetValue(M31Property); } set { SetValue(M31Property, value); } }
160 
161  /// <summary>
162  /// The value at the second column of the third row of the <see cref="Matrix"/> associated to this control.
163  /// </summary>
164  public float M32 { get { return (float)GetValue(M32Property); } set { SetValue(M32Property, value); } }
165 
166  /// <summary>
167  /// The value at the third column of the third row of the <see cref="Matrix"/> associated to this control.
168  /// </summary>
169  public float M33 { get { return (float)GetValue(M33Property); } set { SetValue(M33Property, value); } }
170 
171  /// <summary>
172  /// The value at the fourth column of the third row of the <see cref="Matrix"/> associated to this control.
173  /// </summary>
174  public float M34 { get { return (float)GetValue(M34Property); } set { SetValue(M34Property, value); } }
175 
176  /// <summary>
177  /// The value at the first column of the fourth row of the <see cref="Matrix"/> associated to this control.
178  /// </summary>
179  public float M41 { get { return (float)GetValue(M41Property); } set { SetValue(M41Property, value); } }
180 
181  /// <summary>
182  /// The value at the second column of the fourth row of the <see cref="Matrix"/> associated to this control.
183  /// </summary>
184  public float M42 { get { return (float)GetValue(M42Property); } set { SetValue(M42Property, value); } }
185 
186  /// <summary>
187  /// The value at the third column of the fourth row of the <see cref="Matrix"/> associated to this control.
188  /// </summary>
189  public float M43 { get { return (float)GetValue(M43Property); } set { SetValue(M43Property, value); } }
190 
191  /// <summary>
192  /// The value at the fourth column of the fourth row of the <see cref="Matrix"/> associated to this control.
193  /// </summary>
194  public float M44 { get { return (float)GetValue(M44Property); } set { SetValue(M44Property, value); } }
195 
196  /// <summary>
197  /// Raised when the <see cref="Matrix"/> property is modified.
198  /// </summary>
199  private void OnMatrixValueChanged()
200  {
201  if (!interlock)
202  {
203  interlock = true;
204  foreach (var dependencyProperty in PropertyToIndex)
205  {
206  SetCurrentValue(dependencyProperty.Key, Matrix[dependencyProperty.Value]);
207  }
208  interlock = false;
209  }
210 
211  UpdateBinding(MatrixProperty);
212  }
213 
214  /// <summary>
215  /// Raised when the one of the element properties is modified.
216  /// </summary>
217  /// <param name="e">The dependency property that has changed.</param>
218  private void OnElementValueChanged(DependencyPropertyChangedEventArgs e)
219  {
220  if (!interlock)
221  {
222  interlock = true;
223  var array = new float[16];
224  foreach (var dependencyProperty in PropertyToIndex)
225  {
226  array[dependencyProperty.Value] = e.Property == dependencyProperty.Key ? (float)e.NewValue : Matrix[dependencyProperty.Value];
227  }
228 
229  Matrix = new Matrix(array);
230  interlock = false;
231  }
232 
233  UpdateBinding(e.Property);
234  }
235 
236  /// <summary>
237  /// Updates the binding of the given dependency property.
238  /// </summary>
239  /// <param name="dependencyProperty">The dependency property.</param>
240  private void UpdateBinding(DependencyProperty dependencyProperty)
241  {
242  BindingExpression expression = GetBindingExpression(dependencyProperty);
243  if (expression != null)
244  expression.UpdateSource();
245  }
246 
247  /// <summary>
248  /// Raised by <see cref="MatrixProperty"/> when the <see cref="Matrix"/> dependency property is modified.
249  /// </summary>
250  /// <param name="sender">The dependency object where the event handler is attached.</param>
251  /// <param name="e">The event data.</param>
252  private static void OnMatrixPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
253  {
254  var editor = (MatrixEditor)sender;
255  editor.OnMatrixValueChanged();
256  }
257 
258  /// <summary>
259  /// Raised by any element dependency property when it is modified.
260  /// </summary>
261  /// <param name="sender">The dependency object where the event handler is attached.</param>
262  /// <param name="e">The event data.</param>
263  private static void OnElementValuePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
264  {
265  var editor = (MatrixEditor)sender;
266  editor.OnElementValueChanged(e);
267  }
268  }
269 }
Represents a 4x4 mathematical matrix.
Definition: Matrix.cs:47