diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-04 20:09:04 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-04 20:09:04 +0200 |
commit | 2a8ef1ed01cf91698008717fa7338e1e3c35de0f (patch) | |
tree | 4a305caba64047ccd272d146733144ad26aa2fb3 | |
parent | efe4ba0ca528218d8766be3d54e354eb599cfc67 (diff) | |
download | hurrycurry-2a8ef1ed01cf91698008717fa7338e1e3c35de0f.tar hurrycurry-2a8ef1ed01cf91698008717fa7338e1e3c35de0f.tar.bz2 hurrycurry-2a8ef1ed01cf91698008717fa7338e1e3c35de0f.tar.zst |
Implement workaround for misdetected joypads
-rw-r--r-- | client/disable_wrong_joypads.gd | 45 | ||||
-rw-r--r-- | client/project.godot | 1 |
2 files changed, 46 insertions, 0 deletions
diff --git a/client/disable_wrong_joypads.gd b/client/disable_wrong_joypads.gd new file mode 100644 index 00000000..393a2bf6 --- /dev/null +++ b/client/disable_wrong_joypads.gd @@ -0,0 +1,45 @@ +# Adapted from https://gist.github.com/geekley/5e6daf62d2e86b566c4886f20b604d21 +# which is unlicense lincensed +extends Node + +func _ready() -> void: + Input.joy_connection_changed.connect(joy_check) + +static var banned_words: PackedStringArray = ['touchpad', 'trackpad', 'clickpad', 'mouse', 'pen', 'finger', 'led'] + +static func joy_check(device: int, connected: bool) -> void: + if not connected: + return + var device_name: String = Input.get_joy_name(device) + if not is_banned(device_name): + return + var guid: String = Input.get_joy_guid(device) + var mapping: String = guid + ',' + device_name.replace(',', '') + Input.add_joy_mapping(mapping, true) + for axis: JoyAxis in range(JOY_AXIS_SDL_MAX) as Array[JoyAxis]: + var event: InputEventJoypadMotion = InputEventJoypadMotion.new() + event.device = device + event.axis = axis + Input.parse_input_event(event) + for button_index: JoyButton in range(JOY_BUTTON_SDL_MAX) as Array[JoyButton]: + var event: InputEventJoypadButton = InputEventJoypadButton.new() + event.device = device + event.button_index = button_index + Input.parse_input_event(event) + prints('Ignoring joypad device:', mapping) + +static func is_banned(device_name: String) -> bool: + for word: String in banned_words: + var i: int = device_name.findn(word) + if i < 0: + continue + if i > 0 and not is_ascii_non_letter(device_name[i - 1]): + continue + var j: int = i + word.length() + if j < device_name.length() and not is_ascii_non_letter(device_name[j]): + continue + return true + return false + +static func is_ascii_non_letter(c: String) -> bool: + return c.unicode_at(0) < 128 and not (c >= 'a' and c <= 'z' or c >= 'A' and c <= 'Z') diff --git a/client/project.godot b/client/project.godot index 754c7e21..3638f83e 100644 --- a/client/project.godot +++ b/client/project.godot @@ -23,6 +23,7 @@ config/icon="res://icon.png" Global="*res://global.gd" Server="*res://server.gd" Sound="*res://audio/sound.tscn" +DisableWrongJoypads="*res://disable_wrong_joypads.gd" [display] |