From 51ddc76c13316d29a898423d10919e81daaa0d23 Mon Sep 17 00:00:00 2001 From: cornerloan Date: Sat, 12 Apr 2025 01:18:31 -0700 Subject: [PATCH 01/12] user can remap controls using custom keys issues being worked on still: - UI is not attempting to look clean or good yet - only one implementation, need to separate keyboard and controller into different tabs - need to ensure no duplicate keys for mapped inputs - remapping a key to the "accept" button causes the remapping popup to immediately reopen --- Globals/SaveSystem.cs | 135 ++++++++++- Scenes/NoteManager/Scripts/InputHandler.cs | 3 +- Scenes/UI/Remapping/ControlSettings.cs | 251 ++++++++++++--------- Scenes/UI/Remapping/Remap.tscn | 172 ++++++++++---- project.godot | 30 +-- 5 files changed, 412 insertions(+), 179 deletions(-) diff --git a/Globals/SaveSystem.cs b/Globals/SaveSystem.cs index de754cfb..fc0a08b1 100644 --- a/Globals/SaveSystem.cs +++ b/Globals/SaveSystem.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using System.Text.Json; using Godot; @@ -13,7 +14,15 @@ public static class SaveSystem private static ConfigFile _curConfigData; private const float DefaultVolume = 1f; - private const string DefaultInput = "WASD"; + private const string DefaultInputKey = "WASD"; + private const string DefaultInputKeyboardUp = "W"; + private const string DefaultInputKeyboardLeft = "A"; + private const string DefaultInputKeyboardDown = "S"; + private const string DefaultInputKeyboardRight = "D"; + private const string DefaultInputControllerUp = "0"; + private const string DefaultInputControllerLeft = "0"; + private const string DefaultInputControllerDown = "0"; + private const string DefaultInputControllerRight = "0"; private const string DefaultLanguage = "en"; private const bool DefaultHighCon = false; @@ -21,6 +30,14 @@ public enum ConfigSettings { Volume, InputKey, + InputKeyboardUp, + InputKeyboardLeft, + InputKeyboardDown, + InputKeyboardRight, + InputControllerUp, + InputControllerLeft, + InputControllerDown, + InputControllerRight, LanguageKey, HighContrast, } @@ -32,7 +49,15 @@ private static void InitConfig() { _curConfigData = new ConfigFile(); UpdateConfig(ConfigSettings.Volume, DefaultVolume); - UpdateConfig(ConfigSettings.InputKey, DefaultInput); + UpdateConfig(ConfigSettings.InputKey, DefaultInputKey); + UpdateConfig(ConfigSettings.InputKeyboardUp, DefaultInputKeyboardUp); + UpdateConfig(ConfigSettings.InputKeyboardLeft, DefaultInputKeyboardLeft); + UpdateConfig(ConfigSettings.InputKeyboardDown, DefaultInputKeyboardDown); + UpdateConfig(ConfigSettings.InputKeyboardRight, DefaultInputKeyboardRight); + UpdateConfig(ConfigSettings.InputControllerUp, DefaultInputControllerUp); + UpdateConfig(ConfigSettings.InputControllerLeft, DefaultInputControllerLeft); + UpdateConfig(ConfigSettings.InputControllerDown, DefaultInputControllerDown); + UpdateConfig(ConfigSettings.InputControllerRight, DefaultInputControllerRight); UpdateConfig(ConfigSettings.LanguageKey, DefaultLanguage); UpdateConfig(ConfigSettings.HighContrast, DefaultHighCon); } @@ -54,6 +79,30 @@ public static void UpdateConfig(ConfigSettings setting, Variant value) case ConfigSettings.InputKey: _curConfigData.SetValue("Options", "InputKey", value); break; + case ConfigSettings.InputKeyboardUp: + _curConfigData.SetValue("Options", "InputKeyboardUp", value); + break; + case ConfigSettings.InputKeyboardLeft: + _curConfigData.SetValue("Options", "InputKeyboardLeft", value); + break; + case ConfigSettings.InputKeyboardDown: + _curConfigData.SetValue("Options", "InputKeyboardDown", value); + break; + case ConfigSettings.InputKeyboardRight: + _curConfigData.SetValue("Options", "InputKeyboardRight", value); + break; + case ConfigSettings.InputControllerUp: + _curConfigData.SetValue("Options", "InputControllerUp", value); + break; + case ConfigSettings.InputControllerLeft: + _curConfigData.SetValue("Options", "InputControllerLeft", value); + break; + case ConfigSettings.InputControllerDown: + _curConfigData.SetValue("Options", "InputControllerDown", value); + break; + case ConfigSettings.InputControllerRight: + _curConfigData.SetValue("Options", "InputControllerRight", value); + break; case ConfigSettings.LanguageKey: _curConfigData.SetValue("Options", "LanguageKey", value); break; @@ -74,6 +123,7 @@ private static void AssertConfigFile() if (_curConfigData == null) { LoadConfigData(); + ApplySavedInputBindings(); } } @@ -104,6 +154,7 @@ private static void LoadConfigData() { _curConfigData = new ConfigFile(); VerifyConfig(); + GD.Print(ProjectSettings.GlobalizePath("user://Options.cfg")); if (_curConfigData.Load(UserConfigPath) == Error.Ok) return; GD.PushWarning("Safe. No config could be found, creating a new one."); @@ -119,7 +170,55 @@ public static Variant GetConfigValue(ConfigSettings setting) case ConfigSettings.Volume: return _curConfigData.GetValue("Options", "Volume", DefaultVolume); case ConfigSettings.InputKey: - return _curConfigData.GetValue("Options", "InputKey", DefaultInput); + return _curConfigData.GetValue("Options", "InputKey", DefaultInputKey); + case ConfigSettings.InputKeyboardUp: + return _curConfigData.GetValue( + "Options", + "InputKeyboardUp", + DefaultInputKeyboardUp + ); + case ConfigSettings.InputKeyboardLeft: + return _curConfigData.GetValue( + "Options", + "InputKeyboardLeft", + DefaultInputKeyboardLeft + ); + case ConfigSettings.InputKeyboardDown: + return _curConfigData.GetValue( + "Options", + "InputKeyboardDown", + DefaultInputKeyboardDown + ); + case ConfigSettings.InputKeyboardRight: + return _curConfigData.GetValue( + "Options", + "InputKeyboardRight", + DefaultInputKeyboardRight + ); + case ConfigSettings.InputControllerUp: + return _curConfigData.GetValue( + "Options", + "InputControllerUp", + DefaultInputControllerUp + ); + case ConfigSettings.InputControllerLeft: + return _curConfigData.GetValue( + "Options", + "InputControllerLeft", + DefaultInputControllerLeft + ); + case ConfigSettings.InputControllerDown: + return _curConfigData.GetValue( + "Options", + "InputControllerDown", + DefaultInputControllerDown + ); + case ConfigSettings.InputControllerRight: + return _curConfigData.GetValue( + "Options", + "InputControllerRight", + DefaultInputControllerRight + ); case ConfigSettings.LanguageKey: return _curConfigData.GetValue("Options", "LanguageKey", DefaultLanguage); case ConfigSettings.HighContrast: @@ -212,5 +311,35 @@ public static void ClearSave() DirAccess.RemoveAbsolute(UserSavePath); } + public static void ApplySavedInputBindings() + { + string keyboardUp = GetConfigValue(ConfigSettings.InputKeyboardUp).ToString(); + string keyboardDown = GetConfigValue(ConfigSettings.InputKeyboardDown).ToString(); + string keyboardLeft = GetConfigValue(ConfigSettings.InputKeyboardLeft).ToString(); + string keyboardRight = GetConfigValue(ConfigSettings.InputKeyboardRight).ToString(); + + InputMap.ActionEraseEvents("WASD_arrowUp"); + InputMap.ActionEraseEvents("WASD_arrowDown"); + InputMap.ActionEraseEvents("WASD_arrowLeft"); + InputMap.ActionEraseEvents("WASD_arrowRight"); + + InputMap.ActionAddEvent( + "WASD_arrowUp", + new InputEventKey { Keycode = (Key)Enum.Parse(typeof(Key), keyboardUp) } + ); + InputMap.ActionAddEvent( + "WASD_arrowDown", + new InputEventKey { Keycode = (Key)Enum.Parse(typeof(Key), keyboardDown) } + ); + InputMap.ActionAddEvent( + "WASD_arrowLeft", + new InputEventKey { Keycode = (Key)Enum.Parse(typeof(Key), keyboardLeft) } + ); + InputMap.ActionAddEvent( + "WASD_arrowRight", + new InputEventKey { Keycode = (Key)Enum.Parse(typeof(Key), keyboardRight) } + ); + } + #endregion } diff --git a/Scenes/NoteManager/Scripts/InputHandler.cs b/Scenes/NoteManager/Scripts/InputHandler.cs index b682d9d5..8ff6732e 100644 --- a/Scenes/NoteManager/Scripts/InputHandler.cs +++ b/Scenes/NoteManager/Scripts/InputHandler.cs @@ -61,11 +61,12 @@ public override void _Process(double delta) string scheme = SaveSystem.GetConfigValue(SaveSystem.ConfigSettings.InputKey).As(); if (Input.GetConnectedJoypads().Count <= 0 && scheme == "CONTROLLER") { - SaveSystem.UpdateConfig(SaveSystem.ConfigSettings.InputKey, "ARROWS"); + SaveSystem.UpdateConfig(SaveSystem.ConfigSettings.InputKey, "WASD"); } foreach (var arrow in Arrows) { + GD.Print(scheme + "_" + arrow.Key); if (Input.IsActionJustPressed(scheme + "_" + arrow.Key)) { EmitSignal(nameof(NotePressed), (int)arrow.Type); diff --git a/Scenes/UI/Remapping/ControlSettings.cs b/Scenes/UI/Remapping/ControlSettings.cs index 972c2657..617dc018 100644 --- a/Scenes/UI/Remapping/ControlSettings.cs +++ b/Scenes/UI/Remapping/ControlSettings.cs @@ -23,72 +23,53 @@ public partial class ControlSettings : Node2D, IFocusableMenu [Export] private Button _closeButton; - private Button _controllerButton; + [Export] + private Panel _remapPopup; - private static readonly Dictionary> SpriteMappings = new() - { - { - "WASD", - new Dictionary - { - { "left", "res://Scenes/UI/Remapping/Assets/A_Key_Light.png" }, - { "right", "res://Scenes/UI/Remapping/Assets/D_Key_Light.png" }, - { "up", "res://Scenes/UI/Remapping/Assets/W_Key_Light.png" }, - { "down", "res://Scenes/UI/Remapping/Assets/S_Key_Light.png" }, - } - }, - { - "ARROWS", - new Dictionary - { - { "left", "res://Scenes/UI/Remapping/Assets/Arrow_Left_Key_Light.png" }, - { "right", "res://Scenes/UI/Remapping/Assets/Arrow_Right_Key_Light.png" }, - { "up", "res://Scenes/UI/Remapping/Assets/Arrow_Up_Key_Light.png" }, - { "down", "res://Scenes/UI/Remapping/Assets/Arrow_Down_Key_Light.png" }, - } - }, - { - "QWERT", - new Dictionary - { - { "left", "res://Scenes/UI/Remapping/Assets/Q_Key_Light.png" }, - { "right", "res://Scenes/UI/Remapping/Assets/R_Key_Light.png" }, - { "up", "res://Scenes/UI/Remapping/Assets/W_Key_Light.png" }, - { "down", "res://Scenes/UI/Remapping/Assets/E_Key_Light.png" }, - } - }, - { - "CONTROLLER", - new Dictionary - { - { "left", "res://Scenes/UI/Remapping/Assets/Positional_Prompts_Left.png" }, - { "right", "res://Scenes/UI/Remapping/Assets/Positional_Prompts_Right.png" }, - { "up", "res://Scenes/UI/Remapping/Assets/Positional_Prompts_Up.png" }, - { "down", "res://Scenes/UI/Remapping/Assets/Positional_Prompts_Down.png" }, - } - }, - }; + [Export] + private Label _remapLabel; + + [Export] + private Timer _remapTimer; + + private Label _leftLabel; + private Label _rightLabel; + private Label _upLabel; + private Label _downLabel; + private Label _secondaryPlacementLabel; + + private Key _tempKeyboardKey; + private JoyButton _tempJoyButton; + private string _chosenKey = ""; public override void _Ready() { - GetNode