diff --git a/objects/ui/input_settings.tscn b/objects/ui/input_settings.tscn index 66d4451..f31f3a8 100644 --- a/objects/ui/input_settings.tscn +++ b/objects/ui/input_settings.tscn @@ -1,17 +1,23 @@ -[gd_scene load_steps=3 format=3 uid="uid://cvfsbiy5ggrpg"] +[gd_scene load_steps=4 format=3 uid="uid://cvfsbiy5ggrpg"] [ext_resource type="PackedScene" uid="uid://bxpr4m7lq7clh" path="res://objects/ui/input_button.tscn" id="1_h8s4o"] +[ext_resource type="Script" uid="uid://input_settings_script" path="res://scripts/UI/InputSettings.cs" id="2_inputs"] [sub_resource type="StyleBoxFlat" id="StyleBoxFlat_se25o"] bg_color = Color(0, 0, 0, 1) -[node name="Input Settings" type="Control"] +[node name="Input Settings" type="Control" node_paths=PackedStringArray("ActionsContainer", "ResetButton", "InputSettingsControl")] layout_mode = 3 anchors_preset = 15 anchor_right = 1.0 anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 +script = ExtResource("2_inputs") +ActionsContainer = NodePath("PanelContainer/MarginContainer/VBoxContainer/ScrollContainer/Actions") +ResetButton = NodePath("PanelContainer/MarginContainer/VBoxContainer/Reset to default Button") +InputSettingsControl = NodePath(".") +InputButtonScene = ExtResource("1_h8s4o") [node name="PanelContainer" type="PanelContainer" parent="."] layout_mode = 1 @@ -48,24 +54,6 @@ layout_mode = 2 size_flags_horizontal = 3 theme_override_constants/separation = 8 -[node name="Input button" parent="PanelContainer/MarginContainer/VBoxContainer/ScrollContainer/Actions" instance=ExtResource("1_h8s4o")] -layout_mode = 2 - -[node name="Input button2" parent="PanelContainer/MarginContainer/VBoxContainer/ScrollContainer/Actions" instance=ExtResource("1_h8s4o")] -layout_mode = 2 - -[node name="Input button3" parent="PanelContainer/MarginContainer/VBoxContainer/ScrollContainer/Actions" instance=ExtResource("1_h8s4o")] -layout_mode = 2 - -[node name="Input button4" parent="PanelContainer/MarginContainer/VBoxContainer/ScrollContainer/Actions" instance=ExtResource("1_h8s4o")] -layout_mode = 2 - -[node name="Input button5" parent="PanelContainer/MarginContainer/VBoxContainer/ScrollContainer/Actions" instance=ExtResource("1_h8s4o")] -layout_mode = 2 - -[node name="Input button6" parent="PanelContainer/MarginContainer/VBoxContainer/ScrollContainer/Actions" instance=ExtResource("1_h8s4o")] -layout_mode = 2 - [node name="Reset to default Button" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer"] layout_mode = 2 text = "RESET_TO_DEFAULT_BUTTON" diff --git a/objects/ui/settings_menu.tscn b/objects/ui/settings_menu.tscn index f2fb6ef..faa984a 100644 --- a/objects/ui/settings_menu.tscn +++ b/objects/ui/settings_menu.tscn @@ -53,7 +53,6 @@ size_flags_vertical = 3 [node name="Input Settings Button" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer"] layout_mode = 2 -disabled = true text = "INPUT_BUTTON" flat = true diff --git a/scripts/UI/InputSettings.cs b/scripts/UI/InputSettings.cs new file mode 100644 index 0000000..c15d525 --- /dev/null +++ b/scripts/UI/InputSettings.cs @@ -0,0 +1,205 @@ +using Godot; +using Mr.BrickAdventures.Autoloads; + +namespace Mr.BrickAdventures.scripts.UI; + +public partial class InputSettings : Control +{ + [Export] public VBoxContainer ActionsContainer { get; set; } + [Export] public Button ResetButton { get; set; } + [Export] public Control InputSettingsControl { get; set; } + [Export] public PackedScene InputButtonScene { get; set; } + + private static readonly string[] RemappableActions = + ["left", "right", "jump", "attack", "pause", "show_marketplace"]; + + private UIManager UIManager => UIManager.Instance; + private ConfigFileHandler ConfigFileHandler => ConfigFileHandler.Instance; + + private string _rebindingAction; + private Button _rebindingButton; + private Label _rebindingLabel; + + public override void _Ready() + { + PopulateActions(); + ResetButton.FocusMode = FocusModeEnum.All; + ResetButton.Pressed += OnResetPressed; + + if (InputDeviceManager.Instance != null) + InputDeviceManager.Instance.DeviceChanged += OnDeviceChanged; + InputSettingsControl.VisibilityChanged += OnVisibilityChanged; + } + + public override void _ExitTree() + { + if (InputDeviceManager.Instance != null) + InputDeviceManager.Instance.DeviceChanged -= OnDeviceChanged; + InputSettingsControl.VisibilityChanged -= OnVisibilityChanged; + } + + public override void _UnhandledInput(InputEvent @event) + { + if (_rebindingAction != null) + { + if (@event is InputEventKey keyEvent && keyEvent.Pressed && !keyEvent.Echo) + { + GetViewport().SetInputAsHandled(); + TryBind(_rebindingAction, _rebindingButton, _rebindingLabel, keyEvent); + } + else if (@event.IsActionReleased("ui_cancel")) + { + GetViewport().SetInputAsHandled(); + CancelRebind(); + } + return; + } + + if (!@event.IsActionReleased("ui_cancel")) return; + if (!UIManager.IsScreenOnTop(InputSettingsControl)) return; + UIManager.PopScreen(); + } + + private void PopulateActions() + { + foreach (Node child in ActionsContainer.GetChildren()) + child.QueueFree(); + + bool first = true; + foreach (var action in RemappableActions) + { + var row = InputButtonScene.Instantiate