2020-05-15 00:34:04 +02:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
2020-05-15 13:16:03 +02:00
|
|
|
using System.Text;
|
2020-05-15 00:34:04 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using MLEM.Font;
|
2020-05-15 14:22:33 +02:00
|
|
|
using MLEM.Formatting.Codes;
|
|
|
|
using MLEM.Misc;
|
2020-05-15 00:34:04 +02:00
|
|
|
|
|
|
|
namespace MLEM.Formatting {
|
2020-05-15 14:22:33 +02:00
|
|
|
public class TokenizedString : GenericDataHolder {
|
2020-05-15 00:34:04 +02:00
|
|
|
|
|
|
|
public readonly string RawString;
|
|
|
|
public readonly string String;
|
|
|
|
public readonly Token[] Tokens;
|
2020-05-15 14:22:33 +02:00
|
|
|
public readonly Code[] AllCodes;
|
2020-05-15 00:34:04 +02:00
|
|
|
|
|
|
|
public TokenizedString(string rawString, string strg, Token[] tokens) {
|
|
|
|
this.RawString = rawString;
|
|
|
|
this.String = strg;
|
|
|
|
this.Tokens = tokens;
|
2020-05-15 14:22:33 +02:00
|
|
|
// since a code can be present in multiple tokens, we use Distinct here
|
|
|
|
this.AllCodes = tokens.SelectMany(t => t.AppliedCodes).Distinct().ToArray();
|
2020-05-15 00:34:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Split(GenericFont font, float width, float scale) {
|
2020-05-15 13:16:03 +02:00
|
|
|
// 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);
|
2020-05-15 13:16:03 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-15 14:22:33 +02:00
|
|
|
public void Update(GameTime time) {
|
|
|
|
foreach (var code in this.AllCodes)
|
|
|
|
code.Update(time);
|
|
|
|
}
|
|
|
|
|
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;
|
2020-05-15 14:22:33 +02:00
|
|
|
for (var i = 0; i < token.Substring.Length; i++) {
|
|
|
|
var c = token.Substring[i];
|
2020-05-15 00:34:04 +02:00
|
|
|
if (c == '\n') {
|
|
|
|
innerOffset.X = 0;
|
|
|
|
innerOffset.Y += font.LineHeight * scale;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cString = c.ToString();
|
2020-05-15 14:22:33 +02:00
|
|
|
token.DrawCharacter(time, batch, c, cString, i, pos + innerOffset, drawFont, drawColor, scale, depth);
|
2020-05-15 00:34:04 +02:00
|
|
|
innerOffset.X += font.MeasureString(cString).X * scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|