Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CompareNum.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.Data;
6 
7 namespace SiliconStudio.Presentation.ValueConverters
8 {
9  /// <summary>
10  /// This converter will compare a given numeric value with a numeric value passed as parameter.
11  /// </summary>
12  public abstract class CompareNum<T> : OneWayValueConverter<T> where T : class, IValueConverter, new()
13  {
14  /// <inheritdoc/>
15  public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
16  {
17  var doubleValue = (double)System.Convert.ChangeType(value, typeof(double));
18  var doubleParameter = (double)System.Convert.ChangeType(parameter, typeof(double));
19  var result = Compare(doubleValue, doubleParameter);
20  return System.Convert.ChangeType(result, value.GetType());
21  }
22 
23  protected abstract bool Compare(double left, double right);
24  }
25 
26  /// <summary>
27  /// This converter will return <c>true</c> if the numeric value is greater than the numeric parameter.
28  /// </summary>
29  public class IsGreater : CompareNum<IsGreater>
30  {
31  protected override bool Compare(double left, double right)
32  {
33  return left > right;
34  }
35  }
36 
37  /// <summary>
38  /// This converter will return <c>true</c> if the numeric value is lower than the numeric parameter.
39  /// </summary>
40  public class IsLower : CompareNum<IsLower>
41  {
42  protected override bool Compare(double left, double right)
43  {
44  return left < right;
45  }
46  }
47 
48  /// <summary>
49  /// This converter will return <c>true</c> if the numeric value is greater than or equal to the numeric parameter.
50  /// </summary>
51  public class IsGreaterOrEqual : CompareNum<IsGreaterOrEqual>
52  {
53  protected override bool Compare(double left, double right)
54  {
55  return left >= right;
56  }
57  }
58 
59  /// <summary>
60  /// This converter will return <c>true</c> if the numeric value is lower than or equal to the numeric parameter.
61  /// </summary>
62  public class IsLowerOrEqual : CompareNum<IsLowerOrEqual>
63  {
64  protected override bool Compare(double left, double right)
65  {
66  return left <= right;
67  }
68  }
69 
70  /// <summary>
71  /// This converter will return <c>true</c> if the numeric value is equal to the numeric parameter.
72  /// </summary>
73  public class IsEqual : CompareNum<IsEqual>
74  {
75  protected override bool Compare(double left, double right)
76  {
77  return Math.Abs(left - right) <= double.Epsilon;
78  }
79  }
80 
81  /// <summary>
82  /// This converter will return <c>true</c> if the numeric value is different from the numeric parameter.
83  /// </summary>
84  public class IsDifferent : CompareNum<IsDifferent>
85  {
86  protected override bool Compare(double left, double right)
87  {
88  return Math.Abs(left - right) > double.Epsilon;
89  }
90  }
91 }
This converter will return true if the numeric value is lower than or equal to the numeric parameter...
Definition: CompareNum.cs:62
This converter will return true if the numeric value is lower than the numeric parameter.
Definition: CompareNum.cs:40
override bool Compare(double left, double right)
Definition: CompareNum.cs:31
override bool Compare(double left, double right)
Definition: CompareNum.cs:64
This converter will return true if the numeric value is greater than the numeric parameter.
Definition: CompareNum.cs:29
This converter will return true if the numeric value is different from the numeric parameter...
Definition: CompareNum.cs:84
This converter will return true if the numeric value is equal to the numeric parameter.
Definition: CompareNum.cs:73
override bool Compare(double left, double right)
Definition: CompareNum.cs:42
override bool Compare(double left, double right)
Definition: CompareNum.cs:86
override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
Definition: CompareNum.cs:15
override bool Compare(double left, double right)
Definition: CompareNum.cs:53
This converter will return true if the numeric value is greater than or equal to the numeric paramete...
Definition: CompareNum.cs:51
override bool Compare(double left, double right)
Definition: CompareNum.cs:75