1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-01 21:03:38 +02:00

Fixed a formatting Code only knowing about the last Token that it is applied in

Closes #3
This commit is contained in:
Ell 2022-02-13 22:43:51 +01:00
parent 3a275a1820
commit 856d69b7db
9 changed files with 28 additions and 18 deletions

View file

@ -20,6 +20,7 @@ Improvements
- Added InputHandler mouse and touch position querying that preserves the game's viewport - Added InputHandler mouse and touch position querying that preserves the game's viewport
Fixes Fixes
- **Fixed a formatting Code only knowing about the last Token that it is applied in**
- Fixed StaticSpriteBatch handling rotated sprites incorrectly - Fixed StaticSpriteBatch handling rotated sprites incorrectly
Removals Removals

View file

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
@ -20,9 +21,10 @@ namespace MLEM.Formatting.Codes {
/// </summary> /// </summary>
public readonly Match Match; public readonly Match Match;
/// <summary> /// <summary>
/// The token that this formatting code is a part of /// The tokens that this formatting code is a part of.
/// Note that this array only has multiple entries if additional tokens have to be started while this code is still applied.
/// </summary> /// </summary>
public Token Token { get; internal set; } public IList<Token> Tokens { get; internal set; }
/// <summary> /// <summary>
/// Creates a new formatting code based on a formatting code regex and its match. /// Creates a new formatting code based on a formatting code regex and its match.
@ -71,12 +73,12 @@ namespace MLEM.Formatting.Codes {
} }
/// <inheritdoc cref="Formatting.Token.DrawCharacter"/> /// <inheritdoc cref="Formatting.Token.DrawCharacter"/>
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) { public virtual bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
return false; return false;
} }
/// <inheritdoc cref="Formatting.Token.DrawSelf"/> /// <inheritdoc cref="Formatting.Token.DrawSelf"/>
public virtual void DrawSelf(GameTime time, SpriteBatch batch, Vector2 pos, GenericFont font, Color color, float scale, float depth) {} public virtual void DrawSelf(GameTime time, SpriteBatch batch, Token token, Vector2 pos, GenericFont font, Color color, float scale, float depth) {}
/// <summary> /// <summary>
/// Creates a new formatting code from the given regex and regex match. /// Creates a new formatting code from the given regex and regex match.

View file

@ -36,7 +36,7 @@ namespace MLEM.Formatting.Codes {
} }
/// <inheritdoc /> /// <inheritdoc />
public override void DrawSelf(GameTime time, SpriteBatch batch, Vector2 pos, GenericFont font, Color color, float scale, float depth) { public override void DrawSelf(GameTime time, SpriteBatch batch, Token token, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
var actualColor = this.copyTextColor ? color : Color.White.CopyAlpha(color); var actualColor = this.copyTextColor ? color : Color.White.CopyAlpha(color);
batch.Draw(this.image.CurrentRegion, new RectangleF(pos, new Vector2(font.LineHeight * scale)), actualColor); batch.Draw(this.image.CurrentRegion, new RectangleF(pos, new Vector2(font.LineHeight * scale)), actualColor);
} }

View file

@ -20,13 +20,17 @@ namespace MLEM.Formatting.Codes {
/// </summary> /// </summary>
/// <returns>True if this code is currently selected</returns> /// <returns>True if this code is currently selected</returns>
public virtual bool IsSelected() { public virtual bool IsSelected() {
return this.isSelected(this.Token); foreach (var token in this.Tokens) {
if (this.isSelected(token))
return true;
}
return false;
} }
/// <inheritdoc /> /// <inheritdoc />
public override 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) { public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
// since we inherit from UnderlineCode, we can just call base if selected // since we inherit from UnderlineCode, we can just call base if selected
return this.IsSelected() && base.DrawCharacter(time, batch, c, cString, indexInToken, ref pos, font, ref color, ref scale, depth); return this.IsSelected() && base.DrawCharacter(time, batch, c, cString, token, indexInToken, ref pos, font, ref color, ref scale, depth);
} }
} }

View file

@ -18,7 +18,7 @@ namespace MLEM.Formatting.Codes {
} }
/// <inheritdoc /> /// <inheritdoc />
public override 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) { public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
font.DrawString(batch, cString, pos + this.offset * scale, this.color.CopyAlpha(color), 0, Vector2.Zero, scale, SpriteEffects.None, depth); font.DrawString(batch, cString, pos + this.offset * scale, this.color.CopyAlpha(color), 0, Vector2.Zero, scale, SpriteEffects.None, depth);
// we return false since we still want regular drawing to occur // we return false since we still want regular drawing to occur
return false; return false;

View file

@ -19,9 +19,9 @@ namespace MLEM.Formatting.Codes {
} }
/// <inheritdoc /> /// <inheritdoc />
public override 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) { public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
// don't underline spaces at the end of lines // don't underline spaces at the end of lines
if (c == ' ' && this.Token.DisplayString.Length > indexInToken + 1 && this.Token.DisplayString[indexInToken + 1] == '\n') if (c == ' ' && token.DisplayString.Length > indexInToken + 1 && token.DisplayString[indexInToken + 1] == '\n')
return false; return false;
var (w, h) = font.MeasureString(cString) * scale; var (w, h) = font.MeasureString(cString) * scale;
var t = h * this.thickness; var t = h * this.thickness;

View file

@ -28,8 +28,8 @@ namespace MLEM.Formatting.Codes {
} }
/// <inheritdoc /> /// <inheritdoc />
public override 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) { public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) {
var offset = new Vector2(0, (float) Math.Sin(this.Token.Index + indexInToken + this.TimeIntoAnimation.TotalSeconds * this.modifier) * font.LineHeight * this.heightModifier * scale); var offset = new Vector2(0, (float) Math.Sin(token.Index + indexInToken + this.TimeIntoAnimation.TotalSeconds * this.modifier) * font.LineHeight * this.heightModifier * scale);
pos += offset; pos += offset;
// we return false since we still want regular drawing to occur, we just changed the position // we return false since we still want regular drawing to occur, we just changed the position
return false; return false;

View file

@ -50,9 +50,6 @@ namespace MLEM.Formatting {
this.RawIndex = rawIndex; this.RawIndex = rawIndex;
this.Substring = substring; this.Substring = substring;
this.RawSubstring = rawSubstring; this.RawSubstring = rawSubstring;
foreach (var code in appliedCodes)
code.Token = this;
} }
/// <summary> /// <summary>
@ -96,7 +93,7 @@ namespace MLEM.Formatting {
/// <param name="depth">The depth to draw at</param> /// <param name="depth">The depth to draw at</param>
public void DrawSelf(GameTime time, SpriteBatch batch, Vector2 pos, GenericFont font, Color color, float scale, float depth) { public void DrawSelf(GameTime time, SpriteBatch batch, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
foreach (var code in this.AppliedCodes) foreach (var code in this.AppliedCodes)
code.DrawSelf(time, batch, pos, font, color, scale, depth); code.DrawSelf(time, batch, this, pos, font, color, scale, depth);
} }
/// <summary> /// <summary>
@ -114,7 +111,7 @@ namespace MLEM.Formatting {
/// <param name="depth">The depth to draw at</param> /// <param name="depth">The depth to draw at</param>
public void DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, Vector2 pos, GenericFont font, Color color, float scale, float depth) { public void DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, Vector2 pos, GenericFont font, Color color, float scale, float depth) {
foreach (var code in this.AppliedCodes) { foreach (var code in this.AppliedCodes) {
if (code.DrawCharacter(time, batch, c, cString, indexInToken, ref pos, font, ref color, ref scale, depth)) if (code.DrawCharacter(time, batch, c, cString, this, indexInToken, ref pos, font, ref color, ref scale, depth))
return; return;
} }

View file

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
@ -44,8 +45,13 @@ namespace MLEM.Formatting {
this.RawString = rawString; this.RawString = rawString;
this.String = strg; this.String = strg;
this.Tokens = tokens; this.Tokens = tokens;
// since a code can be present in multiple tokens, we use Distinct here // since a code can be present in multiple tokens, we use Distinct here
this.AllCodes = tokens.SelectMany(t => t.AppliedCodes).Distinct().ToArray(); this.AllCodes = tokens.SelectMany(t => t.AppliedCodes).Distinct().ToArray();
// TODO this can probably be optimized by keeping track of a code's tokens while tokenizing
foreach (var code in this.AllCodes)
code.Tokens = new ReadOnlyCollection<Token>(this.Tokens.Where(t => t.AppliedCodes.Contains(code)).ToList());
this.RecalculateTokenData(font, alignment); this.RecalculateTokenData(font, alignment);
} }