MonoGame - 适当的 SpriteFont 缩放

MonoGame - proper SpriteFont scaling

我正在为我的游戏制作 GUI,但现在我被困在动画上。我需要在鼠标悬停在字体上时放大字体,在鼠标悬停时缩小字体。这是我的代码:

// Update()
if (!IsDisabled)
{
    elapsedSecondsFast = (float)gameTime.ElapsedGameTime.TotalSeconds * 3;
    if (Size.Contains(InputManager.MouseRect))
    {
        scale += elapsedSecondsFast;
        if (scale >= 1.05f) scale = 1.05f;
    }
    else
    {
        scale -= elapsedSecondsFast;
        if (scale <= 1.0f) scale = 1.0f;
    }
}
// Draw()
if ((PrimaryFont != null) && (SecondaryFont != null) && (!string.IsNullOrEmpty(Text)))
{
    if (IsHovered) TextOutliner.DrawBorderedText(spriteBatch, SecondaryFont, Text, new Vector2(TextRectangle.X, TextRectangle.Y), ForeColor, 0.0f, new Vector2((SecondaryFont.MeasureString(Text).X / 2 * scale - SecondaryFont.MeasureString(Text).X / 2), (SecondaryFont.MeasureString(Text).Y / 2 * scale - SecondaryFont.MeasureString(Text).Y / 2)), scale);
    else TextOutliner.DrawBorderedText(spriteBatch, PrimaryFont, Text, new Vector2(TextRectangle.X, TextRectangle.Y), ForeColor, 0.0f, new Vector2(PrimaryFont.MeasureString(Text).X / 2 * scale - PrimaryFont.MeasureString(Text).X / 2, (PrimaryFont.MeasureString(Text).Y / 2 * scale - PrimaryFont.MeasureString(Text).Y / 2)), scale);
}

以上是我的Buttonclass继承的GUIElementclass。让我简要解释一下我的代码:

问题

由于我正在缩放字体(origin = center 我认为)它将不再位于按钮的中心。因为我一直在使用幻数将未缩放的文本定位在按钮纹理的中心,所以我不想被迫对缩放文本做同样的事情。我正在寻找一种算法,无论文本是否缩放,始终将文本定位在我的 270x72 纹理的中间,同时保持上面显示的 scale 动画,每个实例Buttonclass。最好让它的原点在中心。

编辑

所以我应该这样画:

if (IsHovered) TextOutliner.DrawBorderedText(spriteBatch, SecondaryFont, Text, new Vector2(TextRectangle.X, TextRectangle.Y), ForeColor, 0.0f, new Vector2((SecondaryFont.MeasureString(Text).X / 2), (SecondaryFont.MeasureString(Text).Y / 2)), scale);
else TextOutliner.DrawBorderedText(spriteBatch, PrimaryFont, Text, new Vector2(TextRectangle.X, TextRectangle.Y), ForeColor, 0.0f, new Vector2(PrimaryFont.MeasureString(Text).X / 2, (PrimaryFont.MeasureString(Text).Y / 2)), scale);

然后在 btn.Size.Width / 2, btn.Size.Height / 2, (int)MainGame.GameFontLarge.MeasureString("Play").X / 2, (int)MainGame.GameFontLarge.MeasureString("Play").Y / 2

处绘制按钮的文本

所以我最终自己找到了算法,并最终消除了对文本位置使用幻数的需要。这是我的技巧:

TextOutliner.DrawBorderedText(spriteBatch, Font, Text, new Vector2(Size.X + ((Size.Width - Font.MeasureString(Text).X) / 2), Size.Y + ((Size.Height - Font.MeasureString(Text).Y)) / 2), ForeColor, 0.0f, new Vector2((Font.MeasureString(Text).X / 2 * scale - Font.MeasureString(Text).X / 2), (Font.MeasureString(Text).Y / 2 * scale - Font.MeasureString(Text).Y / 2)), scale);

我无法按照@LibertyLocked 的建议从 origin 等式中取出 scale,因为鼠标悬停时字体是从左上角开始缩放的,而不是我想要的中心