2020-05-15 19:55:59 +02:00
|
|
|
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 {
|
2020-05-21 12:53:42 +02:00
|
|
|
/// <inheritdoc />
|
2020-05-15 19:55:59 +02:00
|
|
|
public class ImageCode : Code {
|
|
|
|
|
|
|
|
private readonly SpriteAnimation image;
|
2020-12-29 12:41:29 +01:00
|
|
|
private readonly bool copyTextColor;
|
2020-05-15 19:55:59 +02:00
|
|
|
|
2020-05-21 12:53:42 +02:00
|
|
|
/// <inheritdoc />
|
2020-12-29 12:41:29 +01:00
|
|
|
public ImageCode(Match match, Regex regex, SpriteAnimation image, bool copyTextColor) : base(match, regex) {
|
2020-05-15 19:55:59 +02:00
|
|
|
this.image = image;
|
2020-12-29 12:41:29 +01:00
|
|
|
this.copyTextColor = copyTextColor;
|
2020-05-15 19:55:59 +02:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:53:42 +02:00
|
|
|
/// <inheritdoc />
|
2020-05-15 19:55:59 +02:00
|
|
|
public override bool EndsHere(Code other) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:53:42 +02:00
|
|
|
/// <inheritdoc />
|
2020-05-15 19:55:59 +02:00
|
|
|
public override string GetReplacementString(GenericFont font) {
|
2020-09-28 20:43:37 +02:00
|
|
|
return GenericFont.OneEmSpace.ToCachedString();
|
2020-05-15 19:55:59 +02:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:53:42 +02:00
|
|
|
/// <inheritdoc />
|
2020-05-15 19:55:59 +02:00
|
|
|
public override void Update(GameTime time) {
|
|
|
|
this.image.Update(time);
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:53:42 +02:00
|
|
|
/// <inheritdoc />
|
2020-05-15 19:55:59 +02:00
|
|
|
public override void DrawSelf(GameTime time, SpriteBatch batch, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
|
2020-12-29 12:41:29 +01:00
|
|
|
var actualColor = this.copyTextColor ? color : Color.White.CopyAlpha(color);
|
|
|
|
batch.Draw(this.image.CurrentRegion, new RectangleF(pos, new Vector2(font.LineHeight * scale)), actualColor);
|
2020-05-15 19:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:53:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// A set of extensions that allow easily adding image formatting codes to a text formatter.
|
|
|
|
/// </summary>
|
2020-05-15 19:55:59 +02:00
|
|
|
public static class ImageCodeExtensions {
|
|
|
|
|
2020-05-21 12:53:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Adds a new image formatting code to the given text formatter
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="formatter">The formatter to add the code to</param>
|
|
|
|
/// <param name="name">The name of the formatting code. The regex for this code will be between angle brackets.</param>
|
|
|
|
/// <param name="image">The image to render at the code's position</param>
|
2020-12-29 12:41:29 +01:00
|
|
|
/// <param name="copyTextColor">Whether or not the image code should use the text's color instead of White</param>
|
|
|
|
public static void AddImage(this TextFormatter formatter, string name, TextureRegion image, bool copyTextColor = false) {
|
|
|
|
formatter.AddImage(name, new SpriteAnimation(1, image), copyTextColor);
|
2020-05-15 19:55:59 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 12:41:29 +01:00
|
|
|
/// <inheritdoc cref="AddImage(MLEM.Formatting.TextFormatter,string,MLEM.Textures.TextureRegion,bool)"/>
|
|
|
|
public static void AddImage(this TextFormatter formatter, string name, SpriteAnimation image, bool copyTextColor = false) {
|
|
|
|
formatter.Codes.Add(new Regex($"<i {name}>"), (f, m, r) => new ImageCode(m, r, image, copyTextColor));
|
2020-05-15 19:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|