A game about forced loneliness, made by TACStudios
1using UnityEngine.Scripting; 2 3////REVIEW: handle values dropping below min and above max? 4 5namespace UnityEngine.InputSystem.Processors 6{ 7 /// <summary> 8 /// Normalizes input values in the range <see cref="min"/> and <see cref="max"/> to 9 /// unsigned normalized form [0..1] if <see cref="zero"/> is placed at (or below) <see cref="min"/> 10 /// or to signed normalized form [-1..1] if <see cref="zero"/> is placed in-between 11 /// <see cref="min"/> and <see cref="max"/>. 12 /// </summary> 13 /// <remarks> 14 /// This processor is registered (see <see cref="InputSystem.RegisterProcessor{T}"/>) under the name "normalize". 15 /// 16 /// Note that this processor does not clamp the incoming value to <see cref="min"/> and <see cref="max"/>. 17 /// To achieve this, either add a <see cref="ClampProcessor"/> or use <see cref="AxisDeadzoneProcessor"/> 18 /// which combines clamping and normalizing. 19 /// 20 /// <example> 21 /// <code> 22 /// </code> 23 /// // Bind to right trigger on gamepad such that the value values below 0.3 and above 0.7 get normalized 24 /// // to values between [0..1]. 25 /// new InputAction(binding: "&lt;Gamepad&gt;/rightTrigger", processors: "normalize(min=0.3,max=0.7)"); 26 /// </example> 27 /// </remarks> 28 /// <seealso cref="NormalizeVector2Processor"/> 29 /// <seealso cref="NormalizeVector3Processor"/> 30 public class NormalizeProcessor : InputProcessor<float> 31 { 32 /// <summary> 33 /// Input value (inclusive) that corresponds to 0 or -1 (depending on <see cref="zero"/>), the lower bound. 34 /// </summary> 35 /// <remarks> 36 /// If the input value drops below min, the result is undefined. 37 /// </remarks> 38 public float min; 39 40 /// <summary> 41 /// Input value (inclusive) that corresponds to 1, the upper bound. 42 /// </summary> 43 /// <remarks> 44 /// If the input value goes beyond max, the result is undefined. 45 /// </remarks> 46 public float max; 47 48 /// <summary> 49 /// Input value that corresponds to 0. If this is placed at or below <see cref="min"/>, the resulting normalization 50 /// returns a [0..1] value. If this is placed in-between <see cref="min"/> and <see cref="max"/>, the resulting 51 /// normalization returns a [-1..1] value. 52 /// </summary> 53 public float zero; 54 55 /// <summary> 56 /// Normalize <paramref name="value"/> with respect to <see cref="min"/> and <see cref="max"/>. 57 /// </summary> 58 /// <param name="value">Input value.</param> 59 /// <param name="control">Ignored.</param> 60 /// <returns>Normalized value.</returns> 61 public override float Process(float value, InputControl control) 62 { 63 return Normalize(value, min, max, zero); 64 } 65 66 /// <summary> 67 /// Normalize <paramref name="value"/> with respect to <paramref name="min"/> and <paramref name="max"/>. 68 /// </summary> 69 /// <param name="value">Input value.</param> 70 /// <param name="min">Lower bound. See <see cref="min"/>.</param> 71 /// <param name="max">Upper bound. See <see cref="max"/>.</param> 72 /// <param name="zero">Zero point. See <see cref="zero"/>.</param> 73 /// <returns>Normalized value.</returns> 74 /// <remarks> 75 /// This method performs the same function as <see cref="Process"/>. 76 /// <example> 77 /// <code> 78 /// // Normalize 2 against a [1..5] range. Returns 0.25. 79 /// NormalizeProcessor.Normalize(2, 1, 5, 1) 80 /// </code> 81 /// </example> 82 /// </remarks> 83 public static float Normalize(float value, float min, float max, float zero) 84 { 85 if (zero < min) 86 zero = min; 87 // Prevent NaN/Inf from dividing 0 by something. 88 if (Mathf.Approximately(value, min)) 89 { 90 if (min < zero) 91 return -1f; 92 return 0f; 93 } 94 var percentage = (value - min) / (max - min); 95 if (min < zero) 96 return 2 * percentage - 1; 97 return percentage; 98 } 99 100 internal static float Denormalize(float value, float min, float max, float zero) 101 { 102 if (zero < min) 103 zero = min; 104 105 if (min < zero) 106 { 107 if (value < 0) 108 return min + (zero - min) * (value * -1f); 109 return zero + (max - zero) * value; 110 } 111 112 return min + (max - min) * value; 113 } 114 115 /// <inheritdoc/> 116 public override string ToString() 117 { 118 return $"Normalize(min={min},max={max},zero={zero})"; 119 } 120 } 121}