diff options
| author | Sophia Pearson <codergal89@gmail.com> | 2022-09-05 20:35:53 +0200 |
|---|---|---|
| committer | Sophia Pearson <codergal89@gmail.com> | 2022-09-05 20:35:53 +0200 |
| commit | f20bd89dc4a7bf14a88b1effcaa1887b29314525 (patch) | |
| tree | d114787f68efd2a7d61d95fa9c84e8e5d69a7c11 /Scripts/Terminal/OutputArea.cs | |
| parent | 1b477b62f8be8c546a35dbd1d2688ebf623c496f (diff) | |
| download | texty-f20bd89dc4a7bf14a88b1effcaa1887b29314525.tar.xz texty-f20bd89dc4a7bf14a88b1effcaa1887b29314525.zip | |
gui: split GUI into Terminal components
Diffstat (limited to 'Scripts/Terminal/OutputArea.cs')
| -rw-r--r-- | Scripts/Terminal/OutputArea.cs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/Scripts/Terminal/OutputArea.cs b/Scripts/Terminal/OutputArea.cs new file mode 100644 index 0000000..6de4ac1 --- /dev/null +++ b/Scripts/Terminal/OutputArea.cs @@ -0,0 +1,46 @@ +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using Godot; +using Godot.Collections; +using Texty.Scripts.Game; + +namespace Texty.Scripts.Terminal +{ + public class OutputArea : ScrollContainer + { + [Export(PropertyHint.File, "*.tscn")] public PackedScene OutputBlockScene; + public Array<string> TextBlocks => new Array<string>(BlockNodes.ToList().Select(block => block.Content)); + + private VBoxContainer LineContainer => GetNodeOrNull<VBoxContainer>($"%{nameof(LineContainer)}"); + + private IEnumerable<OutputBlock> BlockNodes => + GD.Range(LineContainer?.GetChildCount() ?? 0) + .Select(index => LineContainer.GetChild(index)) + .Cast<OutputBlock>(); + + public override void _Ready() + { + Debug.Assert(OutputBlockScene != null, "OutputBlockScene has not been configured!"); + Debug.Assert(OutputBlockScene.CanInstance(), "OutputBlockScene can not be instanced!"); + Clear(); + } + + public void Clear() + { + BlockNodes.ToList().ForEach(block => + { + LineContainer.RemoveChild(block); + block.QueueFree(); + }); + } + + public void Push(string text) + { + if (text.Empty()) return; + var block = OutputBlockScene.Instance<OutputBlock>(); + block.Content = text; + LineContainer.AddChild(block); + } + } +}
\ No newline at end of file |
