Dropdown works

This commit is contained in:
Booklordofthedings 2024-10-17 12:37:30 +02:00
parent f6c0049e78
commit 0f12f4359c
4 changed files with 149 additions and 22 deletions

View file

@ -34,7 +34,7 @@ class MainScreen : Screen
AddChild("Toolbar", toAdd); AddChild("Toolbar", toAdd);
AddChild("Button", new Button(new (val) => {Console.WriteLine("Hellau :)");})); AddChild("Button", new Button(new (val) => {Console.WriteLine("Hellau :)");}));
AddChild("dd", new Dropdown(new (val) => {Console.WriteLine("Hellau :)");})); AddChild("dd", new Dropdown("One", "Two", "Three"));
AddChild("Label", new Label("LMAO")); AddChild("Label", new Label("LMAO"));
AddChild("CBox", new Checkbox("Box")); AddChild("CBox", new Checkbox("Box"));
@ -45,6 +45,13 @@ class MainScreen : Screen
AddChild("radio", r); AddChild("radio", r);
AddChild("hslider", new HSlider()); AddChild("hslider", new HSlider());
AddChild("vslider", new VSlider()); AddChild("vslider", new VSlider());
AddChild("lb", new Label("""
Labels can also be used to
contain some larger text since this also
works fine
and without any
issues
"""));
Layout = new (val, width) => Layout = new (val, width) =>
@ -58,6 +65,7 @@ class MainScreen : Screen
val.Component("radio", 5); val.Component("radio", 5);
val.Component("hslider"); val.Component("hslider");
val.Component("vslider"); val.Component("vslider");
val.Component("lb");
val.Linebreak(); val.Linebreak();
val.Component("dd", 5); val.Component("dd", 5);
}; };

View file

@ -1,21 +1,24 @@
namespace TheaterGui.Components; namespace TheaterGui.Components;
using TheaterGui.Components.Internal;
using TheaterGui.Core; using TheaterGui.Core;
using TheaterGui.Core.Structs; using TheaterGui.Core.Structs;
using System; using System;
using System.Collections;
class Dropdown : Component class Dropdown : Component
{ {
private bool _Hovered = false; private bool _Hovered = false;
private bool _AlreadyHovered = false; private bool _AlreadyHovered = false;
private int32[2] _Pos = .();
public String Label ~ delete _; public List<String> Options = new .() ~ DeleteContainerAndItems!(_);
delegate void(TGRuntime rt) Action ~ delete _; public int32 Selected = 0;
public this(delegate void(TGRuntime rt) pAction, StringView pLabel = "Button") public this(params Span<StringView> args)
{ {
Label = new .(pLabel); for(var i in args)
Action = pAction; Options.Add(new .(i));
} }
public override void Update(TheaterGui.Core.TGRuntime rt) public override void Update(TheaterGui.Core.TGRuntime rt)
@ -30,12 +33,13 @@ class Dropdown : Component
public override void Draw(TGRuntime rt, rect rect) public override void Draw(TGRuntime rt, rect rect)
{ {
_Pos = .(rect.x, rect.y + rect.height);
rt.Platform.DrawTexture("Dropdown", rect, rt.Scheme.PrimaryColor); rt.Platform.DrawTexture("Dropdown", rect, rt.Scheme.PrimaryColor);
if(_Hovered) if(_Hovered)
rt.Platform.DrawTexture("Dropdown", rect, rt.Scheme.HoverColor); rt.Platform.DrawTexture("Dropdown", rect, rt.Scheme.HoverColor);
var tsize = rt.Platform.MeasureTextSize(Label, "Font_Normal"); var tsize = rt.Platform.MeasureTextSize(Options[Selected], "Font_Normal");
rt.Platform.DrawText(Label, "Font_Normal", rt.Platform.DrawText(Options[Selected], "Font_Normal",
.((.)(rect.x + 0.5*rect.width - tsize[0]*0.5), .((.)(rect.x + 0.5*rect.width - tsize[0]*0.5),
(.)(rect.y + 0.5*rect.height - tsize[1]*0.5)) (.)(rect.y + 0.5*rect.height - tsize[1]*0.5))
,rt.Scheme.TextColor); ,rt.Scheme.TextColor);
@ -54,15 +58,14 @@ class Dropdown : Component
public override bool OnLeftClick(TGRuntime rt, int32[2] mPos) public override bool OnLeftClick(TGRuntime rt, int32[2] mPos)
{ {
Action.Invoke(rt); var p = new DropdownPopup(Options) {Position = _Pos};
p.[Friend]_Click = new [&] (val) => {
this.Selected = val;
};
rt.OpenPopup(p, "Dropdown");
return true; return true;
} }
public override bool OnScroll(TGRuntime rt, float pOffset) => false; 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;
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;
}
} }

View file

@ -0,0 +1,117 @@
namespace TheaterGui.Components.Internal;
using TheaterGui.Components;
using TheaterGui.Core;
using TheaterGui.Core.Structs;
using System;
using System.Collections;
class DropdownPopup : Popup
{
private int32 _Hovered = -1;
private int32 _AlreadyHovered = -1;
private int32[2] _Size = .();
private delegate void(int32) _Click ~ delete _;
public List<String> Options = new .() ~ DeleteContainerAndItems!(_);
public int32 Selected = 0;
public this(List<String> args) : this()
{
for(var i in args)
Options.Add(new .(i));
}
public override void Update(TGRuntime rt)
{
if(_Hovered != _AlreadyHovered)
{
_Hovered = _AlreadyHovered;
rt.Dirty();
}
_AlreadyHovered = -1;
}
public override void Draw(TGRuntime rt, rect rect)
{
rt.Platform.DrawColorRectOutlined(
.() {x = Position[0], y = Position[1], width = _Size[0], height = _Size[1]}
,rt.Scheme.PrimaryColor, rt.Scheme.AcceptColor);
int32 offsetY = 2;
for(var i in Options)
{
rt.Platform.DrawText(i, "Font_Normal", .(Position[0] + 2, Position[1] + offsetY), rt.Scheme.TextColor);
var size = rt.Platform.MeasureTextSize(i, "Font_Normal");
if(_Hovered == (.)@i.Index)
{
rt.Platform.DrawColorRect(
.() {x = Position[0], y = Position[1] + offsetY - 2, width = _Size[0], height = 4 + size[1]},
rt.Scheme.HoverColor);
}
offsetY += 4 + size[1];
}
}
public override int32[2] Resize(TGRuntime rt, int32 width)
{
int32[2] size = .();
for(var i in Options)
{
size[0] = rt.Platform.MeasureTextureSize("Dropdown")[0];
var tsize = rt.Platform.MeasureTextSize(i, "Font_Normal");
size[1] += tsize[1] + 4; //4 is the padding
}
_Size = size;
return size;
}
public override bool OnHover(TGRuntime rt, int32[2] mPos)
{
if(mPos[0] >= Position[0] && mPos[0] < Position[0] + _Size[0])
{
if(mPos[1] >= Position[1] && mPos[1] < Position[1] + _Size[1])
{
int32[2] offset = .(mPos[0] - Position[0], mPos[1] - Position[1]);
int32 offsetY = 0;
for(var i in Options)
{
var tsize = rt.Platform.MeasureTextSize(i, "Font_Normal");
if(offset[1] >= offsetY && offset[1] < offsetY + tsize[1] + 4)
{
_AlreadyHovered = (.)@i.Index;
return true;
}
offsetY += tsize[1] + 4;
}
}
}
return false;
}
public override bool OnLeftClick(TGRuntime rt, int32[2] mPos)
{
if(mPos[0] >= Position[0] && mPos[0] < Position[0] + _Size[0])
{
if(mPos[1] >= Position[1] && mPos[1] < Position[1] + _Size[1])
{
int32[2] offset = .(mPos[0] - Position[0], mPos[1] - Position[1]);
int32 offsetY = 0;
for(var i in Options)
{
var tsize = rt.Platform.MeasureTextSize(i, "Font_Normal");
if(offset[1] >= offsetY && offset[1] < offsetY + tsize[1] + 4)
{
_Click.Invoke((.)@i.Index);
return false;
}
offsetY += tsize[1] + 4;
}
}
}
return false;
}
}

View file

@ -1,18 +1,17 @@
TODO: TODO:
TabbedContainers TabbedContainers
Fixed size containers Fixed size containers
Collapseables Collapseable
Textfields
Textboxes
TextDisplays
Icon buttons
Code cleanup Code cleanup
Dropdowns render offscreen
Being Worked on: Being Worked on:
Dropdown Textfields
Textboxes
Done: Done:
Radio Buttons Radio Buttons
Horizontal sliders Horizontal sliders
Vertical sliders Vertical sliders
Dropdown