Text and Label functions

This commit is contained in:
Bobby Lucero 2025-07-11 15:34:43 -04:00
parent 920cf5f9e4
commit f2967e2a37
2 changed files with 58 additions and 8 deletions

View File

@ -1175,7 +1175,6 @@ namespace MicroUI
// another root-container's begin/end block; this prevents the inner // another root-container's begin/end block; this prevents the inner
// root-container being clipped to the outer // root-container being clipped to the outer
MicroUI.PushClipRect(ctx, MicroUI.UnclippedRect); MicroUI.PushClipRect(ctx, MicroUI.UnclippedRect);
} }
public static void EndRootContainer(MuContext ctx) public static void EndRootContainer(MuContext ctx)
{ {
@ -1489,5 +1488,46 @@ namespace MicroUI
return new MuResult(res); return new MuResult(res);
} }
public static void Text(MuContext ctx, string text)
{
if (ctx.TextWidth == null || ctx.TextHeight == null) return;
var font = ctx.Style.Font;
var color = ctx.Style.Colors[(int)ColorType.Text];
int width = -1;
int lineHeight = ctx.TextHeight(font);
MuLayoutUtil.LayoutBeginColumn(ctx);
MuLayoutUtil.LayoutRow(ctx, 1, new int[] { width }, lineHeight);
int p = 0;
while (p < text.Length)
{
MuRect r = MuLayoutUtil.LayoutNext(ctx);
int w = 0;
int start = p;
int end = p;
do
{
int word = end;
while (end < text.Length && text[end] != ' ' && text[end] != '\n') end++;
int wordLen = end - word;
w += ctx.TextWidth(font, text.Substring(word, wordLen), wordLen);
if (w > r.W && end != start) { break; }
if (end < text.Length)
{
w += ctx.TextWidth(font, text.Substring(end, 1), 1);
end++;
}
} while (end < text.Length && text[end - 1] != '\n');
string line = text.Substring(start, end - start).TrimEnd('\n');
MuCommandList.DrawText(ctx, font, line, new MuVec2(r.X, r.Y), color);
p = (end < text.Length) ? end + 1 : end;
}
MuLayoutUtil.LayoutEndColumn(ctx);
}
public static void Label(MuContext ctx, string text)
{
MuRect r = MuLayoutUtil.LayoutNext(ctx);
DrawControlText(ctx, text, r, ColorType.Text, 0);
}
} }
} }

View File

@ -174,18 +174,28 @@ class Program
if(MuControl.BeginWindowEx(ctx, $"Test Window {index}", new MuRect(x, y, 300, 200), 0)){ if(MuControl.BeginWindowEx(ctx, $"Test Window {index}", new MuRect(x, y, 300, 200), 0)){
MuLayoutUtil.LayoutBeginColumn(ctx); MuLayoutUtil.LayoutBeginColumn(ctx);
// Test Text component with multi-line content
MuControl.Text(ctx, "This is a test of the Text component with word wrapping. It should automatically wrap long lines and handle multiple paragraphs.");
// Test Label component
// MuControl.Label(ctx, "This is a simple label");
// Test button // Test button
// if (MuControl.ButtonEx(ctx, "Click Me!", 0, 0)) // if (MuControl.ButtonEx(ctx, "Click Me!", 0, 0))
// { // {
// Console.WriteLine("Button clicked!"); // Console.WriteLine("Button clicked!");
// } // }
MuLayoutUtil.LayoutRow(ctx, 2, new int[]{200, 50}, 0); // MuLayoutUtil.LayoutRow(ctx, 2, new int[]{200, 50}, 0);
if(MuControl.Checkbox(ctx, "Checkbox1", ref isChecked)){ // if(MuControl.Checkbox(ctx, "Checkbox1", ref isChecked)){
Console.WriteLine("Checkbox clicked!"); // Console.WriteLine("Checkbox clicked!");
} // }
if(MuControl.ButtonEx(ctx, null, (int)IconType.Check, 0)){ // if(MuControl.ButtonEx(ctx, null, (int)IconType.Check, 0)){
Console.WriteLine("Button clicked!"); // Console.WriteLine("Button clicked!");
} // }
// Test more text with explicit line breaks
//MuControl.Text(ctx, "This text has\nexplicit line breaks\nand should display\non separate lines.");
// if(MuControl.Checkbox(ctx, "Checkbox2", ref isChecked)){ // if(MuControl.Checkbox(ctx, "Checkbox2", ref isChecked)){
// Console.WriteLine("Checkbox clicked!"); // Console.WriteLine("Checkbox clicked!");