Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
StripDefinition.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.Collections.Generic;
5 
6 using SiliconStudio.Core.Mathematics;
7 
8 namespace SiliconStudio.Paradox.UI
9 {
10  /// <summary>
11  /// Represent the definition of a grid strip.
12  /// </summary>
13  public class StripDefinition
14  {
15  private float maximumSize;
16  private float minimumSize;
17  private StripType type;
18  private float sizeValue;
19 
20  /// <summary>
21  /// The actual size of the strip in virtual pixels.
22  /// </summary>
23  public float ActualSize { get; internal set; }
24 
25  /// <summary>
26  /// Creates a 1-Star sized strip definition.
27  /// </summary>
28  public StripDefinition()
29  : this(StripType.Star, 1)
30  {
31  }
32 
33  /// <summary>
34  /// Creates a <see cref="StripDefinition"/> with the provided size and type.
35  /// </summary>
36  /// <param name="type">The type of the strip to create</param>
37  /// <param name="sizeValue">The value of the strip to create</param>
38  public StripDefinition(StripType type, float sizeValue = 1)
39  {
40  Type = type;
41  SizeValue = sizeValue;
42  MaximumSize = float.PositiveInfinity;
43  MinimumSize = 0;
44  }
45 
46  /// <summary>
47  /// The maximum size of the strip in virtual pixels.
48  /// </summary>
49  /// <exception cref="ArgumentOutOfRangeException">The provided value is negative.</exception>
50  /// <exception cref="InvalidOperationException">The provided value is smaller than <see cref="MinimumSize"/></exception>
51  public float MaximumSize
52  {
53  get { return maximumSize; }
54  set
55  {
56  if(value < 0)
57  throw new ArgumentOutOfRangeException("value");
58 
59  if(value < MinimumSize)
60  throw new InvalidOperationException("The provided maximum value is smaller than the current minimum value");
61 
62  maximumSize = value;
63 
64  if (DefinitionChanged != null)
65  DefinitionChanged(this, EventArgs.Empty);
66  }
67  }
68 
69  /// <summary>
70  /// The minimum size of the strip in virtual pixels.
71  /// </summary>
72  /// <exception cref="ArgumentOutOfRangeException">The provided value is negative or infinity.</exception>
73  /// <exception cref="InvalidOperationException">The provided value is bigger than <see cref="MaximumSize"/></exception>
74  public float MinimumSize
75  {
76  get { return minimumSize; }
77  set
78  {
79  if (value < 0 || float.IsPositiveInfinity(value))
80  throw new ArgumentOutOfRangeException("value");
81 
82  if (value > MaximumSize)
83  throw new InvalidOperationException("The provided minimum value is bigger than the current maximum value");
84 
85  minimumSize = value;
86 
87  if (DefinitionChanged != null)
88  DefinitionChanged(this, EventArgs.Empty);
89  }
90  }
91 
92  /// <summary>
93  /// Gets or sets the type of the strip.
94  /// </summary>
95  public StripType Type
96  {
97  get { return type; }
98  set
99  {
100  if(type == value)
101  return;
102 
103  type = value;
104 
105  var handler = DefinitionChanged;
106  if (handler != null)
107  handler(this, EventArgs.Empty);
108  }
109  }
110 
111  /// <summary>
112  /// Gets or sets the size value of the strip.
113  /// Note that the value is interpreted differently depending on the strip <see cref="Type"/>.
114  /// </summary>
115  /// <exception cref="ArgumentOutOfRangeException">The size must be finite positive value.</exception>
116  public float SizeValue
117  {
118  get { return sizeValue; }
119  set
120  {
121  if (value < 0 || float.IsPositiveInfinity(value))
122  throw new ArgumentOutOfRangeException("value");
123 
124  sizeValue = value;
125 
126  var handler = DefinitionChanged;
127  if (handler!= null)
128  handler(this, EventArgs.Empty);
129  }
130  }
131 
132  /// <summary>
133  /// Clamp the provided size by the definition's minimum and maximum values.
134  /// </summary>
135  /// <param name="desiredSize">The size to clamp</param>
136  /// <returns>The size clamped by the minimum and maximum values of the strip definition</returns>
137  public float ClampSizeByMinimumMaximum(float desiredSize)
138  {
139  return Math.Min(MaximumSize, Math.Max(MinimumSize, desiredSize));
140  }
141 
142  internal float ValueRelativeMinimum()
143  {
144  if (sizeValue < MathUtil.ZeroTolerance)
145  return 0;
146 
147  return MinimumSize / SizeValue;
148  }
149 
150  internal float ValueRelativeMaximum()
151  {
152  if (sizeValue < MathUtil.ZeroTolerance)
153  return 0;
154 
155  return MaximumSize / SizeValue;
156  }
157 
158  internal class SortByIncreasingStarRelativeMinimumValues : IComparer<StripDefinition>
159  {
160  public int Compare(StripDefinition def1, StripDefinition def2)
161  {
162  var val1 = def1.ValueRelativeMinimum();
163  var val2 = def2.ValueRelativeMinimum();
164 
165  return val1.CompareTo(val2);
166  }
167  }
168 
169  internal class SortByIncreasingStarRelativeMaximumValues : IComparer<StripDefinition>
170  {
171  public int Compare(StripDefinition def1, StripDefinition def2)
172  {
173  var val1 = def1.ValueRelativeMaximum();
174  var val2 = def2.ValueRelativeMaximum();
175 
176  return val1.CompareTo(val2);
177  }
178  }
179 
180  internal event EventHandler<EventArgs> DefinitionChanged;
181  }
182 }
StripType
The different types of strip possible of a grid.
Definition: StripType.cs:8
Represent the definition of a grid strip.
const float ZeroTolerance
The value for which all absolute numbers smaller than are considered equal to zero.
Definition: MathUtil.cs:38
StripDefinition(StripType type, float sizeValue=1)
Creates a StripDefinition with the provided size and type.
StripDefinition()
Creates a 1-Star sized strip definition.
A strip that occupies the maximum available size, dispatched among the other stared-size columns...
float ClampSizeByMinimumMaximum(float desiredSize)
Clamp the provided size by the definition's minimum and maximum values.