1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00

added the ability to have mixed formatting

This commit is contained in:
Ellpeck 2020-02-03 15:38:27 +01:00
parent 347da85b10
commit e37cfed757
4 changed files with 56 additions and 43 deletions

View file

@ -91,7 +91,7 @@ namespace Demos {
root.AddChild(new VerticalSpace(3)); root.AddChild(new VerticalSpace(3));
// a paragraph with formatting codes. To see them all or to add more, check the TextFormatting class // a paragraph with formatting codes. To see them all or to add more, check the TextFormatting class
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Paragraphs can also contain [Blue]formatting codes[White], including colors and [Italic]text styles[Regular]. The names of all [Orange]MonoGame Colors[White] can be used, as well as the codes [Italic]Italic[Regular], [Bold]Bold[Regular] and [Shadow]Drop Shadow'd[Regular]. \n[Italic]Even [CornflowerBlue]Cornflower Blue[White] works!")); root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Paragraphs can also contain [Blue]formatting codes[White], including colors and [Italic]text styles[Regular]. The names of all [Orange]MonoGame Colors[White] can be used, as well as the codes [Italic]Italic[Regular], [Bold]Bold[Regular], [Shadow]Drop Shadow'd[Regular] and [Shadow][Pink]mixed formatting[Regular][White]. \n[Italic]Even [CornflowerBlue]Cornflower Blue[White] works!"));
// adding some custom image formatting codes // adding some custom image formatting codes
// note that all added formatting codes need to be lowercase, while their casing doesn't matter when used // note that all added formatting codes need to be lowercase, while their casing doesn't matter when used

View file

@ -16,7 +16,7 @@ namespace MLEM.Ui.Elements {
private string text; private string text;
private string splitText; private string splitText;
private Dictionary<int, FormattingCode> codeLocations; public FormattingCodeCollection Formatting;
public StyleProp<IGenericFont> RegularFont; public StyleProp<IGenericFont> RegularFont;
public StyleProp<IGenericFont> BoldFont; public StyleProp<IGenericFont> BoldFont;
public StyleProp<IGenericFont> ItalicFont; public StyleProp<IGenericFont> ItalicFont;
@ -60,7 +60,7 @@ namespace MLEM.Ui.Elements {
var sc = this.TextScale * this.Scale; var sc = this.TextScale * this.Scale;
this.splitText = this.RegularFont.Value.SplitString(this.text.RemoveFormatting(this.RegularFont.Value), size.X - this.ScaledPadding.Width, sc); this.splitText = this.RegularFont.Value.SplitString(this.text.RemoveFormatting(this.RegularFont.Value), size.X - this.ScaledPadding.Width, sc);
this.codeLocations = this.text.GetFormattingCodes(this.RegularFont.Value); this.Formatting = this.text.GetFormattingCodes(this.RegularFont.Value);
var textDims = this.RegularFont.Value.MeasureString(this.splitText) * sc; var textDims = this.RegularFont.Value.MeasureString(this.splitText) * sc;
return new Vector2(this.AutoAdjustWidth ? textDims.X + this.ScaledPadding.Width : size.X, textDims.Y + this.ScaledPadding.Height); return new Vector2(this.AutoAdjustWidth ? textDims.X + this.ScaledPadding.Width : size.X, textDims.Y + this.ScaledPadding.Height);
@ -85,11 +85,11 @@ namespace MLEM.Ui.Elements {
var color = this.TextColor.OrDefault(Color.White) * alpha; var color = this.TextColor.OrDefault(Color.White) * alpha;
// if we don't have any formatting codes, then we don't need to do complex drawing // if we don't have any formatting codes, then we don't need to do complex drawing
if (this.codeLocations.Count <= 0) { if (this.Formatting.Count <= 0) {
this.RegularFont.Value.DrawString(batch, this.splitText, pos, color, 0, Vector2.Zero, sc, SpriteEffects.None, 0); this.RegularFont.Value.DrawString(batch, this.splitText, pos, color, 0, Vector2.Zero, sc, SpriteEffects.None, 0);
} else { } else {
// if we have formatting codes, we should do it // if we have formatting codes, we should do it
this.RegularFont.Value.DrawFormattedString(batch, pos, this.splitText, this.codeLocations, color, sc, this.BoldFont.Value, this.ItalicFont.Value, 0, this.TimeIntoAnimation, this.FormatSettings); this.RegularFont.Value.DrawFormattedString(batch, pos, this.splitText, this.Formatting, color, sc, this.BoldFont.Value, this.ItalicFont.Value, 0, this.TimeIntoAnimation, this.FormatSettings);
} }
base.Draw(time, batch, alpha, blendState, samplerState, matrix); base.Draw(time, batch, alpha, blendState, samplerState, matrix);
} }

View file

@ -0,0 +1,7 @@
using System.Collections.Generic;
namespace MLEM.Formatting {
public class FormattingCodeCollection : Dictionary<int, List<FormattingCode>> {
}
}

View file

@ -58,24 +58,29 @@ namespace MLEM.Formatting {
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);
return code != null ? code.GetReplacementString(font) : string.Empty; return code != null ? code.GetReplacementString(font) : match.Value;
}); });
} }
public static Dictionary<int, FormattingCode> GetFormattingCodes(this string s, IGenericFont font) { public static FormattingCodeCollection GetFormattingCodes(this string s, IGenericFont font) {
var codes = new Dictionary<int, FormattingCode>(); var codes = new FormattingCodeCollection();
var codeLengths = 0; var codeLengths = 0;
foreach (Match match in formatRegex.Matches(s)) { foreach (Match match in formatRegex.Matches(s)) {
var code = FromMatch(match); var code = FromMatch(match);
if (code == null) if (code == null)
continue; continue;
codes[match.Index - codeLengths] = code; var index = match.Index - codeLengths;
if (codes.TryGetValue(index, out var curr)) {
curr.Add(code);
} else {
codes.Add(index, new List<FormattingCode> {code});
}
codeLengths += match.Length - code.GetReplacementString(font).Length; codeLengths += match.Length - code.GetReplacementString(font).Length;
} }
return codes; return codes;
} }
public static void DrawFormattedString(this IGenericFont regularFont, SpriteBatch batch, Vector2 pos, string text, Dictionary<int, FormattingCode> codeLocations, Color color, float scale, IGenericFont boldFont = null, IGenericFont italicFont = null, float depth = 0, TimeSpan timeIntoAnimation = default, FormatSettings formatSettings = null) { public static void DrawFormattedString(this IGenericFont regularFont, SpriteBatch batch, Vector2 pos, string unformattedText, FormattingCodeCollection formatting, Color color, float scale, IGenericFont boldFont = null, IGenericFont italicFont = null, float depth = 0, TimeSpan timeIntoAnimation = default, FormatSettings formatSettings = null) {
var settings = formatSettings ?? FormatSettings.Default; var settings = formatSettings ?? FormatSettings.Default;
var currColor = color; var currColor = color;
var currFont = regularFont; var currFont = regularFont;
@ -84,10 +89,10 @@ namespace MLEM.Formatting {
var animStart = 0; var animStart = 0;
var innerOffset = new Vector2(); var innerOffset = new Vector2();
for (var i = 0; i < text.Length; i++) { for (var i = 0; i < unformattedText.Length; i++) {
// check if the current character's index has a formatting code // check if the current character's index has a formatting code
codeLocations.TryGetValue(i, out var code); if (formatting.TryGetValue(i, out var codes)) {
if (code != null) { foreach (var code in codes) {
// if so, apply it // if so, apply it
switch (code.CodeType) { switch (code.CodeType) {
case FormattingCode.Type.Color: case FormattingCode.Type.Color:
@ -117,16 +122,17 @@ namespace MLEM.Formatting {
break; break;
} }
} }
}
var c = text[i]; var c = unformattedText[i];
var cSt = c.ToString(); var cSt = c.ToString();
if (c == '\n') { if (c == '\n') {
innerOffset.X = 0; innerOffset.X = 0;
innerOffset.Y += regularFont.LineHeight * scale; innerOffset.Y += regularFont.LineHeight * scale;
} else { } else {
if (currStyle == TextStyle.Shadow) if (currStyle == TextStyle.Shadow)
currAnim(settings, currFont, batch, text, i, animStart, cSt, pos + innerOffset + settings.DropShadowOffset * scale, settings.DropShadowColor, scale, depth, timeIntoAnimation); currAnim(settings, currFont, batch, unformattedText, i, animStart, cSt, pos + innerOffset + settings.DropShadowOffset * scale, settings.DropShadowColor, scale, depth, timeIntoAnimation);
currAnim(settings, currFont, batch, text, i, animStart, cSt, pos + innerOffset, currColor, scale, depth, timeIntoAnimation); currAnim(settings, currFont, batch, unformattedText, i, animStart, cSt, pos + innerOffset, currColor, scale, depth, timeIntoAnimation);
innerOffset.X += regularFont.MeasureString(cSt).X * scale; innerOffset.X += regularFont.MeasureString(cSt).X * scale;
} }
} }