mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
added the option to auto-scale both the camera and ui systems
This commit is contained in:
parent
3132074a39
commit
dfe31e6726
3 changed files with 30 additions and 6 deletions
|
@ -58,6 +58,10 @@ namespace Demos {
|
||||||
this.UiSystem.Style = style;
|
this.UiSystem.Style = style;
|
||||||
// scale every ui up by 5
|
// scale every ui up by 5
|
||||||
this.UiSystem.GlobalScale = 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
|
// 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));
|
var root = new Panel(Anchor.Center, new Vector2(80, 100), Vector2.Zero, false, true, new Point(5, 10));
|
||||||
|
|
|
@ -17,9 +17,16 @@ namespace MLEM.Ui {
|
||||||
public readonly InputHandler InputHandler;
|
public readonly InputHandler InputHandler;
|
||||||
private readonly bool isInputOurs;
|
private readonly bool isInputOurs;
|
||||||
|
|
||||||
|
public bool AutoScaleWithScreen;
|
||||||
|
public Point AutoScaleReferenceSize;
|
||||||
|
|
||||||
private float globalScale = 1;
|
private float globalScale = 1;
|
||||||
public float GlobalScale {
|
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 {
|
set {
|
||||||
this.globalScale = value;
|
this.globalScale = value;
|
||||||
foreach (var root in this.rootElements)
|
foreach (var root in this.rootElements)
|
||||||
|
@ -49,6 +56,7 @@ namespace MLEM.Ui {
|
||||||
this.isInputOurs = inputHandler == null;
|
this.isInputOurs = inputHandler == null;
|
||||||
this.style = style;
|
this.style = style;
|
||||||
this.Viewport = device.Viewport.Bounds;
|
this.Viewport = device.Viewport.Bounds;
|
||||||
|
this.AutoScaleReferenceSize = this.Viewport.Size;
|
||||||
|
|
||||||
window.ClientSizeChanged += (sender, args) => {
|
window.ClientSizeChanged += (sender, args) => {
|
||||||
this.Viewport = device.Viewport.Bounds;
|
this.Viewport = device.Viewport.Bounds;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MLEM.Extensions;
|
using MLEM.Extensions;
|
||||||
|
@ -7,12 +8,22 @@ namespace MLEM.Cameras {
|
||||||
|
|
||||||
public Vector2 Position;
|
public Vector2 Position;
|
||||||
public float Scale;
|
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 {
|
public Matrix ViewMatrix {
|
||||||
get {
|
get {
|
||||||
var pos = -this.Position * this.Scale;
|
var sc = this.ActualScale;
|
||||||
|
var pos = -this.Position * sc;
|
||||||
if (this.roundPosition)
|
if (this.roundPosition)
|
||||||
pos = pos.Floor();
|
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 {
|
public Vector2 Max {
|
||||||
|
@ -23,14 +34,15 @@ namespace MLEM.Cameras {
|
||||||
get => this.Position + this.ScaledViewport / 2;
|
get => this.Position + this.ScaledViewport / 2;
|
||||||
set => this.Position = value - this.ScaledViewport / 2;
|
set => this.Position = value - this.ScaledViewport / 2;
|
||||||
}
|
}
|
||||||
public Viewport Viewport => this.graphicsDevice.Viewport;
|
public Rectangle Viewport => this.graphicsDevice.Viewport.Bounds;
|
||||||
public Vector2 ScaledViewport => new Vector2(this.Viewport.Width, this.Viewport.Height) / this.Scale;
|
public Vector2 ScaledViewport => new Vector2(this.Viewport.Width, this.Viewport.Height) / this.ActualScale;
|
||||||
|
|
||||||
private readonly bool roundPosition;
|
private readonly bool roundPosition;
|
||||||
private readonly GraphicsDevice graphicsDevice;
|
private readonly GraphicsDevice graphicsDevice;
|
||||||
|
|
||||||
public Camera(GraphicsDevice graphicsDevice, bool roundPosition = true) {
|
public Camera(GraphicsDevice graphicsDevice, bool roundPosition = true) {
|
||||||
this.graphicsDevice = graphicsDevice;
|
this.graphicsDevice = graphicsDevice;
|
||||||
|
this.AutoScaleReferenceSize = this.Viewport.Size;
|
||||||
this.roundPosition = roundPosition;
|
this.roundPosition = roundPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue