1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

added the option to auto-scale both the camera and ui systems

This commit is contained in:
Ellpeck 2019-08-23 19:46:36 +02:00
parent 3132074a39
commit dfe31e6726
3 changed files with 30 additions and 6 deletions

View file

@ -58,6 +58,10 @@ namespace Demos {
this.UiSystem.Style = style;
// scale every ui up by 5
this.UiSystem.GlobalScale = 5;
// set the ui system to automatically scale with screen size
// this will cause all ui elements to be scaled based on the reference resolution (AutoScaleReferenceSize)
// by default, the reference resolution is set to the initial screen size, however this value can be changed through the ui system
this.UiSystem.AutoScaleWithScreen = true;
// create the root panel that all the other components sit on and add it to the ui system
var root = new Panel(Anchor.Center, new Vector2(80, 100), Vector2.Zero, false, true, new Point(5, 10));

View file

@ -17,9 +17,16 @@ namespace MLEM.Ui {
public readonly InputHandler InputHandler;
private readonly bool isInputOurs;
public bool AutoScaleWithScreen;
public Point AutoScaleReferenceSize;
private float globalScale = 1;
public float GlobalScale {
get => this.globalScale;
get {
if (!this.AutoScaleWithScreen)
return this.globalScale;
return Math.Min(this.Viewport.Width / (float) this.AutoScaleReferenceSize.X, this.Viewport.Height / (float) this.AutoScaleReferenceSize.Y) * this.globalScale;
}
set {
this.globalScale = value;
foreach (var root in this.rootElements)
@ -49,6 +56,7 @@ namespace MLEM.Ui {
this.isInputOurs = inputHandler == null;
this.style = style;
this.Viewport = device.Viewport.Bounds;
this.AutoScaleReferenceSize = this.Viewport.Size;
window.ClientSizeChanged += (sender, args) => {
this.Viewport = device.Viewport.Bounds;

View file

@ -1,3 +1,4 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
@ -7,12 +8,22 @@ namespace MLEM.Cameras {
public Vector2 Position;
public float Scale;
public bool AutoScaleWithScreen;
public Point AutoScaleReferenceSize;
public float ActualScale {
get {
if (!this.AutoScaleWithScreen)
return this.Scale;
return Math.Min(this.Viewport.Width / (float) this.AutoScaleReferenceSize.X, this.Viewport.Height / (float) this.AutoScaleReferenceSize.Y) * this.Scale;
}
}
public Matrix ViewMatrix {
get {
var pos = -this.Position * this.Scale;
var sc = this.ActualScale;
var pos = -this.Position * sc;
if (this.roundPosition)
pos = pos.Floor();
return Matrix.CreateScale(this.Scale, this.Scale, 1) * Matrix.CreateTranslation(new Vector3(pos, 0));
return Matrix.CreateScale(sc, sc, 1) * Matrix.CreateTranslation(new Vector3(pos, 0));
}
}
public Vector2 Max {
@ -23,14 +34,15 @@ namespace MLEM.Cameras {
get => this.Position + this.ScaledViewport / 2;
set => this.Position = value - this.ScaledViewport / 2;
}
public Viewport Viewport => this.graphicsDevice.Viewport;
public Vector2 ScaledViewport => new Vector2(this.Viewport.Width, this.Viewport.Height) / this.Scale;
public Rectangle Viewport => this.graphicsDevice.Viewport.Bounds;
public Vector2 ScaledViewport => new Vector2(this.Viewport.Width, this.Viewport.Height) / this.ActualScale;
private readonly bool roundPosition;
private readonly GraphicsDevice graphicsDevice;
public Camera(GraphicsDevice graphicsDevice, bool roundPosition = true) {
this.graphicsDevice = graphicsDevice;
this.AutoScaleReferenceSize = this.Viewport.Size;
this.roundPosition = roundPosition;
}