Changed Label to RichtTextLabel for scrolling compatabillity

This commit is contained in:
Vico 2026-04-04 20:54:52 +02:00
parent 20158802dd
commit 22108bb58d
9 changed files with 244 additions and 41 deletions

View File

@ -1,7 +1,7 @@
using System;
using YourProject.Commands;
using TextRPG.Commands;
namespace YourProject.Commands
namespace TextRPG.Commands
{
public class ClearCommand : Command
{

View File

@ -1,6 +1,6 @@
using System;
namespace YourProject.Commands // Add namespace
namespace TextRPG.Commands
{
public abstract class Command
{

View File

@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using YourProject.Commands;
using TextRPG.Commands;
namespace YourProject.Commands
namespace TextRPG.Commands
{
public static class ConsoleManager
{
@ -14,6 +14,8 @@ namespace YourProject.Commands
// Register all commands
RegisterCommand(new HelpCommand());
RegisterCommand(new ClearCommand());
RegisterCommand(new TimeCommand());
}
public static void RegisterCommand(Command command)

View File

@ -1,7 +1,7 @@
using System;
using YourProject.Commands; // Reference the namespace
using TextRPG.Commands;
namespace YourProject.Commands
namespace TextRPG.Commands
{
public class HelpCommand : Command
{

View File

@ -0,0 +1,18 @@
using System;
using TextRPG.Commands;
namespace TextRPG.Commands
{
public class TimeCommand : Command
{
public override string Name => "time";
public override string Description => "Outputs the current time";
public override string Usage => "time";
public override void Execute(string[] args, Action<string> outputCallback)
{
// Send a special marker that the console UI will recognize
outputCallback("12:23");
}
}
}

View File

@ -1,20 +1,113 @@
using Godot;
using System;
using YourProject.Commands; // Add this using statement
using System.Collections.Generic;
using TextRPG.Commands; // Add this to access ConsoleManager
public partial class ConsoleUI : CanvasLayer
{
private Label consoleOutput;
// UI Elements
private RichTextLabel consoleOutput;
private LineEdit consoleInput;
// Command History
private List<string> commandHistory = new List<string>();
private int historyIndex = -1;
public override void _Ready()
{
consoleOutput = GetNode<Label>("ConsoleOutput");
consoleInput = GetNode<LineEdit>("ConsoleInput");
// Get UI references
consoleOutput = FindChild("ConsoleOutput", true, false) as RichTextLabel;
consoleInput = FindChild("ConsoleInput", true, false) as LineEdit;
if (consoleOutput == null || consoleInput == null)
{
GD.PrintErr("Failed to find console nodes!");
return;
}
// DON'T use SetAnchorsAndOffsetsPreset with containers
// Instead, let the containers work naturally
// Configure the VBoxContainer to expand and fill
var vbox = consoleOutput.GetParent() as VBoxContainer;
if (vbox != null)
{
vbox.SizeFlagsVertical = Control.SizeFlags.ExpandFill;
vbox.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
vbox.AnchorRight = 1;
vbox.AnchorBottom = 1;
}
// Make the console output expand to fill available space
consoleOutput.SizeFlagsVertical = Control.SizeFlags.ExpandFill;
consoleOutput.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
// Set the LineEdit to not expand vertically (fixed height)
consoleInput.SizeFlagsVertical = Control.SizeFlags.ShrinkCenter;
consoleInput.CustomMinimumSize = new Vector2(0, 35);
// Configure the console
SetupConsole();
// Connect signals
consoleInput.TextSubmitted += OnTextSubmitted;
consoleInput.GuiInput += OnGuiInput;
// Set initial focus
Callable.From(() => consoleInput.GrabFocus()).CallDeferred();
// Display welcome message
DisplayWelcomeMessage();
}
private void SetupConsole()
{
// Enable BBCode
consoleOutput.BbcodeEnabled = true;
// CRITICAL FOR NEWLINES AND WRAPPING:
consoleOutput.AutowrapMode = TextServer.AutowrapMode.Word;
consoleOutput.SizeFlagsVertical = Control.SizeFlags.ExpandFill;
// Make sure newlines are preserved
consoleOutput.ScrollFollowing = true;
consoleOutput.ScrollActive = true;
consoleOutput.SelectionEnabled = true;
consoleOutput.FitContent = false; // Keep false - this is important!
// Styling
var styleBox = new StyleBoxFlat();
styleBox.BgColor = new Color(0.08f, 0.08f, 0.12f);
styleBox.SetCornerRadiusAll(5);
consoleOutput.AddThemeStyleboxOverride("normal", styleBox);
consoleOutput.AddThemeColorOverride("default_color", new Color(0.8f, 0.8f, 0.9f));
var lineEditStyle = new StyleBoxFlat();
lineEditStyle.BgColor = new Color(0.05f, 0.05f, 0.08f);
lineEditStyle.SetCornerRadiusAll(5);
consoleInput.AddThemeStyleboxOverride("normal", lineEditStyle);
consoleInput.AddThemeColorOverride("font_color", new Color(0.9f, 0.9f, 1.0f));
consoleInput.AddThemeColorOverride("caret_color", new Color(0.9f, 0.9f, 1.0f));
}
private void PrintFullTree(Node node, int depth)
{
string indent = new string(' ', depth * 2);
GD.Print($"{indent}{node.Name} ({node.GetType().Name}) - Path: {node.GetPath()}");
foreach (Node child in node.GetChildren())
{
PrintFullTree(child, depth + 1);
}
}
private void DisplayWelcomeMessage()
{
AddToConsole("Welcome!");
AddToConsole("Test");
}
private void OnTextSubmitted(string submittedText)
{
if (string.IsNullOrWhiteSpace(submittedText))
@ -23,13 +116,19 @@ public partial class ConsoleUI : CanvasLayer
return;
}
AddToConsole($"> {submittedText}");
// Add to command history
commandHistory.Add(submittedText);
historyIndex = commandHistory.Count;
// Echo the command
AddToConsole($"[color=yellow]>>[/color] {submittedText}");
// Execute the command using ConsoleManager
bool commandExecuted = ConsoleManager.Execute(submittedText, (output) =>
{
if (output == "CLEAR_CONSOLE")
{
consoleOutput.Text = "";
{
consoleOutput.Clear();
}
else
{
@ -39,22 +138,67 @@ public partial class ConsoleUI : CanvasLayer
if (!commandExecuted)
{
AddToConsole($"Unknown command. Type 'help' for available commands.");
AddToConsole($"[color=red]Unknown command.[/color] Type 'help' for available commands.");
}
consoleInput.Clear();
Callable.From(() => consoleInput.GrabFocus()).CallDeferred();
}
private void AddToConsole(string text)
private void OnGuiInput(InputEvent @event)
{
if (string.IsNullOrEmpty(consoleOutput.Text))
if (@event is InputEventKey key && key.Pressed)
{
consoleOutput.Text = text;
}
else
{
consoleOutput.Text += "\n" + text;
if (key.Keycode == Key.Up)
{
if (commandHistory.Count > 0 && historyIndex > 0)
{
historyIndex--;
consoleInput.Text = commandHistory[historyIndex];
consoleInput.CaretColumn = consoleInput.Text.Length;
}
GetViewport().SetInputAsHandled();
}
else if (key.Keycode == Key.Down)
{
if (historyIndex < commandHistory.Count - 1)
{
historyIndex++;
consoleInput.Text = commandHistory[historyIndex];
consoleInput.CaretColumn = consoleInput.Text.Length;
}
else if (historyIndex == commandHistory.Count - 1)
{
historyIndex = commandHistory.Count;
consoleInput.Text = "";
}
GetViewport().SetInputAsHandled();
}
}
}
private void AddToConsole(string text)
{
// Force a newline before adding if console already has text
if (!string.IsNullOrEmpty(consoleOutput.Text))
{
consoleOutput.AppendText("\n");
}
// Add the text
consoleOutput.AppendText(text + "\n");
// Force scroll to bottom
consoleOutput.ScrollToLine(consoleOutput.GetLineCount() - 1);
}
public void LogMessage(string message, string color = "white")
{
AddToConsole($"[color={color}]{message}[/color]");
}
public void ClearConsole()
{
consoleOutput.Clear();
}
}

30
scenes/TestConsole.cs Normal file
View File

@ -0,0 +1,30 @@
using Godot;
using System;
public partial class TestConsole : CanvasLayer
{
public override void _Ready()
{
var output = GetNode<RichTextLabel>("/root/Playground/Console/Panel/MarginContainer/VBoxContainer/ConsoleOutput");
var panel = GetNode<Panel>("/root/Playground/Console/Panel");
panel.Visible = true;
if (output != null)
{
GD.Print($"Visible: {output.Visible}");
GD.Print($"Modulate: {output.Modulate}");
GD.Print($"SelfModulate: {output.SelfModulate}");
GD.Print($"Size: {output.Size}");
GD.Print($"Position: {output.Position}");
// Force a minimum size
output.SizeFlagsVertical = Control.SizeFlags.ExpandFill;
output.Text = "TEST WORKING!";
// Also try changing background to bright red to see if it exists
var styleBox = new StyleBoxFlat();
styleBox.BgColor = Colors.Red;
output.AddThemeStyleboxOverride("normal", styleBox);
}
}
}

View File

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

View File

@ -4,29 +4,37 @@
[ext_resource type="FontFile" uid="uid://df205wg7tq2a1" path="res://assets/fonts/Orange Kid.otf" id="2_xil4p"]
[node name="Console" type="CanvasLayer" unique_id=29250469]
layer = 10
script = ExtResource("1_gf67r")
[node name="ConsoleBG" type="ColorRect" parent="." unique_id=325200379]
offset_right = 872.0
offset_bottom = 575.0
color = Color(0.446777, 0.44677705, 0.44677705, 1)
[node name="ConsoleOutput" type="Label" parent="." unique_id=340259312]
offset_left = 18.0
offset_top = 14.0
offset_right = 857.0
offset_bottom = 560.0
theme_override_fonts/font = ExtResource("2_xil4p")
theme_override_font_sizes/font_size = 35
[node name="ConsoleInput" type="LineEdit" parent="." unique_id=1233554082]
anchors_preset = 12
anchor_top = 1.0
[node name="Panel" type="Panel" parent="." unique_id=1612205637]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_top = -70.0
grow_horizontal = 2
grow_vertical = 0
grow_vertical = 2
[node name="MarginContainer" type="MarginContainer" parent="Panel" unique_id=800285170]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme_override_constants/margin_right = 300
[node name="VBoxContainer" type="VBoxContainer" parent="Panel/MarginContainer" unique_id=1714535234]
layout_mode = 2
theme_override_constants/separation = 5
[node name="ConsoleOutput" type="RichTextLabel" parent="Panel/MarginContainer/VBoxContainer" unique_id=1029492678]
layout_mode = 2
bbcode_enabled = true
scroll_following = true
[node name="ConsoleInput" type="LineEdit" parent="Panel/MarginContainer/VBoxContainer" unique_id=1233554082]
layout_mode = 2
size_flags_vertical = 10
theme_override_fonts/font = ExtResource("2_xil4p")
theme_override_font_sizes/font_size = 30
keep_editing_on_text_submit = true