88 lines
2.0 KiB
C#
88 lines
2.0 KiB
C#
using Game.Features.Console.Application;
|
|
using Game.Features.Console.Contracts;
|
|
using Game.Features.Console.Infrastructure;
|
|
using Game.Features.Logging.Contracts;
|
|
using Game.Godot;
|
|
using Game.Godot.Extensions;
|
|
using Game.Godot.Mappings;
|
|
using Godot;
|
|
|
|
namespace Game.Features.Console.Godot;
|
|
|
|
public partial class ConsoleController : CanvasLayer, ILogTarget
|
|
{
|
|
[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 OnInputSubmitted(string text)
|
|
{
|
|
CommandInput.Clear();
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
private void AppendLine(string line)
|
|
{
|
|
OutputLog.AppendText(line + "\n");
|
|
}
|
|
|
|
public void UseCommandRegistry(ICommandRegistry registry)
|
|
{
|
|
_commandExecute = new CommandExecute(registry);
|
|
}
|
|
|
|
public void WriteLogLine(string logLine)
|
|
{
|
|
AppendLine(logLine);
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|