Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
UnitSystem.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.Linq;
5 using System.Windows;
6 
7 namespace SiliconStudio.Presentation.Controls
8 {
9  /// <summary>
10  /// Represent an unit system that can be used with a ScaleBar
11  /// </summary>
13  {
14  public static readonly DependencyProperty SymbolProperty = DependencyProperty.Register("Symbol", typeof(string), typeof(UnitSystem));
15  public static readonly DependencyProperty GroupingValuesProperty = DependencyProperty.Register("GroupingValues", typeof(UnitGroupingCollection), typeof(UnitSystem));
16  public static readonly DependencyProperty ConversionsProperty = DependencyProperty.Register("Conversions", typeof(UnitConversionCollection), typeof(UnitSystem));
17 
18  /// <summary>
19  /// The default symbol of the unit that will be appended to numeric value in the ScaleBar
20  /// </summary>
21  public string Symbol { get { return (string)GetValue(SymbolProperty); } set { SetValue(SymbolProperty, value); } }
22  /// <summary>
23  /// A list of <see cref="UnitGrouping" /> that can be used to override the default grouping method (which groups by the closest value of the form {1/2/5} * 10^n)
24  /// </summary>
25  public UnitGroupingCollection GroupingValues { get { return (UnitGroupingCollection)GetValue(GroupingValuesProperty); } set { SetValue(GroupingValuesProperty, value); } }
26  /// <summary>
27  /// A list of <see cref="UnitConversion" /> that can be used for grouping with a different unit (such as nano or mega units).
28  /// </summary>
29  public UnitConversionCollection Conversions { get { return (UnitConversionCollection)GetValue(ConversionsProperty); } set { SetValue(ConversionsProperty, value); } }
30 
31  public UnitSystem()
32  {
33  GroupingValues = new UnitGroupingCollection();
34  Conversions = new UnitConversionCollection();
35  }
36 
37  public void GetAllGroupingValues(ref List<double> values)
38  {
39  GroupingValues.GetAllGroupingValues(ref values, 1.0);
40  Conversions.GetAllGroupingValues(ref values);
41  }
42  }
43 
44  /// <summary>
45  /// Represent an unit conversion for an <see cref="UnitSystem" /> used for grouping large or small values (such as nano or mega units)
46  /// </summary>
48  {
49  public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(UnitConversion));
50  public static readonly DependencyProperty SymbolProperty = DependencyProperty.Register("Symbol", typeof(string), typeof(UnitConversion));
51  public static readonly DependencyProperty GroupingValuesProperty = DependencyProperty.Register("GroupingValues", typeof(UnitGroupingCollection), typeof(UnitConversion));
52  public static readonly DependencyProperty IsMultipliableProperty = DependencyProperty.Register("IsMultipliable", typeof(bool), typeof(UnitConversion));
53 
54  /// <summary>
55  /// The value of this conversion expressed in its parent unit
56  /// </summary>
57  public double Value { get { return (double)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } }
58  /// <summary>
59  /// The symbol of this conversion
60  /// </summary>
61  public string Symbol { get { return (string)GetValue(SymbolProperty); } set { SetValue(SymbolProperty, value); } }
62  /// <summary>
63  /// A list of <see cref="UnitGrouping" /> that can be used to override the default grouping method (which groups by the closest value of the form {1/2/5} * 10^n)
64  /// </summary>
65  public UnitGroupingCollection GroupingValues { get { return (UnitGroupingCollection)GetValue(GroupingValuesProperty); } set { SetValue(GroupingValuesProperty, value); } }
66  /// <summary>
67  /// If the <see cref="GroupingValues" /> list is empty, indicate that the values can be grouped using the default grouping method (which groups by the closest value of the form {1/2/5} * 10^n * <see cref="Value" />)
68  /// </summary>
69  public bool IsMultipliable { get { return (bool)GetValue(IsMultipliableProperty); } set { SetValue(IsMultipliableProperty, value); } }
70 
71  public UnitConversion()
72  {
73  GroupingValues = new UnitGroupingCollection();
74  }
75  }
76 
77  /// <summary>
78  /// Represent an acceptable value for grouping units
79  /// </summary>
81  {
82  public static readonly DependencyProperty LargeIntervalSizeProperty = DependencyProperty.Register("LargeIntervalSize", typeof(double), typeof(UnitGrouping));
83  public static readonly DependencyProperty SmallIntervalCountProperty = DependencyProperty.Register("SmallIntervalCount", typeof(int), typeof(UnitGrouping), new FrameworkPropertyMetadata(10));
84  public static readonly DependencyProperty IsMultipliableProperty = DependencyProperty.Register("IsMultipliable", typeof(bool), typeof(UnitGrouping));
85 
86  /// <summary>
87  /// Grouping value that represent the length of a large tick interval in a ScaleBar
88  /// </summary>
89  public double LargeIntervalSize { get { return (double)GetValue(LargeIntervalSizeProperty); } set { SetValue(LargeIntervalSizeProperty, value); } }
90  /// <summary>
91  /// Number of small intervals (generating small ticks) into a large tick interval of a ScaleBar
92  /// </summary>
93  public int SmallIntervalCount { get { return (int)GetValue(SmallIntervalCountProperty); } set { SetValue(SmallIntervalCountProperty, value); } }
94  /// <summary>
95  /// Indicate that the values can be grouped more efficiently using the default grouping method (which groups by the closest value of the form {1/2/5} * 10^n * <see cref="LargeIntervalSize" />)
96  /// </summary>
97  public bool IsMultipliable { get { return (bool)GetValue(IsMultipliableProperty); } set { SetValue(IsMultipliableProperty, value); } }
98 
99  public UnitGrouping() { }
100 
101  public UnitGrouping(double largeIntervalSize, int smallIntervalCount)
102  {
103  LargeIntervalSize = largeIntervalSize;
104  SmallIntervalCount = smallIntervalCount;
105  }
106  }
107 
108  /// <summary>
109  /// A collection of <see cref="UnitConversion" />. Note that when two multipliable <see cref="UnitConversion" /> conflict (eg. 5mm and 0.5cm), the last one has priority.
110  /// </summary>
111  public class UnitConversionCollection : List<UnitConversion>
112  {
113  public void GetAllGroupingValues(ref List<double> values)
114  {
115  foreach (UnitConversion conversion in this)
116  {
117  conversion.GroupingValues.GetAllGroupingValues(ref values, conversion.Value);
118  }
119  }
120  }
121 
122  /// <summary>
123  /// A collection of <see cref="UnitGrouping" />.Note that when two multipliable <see cref="UnitGrouping" /> conflict, the last one has priority.
124  /// </summary>
125  public class UnitGroupingCollection : List<UnitGrouping>
126  {
127  public void GetAllGroupingValues(ref List<double> values, double multiplier)
128  {
129  foreach (var value in this.Select(grouping => grouping.LargeIntervalSize * multiplier))
130  {
131  if (!values.Contains(value))
132  values.Add(value);
133  }
134  if (Count == 0)
135  {
136  if (!values.Contains(multiplier))
137  values.Add(multiplier);
138  }
139 
140  }
141  }
142 }
UnitGrouping(double largeIntervalSize, int smallIntervalCount)
Definition: UnitSystem.cs:101
void GetAllGroupingValues(ref List< double > values, double multiplier)
Definition: UnitSystem.cs:127
Represent an unit system that can be used with a ScaleBar
Definition: UnitSystem.cs:12
Represent an unit conversion for an UnitSystem used for grouping large or small values (such as nano ...
Definition: UnitSystem.cs:47
A collection of UnitConversion. Note that when two multipliable UnitConversion conflict (eg...
Definition: UnitSystem.cs:111
A collection of UnitGrouping.Note that when two multipliable UnitGrouping conflict, the last one has priority.
Definition: UnitSystem.cs:125
void GetAllGroupingValues(ref List< double > values)
Definition: UnitSystem.cs:37
Represent an acceptable value for grouping units
Definition: UnitSystem.cs:80