feat: add in-game debug console UI

This commit is contained in:
2026-08-22 19:40:12 +02:00
parent 4f57c77b7b
commit a749d97b99
9 changed files with 217 additions and 2 deletions
@@ -0,0 +1,83 @@
using Game.Features.Console.Application;
using Game.Features.Console.Contracts;
using Game.Features.Console.Infrastructure;
using Game.Godot;
using Game.Godot.Extensions;
using Game.Godot.Mappings;
using Godot;
namespace Game.Features.Console.Godot;
public partial class ConsoleController : CanvasLayer
{
[Export] public LineEdit CommandInput;
[Export] public RichTextLabel OutputLog;
private CommandExecute _commandExecute = new(new CommandRegistry());
private MouseCapture.Memento _mouseMemento;
private void Toggle()
{
Visible = !Visible;
if (Visible)
{
GamePause.Pause();
_mouseMemento = MouseCapture.SaveCaptureMode();
CommandInput.GrabFocus();
}
else
{
GamePause.Resume();
_mouseMemento.RestoreCaptureMode();
}
}
private void AppendLine(string line)
{
OutputLog.AppendText(line + "\n");
}
public void UseCommandRegistry(ICommandRegistry registry)
{
_commandExecute = new CommandExecute(registry);
}
private void OnInputSubmitted(string text)
{
CommandInput.Clear();
CommandInput.ReleaseFocus();
CommandInput.GrabFocus();
if (text.Length == 0) return;
AppendLine($"> {text}");
CommandResult result = _commandExecute.Execute(text);
if (result.ClearOutput)
{
OutputLog.Clear();
return;
}
if (result.Message.Length > 0)
{
AppendLine(result.Message);
}
}
public override void _Ready()
{
this.AssertRequiredExports();
Visible = false;
GamePause.SetIgnoresPause(this, true);
}
public override void _Input(InputEvent @event)
{
if (!@event.IsActionPressed(InputAction.ToggleConsole.ActionName())) return;
Toggle();
GetViewport().SetInputAsHandled();
}
}