Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NumericToSize.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.Globalization;
5 using System.Windows;
6 using System.Windows.Data;
7 
8 using SiliconStudio.Presentation.MarkupExtensions;
9 
10 namespace SiliconStudio.Presentation.ValueConverters
11 {
12  /// <summary>
13  /// This converter will convert a double value to a <see cref="Size"/> structure.
14  /// A <see cref="Size"/> must be passed as a parameter of this converter. You can use the <see cref="SizeExtension"/>
15  /// markup extension to easily pass one, with the following syntax: {sskk:Size (arguments)}. The resulting size will
16  /// be the parameter size multiplied bu the scalar double value.
17  /// </summary>
18  [ValueConversion(typeof(double), typeof(Size))]
19  public class NumericToSize : ValueConverterBase<NumericToSize>
20  {
21  /// <inheritdoc/>
22  public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
23  {
24  double scalar;
25  try
26  {
27  scalar = System.Convert.ToDouble(value);
28  }
29  catch (Exception exception)
30  {
31  throw new ArgumentException("The value of this converter must be convertible to a double.", exception);
32  }
33 
34  if (!(parameter is Size))
35  {
36  throw new ArgumentException("The parameter of this converter must be an instance of the Size structure. Use {sskk:Size (arguments)} to construct one.");
37  }
38 
39  var size = (Size)parameter;
40  var result = new Size(size.Width * scalar, size.Height * scalar);
41  return result;
42  }
43 
44  /// <inheritdoc/>
45  public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
46  {
47  if (!(value is Size))
48  {
49  throw new ArgumentException("The value of the ConvertBack method of this converter must be a an instance of the Size structure.");
50  }
51  if (!(parameter is Size))
52  {
53  throw new ArgumentException("The parameter of the ConvertBack method of this converter must be a an instance of the Size structure.");
54  }
55  var sizeValue = (Size)value;
56  var sizeParameter = (Size)parameter;
57 
58  double scalar = 0.0;
59  if (Math.Abs(sizeParameter.Width) > double.Epsilon)
60  {
61  scalar = sizeValue.Width / sizeParameter.Width;
62  }
63  else if (Math.Abs(sizeParameter.Height) > double.Epsilon)
64  {
65  scalar = sizeValue.Height / sizeParameter.Height;
66  }
67 
68  return System.Convert.ChangeType(scalar, targetType);
69  }
70  }
71 }
override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
This converter will convert a double value to a Size structure. A Size must be passed as a parameter ...
override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
_In_ size_t _In_ size_t size
Definition: DirectXTexP.h:175