1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-27 23:09:10 +02:00

made the GetOneEmString method more generic

This commit is contained in:
Ellpeck 2020-03-21 00:12:09 +01:00
parent a3f39f170a
commit 9f43924e47
3 changed files with 10 additions and 12 deletions

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Font;
namespace MLEM.Extensions { namespace MLEM.Extensions {
public static class SpriteFontExtensions { public static class SpriteFontExtensions {
@ -64,5 +65,12 @@ namespace MLEM.Extensions {
return total.ToString(0, total.Length - 2); 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;
}
} }
} }

View file

@ -1,5 +1,6 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using MLEM.Animations; using MLEM.Animations;
using MLEM.Extensions;
using MLEM.Font; using MLEM.Font;
using MLEM.Textures; using MLEM.Textures;
@ -37,7 +38,7 @@ namespace MLEM.Formatting {
} }
public virtual string GetReplacementString(IGenericFont font) { 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 { public enum Type {

View file

@ -12,7 +12,6 @@ namespace MLEM.Formatting {
public static class TextFormatting { public static class TextFormatting {
public static readonly Dictionary<string, FormattingCode> FormattingCodes = new Dictionary<string, FormattingCode>(); 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; private static Regex formatRegex;
static TextFormatting() { static TextFormatting() {
@ -45,16 +44,6 @@ namespace MLEM.Formatting {
formatRegex = new Regex($"{op}[^{op}{cl}]*{cl}"); 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) { public static string RemoveFormatting(this string s, IGenericFont font) {
return formatRegex.Replace(s, match => { return formatRegex.Replace(s, match => {
var code = FromMatch(match); var code = FromMatch(match);