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

2
Engine

Submodule Engine updated: 499f875903...fe0173b091

View File

@@ -53,11 +53,11 @@ public class CameraController : Behaviour, IFirstFrameUpdate, IUpdate
cameraBehaviour.Graphics.IsFullScreen = true; cameraBehaviour.Graphics.IsFullScreen = true;
cameraBehaviour.Graphics.ApplyChanges(); cameraBehaviour.Graphics.ApplyChanges();
float previousScreenSize = Math.Sqrt(Math.Sqr(cameraBehaviour.Viewport.Width) + Math.Sqr(cameraBehaviour.Viewport.Height)); float previousScreenSize = Math.Sqrt(Math.Sqr(cameraBehaviour.Viewport.X) + Math.Sqr(cameraBehaviour.Viewport.Y));
float currentScreenSize = Math.Sqrt(Math.Sqr(cameraBehaviour.Graphics.GraphicsDevice.Viewport.Width) + Math.Sqr(cameraBehaviour.Graphics.GraphicsDevice.Viewport.Height)); float currentScreenSize = Math.Sqrt(Math.Sqr(cameraBehaviour.Graphics.GraphicsDevice.Viewport.Width) + Math.Sqr(cameraBehaviour.Graphics.GraphicsDevice.Viewport.Height));
defaultZoomLevel /= previousScreenSize / currentScreenSize; defaultZoomLevel /= previousScreenSize / currentScreenSize;
cameraBehaviour.Zoom /= previousScreenSize / currentScreenSize; cameraBehaviour.Zoom /= previousScreenSize / currentScreenSize;
cameraBehaviour.Viewport = cameraBehaviour.Graphics.GraphicsDevice.Viewport; cameraBehaviour.Viewport = new(cameraBehaviour.Graphics.GraphicsDevice.Viewport.Width, cameraBehaviour.Graphics.GraphicsDevice.Viewport.Height);
} }
private void ResetCamera(IButtonInputs<Keys> sender, IButtonInputs<Keys>.ButtonCallbackArguments args) private void ResetCamera(IButtonInputs<Keys> sender, IButtonInputs<Keys>.ButtonCallbackArguments args)

View File

@@ -8,24 +8,49 @@ namespace Pong.Behaviours;
public class Label : Behaviour2D, IDrawableSprite, ILoadContent public class Label : Behaviour2D, IDrawableSprite, ILoadContent
{ {
public SpriteFont? Font { get; set; } = null;
public ColorRGBA Color { get; set; } = new ColorRGBA(255, 255, 255, 255); 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) get => field;
return; 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) public void LoadContent(ContentManager content)
{ {
Font ??= content.Load<SpriteFont>("UbuntuMono"); 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() { }
public Label(SpriteFont font) => Font = font; public Label(SpriteFont font) => Font = font;
} }

View File

@@ -38,11 +38,11 @@ public static class PongUniverse
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
universe.InstantiateUniverseObject().SetUniverseObject("Score Left") universe.InstantiateUniverseObject().SetUniverseObject("Score Left")
.BehaviourController.AddBehaviour<Transform2D>().SetTransform(position: new Vector2D(-250f, 250f), scale: Vector2D.One * .25f) .BehaviourController.AddBehaviour<Transform2D>().SetTransform(position: new Vector2D(-250f, 125f), scale: Vector2D.One * .25f)
.BehaviourController.AddBehaviour<ScoreLabel>(true); .BehaviourController.AddBehaviour<ScoreLabel>(true);
universe.InstantiateUniverseObject().SetUniverseObject("Score Right") universe.InstantiateUniverseObject().SetUniverseObject("Score Right")
.BehaviourController.AddBehaviour<Transform2D>().SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f) .BehaviourController.AddBehaviour<Transform2D>().SetTransform(position: new Vector2D(250f, 125f), scale: Vector2D.One * .25f)
.BehaviourController.AddBehaviour<ScoreLabel>(false); .BehaviourController.AddBehaviour<ScoreLabel>(false);
return universe; return universe;
@@ -72,7 +72,7 @@ public static class PongUniverse
universe.InstantiateUniverseObject().SetUniverseObject("Pong Game Starter", parent: pongManager.UniverseObject) universe.InstantiateUniverseObject().SetUniverseObject("Pong Game Starter", parent: pongManager.UniverseObject)
.BehaviourController.AddBehaviour<PongGameStarter>() .BehaviourController.AddBehaviour<PongGameStarter>()
.BehaviourController.AddBehaviour<Transform2D>().SetTransform(position: new Vector2D(-24, 250f), scale: Vector2D.One * .5f) .BehaviourController.AddBehaviour<Transform2D>().SetTransform(position: new Vector2D(-24, 125f), scale: Vector2D.One * .5f)
.BehaviourController.AddBehaviour<Label>(); .BehaviourController.AddBehaviour<Label>();
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////