1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-23 17:13:38 +02:00

allow setting a custom viewport for ui systems

This commit is contained in:
Ell 2021-03-29 02:15:17 +02:00
parent 35af9eee25
commit 28eafffa32
2 changed files with 22 additions and 24 deletions

View file

@ -84,16 +84,16 @@ namespace MLEM.Ui.Elements {
/// Causes this tooltip's position to be snapped to the mouse position.
/// </summary>
public void SnapPositionToMouse() {
var (w, h) = this.System.Viewport.Size;
var viewport = this.System.Viewport;
var offset = (this.Input.MousePosition.ToVector2() + this.MouseOffset.Value) / this.Scale;
if (offset.X < 0)
offset.X = 0;
if (offset.Y < 0)
offset.Y = 0;
if (offset.X * this.Scale + this.Area.Width >= w)
offset.X = (w - this.Area.Width) / this.Scale;
if (offset.Y * this.Scale + this.Area.Height >= h)
offset.Y = (h - this.Area.Height) / this.Scale;
if (offset.X < viewport.X)
offset.X = viewport.X;
if (offset.Y < viewport.Y)
offset.Y = viewport.Y;
if (offset.X * this.Scale + this.Area.Width >= viewport.Right)
offset.X = (viewport.Right - this.Area.Width) / this.Scale;
if (offset.Y * this.Scale + this.Area.Height >= viewport.Bottom)
offset.Y = (viewport.Bottom - this.Area.Height) / this.Scale;
this.PositionOffset = offset;
}

View file

@ -25,17 +25,13 @@ namespace MLEM.Ui {
/// The graphics device that this ui system uses for its size calculations
/// </summary>
public readonly GraphicsDevice GraphicsDevice;
/// <summary>
/// The game window that this ui system renders within
/// </summary>
public readonly GameWindow Window;
private readonly List<RootElement> rootElements = new List<RootElement>();
/// <summary>
/// The viewport that this ui system is rendering inside of.
/// This is automatically updated during <see cref="GameWindow.ClientSizeChanged"/>
/// </summary>
public Rectangle Viewport { get; private set; }
public Rectangle Viewport;
/// <summary>
/// Set this field to true to cause the ui system and all of its elements to automatically scale up or down with greater and lower resolution, respectively.
/// If this field is true, <see cref="AutoScaleReferenceSize"/> is used as the size that uses default <see cref="GlobalScale"/>
@ -184,22 +180,24 @@ namespace MLEM.Ui {
/// <param name="device">The graphics device that should be used for viewport calculations</param>
/// <param name="style">The style settings that this ui should have. Use <see cref="UntexturedStyle"/> for the default, untextured style.</param>
/// <param name="inputHandler">The input handler that this ui's <see cref="UiControls"/> should use. If none is supplied, a new input handler is created for this ui.</param>
public UiSystem(Game game, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) : base(game) {
/// <param name="automaticViewport">If this value is set to true, the ui system's <see cref="Viewport"/> will be set automatically based on the <see cref="GameWindow"/>'s size. Defaults to true.</param>
public UiSystem(Game game, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null, bool automaticViewport = true) : base(game) {
this.Controls = new UiControls(this, inputHandler);
this.GraphicsDevice = device;
this.Window = game.Window;
this.style = style;
this.Viewport = new Rectangle(Point.Zero, this.Window.ClientBounds.Size);
this.AutoScaleReferenceSize = this.Viewport.Size;
this.Window.ClientSizeChanged += (sender, args) => {
this.Viewport = new Rectangle(Point.Zero, this.Window.ClientBounds.Size);
foreach (var root in this.rootElements)
root.Element.ForceUpdateArea();
};
if (automaticViewport) {
this.Viewport = new Rectangle(Point.Zero, game.Window.ClientBounds.Size);
this.AutoScaleReferenceSize = this.Viewport.Size;
game.Window.ClientSizeChanged += (sender, args) => {
this.Viewport = new Rectangle(Point.Zero, game.Window.ClientBounds.Size);
foreach (var root in this.rootElements)
root.Element.ForceUpdateArea();
};
}
if (TextInputWrapper.Current != null)
TextInputWrapper.Current.AddListener(this.Window, (sender, key, character) => this.ApplyToAll(e => e.OnTextInput?.Invoke(e, key, character)));
TextInputWrapper.Current.AddListener(game.Window, (sender, key, character) => this.ApplyToAll(e => e.OnTextInput?.Invoke(e, key, character)));
this.OnMousedElementChanged = e => this.ApplyToAll(t => t.OnMousedElementChanged?.Invoke(t, e));
this.OnTouchedElementChanged = e => this.ApplyToAll(t => t.OnTouchedElementChanged?.Invoke(t, e));
this.OnSelectedElementChanged = e => this.ApplyToAll(t => t.OnSelectedElementChanged?.Invoke(t, e));