Skip to content

Commit

Permalink
allow GetTokenUnderPos to work with rotation, origin and effects
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellpeck committed Apr 10, 2024
1 parent fdb0571 commit fc2d705
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
15 changes: 10 additions & 5 deletions Demos/TextFormattingDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public override void DoDraw(GameTime time) {
var size = this.tokenizedText.GetArea(Vector2.Zero, this.Scale).Size;
var pos = new Vector2(this.GraphicsDevice.Viewport.Width / 2, (this.GraphicsDevice.Viewport.Height - size.Y) / 2);

var rotation = this.transform ? 0.25F : 0;
var origin = this.transform ? new Vector2(size.X / this.Scale, 0) : Vector2.Zero;
var effects = this.transform ? SpriteEffects.FlipHorizontally : SpriteEffects.None;

// draw bounds, which can be toggled with B in this demo
if (this.drawBounds) {
var blank = this.SpriteBatch.GetBlankTexture();
Expand All @@ -86,13 +90,14 @@ public override void DoDraw(GameTime time) {
}

// draw the text itself (start and end indices are optional)
this.tokenizedText.Draw(time, this.SpriteBatch, pos, this.font, Color.White, this.Scale, 0,
this.transform ? 0.25F : 0,
this.transform ? new Vector2(size.X / this.Scale, 128) : Vector2.Zero,
this.transform ? SpriteEffects.FlipHorizontally : SpriteEffects.None,
this.startIndex, this.endIndex);
this.tokenizedText.Draw(time, this.SpriteBatch, pos, this.font, Color.White, this.Scale, 0, rotation, origin, effects, this.startIndex, this.endIndex);

this.SpriteBatch.End();

// an example of how to interact with the text
var hovered = this.tokenizedText.GetTokenUnderPos(pos, this.InputHandler.ViewportMousePosition.ToVector2(), this.Scale, this.font, rotation, origin, effects);
if (hovered != null)
Console.WriteLine($"Hovering \"{hovered.Substring}\"");
}

public override void Update(GameTime time) {
Expand Down
7 changes: 6 additions & 1 deletion MLEM/Formatting/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,19 @@ public void DrawCharacter(GameTime time, SpriteBatch batch, int codePoint, strin
font.DrawCharacter(batch, codePoint, character, finalPos, color, rotation, scale, effects, depth);
}

/// <inheritdoc cref="GetArea(Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2)"/>
public IEnumerable<RectangleF> GetArea(Vector2 stringPos, float scale) {
return this.GetArea(stringPos, new Vector2(scale));
}

/// <summary>
/// Gets a list of rectangles that encompass this token's area.
/// This can be used to invoke events when the mouse is hovered over the token, for example.
/// </summary>
/// <param name="stringPos">The position that the string is drawn at</param>
/// <param name="scale">The scale that the string is drawn at</param>
/// <returns>A set of rectangles that this token contains</returns>
public IEnumerable<RectangleF> GetArea(Vector2 stringPos, float scale) {
public IEnumerable<RectangleF> GetArea(Vector2 stringPos, Vector2 scale) {
return this.Area.Select(a => new RectangleF(stringPos + a.Location * scale, a.Size * scale));
}

Expand Down
25 changes: 19 additions & 6 deletions MLEM/Formatting/TokenizedString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,30 @@ public void Update(GameTime time) {
code.Update(time);
}

/// <inheritdoc cref="GetTokenUnderPos(Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2,MLEM.Font.GenericFont,float,Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Graphics.SpriteEffects)"/>
public Token GetTokenUnderPos(Vector2 stringPos, Vector2 target, float scale, GenericFont font = null, float rotation = 0, Vector2 origin = default, SpriteEffects effects = SpriteEffects.None) {
return this.GetTokenUnderPos(stringPos, target, new Vector2(scale), font, rotation, origin, effects);
}

/// <summary>
/// Returns the token under the given position.
/// This can be used for hovering effects when the mouse is over a token, etc.
/// </summary>
/// <param name="stringPos">The position that the string is drawn at</param>
/// <param name="target">The position to use for checking the token</param>
/// <param name="scale">The scale that the string is drawn at</param>
/// <returns>The token under the target position</returns>
public Token GetTokenUnderPos(Vector2 stringPos, Vector2 target, float scale) {
/// <param name="stringPos">The position that the string is drawn at.</param>
/// <param name="target">The position to use for checking the token.</param>
/// <param name="scale">The scale that the string is drawn at.</param>
/// <param name="font">The font that the string is being drawn with. If this is <see langword="null"/>, all following parameters are ignored.</param>
/// <param name="rotation">The rotation that the string is being drawn with. If <paramref name="font"/> is <see langword="null"/>, this this is ignored.</param>
/// <param name="origin">The origin that the string is being drawn with. If <paramref name="font"/> is <see langword="null"/>, this this is ignored.</param>
/// <param name="effects">The sprite effects that the string is being drawn with. If <paramref name="font"/> is <see langword="null"/>, this is ignored.</param>
/// <returns>The token under the target position</returns>^
public Token GetTokenUnderPos(Vector2 stringPos, Vector2 target, Vector2 scale, GenericFont font = null, float rotation = 0, Vector2 origin = default, SpriteEffects effects = SpriteEffects.None) {
if (font != null) {
var transform = font.CalculateStringTransform(stringPos, rotation, origin, scale, effects, this.area.Size);
target = Vector2.Transform(target, Matrix.Invert(transform));
}
foreach (var token in this.Tokens) {
foreach (var rect in token.GetArea(stringPos, scale)) {
foreach (var rect in font != null ? token.GetArea(Vector2.Zero, 1) : token.GetArea(stringPos, scale)) {
if (rect.Contains(target))
return token;
}
Expand Down

0 comments on commit fc2d705

Please sign in to comment.