using System;
using UnityEngine.Scripting;
#if UNITY_EDITOR
using UnityEngine.InputSystem.Editor;
using UnityEngine.UIElements;
#endif
namespace UnityEngine.InputSystem.Processors
{
///
/// Clamps values to the range given by and and re-normalizes the resulting
/// value to [0..1].
///
///
/// This processor is registered (see ) under the name "AxisDeadzone".
///
/// It acts like a combination of and .
///
///
///
///
/// // Bind to right trigger on gamepad such that the value is clamped and normalized between
/// // 0.3 and 0.7.
/// new InputAction(binding: "<Gamepad>/rightTrigger", processors: "axisDeadzone(min=0.3,max=0.7)");
///
///
///
public class AxisDeadzoneProcessor : InputProcessor
{
///
/// Lower bound (inclusive) below which input values get clamped. Corresponds to 0 in the normalized range.
///
///
/// If this is equal to 0 (the default), is used instead.
///
public float min;
///
/// Upper bound (inclusive) beyond which input values get clamped. Corresponds to 1 in the normalized range.
///
///
/// If this is equal to 0 (the default), is used instead.
///
public float max;
private float minOrDefault => min == default ? InputSystem.settings.defaultDeadzoneMin : min;
private float maxOrDefault => max == default ? InputSystem.settings.defaultDeadzoneMax : max;
///
/// Normalize according to and .
///
/// Input value.
/// Ignored.
/// Normalized value.
public override float Process(float value, InputControl control = null)
{
var min = minOrDefault;
var max = maxOrDefault;
var absValue = Mathf.Abs(value);
if (absValue < min)
return 0;
if (absValue > max)
return Mathf.Sign(value);
return Mathf.Sign(value) * ((absValue - min) / (max - min));
}
///
public override string ToString()
{
return $"AxisDeadzone(min={minOrDefault},max={maxOrDefault})";
}
}
#if UNITY_EDITOR
internal class AxisDeadzoneProcessorEditor : InputParameterEditor
{
protected override void OnEnable()
{
m_MinSetting.Initialize("Min",
"Value below which input values will be clamped. After clamping, values will be renormalized to [0..1] between min and max.",
"Default Deadzone Min",
() => target.min, v => target.min = v,
() => InputSystem.settings.defaultDeadzoneMin);
m_MaxSetting.Initialize("Max",
"Value above which input values will be clamped. After clamping, values will be renormalized to [0..1] between min and max.",
"Default Deadzone Max",
() => target.max, v => target.max = v,
() => InputSystem.settings.defaultDeadzoneMax);
}
public override void OnGUI()
{
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
if (!InputSystem.settings.IsFeatureEnabled(InputFeatureNames.kUseIMGUIEditorForAssets)) return;
#endif
m_MinSetting.OnGUI();
m_MaxSetting.OnGUI();
}
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
public override void OnDrawVisualElements(VisualElement root, Action onChangedCallback)
{
m_MinSetting.OnDrawVisualElements(root, onChangedCallback);
m_MaxSetting.OnDrawVisualElements(root, onChangedCallback);
}
#endif
private CustomOrDefaultSetting m_MinSetting;
private CustomOrDefaultSetting m_MaxSetting;
}
#endif
}