1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01: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. /// Causes this tooltip's position to be snapped to the mouse position.
/// </summary> /// </summary>
public void SnapPositionToMouse() { 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; var offset = (this.Input.MousePosition.ToVector2() + this.MouseOffset.Value) / this.Scale;
if (offset.X < 0) if (offset.X < viewport.X)
offset.X = 0; offset.X = viewport.X;
if (offset.Y < 0) if (offset.Y < viewport.Y)
offset.Y = 0; offset.Y = viewport.Y;
if (offset.X * this.Scale + this.Area.Width >= w) if (offset.X * this.Scale + this.Area.Width >= viewport.Right)
offset.X = (w - this.Area.Width) / this.Scale; offset.X = (viewport.Right - this.Area.Width) / this.Scale;
if (offset.Y * this.Scale + this.Area.Height >= h) if (offset.Y * this.Scale + this.Area.Height >= viewport.Bottom)
offset.Y = (h - this.Area.Height) / this.Scale; offset.Y = (viewport.Bottom - this.Area.Height) / this.Scale;
this.PositionOffset = offset; this.PositionOffset = offset;
} }

View file

@ -25,17 +25,13 @@ namespace MLEM.Ui {
/// The graphics device that this ui system uses for its size calculations /// The graphics device that this ui system uses for its size calculations
/// </summary> /// </summary>
public readonly GraphicsDevice GraphicsDevice; 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>(); private readonly List<RootElement> rootElements = new List<RootElement>();
/// <summary> /// <summary>
/// The viewport that this ui system is rendering inside of. /// The viewport that this ui system is rendering inside of.
/// This is automatically updated during <see cref="GameWindow.ClientSizeChanged"/> /// This is automatically updated during <see cref="GameWindow.ClientSizeChanged"/>
/// </summary> /// </summary>
public Rectangle Viewport { get; private set; } public Rectangle Viewport;
/// <summary> /// <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. /// 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"/> /// 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="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="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> /// <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.Controls = new UiControls(this, inputHandler);
this.GraphicsDevice = device; this.GraphicsDevice = device;
this.Window = game.Window;
this.style = style; this.style = style;
this.Viewport = new Rectangle(Point.Zero, this.Window.ClientBounds.Size);
this.AutoScaleReferenceSize = this.Viewport.Size;
this.Window.ClientSizeChanged += (sender, args) => { if (automaticViewport) {
this.Viewport = new Rectangle(Point.Zero, this.Window.ClientBounds.Size); 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) foreach (var root in this.rootElements)
root.Element.ForceUpdateArea(); root.Element.ForceUpdateArea();
}; };
}
if (TextInputWrapper.Current != null) 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.OnMousedElementChanged = e => this.ApplyToAll(t => t.OnMousedElementChanged?.Invoke(t, e));
this.OnTouchedElementChanged = e => this.ApplyToAll(t => t.OnTouchedElementChanged?.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)); this.OnSelectedElementChanged = e => this.ApplyToAll(t => t.OnSelectedElementChanged?.Invoke(t, e));