mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-10-31 21:00:51 +01:00
Compare commits
2 commits
fdb0571860
...
dd9230abf0
Author | SHA1 | Date | |
---|---|---|---|
dd9230abf0 | |||
fc2d705526 |
4 changed files with 39 additions and 16 deletions
|
@ -72,9 +72,13 @@ namespace Demos {
|
||||||
|
|
||||||
// we draw the tokenized text in the center of the screen
|
// we draw the tokenized text in the center of the screen
|
||||||
// since the text is already center-aligned, we only need to align it on the y axis here
|
// since the text is already center-aligned, we only need to align it on the y axis here
|
||||||
var size = this.tokenizedText.GetArea(Vector2.Zero, this.Scale).Size;
|
var size = this.tokenizedText.GetArea(scale: this.Scale).Size;
|
||||||
var pos = new Vector2(this.GraphicsDevice.Viewport.Width / 2, (this.GraphicsDevice.Viewport.Height - size.Y) / 2);
|
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
|
// draw bounds, which can be toggled with B in this demo
|
||||||
if (this.drawBounds) {
|
if (this.drawBounds) {
|
||||||
var blank = this.SpriteBatch.GetBlankTexture();
|
var blank = this.SpriteBatch.GetBlankTexture();
|
||||||
|
@ -86,13 +90,14 @@ namespace Demos {
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw the text itself (start and end indices are optional)
|
// 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.tokenizedText.Draw(time, this.SpriteBatch, pos, this.font, Color.White, this.Scale, 0, rotation, origin, effects, this.startIndex, this.endIndex);
|
||||||
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.SpriteBatch.End();
|
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) {
|
public override void Update(GameTime time) {
|
||||||
|
|
|
@ -228,7 +228,7 @@ namespace MLEM.Ui.Elements {
|
||||||
this.CheckTextChange();
|
this.CheckTextChange();
|
||||||
this.TokenizeIfNecessary();
|
this.TokenizeIfNecessary();
|
||||||
this.AlignAndSplitIfNecessary(size);
|
this.AlignAndSplitIfNecessary(size);
|
||||||
var textSize = this.tokenizedText.GetArea(Vector2.Zero, this.TextScale * this.TextScaleMultiplier * this.Scale).Size;
|
var textSize = this.tokenizedText.GetArea(scale: this.TextScale * this.TextScaleMultiplier * this.Scale).Size;
|
||||||
// if we auto-adjust our width, then we would also split the same way with our adjusted width, so cache that
|
// if we auto-adjust our width, then we would also split the same way with our adjusted width, so cache that
|
||||||
if (this.AutoAdjustWidth)
|
if (this.AutoAdjustWidth)
|
||||||
this.lastAlignSplitWidth = textSize.X;
|
this.lastAlignSplitWidth = textSize.X;
|
||||||
|
|
|
@ -149,6 +149,11 @@ namespace MLEM.Formatting {
|
||||||
font.DrawCharacter(batch, codePoint, character, finalPos, color, rotation, scale, effects, depth);
|
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 = default, float scale = 1) {
|
||||||
|
return this.GetArea(stringPos, new Vector2(scale));
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a list of rectangles that encompass this token's area.
|
/// 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.
|
/// 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="stringPos">The position that the string is drawn at</param>
|
||||||
/// <param name="scale">The scale 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>
|
/// <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));
|
return this.Area.Select(a => new RectangleF(stringPos + a.Location * scale, a.Size * scale));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -140,7 +140,7 @@ namespace MLEM.Formatting {
|
||||||
/// <inheritdoc cref="GenericFont.MeasureString(string,bool)"/>
|
/// <inheritdoc cref="GenericFont.MeasureString(string,bool)"/>
|
||||||
[Obsolete("Measure is deprecated. Use GetArea, which returns the string's total size measurement, instead.")]
|
[Obsolete("Measure is deprecated. Use GetArea, which returns the string's total size measurement, instead.")]
|
||||||
public Vector2 Measure(GenericFont font) {
|
public Vector2 Measure(GenericFont font) {
|
||||||
return this.GetArea(Vector2.Zero, 1).Size;
|
return this.GetArea().Size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -149,7 +149,7 @@ namespace MLEM.Formatting {
|
||||||
/// <param name="stringPos">The position that this string is being rendered at, which will offset the resulting <see cref="RectangleF"/>.</param>
|
/// <param name="stringPos">The position that this string is being rendered at, which will offset the resulting <see cref="RectangleF"/>.</param>
|
||||||
/// <param name="scale">The scale that this string is being rendered with, which will scale the resulting <see cref="RectangleF"/>.</param>
|
/// <param name="scale">The scale that this string is being rendered with, which will scale the resulting <see cref="RectangleF"/>.</param>
|
||||||
/// <returns>The area that this tokenized string takes up.</returns>
|
/// <returns>The area that this tokenized string takes up.</returns>
|
||||||
public RectangleF GetArea(Vector2 stringPos, float scale) {
|
public RectangleF GetArea(Vector2 stringPos = default, float scale = 1) {
|
||||||
return new RectangleF(stringPos + this.area.Location * scale, this.area.Size * scale);
|
return new RectangleF(stringPos + this.area.Location * scale, this.area.Size * scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,17 +162,30 @@ namespace MLEM.Formatting {
|
||||||
code.Update(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>
|
/// <summary>
|
||||||
/// Returns the token under the given position.
|
/// Returns the token under the given position.
|
||||||
/// This can be used for hovering effects when the mouse is over a token, etc.
|
/// This can be used for hovering effects when the mouse is over a token, etc.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="stringPos">The position that the string is drawn at</param>
|
/// <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="target">The position to use for checking the token.</param>
|
||||||
/// <param name="scale">The scale that the string is drawn at</param>
|
/// <param name="scale">The scale that the string is drawn at.</param>
|
||||||
/// <returns>The token under the target position</returns>
|
/// <param name="font">The font that the string is being drawn with. If this is <see langword="null"/>, all following parameters are ignored.</param>
|
||||||
public Token GetTokenUnderPos(Vector2 stringPos, Vector2 target, float scale) {
|
/// <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 token in this.Tokens) {
|
||||||
foreach (var rect in token.GetArea(stringPos, scale)) {
|
foreach (var rect in font != null ? token.GetArea() : token.GetArea(stringPos, scale)) {
|
||||||
if (rect.Contains(target))
|
if (rect.Contains(target))
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue