2020-05-15 00:34:04 +02:00
using System.Text.RegularExpressions ;
using Microsoft.Xna.Framework ;
using Microsoft.Xna.Framework.Graphics ;
using MLEM.Font ;
2020-05-15 14:22:33 +02:00
using MLEM.Misc ;
2020-05-15 00:34:04 +02:00
namespace MLEM.Formatting.Codes {
2020-05-21 12:53:42 +02:00
/// <summary>
/// An instance of a formatting code that can be used for a <see cref="TextFormatter"/>.
/// To add a new formatting code, see <see cref="TextFormatter.Codes"/>
/// </summary>
2020-05-15 14:22:33 +02:00
public class Code : GenericDataHolder {
2020-05-15 00:34:04 +02:00
2020-05-21 12:53:42 +02:00
/// <summary>
/// The regex that this code was created from
/// </summary>
2020-05-15 19:55:59 +02:00
public readonly Regex Regex ;
2020-05-21 12:53:42 +02:00
/// <summary>
/// The match that this code encompasses
/// </summary>
2020-05-15 00:34:04 +02:00
public readonly Match Match ;
2020-05-21 12:53:42 +02:00
/// <summary>
/// The token that this formatting code is a part of
/// </summary>
2020-05-15 00:34:04 +02:00
public Token Token { get ; internal set ; }
2020-05-21 12:53:42 +02:00
/// <summary>
/// Creates a new formatting code based on a formatting code regex and its match.
/// </summary>
/// <param name="match">The match</param>
/// <param name="regex">The regex</param>
2020-05-15 19:55:59 +02:00
protected Code ( Match match , Regex regex ) {
2020-05-15 00:34:04 +02:00
this . Match = match ;
2020-05-15 19:55:59 +02:00
this . Regex = regex ;
2020-05-15 00:34:04 +02:00
}
2020-05-21 12:53:42 +02:00
/// <summary>
/// Returns whether this formatting code should end when the passed formatting code starts.
/// If this method returns true, a new <see cref="Token"/> is started at its position.
/// </summary>
/// <param name="other">The code that is started here</param>
/// <returns>If this code should end</returns>
2020-05-15 00:34:04 +02:00
public virtual bool EndsHere ( Code other ) {
return other . GetType ( ) = = this . GetType ( ) ;
}
2020-05-21 12:53:42 +02:00
/// <inheritdoc cref="Formatting.Token.GetColor"/>
2020-05-17 00:10:29 +02:00
public virtual Color ? GetColor ( Color defaultPick ) {
2020-05-15 00:34:04 +02:00
return null ;
}
2020-05-21 12:53:42 +02:00
/// <inheritdoc cref="Formatting.Token.GetFont"/>
2020-05-17 00:10:29 +02:00
public virtual GenericFont GetFont ( GenericFont defaultPick ) {
2020-05-15 00:34:04 +02:00
return null ;
}
2020-05-21 12:53:42 +02:00
/// <summary>
/// Update this formatting code's animations etc.
/// </summary>
/// <param name="time">The game's time</param>
2020-05-15 14:22:33 +02:00
public virtual void Update ( GameTime time ) {
}
2020-05-21 12:53:42 +02:00
/// <summary>
/// Returns the string that this formatting code should be replaced with.
/// Usually, you'll just want an empty string here, but some formatting codes (like <see cref="ImageCode"/>) require their space to be filled by spaces.
/// </summary>
/// <param name="font">The font that is used</param>
/// <returns>The replacement string for this formatting code</returns>
2020-05-15 19:55:59 +02:00
public virtual string GetReplacementString ( GenericFont font ) {
return string . Empty ;
}
2020-05-21 12:53:42 +02:00
/// <inheritdoc cref="Formatting.Token.DrawCharacter"/>
2020-05-15 14:22:33 +02:00
public virtual bool DrawCharacter ( GameTime time , SpriteBatch batch , char c , string cString , int indexInToken , ref Vector2 pos , GenericFont font , ref Color color , ref float scale , float depth ) {
2020-05-15 00:34:04 +02:00
return false ;
}
2020-05-21 12:53:42 +02:00
/// <inheritdoc cref="Formatting.Token.DrawSelf"/>
2020-05-15 19:55:59 +02:00
public virtual void DrawSelf ( GameTime time , SpriteBatch batch , Vector2 pos , GenericFont font , Color color , float scale , float depth ) {
}
2020-05-21 12:53:42 +02:00
/// <summary>
/// Creates a new formatting code from the given regex and regex match.
/// <seealso cref="TextFormatter.Codes"/>
/// </summary>
/// <param name="formatter">The text formatter that created this code</param>
/// <param name="match">The match for the code's regex</param>
/// <param name="regex">The regex used to create this code</param>
2020-05-15 19:55:59 +02:00
public delegate Code Constructor ( TextFormatter formatter , Match match , Regex regex ) ;
2020-05-15 00:34:04 +02:00
}
}