using System.Collections.Generic; using System.Diagnostics; using System.Linq; using Godot; using Godot.Collections; namespace Texty.Scripts.Terminal; public partial class OutputArea : ScrollContainer { [Export(PropertyHint.File, "*.tscn")] public PackedScene OutputBlockScene; public Array TextBlocks => new(BlockNodes.ToList().Select(block => block.Content)); private VBoxContainer LineContainer => GetNodeOrNull($"%{nameof(LineContainer)}"); private IEnumerable BlockNodes => GD.Range(LineContainer?.GetChildCount() ?? 0) .Select(index => LineContainer.GetChild(index)) .Cast(); public override void _Ready() { Debug.Assert(OutputBlockScene != null, "OutputBlockScene has not been configured!"); Debug.Assert(OutputBlockScene.CanInstantiate(), "OutputBlockScene can not be instanced!"); Clear(); } public void Clear() { BlockNodes.ToList().ForEach(block => { LineContainer.RemoveChild(block); block.QueueFree(); }); } public void Push(string text) { if (string.IsNullOrEmpty(text)) return; var block = OutputBlockScene.Instantiate(); block.Content = text; LineContainer.AddChild(block); } }