mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-12-24 17:29:23 +01:00
allow GetTokenUnderPos to work with rotation, origin and effects
This commit is contained in:
parent
fdb0571860
commit
fc2d705526
3 changed files with 35 additions and 12 deletions
|
@ -75,6 +75,10 @@ namespace Demos {
|
|||
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();
|
||||
|
@ -86,13 +90,14 @@ namespace Demos {
|
|||
}
|
||||
|
||||
// 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) {
|
||||
|
|
|
@ -149,6 +149,11 @@ namespace MLEM.Formatting {
|
|||
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.
|
||||
|
@ -156,7 +161,7 @@ namespace MLEM.Formatting {
|
|||
/// <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));
|
||||
}
|
||||
|
||||
|
|
|
@ -162,17 +162,30 @@ namespace MLEM.Formatting {
|
|||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue