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 +++++++++++++++++++++++++++ UI/Menu/KeyBindings.tscn | 123 +++++++++++++++++++++++++++++++++++++++++++++++ UI/Menu/Menu.gd | 15 ++++++ UI/Menu/Menu.tscn | 41 ++++++++++++++++ UI/Menu/MenuButton.gd | 6 +++ UI/Menu/MenuButton.tscn | 15 ++++++ 6 files changed, 271 insertions(+) create mode 100644 UI/Menu/KeyBindings.gd create mode 100644 UI/Menu/KeyBindings.tscn create mode 100644 UI/Menu/Menu.gd create mode 100644 UI/Menu/Menu.tscn create mode 100644 UI/Menu/MenuButton.gd create mode 100644 UI/Menu/MenuButton.tscn (limited to 'UI/Menu') 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 diff --git a/UI/Menu/KeyBindings.tscn b/UI/Menu/KeyBindings.tscn new file mode 100644 index 0000000..948b44b --- /dev/null +++ b/UI/Menu/KeyBindings.tscn @@ -0,0 +1,123 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://UI/Menu/KeyBindings.gd" type="Script" id=1] + +[node name="KeyBindings" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_left = 64.0 +margin_top = 64.0 +margin_right = -64.0 +margin_bottom = -88.0 +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Panel" type="Panel" parent="."] +margin_right = 896.0 +margin_bottom = 448.0 + +[node name="VBoxContainer" type="VBoxContainer" parent="Panel"] +margin_left = 256.0 +margin_top = 24.0 +margin_right = 640.0 +margin_bottom = 424.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="DIRECTION_LEFT" type="HBoxContainer" parent="Panel/VBoxContainer"] +margin_right = 384.0 +margin_bottom = 20.0 + +[node name="Label" type="Label" parent="Panel/VBoxContainer/DIRECTION_LEFT"] +margin_top = 3.0 +margin_right = 24.0 +margin_bottom = 17.0 +text = "Left" + +[node name="Input" type="Button" parent="Panel/VBoxContainer/DIRECTION_LEFT"] +margin_left = 28.0 +margin_right = 40.0 +margin_bottom = 20.0 + +[node name="Clear" type="Button" parent="Panel/VBoxContainer/DIRECTION_LEFT"] +margin_left = 44.0 +margin_right = 88.0 +margin_bottom = 20.0 +text = "Clear" + +[node name="DIRECTION_RIGHT" type="HBoxContainer" parent="Panel/VBoxContainer"] +margin_top = 24.0 +margin_right = 384.0 +margin_bottom = 44.0 + +[node name="Label" type="Label" parent="Panel/VBoxContainer/DIRECTION_RIGHT"] +margin_top = 3.0 +margin_right = 32.0 +margin_bottom = 17.0 +text = "Right" + +[node name="Input" type="Button" parent="Panel/VBoxContainer/DIRECTION_RIGHT"] +margin_left = 36.0 +margin_right = 48.0 +margin_bottom = 20.0 + +[node name="Clear" type="Button" parent="Panel/VBoxContainer/DIRECTION_RIGHT"] +margin_left = 52.0 +margin_right = 96.0 +margin_bottom = 20.0 +text = "Clear" + +[node name="JUMP" type="HBoxContainer" parent="Panel/VBoxContainer"] +margin_top = 48.0 +margin_right = 384.0 +margin_bottom = 68.0 + +[node name="Label" type="Label" parent="Panel/VBoxContainer/JUMP"] +margin_top = 3.0 +margin_right = 32.0 +margin_bottom = 17.0 +text = "Jump" + +[node name="Input" type="Button" parent="Panel/VBoxContainer/JUMP"] +margin_left = 36.0 +margin_right = 48.0 +margin_bottom = 20.0 + +[node name="Clear" type="Button" parent="Panel/VBoxContainer/JUMP"] +margin_left = 52.0 +margin_right = 96.0 +margin_bottom = 20.0 +text = "Clear" + +[node name="RUN" type="HBoxContainer" parent="Panel/VBoxContainer"] +margin_top = 72.0 +margin_right = 384.0 +margin_bottom = 92.0 + +[node name="Label" type="Label" parent="Panel/VBoxContainer/RUN"] +margin_top = 3.0 +margin_right = 24.0 +margin_bottom = 17.0 +text = "Run" + +[node name="Input" type="Button" parent="Panel/VBoxContainer/RUN"] +margin_left = 28.0 +margin_right = 40.0 +margin_bottom = 20.0 + +[node name="Clear" type="Button" parent="Panel/VBoxContainer/RUN"] +margin_left = 44.0 +margin_right = 88.0 +margin_bottom = 20.0 +text = "Clear" +[connection signal="pressed" from="Panel/VBoxContainer/DIRECTION_LEFT/Input" to="." method="_on_Input_pressed" binds= [ "DIRECTION_LEFT" ]] +[connection signal="pressed" from="Panel/VBoxContainer/DIRECTION_LEFT/Clear" to="." method="_on_Clear_pressed" binds= [ "DIRECTION_LEFT" ]] +[connection signal="pressed" from="Panel/VBoxContainer/DIRECTION_RIGHT/Input" to="." method="_on_Input_pressed" binds= [ "DIRECTION_RIGHT" ]] +[connection signal="pressed" from="Panel/VBoxContainer/DIRECTION_RIGHT/Clear" to="." method="_on_Clear_pressed" binds= [ "DIRECTION_RIGHT" ]] +[connection signal="pressed" from="Panel/VBoxContainer/JUMP/Input" to="." method="_on_Input_pressed" binds= [ "JUMP" ]] +[connection signal="pressed" from="Panel/VBoxContainer/JUMP/Clear" to="." method="_on_Clear_pressed" binds= [ "JUMP" ]] +[connection signal="pressed" from="Panel/VBoxContainer/RUN/Input" to="." method="_on_Input_pressed" binds= [ "RUN" ]] +[connection signal="pressed" from="Panel/VBoxContainer/RUN/Clear" to="." method="_on_Clear_pressed" binds= [ "RUN" ]] diff --git a/UI/Menu/Menu.gd b/UI/Menu/Menu.gd new file mode 100644 index 0000000..400f669 --- /dev/null +++ b/UI/Menu/Menu.gd @@ -0,0 +1,15 @@ +extends CanvasLayer +# needs to be a CanvasLayer as parent so it's drawn on top + + +func _ready(): + self.pause_mode = PAUSE_MODE_PROCESS + + +func close(): + get_tree().paused = false + self.queue_free() + + +func _on_Close_pressed(): + self.close() diff --git a/UI/Menu/Menu.tscn b/UI/Menu/Menu.tscn new file mode 100644 index 0000000..69c14c2 --- /dev/null +++ b/UI/Menu/Menu.tscn @@ -0,0 +1,41 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://UI/Menu/Menu.gd" type="Script" id=1] +[ext_resource path="res://UI/Menu/KeyBindings.tscn" type="PackedScene" id=2] + +[sub_resource type="StyleBoxFlat" id=3] +bg_color = Color( 0, 0, 0, 0.882353 ) + +[sub_resource type="StyleBoxFlat" id=2] +bg_color = Color( 1, 1, 1, 0 ) +border_width_left = 1 +border_width_top = 1 +border_width_right = 1 +border_width_bottom = 1 + +[node name="Menu" type="CanvasLayer"] +pause_mode = 2 +script = ExtResource( 1 ) + +[node name="Container" type="Panel" parent="."] +margin_right = 1024.0 +margin_bottom = 600.0 +custom_styles/panel = SubResource( 3 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Close" type="Button" parent="Container"] +margin_left = 976.0 +margin_right = 1023.0 +margin_bottom = 20.0 +custom_styles/normal = SubResource( 2 ) +text = "Close" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="KeyBindings" parent="Container" instance=ExtResource( 2 )] +margin_right = 24.0 +margin_bottom = 24.0 +[connection signal="pressed" from="Container/Close" to="." method="_on_Close_pressed"] diff --git a/UI/Menu/MenuButton.gd b/UI/Menu/MenuButton.gd new file mode 100644 index 0000000..e116acb --- /dev/null +++ b/UI/Menu/MenuButton.gd @@ -0,0 +1,6 @@ +extends Button + + +# freeze current scene and overlay Menu.tscn +func _on_MenuButton_pressed(): + Global.open_menu() diff --git a/UI/Menu/MenuButton.tscn b/UI/Menu/MenuButton.tscn new file mode 100644 index 0000000..b5626fb --- /dev/null +++ b/UI/Menu/MenuButton.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://UI/Menu/MenuButton.gd" type="Script" id=1] + +[node name="MenuButton" type="Button"] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_right = -976.0 +margin_bottom = -580.0 +text = "Menu" +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} +[connection signal="pressed" from="." to="." method="_on_MenuButton_pressed"] -- cgit v1.2.3