mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
made the GetOneEmString method more generic
This commit is contained in:
parent
a3f39f170a
commit
9f43924e47
3 changed files with 10 additions and 12 deletions
|
@ -2,6 +2,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MLEM.Font;
|
||||
|
||||
namespace MLEM.Extensions {
|
||||
public static class SpriteFontExtensions {
|
||||
|
@ -64,5 +65,12 @@ namespace MLEM.Extensions {
|
|||
return total.ToString(0, total.Length - 2);
|
||||
}
|
||||
|
||||
public static string GetWidthString(IGenericFont font, float width, char content = ' ') {
|
||||
var strg = content.ToString();
|
||||
while (font.MeasureString(strg).X < width)
|
||||
strg += content;
|
||||
return strg;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
using Microsoft.Xna.Framework;
|
||||
using MLEM.Animations;
|
||||
using MLEM.Extensions;
|
||||
using MLEM.Font;
|
||||
using MLEM.Textures;
|
||||
|
||||
|
@ -37,7 +38,7 @@ namespace MLEM.Formatting {
|
|||
}
|
||||
|
||||
public virtual string GetReplacementString(IGenericFont font) {
|
||||
return this.CodeType == Type.Icon ? TextFormatting.GetOneEmString(font) : string.Empty;
|
||||
return this.CodeType == Type.Icon ? SpriteFontExtensions.GetWidthString(font, font.LineHeight) : string.Empty;
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
|
|
|
@ -12,7 +12,6 @@ namespace MLEM.Formatting {
|
|||
public static class TextFormatting {
|
||||
|
||||
public static readonly Dictionary<string, FormattingCode> FormattingCodes = new Dictionary<string, FormattingCode>();
|
||||
private static readonly Dictionary<IGenericFont, string> OneEmStrings = new Dictionary<IGenericFont, string>();
|
||||
private static Regex formatRegex;
|
||||
|
||||
static TextFormatting() {
|
||||
|
@ -45,16 +44,6 @@ namespace MLEM.Formatting {
|
|||
formatRegex = new Regex($"{op}[^{op}{cl}]*{cl}");
|
||||
}
|
||||
|
||||
public static string GetOneEmString(IGenericFont font) {
|
||||
if (!OneEmStrings.TryGetValue(font, out var strg)) {
|
||||
strg = " ";
|
||||
while (font.MeasureString(strg + ' ').X < font.LineHeight)
|
||||
strg += ' ';
|
||||
OneEmStrings[font] = strg;
|
||||
}
|
||||
return strg;
|
||||
}
|
||||
|
||||
public static string RemoveFormatting(this string s, IGenericFont font) {
|
||||
return formatRegex.Replace(s, match => {
|
||||
var code = FromMatch(match);
|
||||
|
|
Loading…
Reference in a new issue