mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-12-25 17:59:24 +01:00
Improved the way terminating formatting codes work by introducing SimpleEndCode
This commit is contained in:
parent
92018eea1e
commit
6e2c2b3730
9 changed files with 40 additions and 39 deletions
|
@ -24,6 +24,7 @@ Improvements
|
|||
- Allow comparing Keybind and Combination based on the amount of modifiers they have
|
||||
- Allow using multiple textures in a StaticSpriteBatch
|
||||
- Added GenericInput support for Buttons.None
|
||||
- Improved the way terminating formatting codes work by introducing SimpleEndCode
|
||||
|
||||
Removals
|
||||
- Marked AStar.InfiniteCost as obsolete
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace Demos {
|
|||
|
||||
private const string Text =
|
||||
"MLEM's text formatting system allows for various <b>formatting codes</b> to be applied in the middle of a string. Here's a demonstration of some of them.\n\n" +
|
||||
"You can write in <b>bold</i>, <i>italics</i>, <u>with an underline</u>, <st>strikethrough</st>, or with a <s #000000 4>drop shadow</s> whose <s #ff0000 4>color</s> and <s #000000 10>offset</s> you can modify in each application of the code.\n\n" +
|
||||
"You can write in <b>bold</i>, <i>italics</i>, <u>with an underline</u>, <st>strikethrough</st>, with a <s #000000 4>drop shadow</s> whose <s #ff0000 4>color</s> and <s #000000 10>offset</s> you can modify in each application of the code, or with various types of <b>combined <c Pink>formatting</c> codes</b>.\n\n" +
|
||||
"You can apply <c CornflowerBlue>custom</c> <c Yellow>colors</c> to text, including all default <c Orange>MonoGame colors</c> and <c #aabb00>inline custom colors</c>.\n\n" +
|
||||
"You can also use animations like <a wobbly>a wobbly one</a>, as well as create custom ones using the <a wobbly>Code class</a>.\n\n" +
|
||||
"You can also display <i grass> icons in your text!\n\n" +
|
||||
|
|
|
@ -39,13 +39,25 @@ namespace MLEM.Formatting.Codes {
|
|||
/// <summary>
|
||||
/// Returns whether this formatting code should end when the passed formatting code starts.
|
||||
/// If this method returns true, a new <see cref="Token"/> is started at its position.
|
||||
/// This is the opposite version of <see cref="EndsOther"/>.
|
||||
/// </summary>
|
||||
/// <param name="other">The code that is started here</param>
|
||||
/// <returns>If this code should end</returns>
|
||||
/// <param name="other">The code that is started here.</param>
|
||||
/// <returns>If this code should end here.</returns>
|
||||
public virtual bool EndsHere(Code other) {
|
||||
return other.GetType() == this.GetType();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the <paramref name="other"/> <see cref="Code"/> should end when this formatting code starts.
|
||||
/// If this method returns true, a new <see cref="Token"/> is started at this code's position.
|
||||
/// This is the opposite version of <see cref="EndsHere"/>.
|
||||
/// </summary>
|
||||
/// <param name="other">The code that could end here.</param>
|
||||
/// <returns>Whether the <paramref name="other"/> code should end here.</returns>
|
||||
public virtual bool EndsOther(Code other) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Formatting.Token.GetColor"/>
|
||||
public virtual Color? GetColor(Color defaultPick) {
|
||||
return null;
|
||||
|
|
|
@ -18,10 +18,5 @@ namespace MLEM.Formatting.Codes {
|
|||
return this.font?.Invoke(defaultPick);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool EndsHere(Code other) {
|
||||
return other is FontCode;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace MLEM.Formatting.Codes {
|
||||
/// <inheritdoc />
|
||||
public class ResetFormattingCode : Code {
|
||||
|
||||
/// <inheritdoc />
|
||||
public ResetFormattingCode(Match match, Regex regex) : base(match, regex) {}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool EndsHere(Code other) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -24,10 +24,5 @@ namespace MLEM.Formatting.Codes {
|
|||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool EndsHere(Code other) {
|
||||
return other is ShadowCode || other is ResetFormattingCode;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
20
MLEM/Formatting/Codes/SimpleEndCode.cs
Normal file
20
MLEM/Formatting/Codes/SimpleEndCode.cs
Normal file
|
@ -0,0 +1,20 @@
|
|||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace MLEM.Formatting.Codes {
|
||||
/// <inheritdoc />
|
||||
public class SimpleEndCode : Code {
|
||||
|
||||
private readonly Regex codeToEnd;
|
||||
|
||||
/// <inheritdoc />
|
||||
public SimpleEndCode(Match match, Regex regex, string codeNameToEnd) : base(match, regex) {
|
||||
this.codeToEnd = new Regex($"<{codeNameToEnd}.*>");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool EndsOther(Code other) {
|
||||
return this.codeToEnd.IsMatch(other.Regex.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -29,10 +29,5 @@ namespace MLEM.Formatting.Codes {
|
|||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool EndsHere(Code other) {
|
||||
return other is UnderlineCode || other is ResetFormattingCode;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,8 +40,6 @@ namespace MLEM.Formatting {
|
|||
new Vector2(float.TryParse(m.Groups[2].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var offset) ? offset : 2)));
|
||||
this.Codes.Add(new Regex("<u>"), (f, m, r) => new UnderlineCode(m, r, 1 / 16F, 0.85F));
|
||||
this.Codes.Add(new Regex("<st>"), (f, m, r) => new UnderlineCode(m, r, 1 / 16F, 0.55F));
|
||||
this.Codes.Add(new Regex("</(s|u|st|l)>"), (f, m, r) => new ResetFormattingCode(m, r));
|
||||
this.Codes.Add(new Regex("</(b|i|f)>"), (f, m, r) => new FontCode(m, r, null));
|
||||
|
||||
// color codes
|
||||
foreach (var c in typeof(Color).GetProperties()) {
|
||||
|
@ -51,13 +49,14 @@ namespace MLEM.Formatting {
|
|||
}
|
||||
}
|
||||
this.Codes.Add(new Regex(@"<c #([0-9\w]{6,8})>"), (f, m, r) => new ColorCode(m, r, ColorHelper.FromHexString(m.Groups[1].Value)));
|
||||
this.Codes.Add(new Regex("</c>"), (f, m, r) => new ColorCode(m, r, null));
|
||||
|
||||
// animation codes
|
||||
this.Codes.Add(new Regex(@"<a wobbly(?: ([+-.0-9]*) ([+-.0-9]*))?>"), (f, m, r) => new WobblyCode(m, r,
|
||||
float.TryParse(m.Groups[1].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var mod) ? mod : 5,
|
||||
float.TryParse(m.Groups[2].Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var heightMod) ? heightMod : 1 / 8F));
|
||||
this.Codes.Add(new Regex("</a>"), (f, m, r) => new AnimatedCode(m, r));
|
||||
|
||||
// control codes
|
||||
this.Codes.Add(new Regex(@"</(\w+)>"), (f, m, r) => new SimpleEndCode(m, r, m.Groups[1].Value));
|
||||
|
||||
// macros
|
||||
this.Macros.Add(new Regex("~"), (f, m, r) => GenericFont.Nbsp.ToCachedString());
|
||||
|
@ -101,7 +100,7 @@ namespace MLEM.Formatting {
|
|||
index += strippedRet.Length;
|
||||
|
||||
// remove all codes that are incompatible with the next one and apply it
|
||||
codes.RemoveAll(c => c.EndsHere(next));
|
||||
codes.RemoveAll(c => c.EndsHere(next) || next.EndsOther(c));
|
||||
codes.Add(next);
|
||||
}
|
||||
return new TokenizedString(font, alignment, s, TextFormatter.StripFormatting(font, s, tokens.SelectMany(t => t.AppliedCodes)), tokens.ToArray());
|
||||
|
|
Loading…
Reference in a new issue