2019-08-25 19:07:45 +02:00
|
|
|
using System;
|
2019-08-06 14:20:11 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
|
|
|
namespace MLEM.Extensions {
|
|
|
|
public static class SpriteFontExtensions {
|
|
|
|
|
2019-08-25 19:07:45 +02:00
|
|
|
public static string SplitString(this SpriteFont font, string text, float width, float scale) {
|
|
|
|
return SplitString(s => font.MeasureString(s).X, text, width, scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string SplitString(Func<StringBuilder, float> widthFunc, string text, float width, float scale) {
|
|
|
|
var total = new StringBuilder();
|
2019-08-24 00:07:54 +02:00
|
|
|
foreach (var line in text.Split('\n')) {
|
2019-08-25 19:07:45 +02:00
|
|
|
var curr = new StringBuilder();
|
2019-08-24 00:07:54 +02:00
|
|
|
foreach (var word in line.Split(' ')) {
|
2019-08-25 19:07:45 +02:00
|
|
|
curr.Append(word).Append(' ');
|
|
|
|
if (widthFunc(curr) * scale >= width) {
|
|
|
|
var len = curr.Length - word.Length - 1;
|
|
|
|
total.Append(curr.ToString(0, len - 1)).Append('\n');
|
|
|
|
curr.Remove(0, len);
|
2019-08-24 00:07:54 +02:00
|
|
|
}
|
2019-08-06 14:20:11 +02:00
|
|
|
}
|
2019-08-25 19:07:45 +02:00
|
|
|
total.Append(curr.ToString(0, curr.Length - 1)).Append('\n');
|
2019-08-06 14:20:11 +02:00
|
|
|
}
|
2019-08-25 19:07:45 +02:00
|
|
|
return total.ToString(0, total.Length - 1);
|
2019-08-06 14:20:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|