using System.Collections.Generic; using System.Reflection; using Cogwheel; using Godot; namespace ADEPT.Core.Cogwheel; public partial class GodotCogwheelConsole : Control, ICogwheelConsole { public string OpeningMessage { get; set; } public bool IsRunning { get; set; } public CommandsManager CommandsManager { get; set; } [Export] private string _openingMessage = "ADEPT Console"; [Export] private TextEdit _input; [Export] private RichTextLabel _output; [Export] private Godot.Collections.Dictionary _colors = new() { {"error", new Color(1, 0, 0)}, {"warning", new Color(1, 1, 0)}, {"log", new Color(1, 1, 1)}, {"info", new Color(0, 0, 1)}, {"commandHighlight", new Color(0, 0.8f, 0.4f)}, }; private CommandsManager _commandsManager; private CodeHighlighter _codeHighlighter; public void Initialize(CommandsManager commandsManager) { _commandsManager = commandsManager; _codeHighlighter = new CodeHighlighter(); _input.SyntaxHighlighter = _codeHighlighter; _codeHighlighter.KeywordColors = BuildKeywords(); } public override void _Ready() { _output.Text += _openingMessage + "\n"; } public void Log(string message) { Write(message + "\n"); } public void LogError(string message) { Log($"[color=#{_colors["error"].ToHtml()}]{message}[/color]"); } public void LogWarning(string message) { Log($"[color=#{_colors["warning"].ToHtml()}]{message}[/color]"); } public void LogInfo(string message) { Log($"[color=#{_colors["info"].ToHtml()}]{message}[/color]"); } public void Write(string message) { _output.Text += message; } public void ClearConsole() { _output.Text = ""; } public void Exit() { Visible = false; } public override void _Process(double delta) { if (Input.IsActionJustPressed("Enter") && _input.HasFocus() && _input.Text.Length > 0) { var command = _input.Text; _input.Clear(); _output.Text += $"> {command}\n"; _commandsManager.RunCommand(command); } } private Godot.Collections.Dictionary BuildKeywords() { var keywords = new Godot.Collections.Dictionary(); foreach (var commandName in _commandsManager.Commands.Keys) { keywords.Add(commandName, _colors["commandHighlight"].ToHtml()); } return keywords; } }