Files
Engine-Pong/Shared/Behaviours/Label.cs

57 lines
1.3 KiB
C#

using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Engine.Core;
using Engine.Integration.MonoGame;
namespace Pong.Behaviours;
public class Label : Behaviour2D, IDrawableSprite, ILoadContent
{
public ColorRGBA Color { get; set; } = new ColorRGBA(255, 255, 255, 255);
public SpriteFont? Font
{
get => field;
set
{
field = value;
UpdateTextSize();
}
} = null;
public string Text
{
get => field;
set
{
field = value;
UpdateTextSize();
}
} = string.Empty;
private Vector2D textSize = Vector2D.Zero;
public void LoadContent(ContentManager content)
{
Font ??= content.Load<SpriteFont>("UbuntuMono");
}
public void Draw(ISpriteBatch spriteBatch)
{
if (Font is null)
return;
spriteBatch.DrawString(Font, Text, Transform.Position, Color.ToPreMultipliedColor(), Transform.Rotation, textSize * .5f, Transform.Scale.Magnitude, SpriteEffects.FlipVertically, 0f);
}
private void UpdateTextSize()
{
if (Font is not null)
textSize = Font.MeasureString(Text).ToVector2D();
}
public Label() { }
public Label(SpriteFont font) => Font = font;
}