From cc4f4149747f354e832a89bb806af4f0059ee6f9 Mon Sep 17 00:00:00 2001 From: Ragan Webber Date: Fri, 26 Apr 2024 19:26:22 -0700 Subject: [PATCH] Add deadzone for joypad motion detection (#3) Prior to this change, if there was both a keyboard and joypad connected and the user was using the keyboard, but there was some stick drift with the joypad, input detection would bounce between keyboard and joypad. This change adds a minimum axis_value that an InputEventJoypadMotion must have before being detecting the input type as a joypad. The minimum axis_value is configurable via ProjectSettings. Co-authored-by: John Pennycook --- addons/input_prompts/input_prompt_manager.gd | 10 ++++++++++ addons/input_prompts/plugin.gd | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/addons/input_prompts/input_prompt_manager.gd b/addons/input_prompts/input_prompt_manager.gd index 80580ec..93ee859 100644 --- a/addons/input_prompts/input_prompt_manager.gd +++ b/addons/input_prompts/input_prompt_manager.gd @@ -44,6 +44,12 @@ var preferred_icons := InputPrompt.Icons.AUTOMATIC: icons = value emit_signal("icons_changed") +## The deadzone value used to detect joypad activity. The default value is determined by the +## "addons/input_prompts/joypad_detection_deadzone" setting in [ProjectSettings]. +var joypad_detection_deadzone := ProjectSettings.get_setting( + "addons/input_prompts/joypad_detection_deadzone", 0.5 +) + ## Force all [InputPrompt] nodes to refresh their icons and events. ## Must be called if the [InputMap] is changed. @@ -106,6 +112,10 @@ func _input(event: InputEvent): icons = InputPrompt.Icons.KEYBOARD emit_signal("icons_changed") if event is InputEventJoypadButton or event is InputEventJoypadMotion: + # Do not detect Joypad unless value exceeds deadzone + if event is InputEventJoypadMotion and absf(event.axis_value) < joypad_detection_deadzone: + return + var device = event.device var joy_name = Input.get_joy_name(device) if joy_name.find("Xbox"): diff --git a/addons/input_prompts/plugin.gd b/addons/input_prompts/plugin.gd index 136dd19..7bb02a7 100644 --- a/addons/input_prompts/plugin.gd +++ b/addons/input_prompts/plugin.gd @@ -10,6 +10,20 @@ func _enter_tree(): add_autoload_singleton("PromptManager", "res://addons/input_prompts/input_prompt_manager.gd") add_inspector_plugin(inspector_plugin) + if Engine.is_editor_hint(): + var deadzone_setting := "addons/input_prompts/joypad_detection_deadzone" + if not ProjectSettings.has_setting(deadzone_setting): + ProjectSettings.set_setting(deadzone_setting, 0.5) + ProjectSettings.set_initial_value(deadzone_setting, 0.5) + ProjectSettings.set_as_basic(deadzone_setting, true) + var property_info = { + "name": deadzone_setting, + "type": TYPE_FLOAT, + "hint": PROPERTY_HINT_RANGE, + "hint_string": "0,1" + } + ProjectSettings.save() + func _exit_tree(): remove_inspector_plugin(inspector_plugin)