Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AxisDeadzoneProcessor.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using UnityEngine.Scripting;
  3. #if UNITY_EDITOR
  4. using UnityEngine.InputSystem.Editor;
  5. using UnityEngine.UIElements;
  6. #endif
  7. namespace UnityEngine.InputSystem.Processors
  8. {
  9. /// <summary>
  10. /// Clamps values to the range given by <see cref="min"/> and <see cref="max"/> and re-normalizes the resulting
  11. /// value to [0..1].
  12. /// </summary>
  13. /// <remarks>
  14. /// This processor is registered (see <see cref="InputSystem.RegisterProcessor{T}"/>) under the name "AxisDeadzone".
  15. ///
  16. /// It acts like a combination of <see cref="ClampProcessor"/> and <see cref="NormalizeProcessor"/>.
  17. ///
  18. /// <example>
  19. /// <code>
  20. /// </code>
  21. /// // Bind to right trigger on gamepad such that the value is clamped and normalized between
  22. /// // 0.3 and 0.7.
  23. /// new InputAction(binding: "&lt;Gamepad&gt;/rightTrigger", processors: "axisDeadzone(min=0.3,max=0.7)");
  24. /// </example>
  25. /// </remarks>
  26. /// <seealso cref="StickDeadzoneProcessor"/>
  27. public class AxisDeadzoneProcessor : InputProcessor<float>
  28. {
  29. /// <summary>
  30. /// Lower bound (inclusive) below which input values get clamped. Corresponds to 0 in the normalized range.
  31. /// </summary>
  32. /// <remarks>
  33. /// If this is equal to 0 (the default), <see cref="InputSettings.defaultDeadzoneMin"/> is used instead.
  34. /// </remarks>
  35. public float min;
  36. /// <summary>
  37. /// Upper bound (inclusive) beyond which input values get clamped. Corresponds to 1 in the normalized range.
  38. /// </summary>
  39. /// <remarks>
  40. /// If this is equal to 0 (the default), <see cref="InputSettings.defaultDeadzoneMax"/> is used instead.
  41. /// </remarks>
  42. public float max;
  43. private float minOrDefault => min == default ? InputSystem.settings.defaultDeadzoneMin : min;
  44. private float maxOrDefault => max == default ? InputSystem.settings.defaultDeadzoneMax : max;
  45. /// <summary>
  46. /// Normalize <paramref name="value"/> according to <see cref="min"/> and <see cref="max"/>.
  47. /// </summary>
  48. /// <param name="value">Input value.</param>
  49. /// <param name="control">Ignored.</param>
  50. /// <returns>Normalized value.</returns>
  51. public override float Process(float value, InputControl control = null)
  52. {
  53. var min = minOrDefault;
  54. var max = maxOrDefault;
  55. var absValue = Mathf.Abs(value);
  56. if (absValue < min)
  57. return 0;
  58. if (absValue > max)
  59. return Mathf.Sign(value);
  60. return Mathf.Sign(value) * ((absValue - min) / (max - min));
  61. }
  62. /// <inheritdoc/>
  63. public override string ToString()
  64. {
  65. return $"AxisDeadzone(min={minOrDefault},max={maxOrDefault})";
  66. }
  67. }
  68. #if UNITY_EDITOR
  69. internal class AxisDeadzoneProcessorEditor : InputParameterEditor<AxisDeadzoneProcessor>
  70. {
  71. protected override void OnEnable()
  72. {
  73. m_MinSetting.Initialize("Min",
  74. "Value below which input values will be clamped. After clamping, values will be renormalized to [0..1] between min and max.",
  75. "Default Deadzone Min",
  76. () => target.min, v => target.min = v,
  77. () => InputSystem.settings.defaultDeadzoneMin);
  78. m_MaxSetting.Initialize("Max",
  79. "Value above which input values will be clamped. After clamping, values will be renormalized to [0..1] between min and max.",
  80. "Default Deadzone Max",
  81. () => target.max, v => target.max = v,
  82. () => InputSystem.settings.defaultDeadzoneMax);
  83. }
  84. public override void OnGUI()
  85. {
  86. m_MinSetting.OnGUI();
  87. m_MaxSetting.OnGUI();
  88. }
  89. #if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
  90. public override void OnDrawVisualElements(VisualElement root, Action onChangedCallback)
  91. {
  92. m_MinSetting.OnDrawVisualElements(root, onChangedCallback);
  93. m_MaxSetting.OnDrawVisualElements(root, onChangedCallback);
  94. }
  95. #endif
  96. private CustomOrDefaultSetting m_MinSetting;
  97. private CustomOrDefaultSetting m_MaxSetting;
  98. }
  99. #endif
  100. }