|
| 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 | +} |
0 commit comments