mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
added old formatting codes to the new system
This commit is contained in:
parent
d28239291c
commit
011f9dd4f1
9 changed files with 109 additions and 12 deletions
14
MLEM/Formatting/Codes/AnimatedCode.cs
Normal file
14
MLEM/Formatting/Codes/AnimatedCode.cs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace MLEM.Formatting.Codes {
|
||||||
|
public class AnimatedCode : Code {
|
||||||
|
|
||||||
|
public AnimatedCode(Match match) : base(match) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool EndsHere(Code other) {
|
||||||
|
return other is AnimatedCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,9 +2,10 @@ using System.Text.RegularExpressions;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MLEM.Font;
|
using MLEM.Font;
|
||||||
|
using MLEM.Misc;
|
||||||
|
|
||||||
namespace MLEM.Formatting.Codes {
|
namespace MLEM.Formatting.Codes {
|
||||||
public class Code {
|
public class Code : GenericDataHolder {
|
||||||
|
|
||||||
public readonly Match Match;
|
public readonly Match Match;
|
||||||
public Token Token { get; internal set; }
|
public Token Token { get; internal set; }
|
||||||
|
@ -25,7 +26,10 @@ namespace MLEM.Formatting.Codes {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
|
public virtual void Update(GameTime time) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,5 +14,9 @@ namespace MLEM.Formatting.Codes {
|
||||||
return this.font;
|
return this.font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override bool EndsHere(Code other) {
|
||||||
|
return other is FontCode;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
25
MLEM/Formatting/Codes/ShadowCode.cs
Normal file
25
MLEM/Formatting/Codes/ShadowCode.cs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MLEM.Extensions;
|
||||||
|
using MLEM.Font;
|
||||||
|
|
||||||
|
namespace MLEM.Formatting.Codes {
|
||||||
|
public class ShadowCode : FontCode {
|
||||||
|
|
||||||
|
private readonly Color color;
|
||||||
|
private readonly Vector2 offset;
|
||||||
|
|
||||||
|
public ShadowCode(Match match, Color color, Vector2 offset) : base(match, null) {
|
||||||
|
this.color = color;
|
||||||
|
this.offset = offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
||||||
|
font.DrawString(batch, cString, pos + this.offset * scale, this.color.CopyAlpha(color), 0, Vector2.Zero, scale, SpriteEffects.None, depth);
|
||||||
|
// we return false since we still want regular drawing to occur
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
31
MLEM/Formatting/Codes/WobblyCode.cs
Normal file
31
MLEM/Formatting/Codes/WobblyCode.cs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
using System;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MLEM.Font;
|
||||||
|
|
||||||
|
namespace MLEM.Formatting.Codes {
|
||||||
|
public class WobblyCode : AnimatedCode {
|
||||||
|
|
||||||
|
private readonly float modifier;
|
||||||
|
private readonly float heightModifier;
|
||||||
|
public TimeSpan TimeIntoAnimation;
|
||||||
|
|
||||||
|
public WobblyCode(Match match, float modifier, float heightModifier) : base(match) {
|
||||||
|
this.modifier = modifier;
|
||||||
|
this.heightModifier = heightModifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update(GameTime time) {
|
||||||
|
this.TimeIntoAnimation += time.ElapsedGameTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
|
||||||
|
var offset = new Vector2(0, (float) Math.Sin(this.Token.Index + indexInToken + this.TimeIntoAnimation.TotalSeconds * this.modifier) * font.LineHeight * this.heightModifier * scale);
|
||||||
|
pos += offset;
|
||||||
|
// we return false since we still want regular drawing to occur, we just changed the position
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,9 +7,10 @@ using Microsoft.Xna.Framework;
|
||||||
using MLEM.Extensions;
|
using MLEM.Extensions;
|
||||||
using MLEM.Font;
|
using MLEM.Font;
|
||||||
using MLEM.Formatting.Codes;
|
using MLEM.Formatting.Codes;
|
||||||
|
using MLEM.Misc;
|
||||||
|
|
||||||
namespace MLEM.Formatting {
|
namespace MLEM.Formatting {
|
||||||
public class TextFormatter {
|
public class TextFormatter : GenericDataHolder {
|
||||||
|
|
||||||
public readonly Dictionary<Regex, Code.Constructor> Codes = new Dictionary<Regex, Code.Constructor>();
|
public readonly Dictionary<Regex, Code.Constructor> Codes = new Dictionary<Regex, Code.Constructor>();
|
||||||
|
|
||||||
|
@ -17,7 +18,8 @@ namespace MLEM.Formatting {
|
||||||
// font codes
|
// font codes
|
||||||
this.Codes.Add(new Regex("<b>"), (f, m) => new FontCode(m, boldFont?.Invoke()));
|
this.Codes.Add(new Regex("<b>"), (f, m) => new FontCode(m, boldFont?.Invoke()));
|
||||||
this.Codes.Add(new Regex("<i>"), (f, m) => new FontCode(m, italicFont?.Invoke()));
|
this.Codes.Add(new Regex("<i>"), (f, m) => new FontCode(m, italicFont?.Invoke()));
|
||||||
this.Codes.Add(new Regex("</(b|i)>"), (f, m) => new FontCode(m, null));
|
this.Codes.Add(new Regex(@"<s(?: #([0-9\w]{6,8}) (([+-.0-9]*)))?>"), (f, m) => new ShadowCode(m, m.Groups[1].Success ? ColorExtensions.FromHex(m.Groups[1].Value) : Color.Black, new Vector2(float.TryParse(m.Groups[2].Value, out var offset) ? offset : 2)));
|
||||||
|
this.Codes.Add(new Regex("</(b|i|s)>"), (f, m) => new FontCode(m, null));
|
||||||
|
|
||||||
// color codes
|
// color codes
|
||||||
foreach (var c in typeof(Color).GetProperties()) {
|
foreach (var c in typeof(Color).GetProperties()) {
|
||||||
|
@ -28,6 +30,10 @@ namespace MLEM.Formatting {
|
||||||
}
|
}
|
||||||
this.Codes.Add(new Regex(@"<c #([0-9\w]{6,8})>"), (f, m) => new ColorCode(m, ColorExtensions.FromHex(m.Groups[1].Value)));
|
this.Codes.Add(new Regex(@"<c #([0-9\w]{6,8})>"), (f, m) => new ColorCode(m, ColorExtensions.FromHex(m.Groups[1].Value)));
|
||||||
this.Codes.Add(new Regex("</c>"), (f, m) => new ColorCode(m, null));
|
this.Codes.Add(new Regex("</c>"), (f, m) => new ColorCode(m, null));
|
||||||
|
|
||||||
|
// animation codes
|
||||||
|
this.Codes.Add(new Regex(@"<a wobbly(?: ([+-.0-9]*) ([+-.0-9]*))?>"), (f, m) => new WobblyCode(m, float.TryParse(m.Groups[1].Value, out var mod) ? mod : 5, float.TryParse(m.Groups[2].Value, out var heightMod) ? heightMod : 1 / 8F));
|
||||||
|
this.Codes.Add(new Regex("</a>"), (f, m) => new AnimatedCode(m));
|
||||||
}
|
}
|
||||||
|
|
||||||
public TokenizedString Tokenize(string s) {
|
public TokenizedString Tokenize(string s) {
|
||||||
|
|
|
@ -4,9 +4,10 @@ using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MLEM.Font;
|
using MLEM.Font;
|
||||||
using MLEM.Formatting.Codes;
|
using MLEM.Formatting.Codes;
|
||||||
|
using MLEM.Misc;
|
||||||
|
|
||||||
namespace MLEM.Formatting {
|
namespace MLEM.Formatting {
|
||||||
public class Token {
|
public class Token : GenericDataHolder {
|
||||||
|
|
||||||
public readonly Code[] AppliedCodes;
|
public readonly Code[] AppliedCodes;
|
||||||
public readonly int Index;
|
public readonly int Index;
|
||||||
|
@ -33,9 +34,9 @@ namespace MLEM.Formatting {
|
||||||
return this.AppliedCodes.Select(c => c.GetFont()).FirstOrDefault();
|
return this.AppliedCodes.Select(c => c.GetFont()).FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
|
public void DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
|
||||||
foreach (var code in this.AppliedCodes) {
|
foreach (var code in this.AppliedCodes) {
|
||||||
if (code.DrawCharacter(time, batch, c, cString, pos, font, color, scale, depth))
|
if (code.DrawCharacter(time, batch, c, cString, indexInToken, ref pos, font, ref color, ref scale, depth))
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,18 +4,23 @@ using System.Text;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MLEM.Font;
|
using MLEM.Font;
|
||||||
|
using MLEM.Formatting.Codes;
|
||||||
|
using MLEM.Misc;
|
||||||
|
|
||||||
namespace MLEM.Formatting {
|
namespace MLEM.Formatting {
|
||||||
public struct TokenizedString {
|
public class TokenizedString : GenericDataHolder {
|
||||||
|
|
||||||
public readonly string RawString;
|
public readonly string RawString;
|
||||||
public readonly string String;
|
public readonly string String;
|
||||||
public readonly Token[] Tokens;
|
public readonly Token[] Tokens;
|
||||||
|
public readonly Code[] AllCodes;
|
||||||
|
|
||||||
public TokenizedString(string rawString, string strg, Token[] tokens) {
|
public TokenizedString(string rawString, string strg, Token[] tokens) {
|
||||||
this.RawString = rawString;
|
this.RawString = rawString;
|
||||||
this.String = strg;
|
this.String = strg;
|
||||||
this.Tokens = tokens;
|
this.Tokens = tokens;
|
||||||
|
// since a code can be present in multiple tokens, we use Distinct here
|
||||||
|
this.AllCodes = tokens.SelectMany(t => t.AppliedCodes).Distinct().ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Split(GenericFont font, float width, float scale) {
|
public void Split(GenericFont font, float width, float scale) {
|
||||||
|
@ -42,12 +47,18 @@ namespace MLEM.Formatting {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Update(GameTime time) {
|
||||||
|
foreach (var code in this.AllCodes)
|
||||||
|
code.Update(time);
|
||||||
|
}
|
||||||
|
|
||||||
public void Draw(GameTime time, SpriteBatch batch, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
|
public void Draw(GameTime time, SpriteBatch batch, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
|
||||||
var innerOffset = new Vector2();
|
var innerOffset = new Vector2();
|
||||||
foreach (var token in this.Tokens) {
|
foreach (var token in this.Tokens) {
|
||||||
var drawFont = token.GetFont() ?? font;
|
var drawFont = token.GetFont() ?? font;
|
||||||
var drawColor = token.GetColor() ?? color;
|
var drawColor = token.GetColor() ?? color;
|
||||||
foreach (var c in token.Substring) {
|
for (var i = 0; i < token.Substring.Length; i++) {
|
||||||
|
var c = token.Substring[i];
|
||||||
if (c == '\n') {
|
if (c == '\n') {
|
||||||
innerOffset.X = 0;
|
innerOffset.X = 0;
|
||||||
innerOffset.Y += font.LineHeight * scale;
|
innerOffset.Y += font.LineHeight * scale;
|
||||||
|
@ -55,7 +66,7 @@ namespace MLEM.Formatting {
|
||||||
}
|
}
|
||||||
|
|
||||||
var cString = c.ToString();
|
var cString = c.ToString();
|
||||||
token.DrawCharacter(time, batch, c, cString, pos + innerOffset, drawFont, drawColor, scale, depth);
|
token.DrawCharacter(time, batch, c, cString, i, pos + innerOffset, drawFont, drawColor, scale, depth);
|
||||||
innerOffset.X += font.MeasureString(cString).X * scale;
|
innerOffset.X += font.MeasureString(cString).X * scale;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,15 +113,16 @@ namespace Sandbox {
|
||||||
};*/
|
};*/
|
||||||
|
|
||||||
var formatter = new TextFormatter();
|
var formatter = new TextFormatter();
|
||||||
var strg = "This <c CornflowerBlue>is a formatted string</c> with <c #ff0000>two bits of formatting</c>! It also includesavery<c Pink>long</c>wordthatis<c Blue>formatted</c>aswell.";
|
var strg = "This <c CornflowerBlue>is a formatted string</c> with <c #ff0000>two bits of formatting</c>! It also includesavery<c Pink>long</c>wordthatis<c Blue>formatted</c>aswell. Additionally, it <a wobbly>wobbles</a> and has a <s>shadow</s> or a <s #ff0000 4>weird shadow</s>";
|
||||||
this.tokenized = formatter.Tokenize(strg);
|
this.tokenized = formatter.Tokenize(strg);
|
||||||
this.tokenized.Split(font, 400, 1);
|
this.tokenized.Split(font, 400, 1);
|
||||||
|
|
||||||
this.OnDraw += (g, time) => {
|
this.OnDraw += (g, time) => {
|
||||||
this.SpriteBatch.Begin();
|
this.SpriteBatch.Begin();
|
||||||
this.tokenized.Draw(time, this.SpriteBatch, new Vector2(100, 20), font, Color.White, 1, 0);
|
this.tokenized.Draw(time, this.SpriteBatch, new Vector2(400, 20), font, Color.White, 1, 0);
|
||||||
this.SpriteBatch.End();
|
this.SpriteBatch.End();
|
||||||
};
|
};
|
||||||
|
this.OnUpdate += (g, time) => this.tokenized.Update(time);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void DoUpdate(GameTime gameTime) {
|
protected override void DoUpdate(GameTime gameTime) {
|
||||||
|
|
Loading…
Reference in a new issue