Added simple, modular command parsing
This commit is contained in:
+34
-10
@@ -1,5 +1,6 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using YourProject.Commands; // Add this using statement
|
||||
|
||||
public partial class ConsoleUI : CanvasLayer
|
||||
{
|
||||
@@ -10,27 +11,50 @@ public partial class ConsoleUI : CanvasLayer
|
||||
{
|
||||
consoleOutput = GetNode<Label>("ConsoleOutput");
|
||||
consoleInput = GetNode<LineEdit>("ConsoleInput");
|
||||
|
||||
consoleInput.TextSubmitted += OnTextSubmitted;
|
||||
|
||||
consoleInput.GrabFocus();
|
||||
Callable.From(() => consoleInput.GrabFocus()).CallDeferred();
|
||||
}
|
||||
|
||||
private void OnTextSubmitted(string submittedText)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(submittedText))
|
||||
if (string.IsNullOrWhiteSpace(submittedText))
|
||||
{
|
||||
string currentOutput = consoleOutput.Text;
|
||||
if (!string.IsNullOrEmpty(currentOutput))
|
||||
consoleInput.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
AddToConsole($"> {submittedText}");
|
||||
|
||||
bool commandExecuted = ConsoleManager.Execute(submittedText, (output) =>
|
||||
{
|
||||
if (output == "CLEAR_CONSOLE")
|
||||
{
|
||||
consoleOutput.Text = currentOutput + "\n" + submittedText;
|
||||
consoleOutput.Text = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
consoleOutput.Text = submittedText;
|
||||
AddToConsole(output);
|
||||
}
|
||||
|
||||
consoleInput.Clear();
|
||||
});
|
||||
|
||||
if (!commandExecuted)
|
||||
{
|
||||
AddToConsole($"Unknown command. Type 'help' for available commands.");
|
||||
}
|
||||
|
||||
consoleInput.Clear();
|
||||
Callable.From(() => consoleInput.GrabFocus()).CallDeferred();
|
||||
}
|
||||
|
||||
private void AddToConsole(string text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(consoleOutput.Text))
|
||||
{
|
||||
consoleOutput.Text = text;
|
||||
}
|
||||
else
|
||||
{
|
||||
consoleOutput.Text += "\n" + text;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user