1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-29 03:23:37 +02:00
MLEM/MLEM/Formatting/Token.cs

54 lines
2 KiB
C#
Raw Normal View History

2020-05-15 19:56:16 +02:00
using System;
2020-05-15 00:34:04 +02:00
using System.Collections.Generic;
using System.Linq;
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 Token : GenericDataHolder {
2020-05-15 00:34:04 +02:00
public readonly Code[] AppliedCodes;
public readonly int Index;
public readonly int RawIndex;
public string Substring { get; internal set; }
public readonly string RawSubstring;
public Token(Code[] appliedCodes, int index, int rawIndex, string substring, string rawSubstring) {
this.AppliedCodes = appliedCodes;
this.Index = index;
this.RawIndex = rawIndex;
this.Substring = substring;
this.RawSubstring = rawSubstring;
2020-05-15 22:15:24 +02:00
2020-05-15 00:34:04 +02:00
foreach (var code in appliedCodes)
code.Token = this;
}
public Color? GetColor() {
return this.AppliedCodes.Select(c => c.GetColor()).FirstOrDefault(c => c.HasValue);
}
public GenericFont GetFont() {
return this.AppliedCodes.Select(c => c.GetFont()).FirstOrDefault();
}
2020-05-15 19:56:16 +02:00
public void DrawSelf(GameTime time, SpriteBatch batch, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
foreach (var code in this.AppliedCodes)
code.DrawSelf(time, batch, pos, font, color, scale, depth);
}
public void DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
2020-05-15 00:34:04 +02:00
foreach (var code in this.AppliedCodes) {
if (code.DrawCharacter(time, batch, c, cString, indexInToken, ref pos, font, ref color, ref scale, depth))
2020-05-15 00:34:04 +02:00
return;
}
// if no code drew, we have to do it ourselves
font.DrawString(batch, cString, pos, color, 0, Vector2.Zero, scale, SpriteEffects.None, depth);
}
}
}