Skip to content

Commit 2547ce4

Browse files
committed
Add DoubleRange
1 parent 192af04 commit 2547ce4

File tree

5 files changed

+214
-1
lines changed

5 files changed

+214
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Zigurous.Architecture.Editor
5+
{
6+
[CustomPropertyDrawer(typeof(DoubleRange))]
7+
public class DoubleRangePropertyDrawer : PropertyDrawer
8+
{
9+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
10+
{
11+
EditorGUI.BeginProperty(position, label, property);
12+
13+
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
14+
15+
int indentLevel = EditorGUI.indentLevel;
16+
EditorGUI.indentLevel = 0;
17+
18+
position = DoubleField(position, property.FindPropertyRelative("m_Min"));
19+
position = DoubleField(position, property.FindPropertyRelative("m_Max"));
20+
21+
EditorGUI.indentLevel = indentLevel;
22+
EditorGUI.EndProperty();
23+
}
24+
25+
private Rect DoubleField(Rect position, SerializedProperty property)
26+
{
27+
Rect field = EditorGUIUtility.GetFieldRect(position, 2);
28+
position.x += field.width + EditorGUIUtility.standardHorizontalSpacing;
29+
30+
EditorGUI.BeginChangeCheck();
31+
32+
double value = EditorGUIUtility.FieldWrapper(property.displayName, (label) => {
33+
return EditorGUI.DoubleField(field, label, property.doubleValue);
34+
});
35+
36+
if (EditorGUI.EndChangeCheck()) {
37+
property.doubleValue = value;
38+
}
39+
40+
return position;
41+
}
42+
43+
}
44+
45+
}

Editor/Structs/DoubleRangePropertyDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Architecture
4+
{
5+
/// <summary>
6+
/// A range of double values.
7+
/// </summary>
8+
[System.Serializable]
9+
public struct DoubleRange
10+
{
11+
/// <summary>
12+
/// Shorthand for writing <c>DoubleRange(0.0, 0.0)</c>.
13+
/// </summary>
14+
public static DoubleRange zero => new DoubleRange(0.0, 0.0);
15+
16+
/// <summary>
17+
/// Shorthand for writing <c>DoubleRange(1.0, 1.0)</c>.
18+
/// </summary>
19+
public static DoubleRange one => new DoubleRange(1.0, 1.0);
20+
21+
/// <summary>
22+
/// Shorthand for writing <c>DoubleRange(0.0, 1.0)</c>.
23+
/// </summary>
24+
public static DoubleRange percent => new DoubleRange(0.0, 1.0);
25+
26+
/// <summary>
27+
/// Shorthand for writing <c>DoubleRange(0.0, double.MaxValue)</c>.
28+
/// </summary>
29+
public static DoubleRange positive => new DoubleRange(0.0, double.MaxValue);
30+
31+
/// <summary>
32+
/// Shorthand for writing <c>DoubleRange(double.MinValue, 0.0)</c>.
33+
/// </summary>
34+
public static DoubleRange negative => new DoubleRange(double.MinValue, 0.0);
35+
36+
/// <summary>
37+
/// Shorthand for writing <c>DoubleRange(double.MinValue, double.MaxValue)</c>.
38+
/// </summary>
39+
public static DoubleRange minMax => new DoubleRange(double.MinValue, double.MaxValue);
40+
41+
[SerializeField]
42+
[Tooltip("The lower bound of the range.")]
43+
private double m_Min;
44+
45+
[SerializeField]
46+
[Tooltip("The upper bound of the range.")]
47+
private double m_Max;
48+
49+
/// <summary>
50+
/// The lower bound of the range.
51+
/// </summary>
52+
public double min
53+
{
54+
get => m_Min;
55+
set => m_Min = value;
56+
}
57+
58+
/// <summary>
59+
/// The upper bound of the range.
60+
/// </summary>
61+
public double max
62+
{
63+
get => m_Max;
64+
set => m_Max = value;
65+
}
66+
67+
/// <summary>
68+
/// The difference between the maximum and minimum values (Read only).
69+
/// </summary>
70+
public double delta => max - min;
71+
72+
/// <summary>
73+
/// The median value of the range (Read only).
74+
/// </summary>
75+
public double median => (min + max) / 2.0;
76+
77+
/// <summary>
78+
/// Creates a new range with the specified values.
79+
/// </summary>
80+
/// <param name="min">The lower bound of the range.</param>
81+
/// <param name="max">The upper bound of the range.</param>
82+
public DoubleRange(double min = 0.0, double max = 1.0)
83+
{
84+
m_Min = min;
85+
m_Max = max;
86+
}
87+
88+
/// <summary>
89+
/// Checks if a value is in the range.
90+
/// </summary>
91+
/// <param name="value">The value to check.</param>
92+
/// <returns>True if the value is in the range, false otherwise.</returns>
93+
public bool Includes(double value)
94+
{
95+
return value >= min && value <= max;
96+
}
97+
98+
/// <summary>
99+
/// Checks if a value is in the range.
100+
/// </summary>
101+
/// <param name="value">The value to check.</param>
102+
/// <param name="includeMin">The minimum value is inclusive if true, exclusive if false.</param>
103+
/// <param name="includeMax">The maximum value is inclusive if true, exclusive if false.</param>
104+
/// <returns>True if the value is in the range, false otherwise.</returns>
105+
public bool Includes(double value, bool includeMin, bool includeMax)
106+
{
107+
return value.IsBetween(min, max, includeMin, includeMax);
108+
}
109+
110+
/// <summary>
111+
/// Clamps a value to the range.
112+
/// </summary>
113+
/// <param name="value">The value to clamp.</param>
114+
/// <returns>The clamped value.</returns>
115+
public double Clamp(double value)
116+
{
117+
return value < min ? min : (value > max ? max : value);
118+
}
119+
120+
/// <summary>
121+
/// Linearly interpolates between the range by <paramref name="t"/>.
122+
/// </summary>
123+
/// <param name="t">The interpolant value between [0..1].</param>
124+
/// <returns>The interpolated value.</returns>
125+
public double Lerp(double t)
126+
{
127+
t = t < 0.0 ? 0.0 : (t > 1.0 ? 1.0 : t);
128+
return min + (max - min) * t;
129+
}
130+
131+
/// <summary>
132+
/// Calculates the linear parameter t that produces the interpolant
133+
/// value within the range.
134+
/// </summary>
135+
/// <param name="value">The value within the range you want to calculate.</param>
136+
/// <returns>The interpolant value between [0..1].</returns>
137+
public double InverseLerp(double value)
138+
{
139+
double t = (value - min) / (max - min);
140+
return t < 0.0 ? 0.0 : (t > 1.0 ? 1.0 : t);
141+
}
142+
143+
}
144+
145+
}

Runtime/DataStructures/Ranges/DoubleRange.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Extensions/ComparableExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public static class ComparableExtensions
1717
/// <param name="includeMin">The minimum value is inclusive if true, exclusive if false.</param>
1818
/// <param name="includeMax">The maximum value is inclusive if true, exclusive if false.</param>
1919
/// <returns>True if the value is between the min and max.</returns>
20-
public static bool IsBetween<T>(this T value, T min, T max, bool includeMin, bool includeMax) where T : IComparable<T>
20+
public static bool IsBetween<T>(this T value, T min, T max, bool includeMin, bool includeMax)
21+
where T : IComparable<T>
2122
{
2223
int minCompare = value.CompareTo(min);
2324
int maxCompare = value.CompareTo(max);

0 commit comments

Comments
 (0)