v1.4.4.1 (Blood in the Water Update)
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Barotrauma.Networking;
|
||||
using Color = Microsoft.Xna.Framework.Color;
|
||||
|
||||
@@ -422,6 +423,38 @@ namespace Barotrauma
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes lines on a multi-line string until it fits within the specified height and adds "..." to the end if the string is too long.
|
||||
/// Doesn't really do anything if the string is only one line, should mostly be used with <see cref="GUITextBlock.WrappedText"/>.
|
||||
/// </summary>
|
||||
public static string LimitStringHeight(string str, ScalableFont font, int maxHeight)
|
||||
{
|
||||
if (maxHeight <= 0 || string.IsNullOrWhiteSpace(str)) { return string.Empty; }
|
||||
|
||||
float currHeight = font.MeasureString("...").Y;
|
||||
var lines = str.Split('\n');
|
||||
|
||||
var sb = new StringBuilder();
|
||||
foreach (string line in lines)
|
||||
{
|
||||
var (lineX, lineY) = font.MeasureString(line);
|
||||
currHeight += lineY;
|
||||
if (currHeight > maxHeight)
|
||||
{
|
||||
var modifiedLine = line;
|
||||
while (font.MeasureString($"{modifiedLine}...").X > lineX)
|
||||
{
|
||||
modifiedLine = modifiedLine[..^1];
|
||||
}
|
||||
sb.AppendLine($"{modifiedLine}...");
|
||||
return sb.ToString();
|
||||
}
|
||||
sb.AppendLine(line);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static Color GradientLerp(float t, params Color[] gradient)
|
||||
{
|
||||
if (!MathUtils.IsValid(t)) { return Color.Purple; }
|
||||
|
||||
Reference in New Issue
Block a user