summaryrefslogtreecommitdiff
path: root/UI/Menu/KeyBindings.gd
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2021-02-14 14:53:17 +0100
committerDaniel Weipert <code@drogueronin.de>2021-02-14 14:53:17 +0100
commit7115379ccf5714816eeb91fc16d3a63e019c8082 (patch)
treef28c7595ca3f1324bcffadd40027df6c46f7d321 /UI/Menu/KeyBindings.gd
parent332bab8de321d0358b30d5bf159cbda512c14852 (diff)
Adds user configurable key bindings
Diffstat (limited to 'UI/Menu/KeyBindings.gd')
-rw-r--r--UI/Menu/KeyBindings.gd71
1 files changed, 71 insertions, 0 deletions
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