From c2fd36498b659e947b22be2ba0cd900487b85f1c Mon Sep 17 00:00:00 2001 From: pcbeard Date: Fri, 29 Dec 2023 13:26:49 -0800 Subject: [PATCH] Update to input_helper v4.3.1 This removes get_joypad_motions_for_action(), instead get_joypad_inputs_for_action() returns both InputEventJoypadButton and InputEventJoypadMotion events. Change update_input_icon() to handle both InputEventJoypadButton and InputEventJoypadButton result types. --- COGITO/Components/DynamicInputIcon.gd | 9 +-- addons/input_helper/InputHelper.cs | 12 +-- addons/input_helper/input_helper.gd | 101 +++++++++++++++----------- addons/input_helper/plugin.cfg | 2 +- 4 files changed, 68 insertions(+), 56 deletions(-) diff --git a/COGITO/Components/DynamicInputIcon.gd b/COGITO/Components/DynamicInputIcon.gd index 565416c8..c0e9ef91 100644 --- a/COGITO/Components/DynamicInputIcon.gd +++ b/COGITO/Components/DynamicInputIcon.gd @@ -50,18 +50,17 @@ func update_input_icon(): else: var joypad_input = InputHelper.get_joypad_input_for_action(action_name) - if joypad_input: + if joypad_input is InputEventJoypadButton: #print("DynamicInputIcon: Action=", action_name, ". Joypad btn=", joypad_input.button_index) set_texture(gamepad_icons) frame = joypad_input.button_index - var joypad_motion = InputHelper.get_joypad_motion_for_action(action_name) - if joypad_motion: + elif joypad_input is InputEventJoypadMotion: #print("DynamicInputIcon: Action=", action_name, ". Joypad motion=", joypad_motion.axis) set_texture(gamepad_icons) - if joypad_motion.axis == 5: + if joypad_input.axis == 5: frame = 18 #Sets icon to RT - if joypad_motion.axis == 4: + if joypad_input.axis == 4: frame = 17 #Sets icon to LT diff --git a/addons/input_helper/InputHelper.cs b/addons/input_helper/InputHelper.cs index 5243a531..d72e97e2 100644 --- a/addons/input_helper/InputHelper.cs +++ b/addons/input_helper/InputHelper.cs @@ -130,31 +130,31 @@ public static void ReplaceKeyboardInputAtIndex(string action, int index, InputEv #region Joypad - public static Array GetJoypadInputsForAction(string action) + public static Array GetJoypadInputsForAction(string action) { - return (Array)Instance.Call("get_joypad_inputs_for_action", action); + return (Array)Instance.Call("get_joypad_inputs_for_action", action); } public static InputEvent GetJoypadInputForAction(string action) { - return (InputEventJoypadButton)Instance.Call("get_joypad_input_for_action", action); + return (InputEvent)Instance.Call("get_joypad_input_for_action", action); } - public static void SetJoypadInputForAction(string action, InputEventJoypadButton input, bool swapIfTaken = true) + public static void SetJoypadInputForAction(string action, InputEvent input, bool swapIfTaken = true) { Instance.Call("set_joypad_input_for_action", action, input, swapIfTaken); } - public static void ReplaceJoypadInputForAction(string action, InputEventJoypadButton currentInput, InputEventJoypadButton input, bool swapIfTaken = true) + public static void ReplaceJoypadInputForAction(string action, InputEvent currentInput, InputEvent input, bool swapIfTaken = true) { Instance.Call("replace_joypad_input_for_action", action, currentInput, input, swapIfTaken); } - public static void ReplaceJoypadInputAtIndex(string action, int index, InputEventJoypadButton input, bool swapIfTaken = true) + public static void ReplaceJoypadInputAtIndex(string action, int index, InputEvent input, bool swapIfTaken = true) { Instance.Call("replace_joypad_input_at_index", action, index, input, swapIfTaken); } diff --git a/addons/input_helper/input_helper.gd b/addons/input_helper/input_helper.gd index ae3b3cb9..83f4fa12 100644 --- a/addons/input_helper/input_helper.gd +++ b/addons/input_helper/input_helper.gd @@ -33,7 +33,7 @@ func _input(event: InputEvent) -> void: var next_device_index: int = device_index # Did we just press a key on the keyboard? - if event is InputEventKey: + if event is InputEventKey or event is InputEventMouse: next_device = DEVICE_KEYBOARD next_device_index = -1 @@ -116,7 +116,7 @@ func get_keyboard_or_joypad_input_for_action(action: String) -> InputEvent: if device == DEVICE_KEYBOARD: return get_keyboard_input_for_action(action) else: - return get_joypad_input_for_action(action) as InputEvent + return get_joypad_input_for_action(action) ## Get the key or button for a given action depending on the current device @@ -124,10 +124,7 @@ func get_keyboard_or_joypad_inputs_for_action(action: String) -> Array[InputEven if device == DEVICE_KEYBOARD: return get_keyboard_inputs_for_action(action) else: - var inputs: Array[InputEvent] = [] - for input in get_joypad_inputs_for_action(action): - inputs.append(input as InputEvent) - return inputs + return get_joypad_inputs_for_action(action) ## Get a text label for a given input @@ -155,9 +152,10 @@ func get_label_for_input(input: InputEvent) -> String: return input.as_text() -## Serialize a list of action inputs to string +## Serialize a list of action inputs to string. If actions is empty then it will serialize +## all actions. func serialize_inputs_for_actions(actions: PackedStringArray = []) -> String: - if actions == null or actions.size() == 0: + if actions == null or actions.is_empty(): actions = InputMap.get_actions() var map: Dictionary = {} @@ -170,14 +168,25 @@ func serialize_inputs_for_actions(actions: PackedStringArray = []) -> String: } for input in inputs: if input is InputEventKey: - var s: String = OS.get_keycode_string(input.keycode) - if s == "": - s = OS.get_keycode_string(input.physical_keycode) + var s: String = get_label_for_input(input) + var modifiers: Array[String] = [] + if input.alt_pressed: + modifiers.append("alt") + if input.shift_pressed: + modifiers.append("shift") + if input.ctrl_pressed: + modifiers.append("ctrl") + if input.meta_pressed: + modifiers.append("meta") + if not modifiers.is_empty(): + s += "|" + ",".join(modifiers) action_map["keyboard"].append(s) elif input is InputEventMouseButton: action_map["mouse"].append(input.button_index) elif input is InputEventJoypadButton: action_map["joypad"].append(input.button_index) + elif input is InputEventJoypadMotion: + action_map["joypad"].append("%d|%d" % [input.axis, input.axis_value]) map[action] = action_map @@ -193,7 +202,20 @@ func deserialize_inputs_for_actions(string: String) -> void: for key in map[action]["keyboard"]: var keyboard_input = InputEventKey.new() - keyboard_input.keycode = OS.find_keycode_from_string(key) + if "|" in key: + var bits = key.split("|") + keyboard_input.keycode = OS.find_keycode_from_string(bits[0]) + bits = bits[1].split(",") + if bits.has("alt"): + keyboard_input.alt_pressed = true + if bits.has("shift"): + keyboard_input.shift_pressed = true + if bits.has("ctrl"): + keyboard_input.ctrl_pressed = true + if bits.has("meta"): + keyboard_input.meta_pressed = true + else: + keyboard_input.keycode = OS.find_keycode_from_string(key) InputMap.action_add_event(action, keyboard_input) for button_index in map[action]["mouse"]: @@ -201,10 +223,17 @@ func deserialize_inputs_for_actions(string: String) -> void: mouse_input.button_index = int(button_index) InputMap.action_add_event(action, mouse_input) - for button_index in map[action]["joypad"]: - var joypad_input = InputEventJoypadButton.new() - joypad_input.button_index = int(button_index) - InputMap.action_add_event(action, joypad_input) + for button_index_or_motion in map[action]["joypad"]: + if "|" in str(button_index_or_motion): + var joypad_motion_input = InputEventJoypadMotion.new() + var bits = button_index_or_motion.split("|") + joypad_motion_input.axis = int(bits[0]) + joypad_motion_input.axis_value = float(bits[1]) + InputMap.action_add_event(action, joypad_motion_input) + else: + var joypad_input = InputEventJoypadButton.new() + joypad_input.button_index = int(button_index_or_motion) + InputMap.action_add_event(action, joypad_input) ### Keyboard/mouse input @@ -253,55 +282,39 @@ func _update_keyboard_input_for_action(action: String, input: InputEvent, swap_i ## Get all buttons used for an action -func get_joypad_inputs_for_action(action: String) -> Array[InputEventJoypadButton]: - var inputs: Array[InputEventJoypadButton] - for event in InputMap.action_get_events(action): - if event is InputEventJoypadButton: - inputs.append(event) - return inputs +func get_joypad_inputs_for_action(action: String) -> Array[InputEvent]: + return InputMap.action_get_events(action).filter(func(event): + return event is InputEventJoypadButton or event is InputEventJoypadMotion + ) ## Get the first button for an action -func get_joypad_input_for_action(action: String) -> InputEventJoypadButton: - var buttons: Array[InputEventJoypadButton] = get_joypad_inputs_for_action(action) +func get_joypad_input_for_action(action: String) -> InputEvent: + var buttons: Array[InputEvent] = get_joypad_inputs_for_action(action) return null if buttons.is_empty() else buttons[0] -## Get the first motion for an action -func get_joypad_motion_for_action(action: String) -> InputEventJoypadMotion: - var buttons: Array[InputEventJoypadMotion] = get_joypad_motions_for_action(action) - return null if buttons.is_empty() else buttons[0] - -## Get all motions used for an action -func get_joypad_motions_for_action(action: String) -> Array[InputEventJoypadMotion]: - var inputs: Array[InputEventJoypadMotion] - for event in InputMap.action_get_events(action): - if event is InputEventJoypadMotion: - inputs.append(event) - return inputs - - ## Set the button for an action -func set_joypad_input_for_action(action: String, input: InputEventJoypadButton, swap_if_taken: bool = true) -> Error: +func set_joypad_input_for_action(action: String, input: InputEvent, swap_if_taken: bool = true) -> Error: return _update_joypad_input_for_action(action, input, swap_if_taken, null) ## Replace a specific button for an action -func replace_joypad_input_for_action(action: String, current_input: InputEventJoypadButton, input: InputEventJoypadButton, swap_if_taken: bool = true) -> Error: +func replace_joypad_input_for_action(action: String, current_input: InputEvent, input: InputEventJoypadButton, swap_if_taken: bool = true) -> Error: return _update_joypad_input_for_action(action, input, swap_if_taken, current_input) ## Replace a button, given its index -func replace_joypad_input_at_index(action: String, index: int, input: InputEventJoypadButton, swap_if_taken: bool = true) -> Error: - var inputs: Array[InputEventJoypadButton] = get_joypad_inputs_for_action(action) +func replace_joypad_input_at_index(action: String, index: int, input: InputEvent, swap_if_taken: bool = true) -> Error: + var inputs: Array[InputEvent] = get_joypad_inputs_for_action(action) var replacing_input = InputEventJoypadButton.new() if (inputs.is_empty() or inputs.size() <= index) else inputs[index] return _update_joypad_input_for_action(action, input, swap_if_taken, replacing_input) ## Set the action used for a button -func _update_joypad_input_for_action(action: String, input: InputEventJoypadButton, swap_if_taken: bool = true, replacing_input: InputEventJoypadButton = null) -> Error: +func _update_joypad_input_for_action(action: String, input: InputEvent, swap_if_taken: bool = true, replacing_input: InputEvent = null) -> Error: var is_valid_keyboard_event = func(event): - return event is InputEventJoypadButton + return event is InputEventJoypadButton or event is InputEventJoypadMotion return _update_input_for_action(action, input, swap_if_taken, replacing_input, is_valid_keyboard_event, joypad_input_changed) diff --git a/addons/input_helper/plugin.cfg b/addons/input_helper/plugin.cfg index 24a21f68..5bc087ff 100644 --- a/addons/input_helper/plugin.cfg +++ b/addons/input_helper/plugin.cfg @@ -3,5 +3,5 @@ name="Input Helper" description="Detect which input device the player is using and manage input actions" author="Nathan Hoad" -version="4.2.2" +version="4.3.1" script="plugin.gd"