Cogwheel integration, more code structure

This commit is contained in:
2025-02-15 01:31:26 -06:00
parent 5ff3525a5e
commit 2ecf7ec93f
22 changed files with 206 additions and 47 deletions

View File

@@ -0,0 +1,54 @@
[gd_scene load_steps=2 format=3 uid="uid://caivf0icbv7gk"]
[ext_resource type="Script" uid="uid://cgn58uiwyog25" path="res://Core/Scripts/Cogwheel/GodotCogwheelConsole.cs" id="1_23urx"]
[node name="CogwheelConsole" type="Control" node_paths=PackedStringArray("_input", "_output")]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_23urx")
_input = NodePath("VBoxContainer/Input")
_output = NodePath("VBoxContainer/Panel/MarginContainer/Output")
[node name="VBoxContainer" type="VBoxContainer" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="Panel" type="Panel" parent="VBoxContainer"]
layout_mode = 2
size_flags_vertical = 3
[node name="MarginContainer" type="MarginContainer" parent="VBoxContainer/Panel"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme_override_constants/margin_left = 10
theme_override_constants/margin_top = 10
theme_override_constants/margin_right = 10
theme_override_constants/margin_bottom = 10
[node name="Output" type="RichTextLabel" parent="VBoxContainer/Panel/MarginContainer"]
layout_mode = 2
focus_mode = 2
bbcode_enabled = true
scroll_following = true
selection_enabled = true
[node name="Input" type="TextEdit" parent="VBoxContainer"]
custom_minimum_size = Vector2(0, 30)
layout_mode = 2
placeholder_text = "> Enter Command"
wrap_mode = 1
autowrap_mode = 1
scroll_fit_content_height = true
scroll_fit_content_width = true

View File

@@ -1,6 +1,9 @@
[gd_scene load_steps=2 format=3 uid="uid://udmowtmx1o3l"]
[gd_scene load_steps=3 format=3 uid="uid://udmowtmx1o3l"]
[ext_resource type="Script" uid="uid://bl2h8ot302x3g" path="res://Core/Scripts/MainNode.cs" id="1_wh72m"]
[ext_resource type="Script" uid="uid://dnmj6a1ptvwry" path="res://Core/Scripts/Main.cs" id="1_0ovck"]
[ext_resource type="PackedScene" uid="uid://c35ohffpqfqnr" path="res://Client/Scenes/MainCanvas.tscn" id="2_0ovck"]
[node name="Main" type="Node"]
script = ExtResource("1_wh72m")
script = ExtResource("1_0ovck")
[node name="MainCanvas" parent="." instance=ExtResource("2_0ovck")]

View File

@@ -7,16 +7,10 @@ namespace ADEPT.Core;
public static class ADEPT
{
private static CommandsManager _commandsManager = new CommandsManager();
private static ICogwheelConsole _cogwheelConsole = new GodotCogwheelConsole();
public static Main MainNode { get; private set; }
public static MainNode MainNode { get; private set; }
public static void Initialize(MainNode mainNode)
public static void Initialize(Main mainNode)
{
COGWHEEL.Initialize(_commandsManager, _cogwheelConsole);
COGWHEEL.AddAssembly(Assembly.GetCallingAssembly());
MainNode = mainNode;
}

View File

@@ -0,0 +1 @@
uid://b8cf4qnyf2hyu

View File

@@ -1,45 +1,103 @@
using System.Collections.Generic;
using System.Reflection;
using Cogwheel;
using Godot;
namespace ADEPT.Core.Cogwheel;
public class GodotCogwheelConsole : ICogwheelConsole
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<string, Color> _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 string OpeningMessage { get; set; }
public bool IsRunning { get; set; }
public CommandsManager CommandsManager { get; set; }
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;
}
}

View File

@@ -0,0 +1 @@
uid://cgn58uiwyog25

View File

@@ -0,0 +1 @@
uid://casaduku5g3p0

View File

@@ -0,0 +1 @@
uid://bcf583puvlvt1

View File

@@ -0,0 +1 @@
uid://n46c56nkxhni

View File

@@ -0,0 +1 @@
uid://clgpj02pia5d2

View File

@@ -0,0 +1 @@
uid://cwhhuavq5ximh

View File

@@ -0,0 +1 @@
uid://dy3wuh6e6yjda

View File

@@ -0,0 +1 @@
uid://b8aqy8n1dyd7c

View File

@@ -0,0 +1 @@
uid://dfasrsmo4r2oi

View File

@@ -0,0 +1,42 @@
using System.Reflection;
using Godot;
using ADEPT.Core;
using ADEPT.Core.Cogwheel;
using Cogwheel;
namespace ADEPT.Core;
public partial class Main : Node
{
private static CommandsManager _commandsManager;
private static ICogwheelConsole _cogwheelConsole;
public CanvasLayer MainCanvas { get; private set; }
public override void _Ready()
{
MainCanvas = GetNode<CanvasLayer>("MainCanvas");
ADEPT.Initialize(this);
_commandsManager = new CommandsManager();
_cogwheelConsole = MainCanvas.GetNode<GodotCogwheelConsole>("CogwheelConsole");
if (_cogwheelConsole is null) throw new System.Exception("CogwheelConsole not found in MainNode.");
_commandsManager.AddAssembly(Assembly.GetExecutingAssembly());
_commandsManager.AddAssembly(Assembly.GetAssembly(typeof(CommandsManager)));
COGWHEEL.Initialize(_commandsManager, _cogwheelConsole);
GD.Print("ADEPT initialized.");
COGWHEEL.RegisterObject(this);
}
[Command(Name = "test", Description = "Test command.")]
private void Test()
{
COGWHEEL.LogWarning("Testing!\nTesting!\nTesting!");
}
}

View File

@@ -0,0 +1 @@
uid://dnmj6a1ptvwry

View File

@@ -4,8 +4,5 @@ namespace ADEPT.Core;
public partial class MainNode : Node
{
public override void _Ready()
{
ADEPT.Initialize(this);
}
}