Add MaxWidth property for text rendering in UI controls and fix maxWidth parameter in canvas extension methods

This commit is contained in:
Stone_Red
2026-06-16 19:46:05 +02:00
parent 92cc387f2d
commit e7922bdf05
6 changed files with 9 additions and 2 deletions
+2 -1
View File
@@ -304,10 +304,11 @@ public sealed class CanvasRenderSource : IRenderSource
Color color = Get(cmd.Properties, "Color", Color.White); Color color = Get(cmd.Properties, "Color", Color.White);
string content = Get(cmd.Properties, "Content", string.Empty); string content = Get(cmd.Properties, "Content", string.Empty);
int fontSize = Get(cmd.Properties, "FontSize", 12); int fontSize = Get(cmd.Properties, "FontSize", 12);
int maxWidth = Get(cmd.Properties, "MaxWidth", int.MaxValue);
if (!string.IsNullOrEmpty(content)) if (!string.IsNullOrEmpty(content))
{ {
canvas.DrawStringHeight(content, PCScreenFont.DefaultFont, color, cmd.Position.X, cmd.Position.Y, fontSize); canvas.DrawStringHeight(content, PCScreenFont.DefaultFont, color, cmd.Position.X, cmd.Position.Y, fontSize, maxWidth);
} }
} }
+2
View File
@@ -76,6 +76,7 @@ public class RenderCommand
byte[] utf8 = Encoding.UTF8.GetBytes(text); byte[] utf8 = Encoding.UTF8.GetBytes(text);
WriteVarint(s, utf8.Length); WriteVarint(s, utf8.Length);
s.Write(utf8, 0, utf8.Length); s.Write(utf8, 0, utf8.Length);
WriteVarint(s, GetProp("MaxWidth", int.MaxValue));
break; break;
case RenderCommandType.DrawLine: case RenderCommandType.DrawLine:
@@ -142,6 +143,7 @@ public class RenderCommand
int textLen = ReadVarint(data, ref offset); int textLen = ReadVarint(data, ref offset);
props["Content"] = Encoding.UTF8.GetString(data, offset, textLen); props["Content"] = Encoding.UTF8.GetString(data, offset, textLen);
offset += textLen; offset += textLen;
props["MaxWidth"] = ReadVarint(data, ref offset);
break; break;
case RenderCommandType.DrawLine: case RenderCommandType.DrawLine:
+1
View File
@@ -67,6 +67,7 @@ public class Button() : Control("Button")
["Color"] = TextColor, ["Color"] = TextColor,
["Content"] = Text, ["Content"] = Text,
["FontSize"] = Size.Height - 8, ["FontSize"] = Size.Height - 8,
["MaxWidth"] = Size.Width,
} }
}; };
} }
+1
View File
@@ -93,6 +93,7 @@ public class CheckBox() : Control("CheckBox")
["Color"] = TextColor, ["Color"] = TextColor,
["Content"] = Text, ["Content"] = Text,
["FontSize"] = boxSize, ["FontSize"] = boxSize,
["MaxWidth"] = Size.Width - boxSize - 5,
} }
}; };
} }
@@ -88,6 +88,7 @@ public class RadioButton() : Control("RadioButton")
["Color"] = TextColor, ["Color"] = TextColor,
["Content"] = Text, ["Content"] = Text,
["FontSize"] = Size.Height, ["FontSize"] = Size.Height,
["MaxWidth"] = Size.Width - Size.Height - 5,
} }
}; };
} }
+2 -1
View File
@@ -70,10 +70,11 @@ public static class CanvasExtensions
byte height = font.Height; byte height = font.Height;
int targetWidth = width * targetHeight / height; int targetWidth = width * targetHeight / height;
int endX = maxWidth == int.MaxValue ? int.MaxValue : x + maxWidth;
for (int i = 0; i < str.Length; i++) for (int i = 0; i < str.Length; i++)
{ {
if (x + targetWidth > maxWidth) if (x + targetWidth > endX)
{ {
break; break;
} }