Jypeli  9
The simple game programming library
Clamped.cs
Siirry tämän tiedoston dokumentaatioon.
1 #region MIT License
2 /*
3  * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #endregion
23 
24 
25 
26 #if UseDouble
27 using Scalar = System.Double;
28 #else
29 using Scalar = System.Single;
30 #endif
31 using System;
32 using AdvanceMath.Design;
33 using System.Xml.Serialization;
34 
35 namespace AdvanceMath
36 {
40 #if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE && !NETFX_CORE && !NETFX_CORE
41  [Serializable]
42  [System.ComponentModel.TypeConverter(typeof(AdvTypeConverter<Clamped>))]
43 #endif
44  [AdvBrowsableOrder("Min,Value,Max")]
45  public sealed class Clamped :
46 #if !(WINDOWS_PHONE || XBOX || NETFX_CORE)
47  ICloneable,
48 #endif
49  IComparable<Clamped>, IEquatable<Clamped>
50  {
51  [ParseMethod]
52  public static Clamped Parse(string s)
53  {
54  if (s == null)
55  {
56  throw new ArgumentNullException("s");
57  }
58 #if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT
59 
60  string[] vals = s.Split(new char[] { '<', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
61 #else
62  string[] temp = s.Split(new char[] { '<', '(', ')' });
63  int index2 = 0;
64  for (int index1 = 0; index1 < temp.Length; ++index1)
65  {
66  if (temp[index1].Length > 0)
67  {
68  temp[index2++] = temp[index1];
69  }
70  }
71  string[] vals = new string[index2];
72  Array.Copy(temp, vals, vals.Length);
73 #endif
74  if (vals.Length != 3)
75  {
76  throw new FormatException();
77  }
78  return new Clamped(
79  Scalar.Parse(vals[1]),
80  Scalar.Parse(vals[0]),
81  Scalar.Parse(vals[2]));
82  }
83 #if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT
84  public static bool TryParse(string s, out Clamped result)
85  {
86  if (s != null)
87  {
88  string[] vals = s.Split(new char[] { '<', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
89  if (vals.Length == 3)
90  {
91  Scalar min, value, max;
92  if (Scalar.TryParse(vals[0], out min) &&
93  Scalar.TryParse(vals[1], out value) &&
94  Scalar.TryParse(vals[2], out max))
95  {
96  result = new Clamped(value, min, max);
97  return true;
98  }
99  }
100  }
101  result = null;
102  return false;
103  }
104 #endif
105 
112  public Clamped() { }
118  {
119  if (value < 0)
120  {
121  SetValues(value, value, 0);
122  }
123  else
124  {
125  SetValues(value, 0, value);
126  }
127  }
135  {
136  SetValues(value, min, max);
137  }
138  public Clamped(Clamped copy)
139  {
140  this.value = copy.value;
141  this.min = copy.min;
142  this.max = copy.max;
143  }
147  [AdvBrowsable]
148  public Scalar Value
149  {
150  get { return this.value; }
151  set
152  {
153  MathHelper.Clamp(ref value, ref this.min, ref this.max, out this.value);
154  }
155  }
159  [AdvBrowsable]
160  public Scalar Min
161  {
162  get { return min; }
163  set
164  {
165  if (value > max) { throw new ArgumentOutOfRangeException("value"); }
166  min = value;
167  MathHelper.Clamp(ref this.value, ref min, ref max, out this.value);
168  }
169  }
173  [AdvBrowsable]
174  public Scalar Max
175  {
176  get { return max; }
177  set
178  {
179  if (value < min) { throw new ArgumentOutOfRangeException("value"); }
180  max = value;
181  MathHelper.Clamp(ref this.value, ref min, ref max, out this.value);
182  }
183  }
187  [XmlIgnore]
189  {
190  get
191  {
192  return value - min / max - min;
193  }
194  set
195  {
196  if (value >= 1)
197  {
198  this.value = max;
199  }
200  else if (value <= 0)
201  {
202  this.value = min;
203  }
204  else
205  {
206  MathHelper.Lerp(ref min, ref max, ref value, out this.value);
207  }
208  }
209  }
213  public bool IsMax
214  {
215  get { return this.value == max; }
216  }
220  public bool IsMin
221  {
222  get { return this.value == min; }
223  }
227  public void Maximize()
228  {
229  value = max;
230  }
234  public void Minimize()
235  {
236  value = min;
237  }
244  {
245  Scalar newValue = value + this.value;
246  if (newValue > max)
247  {
248  this.value = max;
249  return newValue - max;
250  }
251  else if (newValue < min)
252  {
253  this.value = min;
254  return newValue - min;
255  }
256  else
257  {
258  this.value = newValue;
259  return 0;
260  }
261  }
267  public void Add(ref Scalar value, out Scalar result)
268  {
269  Scalar newValue = value + this.value;
270  if (newValue > max)
271  {
272  this.value = max;
273  result = newValue - max;
274  }
275  else if (newValue < min)
276  {
277  this.value = min;
278  result = newValue - min;
279  }
280  else
281  {
282  this.value = newValue;
283  result = 0;
284  }
285  }
293  {
294  if (min > max) { throw new ArgumentOutOfRangeException("min"); }
295  this.min = min;
296  this.max = max;
297  MathHelper.Clamp(ref value, ref min, ref max, out this.value);
298  }
299 
300  public override string ToString()
301  {
302  return string.Format("({0} < {1} < {2})", min, value, max);
303  }
304  public override int GetHashCode()
305  {
306  return this.value.GetHashCode();
307  }
308  public override bool Equals(object obj)
309  {
310  Clamped other = obj as Clamped;
311  return other != null && Equals(other);
312  }
313  public bool Equals(Clamped other)
314  {
315  return this.value.Equals(other.value);
316  }
317  public int CompareTo(Clamped other)
318  {
319  return this.value.CompareTo(other.value);
320  }
321  public object Clone()
322  {
323  return new Clamped(this);
324  }
325  }
326 }
AdvanceMath.Clamped.value
Scalar value
Definition: Clamped.cs:106
AdvanceMath.Clamped.ToString
override string ToString()
Definition: Clamped.cs:300
AdvanceMath.Clamped.GetHashCode
override int GetHashCode()
Definition: Clamped.cs:304
AdvanceMath.Design.AdvTypeConverter
Definition: AdvTypeConverter'.cs:39
AdvanceMath.Clamped.Clamped
Clamped()
Creates a new Clamped instance all values being zero.
Definition: Clamped.cs:112
AdvanceMath.Clamped.Add
Scalar Add(Scalar value)
Adds a value to the clamped vaule and returns the overflow/underflow.
Definition: Clamped.cs:243
AdvanceMath.Clamped.Maximize
void Maximize()
Sets it to its maximum value;
Definition: Clamped.cs:227
AdvanceMath.Clamped.CompareTo
int CompareTo(Clamped other)
Definition: Clamped.cs:317
AdvanceMath.Clamped.Min
Scalar Min
Gets and Sets the minimum value.
Definition: Clamped.cs:161
AdvanceMath.Clamped.Add
void Add(ref Scalar value, out Scalar result)
Adds a value to the clamped vaule and returns the overflow/underflow.
Definition: Clamped.cs:267
AdvanceMath.Clamped.IsMax
bool IsMax
Gets if the value is at its maximum value;
Definition: Clamped.cs:214
AdvanceMath.MathHelper
Definition: MathHelper.cs:38
AdvanceMath.Clamped.SetValues
void SetValues(Scalar value, Scalar min, Scalar max)
Sets all the values at once.
Definition: Clamped.cs:292
AdvanceMath.MathHelper.Lerp
static Scalar Lerp(Scalar left, Scalar right, Scalar amount)
Definition: MathHelper.cs:56
AdvanceMath.MathHelper.Clamp
static Scalar Clamp(Scalar value, Scalar min, Scalar max)
Definition: MathHelper.cs:111
AdvanceMath.Clamped.IsMin
bool IsMin
Gets if the value is at its minimum value;
Definition: Clamped.cs:221
AdvanceMath.Clamped
A class that keeps a value clamped.
Definition: Clamped.cs:50
AdvanceMath.Clamped.TryParse
static bool TryParse(string s, out Clamped result)
Definition: Clamped.cs:84
AdvanceMath.Clamped.Clamped
Clamped(Scalar value)
Creates a new Clamped instance with zero being either the max or min.
Definition: Clamped.cs:117
AdvanceMath.Clamped.Clamped
Clamped(Clamped copy)
Definition: Clamped.cs:138
AdvanceMath.Clamped.Value
Scalar Value
Gets and Sets the current value.
Definition: Clamped.cs:149
AdvanceMath.Clamped.Clone
object Clone()
Definition: Clamped.cs:321
AdvanceMath.Clamped.Minimize
void Minimize()
Sets it to its minimum value;
Definition: Clamped.cs:234
Scalar
System.Single Scalar
Definition: Clamped.cs:29
AdvanceMath.Clamped.Equals
override bool Equals(object obj)
Definition: Clamped.cs:308
AdvanceMath.Clamped.Percent
Scalar Percent
Gets and Sets the percent with Min being 0 (0%) and Max being 1 (100%)
Definition: Clamped.cs:189
AdvanceMath.Clamped.min
Scalar min
Definition: Clamped.cs:107
AdvanceMath.Clamped.max
Scalar max
Definition: Clamped.cs:108
System
Definition: CFFauxAttributes.cs:29
AdvanceMath.Design
Definition: AdvBrowsableAttribute.cs:29
AdvanceMath.Clamped.Parse
static Clamped Parse(string s)
Definition: Clamped.cs:52
AdvanceMath.Clamped.Equals
bool Equals(Clamped other)
Definition: Clamped.cs:313
AdvanceMath.Clamped.Clamped
Clamped(Scalar value, Scalar min, Scalar max)
Creates a new Clamped instance.
Definition: Clamped.cs:134
AdvanceMath.Clamped.Max
Scalar Max
Gets and Sets the maximum value.
Definition: Clamped.cs:175
AdvanceMath
Definition: Clamped.cs:36