diff options
| author | Sophia Pearson <codergal89@gmail.com> | 2022-05-20 00:45:25 +0200 |
|---|---|---|
| committer | Sophia Pearson <codergal89@gmail.com> | 2022-05-20 18:56:04 +0200 |
| commit | 05d29ccce1898ed89c0b650c77242c2fa2805128 (patch) | |
| tree | e8ee3bcb570fa6f3d9d96273c2bf4d4c8618d08b /Scripts | |
| download | texty-05d29ccce1898ed89c0b650c77242c2fa2805128.tar.xz texty-05d29ccce1898ed89c0b650c77242c2fa2805128.zip | |
texty: initial commit
Diffstat (limited to 'Scripts')
| -rw-r--r-- | Scripts/Game.cs | 30 | ||||
| -rw-r--r-- | Scripts/InputContainer.cs | 44 | ||||
| -rw-r--r-- | Scripts/OutputContainer.cs | 53 | ||||
| -rw-r--r-- | Scripts/OutputRow.cs | 72 |
4 files changed, 199 insertions, 0 deletions
diff --git a/Scripts/Game.cs b/Scripts/Game.cs new file mode 100644 index 0000000..645c922 --- /dev/null +++ b/Scripts/Game.cs @@ -0,0 +1,30 @@ +using System.Diagnostics; +using Godot; + +namespace Texty.Scripts +{ + public abstract class Game : MarginContainer + { + public VBoxContainer LayoutContainer; + public OutputContainer OutputContainer; + + [Export(PropertyHint.File, "*.tscn")] public PackedScene OutputRowScene; + + public override void _Ready() + { + Debug.Assert(OutputRowScene != null, $"The {nameof(OutputRowScene)} was not set!"); + + LayoutContainer = GetNode<VBoxContainer>(nameof(LayoutContainer)); + OutputContainer = LayoutContainer.GetNode<OutputContainer>(nameof(OutputContainer)); + } + + public void OnInputSubmitted(string text) + { + var newRow = OutputRowScene.Instance<OutputRow>(); + newRow.InputText = text; + newRow.OutputText = "THE OUTPUT SHOULD GO HERE!"; + + OutputContainer.Add(newRow); + } + } +}
\ No newline at end of file diff --git a/Scripts/InputContainer.cs b/Scripts/InputContainer.cs new file mode 100644 index 0000000..485086e --- /dev/null +++ b/Scripts/InputContainer.cs @@ -0,0 +1,44 @@ +using Godot; + +namespace Texty.Scripts +{ + public abstract class InputContainer : HBoxContainer + { + [Signal] + public delegate void InputSubmitted(string text); + + public LineEdit InputField; + public Button SubmitButton; + + public override void _Ready() + { + InputField = GetNode<LineEdit>(nameof(InputField)); + SubmitButton = GetNode<Button>(nameof(SubmitButton)); + + InputField.GrabFocus(); + SubmitButton.Disabled = InputField.Text.Empty(); + } + + public void OnInputChanged(string newText) + { + SubmitButton.Disabled = newText.Empty(); + } + + public void OnInputSubmitted(string newText) + { + SubmitText(newText); + } + + public void OnInputSubmitted() + { + SubmitText(InputField.Text); + } + + private void SubmitText(string newText) + { + if (newText.Empty()) return; + EmitSignal(nameof(InputSubmitted), newText); + InputField.Clear(); + } + } +}
\ No newline at end of file diff --git a/Scripts/OutputContainer.cs b/Scripts/OutputContainer.cs new file mode 100644 index 0000000..d5a5bea --- /dev/null +++ b/Scripts/OutputContainer.cs @@ -0,0 +1,53 @@ +using System.Linq; +using Godot; + +namespace Texty.Scripts +{ + public abstract class OutputContainer : PanelContainer + { + private double _lastScrollLimit; + + private VScrollBar _scrollBar; + + [Export(PropertyHint.Range, "10,40,1")] + public int MaximumRows = 20; + + public VBoxContainer OutputRows; + public ScrollContainer ScrollContainer; + + public override void _Ready() + { + ScrollContainer = GetNode<ScrollContainer>(nameof(ScrollContainer)); + _scrollBar = ScrollContainer.GetVScrollbar(); + OutputRows = ScrollContainer.GetNode<VBoxContainer>(nameof(OutputRows)); + + _lastScrollLimit = _scrollBar.MaxValue; + _scrollBar.Connect("changed", this, nameof(OnScrollLimitChanged)); + } + + public void Add(OutputRow row) + { + OutputRows.AddChild(row); + ExpungeOldRows(); + } + + public void OnScrollLimitChanged() + { + if (!(_lastScrollLimit < _scrollBar.MaxValue)) return; + _lastScrollLimit = _scrollBar.MaxValue; + ScrollContainer.ScrollVertical = (int)_scrollBar.MaxValue; + } + + private void ExpungeOldRows() + { + GD.Range(OutputRows.GetChildCount() - MaximumRows) + .Select(idx => OutputRows.GetChild(idx)) + .ToList() + .ForEach(row => + { + OutputRows.RemoveChild(row); + row.QueueFree(); + }); + } + } +}
\ No newline at end of file diff --git a/Scripts/OutputRow.cs b/Scripts/OutputRow.cs new file mode 100644 index 0000000..15a6454 --- /dev/null +++ b/Scripts/OutputRow.cs @@ -0,0 +1,72 @@ +using System; +using Godot; + +namespace Texty.Scripts +{ + public abstract class OutputRow : VBoxContainer + { + [Signal] + public delegate void InputTextChanged(string newText); + + [Signal] + public delegate void OutputTextChanged(string newText); + + public Label Input; + public Label Output; + + private string _inputText = ""; + private string _outputText = ""; + + [Export] + public string InputText + { + get => _inputText; + set => UpdateText(Field.Input, value ?? ""); + } + + [Export] + public string OutputText + { + get => _outputText; + set => UpdateText(Field.Output, value ?? ""); + } + + public override void _Ready() + { + Input = GetNode<Label>(nameof(Input)); + UpdateLabel(Input, _inputText); + Output = GetNode<Label>(nameof(Output)); + UpdateLabel(Output, _outputText); + } + + private void UpdateText(Field field, string value) + { + switch (field) + { + case Field.Input: + _inputText = value; + EmitSignal(nameof(InputTextChanged), _inputText); + UpdateLabel(Input, value); + break; + case Field.Output: + _outputText = value; + EmitSignal(nameof(OutputTextChanged), value); + UpdateLabel(Output, value); + break; + default: + throw new ArgumentOutOfRangeException(nameof(field), field, "No such field!"); + } + } + + private static void UpdateLabel(Label label, string value) + { + if (label != null) label.Text = value; + } + + private enum Field + { + Input, + Output + } + } +}
\ No newline at end of file |
