Input remapping

This commit is contained in:
Marco 2025-03-27 12:15:46 +01:00
commit 82edd196b0
5 changed files with 104 additions and 10 deletions

View file

@ -12,18 +12,40 @@ public partial class KeyRemappingItem : HBoxContainer
[Export] public Label InputLabel { get; private set; }
private bool _active = false;
public override void _Ready()
{
NameLabel.Text = KeyName;
var actions = InputMap.GetActions();
var action = actions.FirstOrDefault(x => x == KeyId);
var events = InputMap.ActionGetEvents(action);
InputLabel.Text = string.Join(",", events.Select(x => x.AsText()));
foreach (var e in events)
{
var button = new Button();
button.Text = e switch
{
InputEventKey key => key.PhysicalKeycode.ToString(),
InputEventJoypadButton jButton => $"Joypad {jButton.ButtonIndex}",
InputEventMouseButton mouseButton => $"Mouse {mouseButton.ButtonIndex}",
_ => e.AsText()
};
button.Pressed += () => { BeginRemap(button, action, e); };
this.AddChild(button);
}
//InputLabel.Text = string.Join(",", events.Select(x => x.AsText()));
}
private void BeginRemap(Button button, StringName action, InputEvent e)
{
button.Text = "Remapping...";
_active = true;
}
public void StartRemap()
{
_active = true;
@ -32,11 +54,12 @@ public partial class KeyRemappingItem : HBoxContainer
public override void _Input(InputEvent e)
{
if (!_active) return;
if (e is InputEventKey keyEvent && keyEvent.Pressed)
{
GD.Print(keyEvent.Keycode);
InputLabel.Text = keyEvent.Keycode.ToString();
//InputLabel.Text = keyEvent.Keycode.ToString();
_active = false;
}
}