Skip to content

Commit

Permalink
Add deadzone for joypad motion detection (#3)
Browse files Browse the repository at this point in the history
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 <john.pennycook@gmail.com>
  • Loading branch information
raganw and Pennycook authored Apr 27, 2024
1 parent 3d4bd0f commit cc4f414
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions addons/input_prompts/input_prompt_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"):
Expand Down
14 changes: 14 additions & 0 deletions addons/input_prompts/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cc4f414

Please sign in to comment.