Controller Gamepad Suport for Rebind UI (Kindof, still needs polish)

This commit is contained in:
Snoweuph 2023-04-11 02:13:08 +02:00
parent 07aa3c846b
commit d3a27af77b
6 changed files with 185 additions and 21 deletions

View file

@ -7,13 +7,14 @@
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_02uod"]
bg_color = Color(0.439216, 0.631373, 0.537255, 1)
[node name="SettingsMenu" type="Node" node_paths=PackedStringArray("back_button", "tab_container", "bottom_item_video", "bottom_item_audio", "bottom_item_controls", "display_options_button", "vsync_toggle", "resolutions_options_button", "master_volume_slider", "music_volume_slider", "sfx_volume_slider")]
[node name="SettingsMenu" type="Node" node_paths=PackedStringArray("back_button", "tab_container", "bottom_item_video", "bottom_item_audio", "bottom_item_controls", "bottom_item_controls_gamepad", "display_options_button", "vsync_toggle", "resolutions_options_button", "master_volume_slider", "music_volume_slider", "sfx_volume_slider")]
script = ExtResource("1_2rgd2")
back_button = NodePath("Back Button")
tab_container = NodePath("SettingsTabs")
bottom_item_video = NodePath("SettingsTabs/Video/MarginContainer/GridContainer/ResolutionsOptions")
bottom_item_audio = NodePath("SettingsTabs/Audio/MarginContainer/GridContainer/SFXVolSlider")
bottom_item_controls = NodePath("SettingsTabs/Controls/MarginContainer/GridContainer/MoveRightRemapButton")
bottom_item_controls = NodePath("SettingsTabs/Controls/MarginContainer/GridContainer/AttackRemapButton")
bottom_item_controls_gamepad = NodePath("SettingsTabs/Gamepad/MarginContainer/GridContainer/AttackRemapButton")
display_options_button = NodePath("SettingsTabs/Video/MarginContainer/GridContainer/DisplayModeOptions")
vsync_toggle = NodePath("SettingsTabs/Video/MarginContainer/GridContainer/VsyncButton")
resolutions_options_button = NodePath("SettingsTabs/Video/MarginContainer/GridContainer/ResolutionsOptions")
@ -239,6 +240,104 @@ script = ExtResource("2_4asaa")
input_action_name = "move_right"
remap_controller = NodePath("../../../../../Remap Controller")
[node name="AttackLabel" type="Label" parent="SettingsTabs/Controls/MarginContainer/GridContainer"]
custom_minimum_size = Vector2(400, 40)
layout_mode = 2
text = "Attack
"
vertical_alignment = 1
[node name="AttackRemapButton" type="Button" parent="SettingsTabs/Controls/MarginContainer/GridContainer" node_paths=PackedStringArray("remap_controller")]
layout_mode = 2
size_flags_horizontal = 3
focus_neighbor_bottom = NodePath("../../../../../Back Button")
script = ExtResource("2_4asaa")
input_action_name = "attack"
remap_controller = NodePath("../../../../../Remap Controller")
[node name="Gamepad" type="VBoxContainer" parent="SettingsTabs"]
visible = false
layout_mode = 2
[node name="MarginContainer" type="MarginContainer" parent="SettingsTabs/Gamepad"]
custom_minimum_size = Vector2(0, 425)
layout_mode = 2
theme_override_constants/margin_left = 25
theme_override_constants/margin_top = 25
theme_override_constants/margin_right = 25
theme_override_constants/margin_bottom = 25
[node name="GridContainer" type="GridContainer" parent="SettingsTabs/Gamepad/MarginContainer"]
layout_mode = 2
columns = 2
[node name="MoveUpLabel" type="Label" parent="SettingsTabs/Gamepad/MarginContainer/GridContainer"]
custom_minimum_size = Vector2(400, 40)
layout_mode = 2
text = "Move Up"
vertical_alignment = 1
[node name="MoveUpRemapButton" type="Button" parent="SettingsTabs/Gamepad/MarginContainer/GridContainer" node_paths=PackedStringArray("remap_controller")]
layout_mode = 2
script = ExtResource("2_4asaa")
input_action_name = "move_up_controller"
remap_controller = NodePath("../../../../../Remap Controller")
[node name="MoveLeftLabel" type="Label" parent="SettingsTabs/Gamepad/MarginContainer/GridContainer"]
custom_minimum_size = Vector2(400, 40)
layout_mode = 2
text = "Move Left"
vertical_alignment = 1
[node name="MoveLeftRemapButton" type="Button" parent="SettingsTabs/Gamepad/MarginContainer/GridContainer" node_paths=PackedStringArray("remap_controller")]
layout_mode = 2
size_flags_horizontal = 3
script = ExtResource("2_4asaa")
input_action_name = "move_left_controller"
remap_controller = NodePath("../../../../../Remap Controller")
[node name="MoveDownLabel" type="Label" parent="SettingsTabs/Gamepad/MarginContainer/GridContainer"]
custom_minimum_size = Vector2(400, 40)
layout_mode = 2
text = "Move Down"
vertical_alignment = 1
[node name="MoveDownRemapButton" type="Button" parent="SettingsTabs/Gamepad/MarginContainer/GridContainer" node_paths=PackedStringArray("remap_controller")]
layout_mode = 2
size_flags_horizontal = 3
script = ExtResource("2_4asaa")
input_action_name = "move_down_controller"
remap_controller = NodePath("../../../../../Remap Controller")
[node name="MoveRightLabel" type="Label" parent="SettingsTabs/Gamepad/MarginContainer/GridContainer"]
custom_minimum_size = Vector2(400, 40)
layout_mode = 2
text = "Move RIght"
vertical_alignment = 1
[node name="MoveRightRemapButton" type="Button" parent="SettingsTabs/Gamepad/MarginContainer/GridContainer" node_paths=PackedStringArray("remap_controller")]
layout_mode = 2
size_flags_horizontal = 3
focus_neighbor_bottom = NodePath("../../../../../Back Button")
script = ExtResource("2_4asaa")
input_action_name = "move_right_controller"
remap_controller = NodePath("../../../../../Remap Controller")
[node name="AttackLabel" type="Label" parent="SettingsTabs/Gamepad/MarginContainer/GridContainer"]
custom_minimum_size = Vector2(400, 40)
layout_mode = 2
text = "Attack
"
vertical_alignment = 1
[node name="AttackRemapButton" type="Button" parent="SettingsTabs/Gamepad/MarginContainer/GridContainer" node_paths=PackedStringArray("remap_controller")]
layout_mode = 2
size_flags_horizontal = 3
focus_neighbor_bottom = NodePath("../../../../../Back Button")
script = ExtResource("2_4asaa")
input_action_name = "attack"
remap_controller = NodePath("../../../../../Remap Controller")
[node name="Back Button" type="Button" parent="."]
custom_minimum_size = Vector2(500, 40)
anchors_preset = 7
@ -268,4 +367,10 @@ text = "Back"
[connection signal="pressed" from="SettingsTabs/Controls/MarginContainer/GridContainer/MoveLeftRemapButton" to="SettingsTabs/Controls/MarginContainer/GridContainer/MoveLeftRemapButton" method="_on_pressed"]
[connection signal="pressed" from="SettingsTabs/Controls/MarginContainer/GridContainer/MoveDownRemapButton" to="SettingsTabs/Controls/MarginContainer/GridContainer/MoveDownRemapButton" method="_on_pressed"]
[connection signal="pressed" from="SettingsTabs/Controls/MarginContainer/GridContainer/MoveRightRemapButton" to="SettingsTabs/Controls/MarginContainer/GridContainer/MoveRightRemapButton" method="_on_pressed"]
[connection signal="pressed" from="SettingsTabs/Controls/MarginContainer/GridContainer/AttackRemapButton" to="SettingsTabs/Controls/MarginContainer/GridContainer/AttackRemapButton" method="_on_pressed"]
[connection signal="pressed" from="SettingsTabs/Gamepad/MarginContainer/GridContainer/MoveUpRemapButton" to="SettingsTabs/Gamepad/MarginContainer/GridContainer/MoveUpRemapButton" method="_on_pressed"]
[connection signal="pressed" from="SettingsTabs/Gamepad/MarginContainer/GridContainer/MoveLeftRemapButton" to="SettingsTabs/Gamepad/MarginContainer/GridContainer/MoveLeftRemapButton" method="_on_pressed"]
[connection signal="pressed" from="SettingsTabs/Gamepad/MarginContainer/GridContainer/MoveDownRemapButton" to="SettingsTabs/Gamepad/MarginContainer/GridContainer/MoveDownRemapButton" method="_on_pressed"]
[connection signal="pressed" from="SettingsTabs/Gamepad/MarginContainer/GridContainer/MoveRightRemapButton" to="SettingsTabs/Gamepad/MarginContainer/GridContainer/MoveRightRemapButton" method="_on_pressed"]
[connection signal="pressed" from="SettingsTabs/Gamepad/MarginContainer/GridContainer/AttackRemapButton" to="SettingsTabs/Gamepad/MarginContainer/GridContainer/AttackRemapButton" method="_on_pressed"]
[connection signal="pressed" from="Back Button" to="." method="_on_back_pressed"]

View file

@ -13,13 +13,14 @@ func load_data():
"display_mode": 0,
"vsync_on": false,
"current_resolution_index": 0,
"move_up": KEY_W,
"move_down": KEY_S,
"move_left": KEY_A,
"move_right": KEY_D,
"master_volume": 0,
"music_volume": 0,
"sfx_volume": 0,
"move_up": KEY_W,
"move_left": KEY_A,
"move_down": KEY_S,
"move_right": KEY_D,
"attack": "m" + str(MOUSE_BUTTON_LEFT),
}
save_data()
else:

View file

@ -4,12 +4,18 @@ extends Node
@export var music_bus_index : int = 1
@export var sfx_bus_index : int = 2
@export var remappable_input_actions = ["move_up", "move_down", "move_left", "move_right"]
@export var remappable_input_actions = [
"move_up",
"move_down",
"move_left",
"move_right",
"attack"
]
const resolutions_dictionary : Dictionary = {
"800x600" : Vector2(800, 600),
"1024x546" : Vector2(1024, 546),
"1152x648" : Vector2(1152, 648),
"1280x720" : Vector2(1280, 720),
"1600x900" : Vector2(1600, 900),
"1366x768" : Vector2(1366, 768),
"1920x1080" : Vector2(1920, 1080),
@ -65,15 +71,26 @@ func update_sfx_volume(vol : int) -> void:
# Controls
func set_controls_from_save_file() -> void:
for remappable_input_action in remappable_input_actions:
for i in Save.game_data.keys().size():
if Save.game_data.keys()[i] == remappable_input_action:
for key in InputMap.action_get_events(remappable_input_action):
InputMap.action_erase_event(remappable_input_action, key)
var control_key = InputEventKey.new()
control_key.set_keycode(Save.game_data.values()[i])
InputMap.action_add_event(remappable_input_action, control_key)
pass
func set_control_binding(action : String, keycode : int):
print(keycode)
var action = Save.game_data.keys().find(remappable_input_action)
for key in InputMap.action_get_events(remappable_input_action):
InputMap.action_erase_event(remappable_input_action, key)
var binding = Save.game_data.values()[action]
var control_event
match str(binding)[0]:
"m":
binding = int(binding.right(binding.length()-1))
control_event = InputEventMouseButton.new()
control_event.button_index = binding
"b":
binding = int(binding.right(binding.length()-1))
control_event = InputEventJoypadButton.new()
control_event.button_index = binding
"j":
binding = int(binding.right(binding.length()-1))
control_event = InputEventJoypadMotion.new()
control_event.axis = binding
_:
control_event = InputEventKey.new()
control_event.set_keycode(binding)
InputMap.action_add_event(remappable_input_action, control_event)
pass

View file

@ -7,7 +7,13 @@ func _ready():
display_key()
func display_key ():
text = "%s" % InputMap.action_get_events(input_action_name)[0].as_text()
self.text = "%s" % InputMap.action_get_events(input_action_name)[0].as_text()
if self.text.begins_with("Joypad Motion on Axis"):
var split_text = self.text.split(" ")
self.text = split_text[3] + " " + split_text[4]
if self.text.begins_with("Joypad Button"):
var split_text = self.text.split(" ")
self.text = split_text[1] + " " + split_text[2]
pass
func _on_pressed():

View file

@ -1,24 +1,46 @@
extends Control
@export var min_time_between_remaps := 0.1
var remap_button : Button
var remap_action : String
var time_since_remap := 0.0
func _unhandled_key_input(event):
if remap_button != null and event.pressed:
remap_key(event)
pass
func _input(event):
if remap_button != null:
if event is InputEventMouseButton and event.pressed:
remap_key(event)
if event is InputEventJoypadButton and event.pressed:
print(event)
remap_key(event)
if event is InputEventJoypadMotion:
remap_key(event)
pass
func _ready():
set_process_unhandled_key_input(false)
remap_button = null
pass
func _process(delta):
if remap_button == null:
time_since_remap += delta
pass
func start_remap(button : Button, action : String):
if(time_since_remap < min_time_between_remaps): return
if(remap_button != null):
remap_button.display_key()
remap_button = button
remap_action = action
remap_button.text = "..."
remap_button.disabled = true
set_process_unhandled_key_input(true)
button.release_focus()
pass
@ -28,8 +50,19 @@ func remap_key(event):
for key in InputMap.action_get_events(remap_action):
InputMap.action_erase_event(remap_action, key)
InputMap.action_add_event(remap_action, event)
Save.game_data["%s" % [remap_action]] = event.keycode
match str(event.get_class()):
"InputEventJoypadButton":
Save.game_data["%s" % [remap_action]] = "b" + str(event.button_index)
"InputEventJoypadMotion":
Save.game_data["%s" % [remap_action]] = "j" + str(event.axis)
"InputEventMouseButton":
Save.game_data["%s" % [remap_action]] = "m" + str(event.button_index)
"InputEventKey":
Save.game_data["%s" % [remap_action]] = event.keycode
remap_button.display_key()
remap_button.grab_focus()
remap_button.disabled = false
time_since_remap = 0
remap_button = null
pass

View file

@ -6,6 +6,7 @@ extends Node
@export var bottom_item_video : Control
@export var bottom_item_audio : Control
@export var bottom_item_controls : Control
@export var bottom_item_controls_gamepad : Control
@export var display_options_button : OptionButton
@export var vsync_toggle : CheckButton
@ -58,6 +59,7 @@ func update_change_tab():
0: back_button.focus_neighbor_top = bottom_item_video.get_path()
1: back_button.focus_neighbor_top = bottom_item_audio.get_path()
2: back_button.focus_neighbor_top = bottom_item_controls.get_path()
3: back_button.focus_neighbor_top = bottom_item_controls_gamepad.get_path()
back_button.grab_focus()
pass