using System.Text.RegularExpressions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Animations;
using MLEM.Extensions;
using MLEM.Font;
using MLEM.Misc;
using MLEM.Textures;
namespace MLEM.Formatting.Codes {
///
public class ImageCode : Code {
private readonly SpriteAnimation image;
private readonly bool copyTextColor;
///
public ImageCode(Match match, Regex regex, SpriteAnimation image, bool copyTextColor) : base(match, regex) {
this.image = image;
this.copyTextColor = copyTextColor;
}
///
public override bool EndsHere(Code other) {
return true;
}
///
public override float GetSelfWidth(GenericFont font) {
return font.LineHeight;
}
///
public override void Update(GameTime time) {
this.image.Update(time);
}
///
public override void DrawSelf(GameTime time, SpriteBatch batch, Token token, Vector2 stringPos, Vector2 charPosOffset, GenericFont font, Color color, Vector2 scale, float rotation, Vector2 origin, float depth, SpriteEffects effects, Vector2 stringSize) {
var actualColor = this.copyTextColor ? color : Color.White.CopyAlpha(color);
var size = new Vector2(font.LineHeight);
var finalPos = font.TransformSingleCharacter(stringPos, charPosOffset, rotation, origin, scale, effects, stringSize, size);
batch.Draw(this.image.CurrentRegion, new RectangleF(finalPos, size * scale), actualColor, rotation, Vector2.Zero, effects, 0);
}
}
///
/// A set of extensions that allow easily adding image formatting codes to a text formatter.
///
public static class ImageCodeExtensions {
///
/// Adds a new image formatting code to the given text formatter
///
/// The formatter to add the code to
/// The name of the formatting code. The regex for this code will be between angle brackets.
/// The image to render at the code's position
/// Whether or not the image code should use the text's color instead of White
public static void AddImage(this TextFormatter formatter, string name, TextureRegion image, bool copyTextColor = false) {
formatter.AddImage(name, new SpriteAnimation(1, image), copyTextColor);
}
///
public static void AddImage(this TextFormatter formatter, string name, SpriteAnimation image, bool copyTextColor = false) {
formatter.Codes.Add(new Regex($""), (f, m, r) => new ImageCode(m, r, image, copyTextColor));
}
}
}