feat: add in-game debug console UI
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user