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

107 lines
4.3 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;
using MLEM.Formatting.Codes;
using MLEM.Misc;
2020-05-15 00:34:04 +02:00
namespace MLEM.Formatting {
public class TokenizedString : GenericDataHolder {
2020-05-15 00:34:04 +02:00
public readonly string RawString;
2020-05-15 19:55:59 +02:00
public string String { get; private set; }
2020-05-15 00:34:04 +02:00
public readonly Token[] Tokens;
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;
// 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) {
// a split string has the same character count as the input string
// but with newline characters added
2020-05-15 19:55:59 +02:00
this.String = font.SplitString(this.String, width, scale);
// skip splitting logic for unformatted text
if (this.Tokens.Length == 1) {
this.Tokens[0].Substring = this.String;
return;
}
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
2020-05-15 19:55:59 +02:00
for (var i = 0; i < this.String.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)
2020-05-15 19:55:59 +02:00
ret.Append(this.String[i]);
// if the current char is not a newline, we simulate length increase
2020-05-15 19:55:59 +02:00
if (this.String[i] != '\n') {
if (index >= token.Index)
length++;
index++;
}
}
token.Substring = ret.ToString();
}
2020-05-15 00:34:04 +02:00
}
2020-05-15 19:55:59 +02:00
public Vector2 Measure(GenericFont font) {
return font.MeasureString(this.String);
}
public void Update(GameTime time) {
foreach (var code in this.AllCodes)
code.Update(time);
}
2020-05-15 22:15:24 +02:00
public Token GetTokenUnderPos(GenericFont font, Vector2 stringPos, Vector2 target, float scale) {
var innerOffset = new Vector2();
foreach (var token in this.Tokens) {
var split = token.Substring.Split('\n');
for (var i = 0; i < split.Length; i++) {
var size = font.MeasureString(split[i]) * scale;
var lineArea = new RectangleF(stringPos + innerOffset, size);
if (lineArea.Contains(target))
return token;
if (i < split.Length - 1) {
innerOffset.X = 0;
innerOffset.Y += font.LineHeight * scale;
} else {
innerOffset.X += size.X;
}
}
}
return null;
}
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;
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;
}
2020-05-15 19:55:59 +02:00
if (i == 0)
token.DrawSelf(time, batch, pos + innerOffset, font, color, scale, depth);
2020-05-15 00:34:04 +02:00
var cString = c.ToString();
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;
}
}
}
}
}