From 90b14ed0973e1e39f831d63b987d1978faea8de9 Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 17 Oct 2024 04:58:47 -0600 Subject: [PATCH] Try to prevent inputs from getting stuck active when a device disconnects --- Assets/Script/Input/Bindings/AxisBinding.cs | 7 +++++++ Assets/Script/Input/Bindings/ButtonBinding.cs | 8 ++++++++ Assets/Script/Input/Bindings/ControlBinding.cs | 12 ++++++++++++ 3 files changed, 27 insertions(+) diff --git a/Assets/Script/Input/Bindings/AxisBinding.cs b/Assets/Script/Input/Bindings/AxisBinding.cs index 013a43450..49cd6331b 100644 --- a/Assets/Script/Input/Bindings/AxisBinding.cs +++ b/Assets/Script/Input/Bindings/AxisBinding.cs @@ -164,6 +164,13 @@ public override void UpdateState(double time) InvokeStateChanged(State); } + public override void ResetState() + { + RawState = default; + State = default; + InvokeStateChanged(State); + } + private float CalculateState(float rawValue) { float max; diff --git a/Assets/Script/Input/Bindings/ButtonBinding.cs b/Assets/Script/Input/Bindings/ButtonBinding.cs index ceb542689..70d2c8203 100644 --- a/Assets/Script/Input/Bindings/ButtonBinding.cs +++ b/Assets/Script/Input/Bindings/ButtonBinding.cs @@ -164,6 +164,14 @@ public override void UpdateState(double time) InvokeStateChanged(State); } + public override void ResetState() + { + PreviousState = default; + State = default; + _debounceTimer.Stop(); + InvokeStateChanged(State); + } + private float CalculateState(float rawValue) { return rawValue * _invertSign; diff --git a/Assets/Script/Input/Bindings/ControlBinding.cs b/Assets/Script/Input/Bindings/ControlBinding.cs index d63846244..d85aea868 100644 --- a/Assets/Script/Input/Bindings/ControlBinding.cs +++ b/Assets/Script/Input/Bindings/ControlBinding.cs @@ -193,6 +193,12 @@ public virtual void UpdateState(double time) InvokeStateChanged(State); } + public virtual void ResetState() + { + State = default; + InvokeStateChanged(State); + } + protected void InvokeStateChanged(TState state) { StateChanged?.Invoke(state); @@ -434,6 +440,12 @@ private bool RemoveBindings(Func selector) if (selector(binding)) { removed = true; + + // Reset binding state to prevent phantom inputs + binding.ResetState(); + OnStateChanged(binding, InputManager.CurrentInputTime); + FireStateChanged(); + _bindings.RemoveAt(i); FireBindingRemoved(binding.Control); i--;