From f2967e2a3782b700719063bfad04c7aed9a0c841 Mon Sep 17 00:00:00 2001 From: Bobby Lucero Date: Fri, 11 Jul 2025 15:34:43 -0400 Subject: [PATCH] Text and Label functions --- MicroUI.cs/MicroUI.cs | 42 +++++++++++++++++++++++++++++++++++++++++- MicroUI.cs/Program.cs | 24 +++++++++++++++++------- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/MicroUI.cs/MicroUI.cs b/MicroUI.cs/MicroUI.cs index 30e2c6c..f7e7c94 100644 --- a/MicroUI.cs/MicroUI.cs +++ b/MicroUI.cs/MicroUI.cs @@ -1175,7 +1175,6 @@ namespace MicroUI // another root-container's begin/end block; this prevents the inner // root-container being clipped to the outer MicroUI.PushClipRect(ctx, MicroUI.UnclippedRect); - } public static void EndRootContainer(MuContext ctx) { @@ -1489,5 +1488,46 @@ namespace MicroUI 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); + } } } \ No newline at end of file diff --git a/MicroUI.cs/Program.cs b/MicroUI.cs/Program.cs index 959ddff..177d9b8 100644 --- a/MicroUI.cs/Program.cs +++ b/MicroUI.cs/Program.cs @@ -174,18 +174,28 @@ class Program if(MuControl.BeginWindowEx(ctx, $"Test Window {index}", new MuRect(x, y, 300, 200), 0)){ 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 // if (MuControl.ButtonEx(ctx, "Click Me!", 0, 0)) // { // Console.WriteLine("Button clicked!"); // } - MuLayoutUtil.LayoutRow(ctx, 2, new int[]{200, 50}, 0); - if(MuControl.Checkbox(ctx, "Checkbox1", ref isChecked)){ - Console.WriteLine("Checkbox clicked!"); - } - if(MuControl.ButtonEx(ctx, null, (int)IconType.Check, 0)){ - Console.WriteLine("Button clicked!"); - } + // MuLayoutUtil.LayoutRow(ctx, 2, new int[]{200, 50}, 0); + // if(MuControl.Checkbox(ctx, "Checkbox1", ref isChecked)){ + // Console.WriteLine("Checkbox clicked!"); + // } + // if(MuControl.ButtonEx(ctx, null, (int)IconType.Check, 0)){ + // 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)){ // Console.WriteLine("Checkbox clicked!");