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);
string content = Get(cmd.Properties, "Content", string.Empty);
int fontSize = Get(cmd.Properties, "FontSize", 12);
int maxWidth = Get(cmd.Properties, "MaxWidth", int.MaxValue);
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);
WriteVarint(s, utf8.Length);
s.Write(utf8, 0, utf8.Length);
WriteVarint(s, GetProp("MaxWidth", int.MaxValue));
break;
case RenderCommandType.DrawLine:
@@ -142,6 +143,7 @@ public class RenderCommand
int textLen = ReadVarint(data, ref offset);
props["Content"] = Encoding.UTF8.GetString(data, offset, textLen);
offset += textLen;
props["MaxWidth"] = ReadVarint(data, ref offset);
break;
case RenderCommandType.DrawLine:
+1
View File
@@ -67,6 +67,7 @@ public class Button() : Control("Button")
["Color"] = TextColor,
["Content"] = Text,
["FontSize"] = Size.Height - 8,
["MaxWidth"] = Size.Width,
}
};
}
+1
View File
@@ -93,6 +93,7 @@ public class CheckBox() : Control("CheckBox")
["Color"] = TextColor,
["Content"] = Text,
["FontSize"] = boxSize,
["MaxWidth"] = Size.Width - boxSize - 5,
}
};
}
@@ -88,6 +88,7 @@ public class RadioButton() : Control("RadioButton")
["Color"] = TextColor,
["Content"] = Text,
["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;
int targetWidth = width * targetHeight / height;
int endX = maxWidth == int.MaxValue ? int.MaxValue : x + maxWidth;
for (int i = 0; i < str.Length; i++)
{
if (x + targetWidth > maxWidth)
if (x + targetWidth > endX)
{
break;
}