Started dropdown and added sliders

This commit is contained in:
Booklordofthedings 2024-10-16 13:58:58 +02:00
parent 9e7330d47c
commit f6c0049e78
9 changed files with 243 additions and 13 deletions

View file

@ -34,6 +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("Label", new Label("LMAO")); AddChild("Label", new Label("LMAO"));
AddChild("CBox", new Checkbox("Box")); AddChild("CBox", new Checkbox("Box"));
@ -42,6 +43,9 @@ class MainScreen : Screen
r.Label.Add(new .("Two")); r.Label.Add(new .("Two"));
r.Label.Add(new .("Three")); r.Label.Add(new .("Three"));
AddChild("radio", r); AddChild("radio", r);
AddChild("hslider", new HSlider());
AddChild("vslider", new VSlider());
Layout = new (val, width) => Layout = new (val, width) =>
{ {
@ -51,7 +55,11 @@ class MainScreen : Screen
val.Component("Button"); val.Component("Button");
val.Component("CBox", 5); val.Component("CBox", 5);
val.Linebreak(); val.Linebreak();
val.Component("radio"); val.Component("radio", 5);
val.Component("hslider");
val.Component("vslider");
val.Linebreak();
val.Component("dd", 5);
}; };
} }
} }

View file

@ -12,10 +12,10 @@ class Button : Component
public String Label ~ delete _; public String Label ~ delete _;
delegate void(TGRuntime rt) Action ~ 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 .(pLabel);
Label = new .(pName); Action = pAction;
} }
public override void Update(TheaterGui.Core.TGRuntime rt) public override void Update(TheaterGui.Core.TGRuntime rt)

View file

@ -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;
}
}

75
src/Components/HSlider.bf Normal file
View file

@ -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;
}

View file

@ -59,12 +59,12 @@ class RadioButton : Component
for(var label in Label) for(var label in Label)
{ {
var tsize = rt.Platform.MeasureTextSize(label, "Font_Normal"); var tsize = rt.Platform.MeasureTextSize(label, "Font_Normal");
if(tsize[0] + 5 < xAddition) if(tsize[0] + 5 > xAddition)
xAddition = tsize[0] + 5; 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; return textureSize;
} }

76
src/Components/VSlider.bf Normal file
View file

@ -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;
}

View file

@ -8,6 +8,7 @@ struct theme
private static uint8[?] _Theme_Button = Compiler.ReadBinary("assets/button.png"); 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 = Compiler.ReadBinary("assets/checkbox.png");
private static uint8[?] _Theme_Checkbox_Checked = Compiler.ReadBinary("assets/checkbox_checked.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"); 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[?] Texture_NButton = Compiler.ReadBinary("assets/n_button.png");
private static uint8[?] Horizontal_Patch = Compiler.ReadBinary("assets/horizontal_patch.png"); private static uint8[?] Horizontal_Patch = Compiler.ReadBinary("assets/horizontal_patch.png");
private static uint8[?] Dropdown = Compiler.ReadBinary("assets/dropdown.png");
*/ */
public (Span<uint8>, uint8) Font_Normal = (.(&_Theme_Font, _Theme_Font.Count),16); public (Span<uint8>, uint8) Font_Normal = (.(&_Theme_Font, _Theme_Font.Count),16);
public Span<uint8> Button = .(&_Theme_Button, _Theme_Button.Count); public Span<uint8> Button = .(&_Theme_Button, _Theme_Button.Count);
public Span<uint8> Checkbox = .(&_Theme_Checkbox, _Theme_Checkbox.Count); public Span<uint8> Checkbox = .(&_Theme_Checkbox, _Theme_Checkbox.Count);
public Span<uint8> Checkbox_Checked = .(&_Theme_Checkbox_Checked, _Theme_Checkbox_Checked.Count); public Span<uint8> Checkbox_Checked = .(&_Theme_Checkbox_Checked, _Theme_Checkbox_Checked.Count);
public Span<uint8> Dropdown = .(&_Theme_Dropdown, _Theme_Dropdown.Count);
} }

View file

@ -120,6 +120,7 @@ class TGRuntime
Platform.LoadFont(pStartingTheme.Font_Normal.0, "Font_Normal", pStartingTheme.Font_Normal.1); Platform.LoadFont(pStartingTheme.Font_Normal.0, "Font_Normal", pStartingTheme.Font_Normal.1);
Platform.LoadTexture(pStartingTheme.Button, "Button"); Platform.LoadTexture(pStartingTheme.Button, "Button");
Platform.LoadTexture(pStartingTheme.Checkbox, "Checkbox"); Platform.LoadTexture(pStartingTheme.Checkbox, "Checkbox");
Platform.LoadTexture(pStartingTheme.Dropdown, "Dropdown");
Platform.LoadTexture(pStartingTheme.Checkbox_Checked, "Checkbox_Checked"); Platform.LoadTexture(pStartingTheme.Checkbox_Checked, "Checkbox_Checked");
} }

View file

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