From 7115379ccf5714816eeb91fc16d3a63e019c8082 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 14 Feb 2021 14:53:17 +0100 Subject: Adds user configurable key bindings --- UI/Menu/KeyBindings.gd | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 UI/Menu/KeyBindings.gd (limited to 'UI/Menu/KeyBindings.gd') diff --git a/UI/Menu/KeyBindings.gd b/UI/Menu/KeyBindings.gd new file mode 100644 index 0000000..4650be9 --- /dev/null +++ b/UI/Menu/KeyBindings.gd @@ -0,0 +1,71 @@ +extends Control +# @see https://www.gotut.net/godot-key-bindings-tutorial/ + + +var input_action = null + + +func _ready(): + self.set_keys() + + +func _input(event): + if self.is_input_event(event) and input_action != null: + self.change_key(self.input_action, event) + + +func is_input_event(event): + # TODO: read up on proper controller input handling + return event is InputEventKey or event is InputEventJoypadButton or event is InputEventJoypadMotion + + +func set_keys(): + for action in Enum.ACTIONS: + var input = get_node("Panel/VBoxContainer/" + str(action) + "/Input") + input.set_pressed(false) + + var action_list = InputMap.get_action_list(action) + if action_list.empty(): + input.set_text("SET BUTTON") + else: + var text = "" + for idx in range(action_list.size()): + var btn = action_list[idx] + var btn_text = btn.as_text() + + if btn is InputEventJoypadButton: + btn_text = "BtnIdx: " + str(btn.button_index) + elif btn is InputEventJoypadMotion: + btn_text = "JoyAxis: " + str(btn.axis_value) + + if idx == 0: + text += btn_text + else: + text += ", " + btn_text + + input.set_text(text) + + +func change_key(action, key): + if key is InputEventJoypadMotion: + if key.get_axis_value() > 0: + key.set_axis_value(1) + else: + key.set_axis_value(-1) + + InputMap.action_add_event(action, key) + self.input_action = null + self.set_keys() + + +func clear_keys(action): + InputMap.action_erase_events(action) + self.set_keys() + + +func _on_Clear_pressed(action): + self.clear_keys(action) + + +func _on_Input_pressed(action): + self.input_action = action -- cgit v1.2.3