diff --git a/examples/src/Program.bf b/examples/src/Program.bf index 2f15680..ee2f6a8 100644 --- a/examples/src/Program.bf +++ b/examples/src/Program.bf @@ -34,6 +34,7 @@ class MainScreen : Screen AddChild("Toolbar", toAdd); AddChild("Button", new Button(new (val) => {Console.WriteLine("Hellau :)");})); + AddChild("dd", new Dropdown(new (val) => {Console.WriteLine("Hellau :)");})); AddChild("Label", new Label("LMAO")); AddChild("CBox", new Checkbox("Box")); @@ -42,6 +43,9 @@ class MainScreen : Screen r.Label.Add(new .("Two")); r.Label.Add(new .("Three")); AddChild("radio", r); + AddChild("hslider", new HSlider()); + AddChild("vslider", new VSlider()); + Layout = new (val, width) => { @@ -51,7 +55,11 @@ class MainScreen : Screen val.Component("Button"); val.Component("CBox", 5); val.Linebreak(); - val.Component("radio"); + val.Component("radio", 5); + val.Component("hslider"); + val.Component("vslider"); + val.Linebreak(); + val.Component("dd", 5); }; } } diff --git a/src/Components/Button.bf b/src/Components/Button.bf index 4dc5622..dcae7b0 100644 --- a/src/Components/Button.bf +++ b/src/Components/Button.bf @@ -12,10 +12,10 @@ class Button : Component public String Label ~ delete _; delegate void(TGRuntime rt) Action ~ delete _; - public this(delegate void(TGRuntime rt) pToCall, StringView pName = "Button") + public this(delegate void(TGRuntime rt) pAction, StringView pLabel = "Button") { - Action = pToCall; - Label = new .(pName); + Label = new .(pLabel); + Action = pAction; } public override void Update(TheaterGui.Core.TGRuntime rt) diff --git a/src/Components/Dropdown.bf b/src/Components/Dropdown.bf new file mode 100644 index 0000000..1000f61 --- /dev/null +++ b/src/Components/Dropdown.bf @@ -0,0 +1,68 @@ +namespace TheaterGui.Components; +using TheaterGui.Core; +using TheaterGui.Core.Structs; + +using System; + +class Dropdown : Component +{ + private bool _Hovered = false; + private bool _AlreadyHovered = false; + + public String Label ~ delete _; + delegate void(TGRuntime rt) Action ~ delete _; + + public this(delegate void(TGRuntime rt) pAction, StringView pLabel = "Button") + { + Label = new .(pLabel); + Action = pAction; + } + + public override void Update(TheaterGui.Core.TGRuntime rt) + { + if(_Hovered != _AlreadyHovered) + { + _Hovered = _AlreadyHovered; + rt.Dirty(); + } + _AlreadyHovered = false; + } + + public override void Draw(TGRuntime rt, rect rect) + { + rt.Platform.DrawTexture("Dropdown", rect, rt.Scheme.PrimaryColor); + if(_Hovered) + rt.Platform.DrawTexture("Dropdown", rect, rt.Scheme.HoverColor); + + var tsize = rt.Platform.MeasureTextSize(Label, "Font_Normal"); + rt.Platform.DrawText(Label, "Font_Normal", + .((.)(rect.x + 0.5*rect.width - tsize[0]*0.5), + (.)(rect.y + 0.5*rect.height - tsize[1]*0.5)) + ,rt.Scheme.TextColor); + } + + public override int32[2] Resize(TheaterGui.Core.TGRuntime rt, int32 width) + { + return rt.Platform.MeasureTextureSize("Dropdown"); + } + + public override bool OnHover(TGRuntime rt, int32[2] mPos) + { + _AlreadyHovered = true; + return true; + } + + public override bool OnLeftClick(TGRuntime rt, int32[2] mPos) + { + Action.Invoke(rt); + return true; + } + + public override bool OnScroll(TGRuntime rt, float pOffset) => false; + + public override bool OnRightClick(TGRuntime rt, int32[2] mPos, System.Collections.List<(String, delegate void(TGRuntime))> pList) + { + pList.Add((.)(new String(scope $"Click {Label}"), new (rt) => {this.Action.Invoke(rt);})); + return true; + } +} \ No newline at end of file diff --git a/src/Components/HSlider.bf b/src/Components/HSlider.bf new file mode 100644 index 0000000..c74e19d --- /dev/null +++ b/src/Components/HSlider.bf @@ -0,0 +1,75 @@ +namespace TheaterGui.Components; +using TheaterGui.Core; +using TheaterGui.Core.Structs; + +using System; + +class HSlider : Component +{ + private bool _Hovered = false; + private bool _AlreadyHovered = false; + + public float Value = 0; + + public this() + { + } + + public override void Update(TheaterGui.Core.TGRuntime rt) + { + if (_Hovered != _AlreadyHovered) + { + _Hovered = _AlreadyHovered; + rt.Dirty(); + } + _AlreadyHovered = false; + } + + public override void Draw(TGRuntime rt, rect rect) + { + var ts = rt.Platform.MeasureTextureSize("Checkbox"); + + rt.Platform.DrawColorRect(.() { x = rect.x + ts[0], width = rect.width-2*ts[0], y = rect.y + ts[1] / 2 - 4, height = 8 }, rt.Scheme.AcceptColor); + rt.Platform.DrawTexture("Checkbox", .() { x = (.)(rect.x + ts[0] + Value * 10*ts[0]-ts[0]/2), y = rect.y, width = ts[0], height = ts[1] }, rt.Scheme.PrimaryColor); + if (_Hovered) + rt.Platform.DrawTexture("Checkbox", .() { x = (.)(rect.x + ts[0] + Value * 10*ts[0]-ts[0]/2), y = rect.y, width = ts[0], height = ts[1] }, rt.Scheme.HoverColor); + } + + public override int32[2] Resize(TheaterGui.Core.TGRuntime rt, int32 width) + { + var ts = rt.Platform.MeasureTextureSize("Checkbox"); + return .(ts[0] * 12, ts[1]); + } + + public override bool OnHover(TGRuntime rt, int32[2] mPos) + { + _AlreadyHovered = true; + if(rt.Platform.MouseLeftClick()) + { + float oldVal = Value; + OnLeftClick(rt, mPos); + if(Value != oldVal) + rt.Dirty(); + } + return true; + } + + public override bool OnLeftClick(TGRuntime rt, int32[2] mPos) + { + var ts = rt.Platform.MeasureTextureSize("Checkbox"); + if(mPos[0] <= ts[0]) + Value = 0; + else if(mPos[0] >= ts[0] * 11) + Value = 1; + else + { + float ofst = mPos[0] - ts[0]; + Value = Math.Clamp(ofst / (ts[0]*10) + ,0,1); + } + return true; + } + + public override bool OnScroll(TGRuntime rt, float pOffset) => false; + public override bool OnRightClick(TGRuntime rt, int32[2] mPos, System.Collections.List<(String, delegate void(TGRuntime))> pList) => false; +} \ No newline at end of file diff --git a/src/Components/RadioButton.bf b/src/Components/RadioButton.bf index a1327a4..f0ed98d 100644 --- a/src/Components/RadioButton.bf +++ b/src/Components/RadioButton.bf @@ -59,12 +59,12 @@ class RadioButton : Component for(var label in Label) { var tsize = rt.Platform.MeasureTextSize(label, "Font_Normal"); - if(tsize[0] + 5 < xAddition) + if(tsize[0] + 5 > xAddition) xAddition = tsize[0] + 5; - textureSize[1] += rt.Platform.MeasureTextureSize("Checkbox")[1]; } - textureSize[0] += xAddition; + textureSize[0] = textureSize[0] + xAddition; + textureSize[1] = (.)(textureSize[1] * Label.Count); return textureSize; } diff --git a/src/Components/VSlider.bf b/src/Components/VSlider.bf new file mode 100644 index 0000000..de0be46 --- /dev/null +++ b/src/Components/VSlider.bf @@ -0,0 +1,76 @@ +namespace TheaterGui.Components; +using TheaterGui.Core; +using TheaterGui.Core.Structs; + +using System; + +class VSlider : Component +{ + private bool _Hovered = false; + private bool _AlreadyHovered = false; + + public float Value = 0; + + public this() + { + } + + public override void Update(TheaterGui.Core.TGRuntime rt) + { + if (_Hovered != _AlreadyHovered) + { + _Hovered = _AlreadyHovered; + rt.Dirty(); + } + _AlreadyHovered = false; + } + + public override void Draw(TGRuntime rt, rect rect) + { + var ts = rt.Platform.MeasureTextureSize("Checkbox"); + + rt.Platform.DrawColorRect(.() { x = rect.x + ts[0] / 2 - 4, width = 8, y = rect.y + ts[1], height = rect.height-2*ts[1] }, rt.Scheme.AcceptColor); + + rt.Platform.DrawTexture("Checkbox", .() { y = (.)(rect.y + ts[1] + Value * 10*ts[1]-ts[1]/2), x = rect.x, height = ts[1], width = ts[0] }, rt.Scheme.PrimaryColor); + if (_Hovered) + rt.Platform.DrawTexture("Checkbox", .() { y = (.)(rect.y + ts[1] + Value * 10*ts[1]-ts[1]/2), x = rect.x, height = ts[1], width = ts[0] }, rt.Scheme.HoverColor); + } + + public override int32[2] Resize(TheaterGui.Core.TGRuntime rt, int32 width) + { + var ts = rt.Platform.MeasureTextureSize("Checkbox"); + return .(ts[0], ts[1] * 12); + } + + public override bool OnHover(TGRuntime rt, int32[2] mPos) + { + _AlreadyHovered = true; + if(rt.Platform.MouseLeftClick()) + { + float oldVal = Value; + OnLeftClick(rt, mPos); + if(Value != oldVal) + rt.Dirty(); + } + return true; + } + + public override bool OnLeftClick(TGRuntime rt, int32[2] mPos) + { + var ts = rt.Platform.MeasureTextureSize("Checkbox"); + if(mPos[1] <= ts[1]) + Value = 0; + else if(mPos[1] >= ts[1] * 11) + Value = 1; + else + { + float ofst = mPos[1] - ts[1]; + Value = Math.Clamp(ofst / (ts[1]*10) + ,0,1); + } + return true; + } + + public override bool OnScroll(TGRuntime rt, float pOffset) => false; + public override bool OnRightClick(TGRuntime rt, int32[2] mPos, System.Collections.List<(String, delegate void(TGRuntime))> pList) => false; +} \ No newline at end of file diff --git a/src/Core/Structs/theme.bf b/src/Core/Structs/theme.bf index ae45916..38116f5 100644 --- a/src/Core/Structs/theme.bf +++ b/src/Core/Structs/theme.bf @@ -8,6 +8,7 @@ struct theme private static uint8[?] _Theme_Button = Compiler.ReadBinary("assets/button.png"); private static uint8[?] _Theme_Checkbox = Compiler.ReadBinary("assets/checkbox.png"); private static uint8[?] _Theme_Checkbox_Checked = Compiler.ReadBinary("assets/checkbox_checked.png"); + private static uint8[?] _Theme_Dropdown = Compiler.ReadBinary("assets/dropdown.png"); /* private static uint8[?] Texture_SquareButton = Compiler.ReadBinary("assets/square_button.png"); @@ -15,11 +16,12 @@ struct theme private static uint8[?] Texture_NButton = Compiler.ReadBinary("assets/n_button.png"); private static uint8[?] Horizontal_Patch = Compiler.ReadBinary("assets/horizontal_patch.png"); - private static uint8[?] Dropdown = Compiler.ReadBinary("assets/dropdown.png"); */ public (Span, uint8) Font_Normal = (.(&_Theme_Font, _Theme_Font.Count),16); public Span Button = .(&_Theme_Button, _Theme_Button.Count); public Span Checkbox = .(&_Theme_Checkbox, _Theme_Checkbox.Count); public Span Checkbox_Checked = .(&_Theme_Checkbox_Checked, _Theme_Checkbox_Checked.Count); + public Span Dropdown = .(&_Theme_Dropdown, _Theme_Dropdown.Count); + } \ No newline at end of file diff --git a/src/Core/TGRuntime.core.bf b/src/Core/TGRuntime.core.bf index 2a2a081..e0358dd 100644 --- a/src/Core/TGRuntime.core.bf +++ b/src/Core/TGRuntime.core.bf @@ -120,6 +120,7 @@ class TGRuntime Platform.LoadFont(pStartingTheme.Font_Normal.0, "Font_Normal", pStartingTheme.Font_Normal.1); Platform.LoadTexture(pStartingTheme.Button, "Button"); Platform.LoadTexture(pStartingTheme.Checkbox, "Checkbox"); + Platform.LoadTexture(pStartingTheme.Dropdown, "Dropdown"); Platform.LoadTexture(pStartingTheme.Checkbox_Checked, "Checkbox_Checked"); } diff --git a/src/TODO b/src/TODO index bb05c1c..3670021 100644 --- a/src/TODO +++ b/src/TODO @@ -2,17 +2,17 @@ TODO: TabbedContainers Fixed size containers Collapseables -Dropdown Textfields Textboxes TextDisplays Icon buttons -Horizontal sliders -Vertical sliders Code cleanup Being Worked on: +Dropdown -Done -Radio Buttons \ No newline at end of file +Done: +Radio Buttons +Horizontal sliders +Vertical sliders \ No newline at end of file