From 3fa0920e77a736094452ff9fc7afddbee563c2dd Mon Sep 17 00:00:00 2001 From: Booklordofthedings Date: Sun, 7 Jul 2024 00:19:31 +0200 Subject: [PATCH] parts of the input field --- BeefProj.toml | 2 +- src/App.bf | 4 ++ src/Components/Component.bf | 2 +- src/Components/Container.bf | 3 +- src/Components/InputField.bf | 72 ++++++++++++++++++++++++++++++++++++ src/Extensions.bf | 10 ++++- 6 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 src/Components/InputField.bf diff --git a/BeefProj.toml b/BeefProj.toml index 2bb6413..58f3911 100644 --- a/BeefProj.toml +++ b/BeefProj.toml @@ -1,5 +1,5 @@ FileVersion = 1 -Dependencies = {corlib = "*", raylib-beef = "*"} +Dependencies = {corlib = "*", corlib = "*", raylib-beef = "*"} [Project] Name = "TheaterGui" diff --git a/src/App.bf b/src/App.bf index c6dee70..139b48d 100644 --- a/src/App.bf +++ b/src/App.bf @@ -42,6 +42,7 @@ class App if (_CurrentScreen != null) delete _CurrentScreen; _CurrentScreen = value; + _CurrentScreen.Reorder(Width); } }; @@ -187,6 +188,9 @@ class App Input.HandleRightClick(); else Input.HandleHover(); + + if(Selected != null) + Selected.WhileSelected(); } diff --git a/src/Components/Component.bf b/src/Components/Component.bf index 1f25871..a3618d8 100644 --- a/src/Components/Component.bf +++ b/src/Components/Component.bf @@ -7,7 +7,7 @@ abstract class Component { public this(StringView pComponentName, StringView pLabel) { - _ComponentType.Append(pComponentName); + ComponentType = pComponentName; Label = pLabel; } diff --git a/src/Components/Container.bf b/src/Components/Container.bf index 3f1c799..04d51ff 100644 --- a/src/Components/Container.bf +++ b/src/Components/Container.bf @@ -11,9 +11,10 @@ class Container : Component } private List _LayoutData = new .() ~ delete _; + public List Children = new .() ~ DeleteContainerAndItems!(_); - + ///Recalculate the layout of all entries public virtual void Reorder(int32 w) diff --git a/src/Components/InputField.bf b/src/Components/InputField.bf new file mode 100644 index 0000000..8900f78 --- /dev/null +++ b/src/Components/InputField.bf @@ -0,0 +1,72 @@ +namespace TheaterGui.Components; + +using System; + +using RaylibBeef; + +class InputField : Component +{ + public NPatchInfo PatchInfo; + public String InputContent = new .() ~ delete _; + + + String text = new .() ~ delete _; + public this(StringView pLabel) : base("InputField", pLabel) + { + Label = "Test Data"; + Sprite = App.Textures.GetAsset("n_button"); + Width = 128 * 2; + Height = 32; + Margin = 10; + Tint = Theme.Background; + PatchInfo = .(Sprite.SourceRect, 7, 7, 7, 7, (.)NPatchLayout.NPATCH_NINE_PATCH); + } + public override void Render(int32 soy) + { + Raylib.DrawTextureNPatch( + *Sprite.Source, + PatchInfo, + Rectangle(X, Y + soy, Width, Height), + Vector2(0, 0), + 0, + Tint); + + if (_IsHovered || App.Selected == this) + Raylib.DrawTextureNPatch(*Sprite.Source, PatchInfo, Rectangle(X, Y + soy, Width, Height), Vector2(0, 0), 0, Theme.Tint.SetAlpha(120)); + _IsHovered = false; + + var measure = Raylib.MeasureTextEx(Theme.Font, text, Theme.FontSize, 0); + Raylib.DrawTextEx(Theme.Font, text, Vector2(X + 7, Y + Height / 2 - measure.y / 2 + soy), Theme.FontSize, 0, Theme.Text); + } + + protected bool _IsHovered = false; + public override Component OnHover(int32 x, int32 y) + { + _IsHovered = true; + return this; + } + + public override bool OnClick(int32 x, int32 y) + { + App.Selected = this; + return true; + } + + public override void WhileSelected() + { + int key = Raylib.GetCharPressed(); + while (key > 0) + { + // NOTE: Only allow keys in range [32..125] + if ((key >= 32) && (key <= 125) && (text.Length < 100)) + text.Append((char8)key); + + key = Raylib.GetCharPressed(); // Check next character in the queue + } + + if (Raylib.IsKeyPressed((.)KeyboardKey.KEY_BACKSPACE)) + text.RemoveFromEnd(1); + if(Raylib.IsKeyPressed((.)KeyboardKey.KEY_ESCAPE)) + App.Selected = null; + } +} \ No newline at end of file diff --git a/src/Extensions.bf b/src/Extensions.bf index eeee49a..67fd0fc 100644 --- a/src/Extensions.bf +++ b/src/Extensions.bf @@ -10,6 +10,14 @@ namespace RaylibBeef return false; } } + + extension Color + { + public Color SetRed(uint8 red) => .(red, g, b, a); + public Color SetGreen(uint8 green) => .(r, green, b, a); + public Color SetBlue(uint8 blue) => .(r, g, blue, a); + public Color SetAlpha(uint8 alpha) => .(r, g, b, alpha); + } } namespace System @@ -25,7 +33,7 @@ namespace System Compiler.EmitTypeBody(fieldInfo.DeclaringType, scope $""" public StringView {fieldInfo.Name.Substring(1)} {{ - public get => {fieldInfo.Name}; + public get => {fieldInfo.Name} == null ? "" : {fieldInfo.Name}..EnsureNullTerminator(); public set => String.NewOrSet!({fieldInfo.Name}, value); }}; """);