1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-28 19:13:38 +02:00
MLEM/MLEM/Formatting/TokenizedString.cs

65 lines
2.5 KiB
C#
Raw Normal View History

2020-05-15 00:34:04 +02:00
using System;
using System.Linq;
using System.Text;
2020-05-15 00:34:04 +02:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Font;
namespace MLEM.Formatting {
public struct TokenizedString {
public readonly string RawString;
public readonly string String;
public readonly Token[] Tokens;
public TokenizedString(string rawString, string strg, Token[] tokens) {
this.RawString = rawString;
this.String = strg;
this.Tokens = tokens;
}
public void Split(GenericFont font, float width, float scale) {
// a split string has the same character count as the input string
// but with newline characters added
2020-05-15 00:34:04 +02:00
var split = font.SplitString(this.String, width, scale);
foreach (var token in this.Tokens) {
var index = 0;
var length = 0;
var ret = new StringBuilder();
// this is basically a substring function that ignores newlines for indexing
for (var i = 0; i < split.Length; i++) {
// if we're within the bounds of the token's substring, append to the new substring
if (index >= token.Index && length < token.Substring.Length)
ret.Append(split[i]);
// if the current char is not a newline, we simulate length increase
if (split[i] != '\n') {
if (index >= token.Index)
length++;
index++;
}
}
token.Substring = ret.ToString();
}
2020-05-15 00:34:04 +02:00
}
public void Draw(GameTime time, SpriteBatch batch, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
var innerOffset = new Vector2();
foreach (var token in this.Tokens) {
var drawFont = token.GetFont() ?? font;
var drawColor = token.GetColor() ?? color;
foreach (var c in token.Substring) {
if (c == '\n') {
innerOffset.X = 0;
innerOffset.Y += font.LineHeight * scale;
continue;
}
var cString = c.ToString();
token.DrawCharacter(time, batch, c, cString, pos + innerOffset, drawFont, drawColor, scale, depth);
innerOffset.X += font.MeasureString(cString).X * scale;
}
}
}
}
}