fix: label positions and sizes

This commit is contained in:
2026-03-28 23:35:15 +03:00
parent 5cb259dba1
commit eb8bf5837c
4 changed files with 39 additions and 14 deletions

View File

@@ -8,24 +8,49 @@ namespace Pong.Behaviours;
public class Label : Behaviour2D, IDrawableSprite, ILoadContent
{
public SpriteFont? Font { get; set; } = null;
public ColorRGBA Color { get; set; } = new ColorRGBA(255, 255, 255, 255);
public int Size { get; set; } = 16;
public string Text { get; set; } = string.Empty;
public void Draw(ISpriteBatch spriteBatch)
public SpriteFont? Font
{
if (Font is null)
return;
get => field;
set
{
field = value;
UpdateTextSize();
}
} = null;
spriteBatch.DrawString(Font, Text, Transform.Position, Color.ToPreMultipliedColor(), Transform.Rotation, Vector2D.One * .5f, Transform.Scale.Magnitude, SpriteEffects.None, 0f);
}
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;
}