From 7f4dc45742b9db0157ebbbec4d04517a99c378b7 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Tue, 16 Jun 2026 20:00:31 +0200 Subject: [PATCH] Improve button text alignment and truncate if too wide for its size --- UI/GUI/UIEelements/Controls/Button.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/UI/GUI/UIEelements/Controls/Button.cs b/UI/GUI/UIEelements/Controls/Button.cs index f2780d9..3fedb7a 100644 --- a/UI/GUI/UIEelements/Controls/Button.cs +++ b/UI/GUI/UIEelements/Controls/Button.cs @@ -50,10 +50,21 @@ public class Button() : Control("Button") } }; - // Text (centered) + // Text (centered, falls back to left-aligned if too wide) if (!string.IsNullOrEmpty(Text)) { - int tx = Position.X + (Size.Width / 2) - (Text.Length * 4); + int fontSize = Size.Height - 8; + int charWidth = 8 * fontSize / 14; + int maxChars = Size.Width / charWidth; + string display = Text; + if (display.Length > maxChars && maxChars > 0) + { + display = Text[..maxChars]; + } + int textWidth = display.Length * charWidth; + int tx = textWidth >= Size.Width + ? Position.X + : Position.X + ((Size.Width - textWidth) / 2); int ty = Position.Y + (Size.Height / 2) - 8; yield return new RenderCommand