Scaling support

This commit is contained in:
Royce551 2022-09-25 11:11:15 -05:00
parent f8a3b38352
commit 2efae054ca
7 changed files with 95 additions and 27 deletions

View file

@ -17,14 +17,29 @@ namespace TestGame.Screens
{
public class TestScreen : Screen
{
private bool x = false;
private void Input_KeyDown(object sender, Water.Input.KeyEventArgs e)
{
if (e.Key == Microsoft.Xna.Framework.Input.Keys.Space)
{
stackPanel.AddChild(Game.AddObject(new Aquarium()
if (x)
{
Layout = Layout.Fill,
}));
stackPanel.AddChild(Game.AddObject(new Box()
{
Color = Color.Black,
Margins = new(10, 0)
}));
x = false;
}
else
{
stackPanel.AddChild(Game.AddObject(new Box()
{
Color = Color.DarkBlue,
Margins = new(10, 0)
}));
x = true;
}
}
else if (e.Key == Microsoft.Xna.Framework.Input.Keys.E)
{
@ -50,23 +65,13 @@ namespace TestGame.Screens
{
Game.Input.KeyDown += Input_KeyDown;
Game.Input.KeyUp += Input_KeyUp;
var rc = new RenderContainer(Game.GraphicsDevice)
{
RelativePosition = new(0, 0, 720, 576),
Layout = Layout.Center
};
stackPanel = new UniformStackContainer()
{
RelativePosition = new(0, 0, 100, 100),
Layout = Layout.Fill,
Orientation = Orientation.Horizontal,
};
rc.AddChild(Game.AddObject(new Aquarium()
{
RelativePosition = new(0, 0, 1920, 1080)
}));
AddChild(rc);
AddChild(stackPanel);
}
public override void Deinitialize()
@ -79,10 +84,7 @@ namespace TestGame.Screens
{
if (e.Key == Microsoft.Xna.Framework.Input.Keys.Space)
{
stackPanel.AddChild(Game.AddObject(new Aquarium()
{
Layout = Layout.Fill,
}));
}
}

View file

@ -28,7 +28,7 @@ namespace Water.Graphics
/// </summary>
public Rectangle RelativePosition
{
get => relativePosition;
get => new((int)Math.Round(relativePosition.X * scaleX), (int)Math.Round(relativePosition.Y * scaleY), (int)Math.Round(relativePosition.Width * scaleX), (int)Math.Round(relativePosition.Height * scaleY));
set
{
relativePosition = value;
@ -51,6 +51,28 @@ namespace Water.Graphics
public Margins Margins { get; set; } = new(0);
private float scaleX = 1;
public float ScaleX
{
get => scaleX * (Parent?.ScaleX ?? 1);
set
{
scaleX = value;
CalculateChildrenPositions();
}
}
private float scaleY = 1;
public float ScaleY
{
get => scaleY * (Parent?.ScaleY ?? 1);
set
{
scaleY = value;
CalculateChildrenPositions();
}
}
/// <summary>
/// Adds a child to this container
/// </summary>
@ -203,8 +225,9 @@ namespace Water.Graphics
child.RelativePosition.Width,
child.RelativePosition.Height
)
};
child.ScaleX = ScaleX;
child.ScaleY = ScaleY;
child.CalculateChildrenPositions();
}
}

View file

@ -57,18 +57,18 @@ namespace Water.Graphics.Controls
if (HorizontalTextAlignment != HorizontalTextAlignment.Left)
{
if (HorizontalTextAlignment == HorizontalTextAlignment.Right)
pos.X += ActualPosition.Right - m.X;
pos.X += (ActualPosition.Right - m.X) * ScaleX;
else if (HorizontalTextAlignment == HorizontalTextAlignment.Center)
pos.X += (ActualPosition.Width - m.X) / 2;
pos.X += ((ActualPosition.Width - m.X) / 2) * ScaleX;
}
if (VerticalTextAlignment != VerticalTextAlignment.Top)
{
if (VerticalTextAlignment == VerticalTextAlignment.Bottom)
pos.Y = (ActualPosition.Bottom - (m.Y * ((lines.Length - i) + 1)));
pos.Y = ((ActualPosition.Bottom - (m.Y * ((lines.Length - i) + 1)))) * ScaleY;
else if (VerticalTextAlignment == VerticalTextAlignment.Center)
pos.Y += (ActualPosition.Height - m.Y) / 2;
pos.Y += ((ActualPosition.Height - m.Y) / 2) * ScaleY;
}
spriteBatch.DrawString(font, line, pos, Color);
spriteBatch.DrawString(font, line, pos, Color, new(ScaleX, ScaleY));
pos.Y += LineSpacing;
i++;
@ -85,9 +85,16 @@ namespace Water.Graphics.Controls
}
private float prevScale = 1;
public override void Update(GameTime gameTime)
{
//if (ScaleX != prevScale)
//{
// font.Size *= ScaleX;
//}
//prevScale = ScaleX;
}

View file

@ -20,6 +20,9 @@ namespace Water.Graphics
public Margins Margins { get; set; }
public float ScaleX { get; set; }
public float ScaleY { get; set; }
public void AddChild(IContainer child);
public void RemoveChild(IContainer child);

View file

@ -18,6 +18,19 @@ namespace Water.Graphics.Screens
public List<Screen> Screens { get; } = new();
public bool HasScreens { get => Screens.Count > 0; }
private float gameScale = 1;
public float GameScale
{
get => gameScale;
set
{
gameScale = value;
var newSize = new Rectangle(0, 0, (int)Math.Round(currentScreenSize.Width * GameScale), (int)Math.Round(currentScreenSize.Height * GameScale));
gameObjectManager.MainGame.UpdateWindowSize(newSize);
UpdateScreenProperties(newSize);
}
}
private GameObjectManager gameObjectManager;
private GameWindow window;
@ -121,8 +134,15 @@ namespace Water.Graphics.Screens
public void UpdateScreenSize(Rectangle newSize)
{
currentScreenSize = newSize;
UpdateScreenProperties(newSize);
}
private void UpdateScreenProperties(Rectangle newSize)
{
foreach (var screen in Screens)
{
screen.ScaleX = gameScale;
screen.ScaleY = gameScale;
screen.ActualPosition = newSize;
screen.RelativePosition = newSize;
screen.CalculateChildrenPositions();

View file

@ -110,7 +110,7 @@ namespace Water.Screens
{
1 => $"{Math.Round(drawsPerSecond, 0)} frames/s, {Math.Round(updatesPerSecond, 0)} updates/s, {Game.AllObjects.Count} objects, {ScreenManager.Screens.Count} screens",
2 => $"{Game.Audio.ServiceName}: {Game.Audio.Tracks.Count} tracks, {Game.Audio.Effects.Count} effects",
_ or 3 => $"i don't have anything for this yet lol"
_ or 3 => $"{ScaleX} {ScaleY}"
};
}
else framerateText.Text = $"{Math.Round(drawsPerSecond, 0)} fps";

View file

@ -89,6 +89,13 @@ namespace Water
gameObjectManager.Input.KeyDown += Input_KeyDown;
}
public void UpdateWindowSize(Rectangle newSize)
{
Graphics.PreferredBackBufferWidth = newSize.Width;
Graphics.PreferredBackBufferHeight = newSize.Height;
Graphics.ApplyChanges();
}
private async void LoadConfig() => await LoadConfigAsync(); // this is really cursed :sob:
public async Task LoadConfigAsync()
@ -110,6 +117,12 @@ namespace Water
//Graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
//Graphics.ToggleFullScreen();
break;
case Keys.OemPlus:
Screen.GameScale += .1f;
break;
case Keys.OemMinus:
Screen.GameScale -= .1f;
break;
case Keys.F9:
GC.Collect(2);
break;