Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NumericToThickness.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="Thickness"/> structure that can be used for Margin, Padding, etc.
14  /// A <see cref="Thickness"/> must be passed as a parameter of this converter. You can use the <see cref="ThicknessExtension"/>
15  /// markup extension to easily pass one, with the following syntax: {sskk:Thickness (arguments)}. The resulting thickness will
16  /// be the parameter thickness multiplied bu the scalar double value.
17  /// </summary>
18  [ValueConversion(typeof(double), typeof(Thickness))]
19  public class NumericToThickness : ValueConverterBase<NumericToThickness>
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 Thickness))
35  {
36  throw new ArgumentException("The parameter of this converter must be an instance of the Thickness structure. Use {sskk:Thickness (arguments)} to construct one.");
37  }
38 
39  var thickness = (Thickness)parameter;
40  var result = new Thickness(thickness.Left * scalar, thickness.Top * scalar, thickness.Right * scalar, thickness.Bottom * 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 Thickness))
48  {
49  throw new ArgumentException("The value of the ConvertBack method of this converter must be a an instance of the Thickness structure.");
50  }
51  if (!(parameter is Thickness))
52  {
53  throw new ArgumentException("The parameter of the ConvertBack method of this converter must be a an instance of the Thickness structure.");
54  }
55  var thicknessValue = (Thickness)value;
56  var thicknessParameter = (Thickness)parameter;
57 
58  double scalar = 0.0;
59  if (Math.Abs(thicknessParameter.Left) > double.Epsilon)
60  {
61  scalar = thicknessValue.Left / thicknessParameter.Left;
62  }
63  else if (Math.Abs(thicknessParameter.Right) > double.Epsilon)
64  {
65  scalar = thicknessValue.Right / thicknessParameter.Right;
66  }
67  else if (Math.Abs(thicknessParameter.Top) > double.Epsilon)
68  {
69  scalar = thicknessValue.Top / thicknessParameter.Top;
70  }
71  else if (Math.Abs(thicknessParameter.Bottom) > double.Epsilon)
72  {
73  scalar = thicknessValue.Bottom / thicknessParameter.Bottom;
74  }
75 
76  return System.Convert.ChangeType(scalar, targetType);
77  }
78  }
79 }
override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
This converter will convert a double value to a Thickness structure that can be used for Margin...
override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)