1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 05:58:35 +01:00

Compare commits

..

4 commits

7 changed files with 143 additions and 86 deletions

View file

@ -17,6 +17,7 @@ Additions
Improvements Improvements
- Generify GenericFont's string drawing - Generify GenericFont's string drawing
- Added InputHandler mouse and touch position querying that preserves the game's viewport
Fixes Fixes
- Fixed StaticSpriteBatch handling rotated sprites incorrectly - Fixed StaticSpriteBatch handling rotated sprites incorrectly
@ -36,9 +37,11 @@ Improvements
- Allow setting a default text alignment for paragraphs in UiStyle - Allow setting a default text alignment for paragraphs in UiStyle
- Made custom values of Element.Style persist when a new ui style is set - Made custom values of Element.Style persist when a new ui style is set
- Update elements less aggressively when changing a ui system's style - Update elements less aggressively when changing a ui system's style
- Automatically update all elements when changing a ui system's viewport
Fixes Fixes
- Fixed paragraph links having incorrect hover locations when using special text alignments - Fixed paragraph links having incorrect hover locations when using special text alignments
- Fixed the graphics device's viewport being ignored for mouse and touch queries
Removals Removals
- Marked StyleProp equality members as obsolete - Marked StyleProp equality members as obsolete

View file

@ -137,14 +137,14 @@ namespace MLEM.Ui.Elements {
// MOUSE INPUT // MOUSE INPUT
var moused = this.Controls.MousedElement; var moused = this.Controls.MousedElement;
if (moused == this && this.Controls.Input.IsMouseButtonPressed(MouseButton.Left)) { if (moused == this && this.Input.IsMouseButtonPressed(MouseButton.Left)) {
this.isMouseHeld = true; this.isMouseHeld = true;
this.scrollStartOffset = this.TransformInverseAll(this.Input.MousePosition.ToVector2()) - this.ScrollerPosition; this.scrollStartOffset = this.TransformInverseAll(this.Input.ViewportMousePosition.ToVector2()) - this.ScrollerPosition;
} else if (this.isMouseHeld && !this.Controls.Input.IsMouseButtonDown(MouseButton.Left)) { } else if (this.isMouseHeld && !this.Input.IsMouseButtonDown(MouseButton.Left)) {
this.isMouseHeld = false; this.isMouseHeld = false;
} }
if (this.isMouseHeld) if (this.isMouseHeld)
this.ScrollToPos(this.TransformInverseAll(this.Input.MousePosition.ToVector2())); this.ScrollToPos(this.TransformInverseAll(this.Input.ViewportMousePosition.ToVector2()));
if (!this.Horizontal && moused != null && (moused == this.Parent || moused.GetParentTree().Contains(this.Parent))) { if (!this.Horizontal && moused != null && (moused == this.Parent || moused.GetParentTree().Contains(this.Parent))) {
var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel; var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel;
if (scroll != 0) if (scroll != 0)
@ -154,7 +154,7 @@ namespace MLEM.Ui.Elements {
// TOUCH INPUT // TOUCH INPUT
if (!this.Horizontal) { if (!this.Horizontal) {
// are we dragging on top of the panel? // are we dragging on top of the panel?
if (this.Input.GetGesture(GestureType.VerticalDrag, out var drag)) { if (this.Input.GetViewportGesture(GestureType.VerticalDrag, out var drag)) {
// if the element under the drag's start position is on top of the panel, start dragging // if the element under the drag's start position is on top of the panel, start dragging
var touched = this.Parent.GetElementUnderPos(this.TransformInverseAll(drag.Position)); var touched = this.Parent.GetElementUnderPos(this.TransformInverseAll(drag.Position));
if (touched != null && touched != this) if (touched != null && touched != this)
@ -167,11 +167,11 @@ namespace MLEM.Ui.Elements {
this.isDragging = false; this.isDragging = false;
} }
} }
if (this.Input.TouchState.Count <= 0) { if (this.Input.ViewportTouchState.Count <= 0) {
// if no touch has occured this tick, then reset the variable // if no touch has occured this tick, then reset the variable
this.isTouchHeld = false; this.isTouchHeld = false;
} else { } else {
foreach (var loc in this.Input.TouchState) { foreach (var loc in this.Input.ViewportTouchState) {
var pos = this.TransformInverseAll(loc.Position); var pos = this.TransformInverseAll(loc.Position);
// if we just started touching and are on top of the scroller, then we should start scrolling // if we just started touching and are on top of the scroller, then we should start scrolling
if (this.DisplayArea.Contains(pos) && !loc.TryGetPreviousLocation(out _)) { if (this.DisplayArea.Contains(pos) && !loc.TryGetPreviousLocation(out _)) {

View file

@ -91,7 +91,7 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public void SnapPositionToMouse() { public void SnapPositionToMouse() {
var viewport = this.System.Viewport; var viewport = this.System.Viewport;
var offset = (this.Input.MousePosition.ToVector2() + this.MouseOffset.Value) / this.Scale; var offset = (this.Input.ViewportMousePosition.ToVector2() + this.MouseOffset.Value) / this.Scale;
if (offset.X < viewport.X) if (offset.X < viewport.X)
offset.X = viewport.X; offset.X = viewport.X;
if (offset.Y < viewport.Y) if (offset.Y < viewport.Y)

View file

@ -143,7 +143,7 @@ namespace MLEM.Ui {
// MOUSE INPUT // MOUSE INPUT
if (this.HandleMouse) { if (this.HandleMouse) {
var mousedNow = this.GetElementUnderPos(this.Input.MousePosition.ToVector2()); var mousedNow = this.GetElementUnderPos(this.Input.ViewportMousePosition.ToVector2());
this.SetMousedElement(mousedNow); this.SetMousedElement(mousedNow);
if (this.Input.IsMouseButtonPressed(MouseButton.Left)) { if (this.Input.IsMouseButtonPressed(MouseButton.Left)) {
@ -184,22 +184,22 @@ namespace MLEM.Ui {
// TOUCH INPUT // TOUCH INPUT
if (this.HandleTouch) { if (this.HandleTouch) {
if (this.Input.GetGesture(GestureType.Tap, out var tap)) { if (this.Input.GetViewportGesture(GestureType.Tap, out var tap)) {
this.IsAutoNavMode = false; this.IsAutoNavMode = false;
var tapped = this.GetElementUnderPos(tap.Position); var tapped = this.GetElementUnderPos(tap.Position);
this.SelectElement(this.ActiveRoot, tapped); this.SelectElement(this.ActiveRoot, tapped);
if (tapped != null && tapped.CanBePressed) if (tapped != null && tapped.CanBePressed)
this.System.InvokeOnElementPressed(tapped); this.System.InvokeOnElementPressed(tapped);
} else if (this.Input.GetGesture(GestureType.Hold, out var hold)) { } else if (this.Input.GetViewportGesture(GestureType.Hold, out var hold)) {
this.IsAutoNavMode = false; this.IsAutoNavMode = false;
var held = this.GetElementUnderPos(hold.Position); var held = this.GetElementUnderPos(hold.Position);
this.SelectElement(this.ActiveRoot, held); this.SelectElement(this.ActiveRoot, held);
if (held != null && held.CanBePressed) if (held != null && held.CanBePressed)
this.System.InvokeOnElementSecondaryPressed(held); this.System.InvokeOnElementSecondaryPressed(held);
} else if (this.Input.TouchState.Count <= 0) { } else if (this.Input.ViewportTouchState.Count <= 0) {
this.SetTouchedElement(null); this.SetTouchedElement(null);
} else { } else {
foreach (var location in this.Input.TouchState) { foreach (var location in this.Input.ViewportTouchState) {
var element = this.GetElementUnderPos(location.Position); var element = this.GetElementUnderPos(location.Position);
if (location.State == TouchLocationState.Pressed) { if (location.State == TouchLocationState.Pressed) {
// start touching an element if we just touched down on it // start touching an element if we just touched down on it

View file

@ -23,9 +23,16 @@ namespace MLEM.Ui {
/// <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"/> by default.
/// </summary> /// </summary>
public Rectangle Viewport; public Rectangle Viewport {
get => this.viewport;
set {
this.viewport = value;
foreach (var root in this.rootElements)
root.Element.ForceUpdateArea();
}
}
/// <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"/>
@ -181,6 +188,7 @@ namespace MLEM.Ui {
private float globalScale = 1; private float globalScale = 1;
private bool drewEarly; private bool drewEarly;
private UiStyle style; private UiStyle style;
private Rectangle viewport;
/// <summary> /// <summary>
/// Creates a new ui system with the given settings. /// Creates a new ui system with the given settings.
@ -228,8 +236,6 @@ namespace MLEM.Ui {
this.AutoScaleReferenceSize = this.Viewport.Size; this.AutoScaleReferenceSize = this.Viewport.Size;
game.Window.ClientSizeChanged += (sender, args) => { game.Window.ClientSizeChanged += (sender, args) => {
this.Viewport = new Rectangle(Point.Zero, game.Window.ClientBounds.Size); this.Viewport = new Rectangle(Point.Zero, game.Window.ClientBounds.Size);
foreach (var root in this.rootElements)
root.Element.ForceUpdateArea();
}; };
} }

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch; using Microsoft.Xna.Framework.Input.Touch;
using MLEM.Misc; using MLEM.Misc;
@ -15,78 +16,27 @@ namespace MLEM.Input {
public class InputHandler : GameComponent { public class InputHandler : GameComponent {
/// <summary> /// <summary>
/// Contains the keyboard state from the last update call /// Contains all of the gestures that have finished during the last update call.
/// To easily query these gestures, use <see cref="GetGesture"/> or <see cref="GetViewportGesture"/>.
/// </summary> /// </summary>
public KeyboardState LastKeyboardState { get; private set; } public readonly ReadOnlyCollection<GestureSample> Gestures;
/// <summary>
/// Contains the current keyboard state
/// </summary>
public KeyboardState KeyboardState { get; private set; }
/// <summary> /// <summary>
/// Set this field to false to disable keyboard handling for this input handler. /// Set this field to false to disable keyboard handling for this input handler.
/// </summary> /// </summary>
public bool HandleKeyboard; public bool HandleKeyboard;
/// <summary>
/// Contains the mouse state from the last update call
/// </summary>
public MouseState LastMouseState { get; private set; }
/// <summary>
/// Contains the current mouse state
/// </summary>
public MouseState MouseState { get; private set; }
/// <summary>
/// Contains the current position of the mouse, extracted from <see cref="MouseState"/>
/// </summary>
public Point MousePosition => this.MouseState.Position;
/// <summary>
/// Contains the position of the mouse from the last update call, extracted from <see cref="LastMouseState"/>
/// </summary>
public Point LastMousePosition => this.LastMouseState.Position;
/// <summary>
/// Contains the current scroll wheel value, in increments of 120
/// </summary>
public int ScrollWheel => this.MouseState.ScrollWheelValue;
/// <summary>
/// Contains the scroll wheel value from the last update call, in increments of 120
/// </summary>
public int LastScrollWheel => this.LastMouseState.ScrollWheelValue;
/// <summary> /// <summary>
/// Set this field to false to disable mouse handling for this input handler. /// Set this field to false to disable mouse handling for this input handler.
/// </summary> /// </summary>
public bool HandleMouse; public bool HandleMouse;
private readonly GamePadState[] lastGamepads = new GamePadState[GamePad.MaximumGamePadCount];
private readonly GamePadState[] gamepads = new GamePadState[GamePad.MaximumGamePadCount];
/// <summary>
/// Contains the amount of gamepads that are currently connected.
/// This field is automatically updated in <see cref="Update()"/>
/// </summary>
public int ConnectedGamepads { get; private set; }
/// <summary> /// <summary>
/// Set this field to false to disable keyboard handling for this input handler. /// Set this field to false to disable keyboard handling for this input handler.
/// </summary> /// </summary>
public bool HandleGamepads; public bool HandleGamepads;
/// <summary>
/// Contains the touch state from the last update call
/// </summary>
public TouchCollection LastTouchState { get; private set; }
/// <summary>
/// Contains the current touch state
/// </summary>
public TouchCollection TouchState { get; private set; }
/// <summary>
/// Contains all of the gestures that have finished during the last update call.
/// To easily query these gestures, use <see cref="GetGesture"/>
/// </summary>
public readonly ReadOnlyCollection<GestureSample> Gestures;
private readonly List<GestureSample> gestures = new List<GestureSample>();
/// <summary> /// <summary>
/// Set this field to false to disable touch handling for this input handler. /// Set this field to false to disable touch handling for this input handler.
/// </summary> /// </summary>
public bool HandleTouch; public bool HandleTouch;
/// <summary> /// <summary>
/// This is the amount of time that has to pass before the first keyboard repeat event is triggered. /// This is the amount of time that has to pass before the first keyboard repeat event is triggered.
/// <seealso cref="KeyRepeatRate"/> /// <seealso cref="KeyRepeatRate"/>
@ -97,24 +47,18 @@ namespace MLEM.Input {
/// <seealso cref="KeyRepeatDelay"/> /// <seealso cref="KeyRepeatDelay"/>
/// </summary> /// </summary>
public TimeSpan KeyRepeatRate = TimeSpan.FromSeconds(0.05); public TimeSpan KeyRepeatRate = TimeSpan.FromSeconds(0.05);
/// <summary> /// <summary>
/// Set this field to false to disable keyboard repeat event handling. /// Set this field to false to disable keyboard repeat event handling.
/// </summary> /// </summary>
public bool HandleKeyboardRepeats = true; public bool HandleKeyboardRepeats = true;
private DateTime heldKeyStart;
private DateTime lastKeyRepeat;
private bool triggerKeyRepeat;
private Keys heldKey;
/// <summary> /// <summary>
/// Set this field to false to disable gamepad repeat event handling. /// Set this field to false to disable gamepad repeat event handling.
/// </summary> /// </summary>
public bool HandleGamepadRepeats = true; public bool HandleGamepadRepeats = true;
private readonly DateTime[] heldGamepadButtonStarts = new DateTime[GamePad.MaximumGamePadCount]; /// <summary>
private readonly DateTime[] lastGamepadButtonRepeats = new DateTime[GamePad.MaximumGamePadCount]; /// Set this field to false to enable <see cref="InputsDown"/> and <see cref="InputsPressed"/> being calculated.
private readonly bool[] triggerGamepadButtonRepeat = new bool[GamePad.MaximumGamePadCount]; /// </summary>
private readonly Buttons?[] heldGamepadButtons = new Buttons?[GamePad.MaximumGamePadCount]; public bool StoreAllActiveInputs;
/// <summary> /// <summary>
/// An array of all <see cref="Keys"/>, <see cref="Buttons"/> and <see cref="MouseButton"/> values that are currently down. /// An array of all <see cref="Keys"/>, <see cref="Buttons"/> and <see cref="MouseButton"/> values that are currently down.
@ -127,11 +71,82 @@ namespace MLEM.Input {
/// Note that this value only gets set if <see cref="StoreAllActiveInputs"/> is true. /// Note that this value only gets set if <see cref="StoreAllActiveInputs"/> is true.
/// </summary> /// </summary>
public GenericInput[] InputsPressed { get; private set; } = Array.Empty<GenericInput>(); public GenericInput[] InputsPressed { get; private set; } = Array.Empty<GenericInput>();
private readonly List<GenericInput> inputsDownAccum = new List<GenericInput>();
/// <summary> /// <summary>
/// Set this field to false to enable <see cref="InputsDown"/> and <see cref="InputsPressed"/> being calculated. /// Contains the touch state from the last update call
/// </summary> /// </summary>
public bool StoreAllActiveInputs; public TouchCollection LastTouchState { get; private set; }
/// <summary>
/// Contains the current touch state
/// </summary>
public TouchCollection TouchState { get; private set; }
/// <summary>
/// Contains the <see cref="LastTouchState"/>, but with the <see cref="GraphicsDevice.Viewport"/> taken into account.
/// </summary>
public IList<TouchLocation> LastViewportTouchState { get; private set; }
/// <summary>
/// Contains the <see cref="TouchState"/>, but with the <see cref="GraphicsDevice.Viewport"/> taken into account.
/// </summary>
public IList<TouchLocation> ViewportTouchState { get; private set; }
/// <summary>
/// Contains the amount of gamepads that are currently connected.
/// This field is automatically updated in <see cref="Update()"/>
/// </summary>
public int ConnectedGamepads { get; private set; }
/// <summary>
/// Contains the mouse state from the last update call
/// </summary>
public MouseState LastMouseState { get; private set; }
/// <summary>
/// Contains the current mouse state
/// </summary>
public MouseState MouseState { get; private set; }
/// <summary>
/// Contains the position of the mouse from the last update call, extracted from <see cref="LastMouseState"/>
/// </summary>
public Point LastMousePosition => this.LastMouseState.Position;
/// <summary>
/// Contains the <see cref="LastMousePosition"/>, but with the <see cref="GraphicsDevice.Viewport"/> taken into account.
/// </summary>
public Point LastViewportMousePosition => this.LastMousePosition + this.ViewportOffset;
/// <summary>
/// Contains the current position of the mouse, extracted from <see cref="MouseState"/>
/// </summary>
public Point MousePosition => this.MouseState.Position;
/// <summary>
/// Contains the <see cref="MousePosition"/>, but with the <see cref="GraphicsDevice.Viewport"/> taken into account.
/// </summary>
public Point ViewportMousePosition => this.MousePosition + this.ViewportOffset;
/// <summary>
/// Contains the current scroll wheel value, in increments of 120
/// </summary>
public int ScrollWheel => this.MouseState.ScrollWheelValue;
/// <summary>
/// Contains the scroll wheel value from the last update call, in increments of 120
/// </summary>
public int LastScrollWheel => this.LastMouseState.ScrollWheelValue;
/// <summary>
/// Contains the keyboard state from the last update call
/// </summary>
public KeyboardState LastKeyboardState { get; private set; }
/// <summary>
/// Contains the current keyboard state
/// </summary>
public KeyboardState KeyboardState { get; private set; }
private readonly GamePadState[] lastGamepads = new GamePadState[GamePad.MaximumGamePadCount];
private readonly GamePadState[] gamepads = new GamePadState[GamePad.MaximumGamePadCount];
private readonly DateTime[] heldGamepadButtonStarts = new DateTime[GamePad.MaximumGamePadCount];
private readonly DateTime[] lastGamepadButtonRepeats = new DateTime[GamePad.MaximumGamePadCount];
private readonly bool[] triggerGamepadButtonRepeat = new bool[GamePad.MaximumGamePadCount];
private readonly Buttons?[] heldGamepadButtons = new Buttons?[GamePad.MaximumGamePadCount];
private readonly List<GenericInput> inputsDownAccum = new List<GenericInput>();
private readonly List<GestureSample> gestures = new List<GestureSample>();
private Point ViewportOffset => new Point(-this.Game.GraphicsDevice.Viewport.X, -this.Game.GraphicsDevice.Viewport.Y);
private DateTime heldKeyStart;
private DateTime lastKeyRepeat;
private bool triggerKeyRepeat;
private Keys heldKey;
/// <summary> /// <summary>
/// Creates a new input handler with optional initial values. /// Creates a new input handler with optional initial values.
@ -270,7 +285,18 @@ namespace MLEM.Input {
if (this.HandleTouch) { if (this.HandleTouch) {
this.LastTouchState = this.TouchState; this.LastTouchState = this.TouchState;
this.LastViewportTouchState = this.ViewportTouchState;
this.TouchState = active ? TouchPanel.GetState() : default; this.TouchState = active ? TouchPanel.GetState() : default;
if (this.TouchState.Count > 0 && this.ViewportOffset != Point.Zero) {
this.ViewportTouchState = new List<TouchLocation>();
foreach (var touch in this.TouchState) {
touch.TryGetPreviousLocation(out var previous);
this.ViewportTouchState.Add(new TouchLocation(touch.Id, touch.State, touch.Position + this.ViewportOffset.ToVector2(), previous.State, previous.Position + this.ViewportOffset.ToVector2()));
}
} else {
this.ViewportTouchState = this.TouchState;
}
this.gestures.Clear(); this.gestures.Clear();
while (active && TouchPanel.IsGestureAvailable) while (active && TouchPanel.IsGestureAvailable)
@ -515,6 +541,22 @@ namespace MLEM.Input {
return false; return false;
} }
/// <summary>
/// Queries for a gesture of the given type that finished during the current update call.
/// Unlike <see cref="GetGesture"/>, the return value of this method takes the <see cref="GraphicsDevice.Viewport"/> into account.
/// </summary>
/// <param name="type">The type of gesture to query for</param>
/// <param name="sample">The resulting gesture sample with the <see cref="GraphicsDevice.Viewport"/> taken into account, or default if there isn't one</param>
/// <returns>True if a gesture of the type was found, otherwise false</returns>
public bool GetViewportGesture(GestureType type, out GestureSample sample) {
if (this.GetGesture(type, out var original)) {
sample = new GestureSample(original.GestureType, original.Timestamp, original.Position + this.ViewportOffset.ToVector2(), original.Position2 + this.ViewportOffset.ToVector2(), original.Delta, original.Delta2);
return true;
}
sample = default;
return false;
}
/// <summary> /// <summary>
/// Returns if a given control of any kind is down. /// Returns if a given control of any kind is down.
/// This is a helper function that can be passed a <see cref="Keys"/>, <see cref="Buttons"/> or <see cref="MouseButton"/>. /// This is a helper function that can be passed a <see cref="Keys"/>, <see cref="Buttons"/> or <see cref="MouseButton"/>.

View file

@ -22,6 +22,7 @@ using MLEM.Ui;
using MLEM.Ui.Elements; using MLEM.Ui.Elements;
using MLEM.Ui.Style; using MLEM.Ui.Style;
using MonoGame.Extended.Tiled; using MonoGame.Extended.Tiled;
using MonoGame.Extended.ViewportAdapters;
namespace Sandbox { namespace Sandbox {
public class GameImpl : MlemGame { public class GameImpl : MlemGame {
@ -298,6 +299,11 @@ namespace Sandbox {
} }
this.SpriteBatch.End(); this.SpriteBatch.End();
}; };
var viewport = new BoxingViewportAdapter(this.Window, this.GraphicsDevice, 1280, 720);
var newPanel = new Panel(Anchor.TopLeft, new Vector2(200, 100), new Vector2(10, 10));
newPanel.AddChild(new Button(Anchor.TopLeft, new Vector2(100, 20), "Text", "Tooltip text"));
this.UiSystem.Add("Panel", newPanel);
} }
protected override void DoUpdate(GameTime gameTime) { protected override void DoUpdate(GameTime gameTime) {
@ -307,7 +313,7 @@ namespace Sandbox {
var delta = this.InputHandler.ScrollWheel - this.InputHandler.LastScrollWheel; var delta = this.InputHandler.ScrollWheel - this.InputHandler.LastScrollWheel;
if (delta != 0) { if (delta != 0) {
this.camera.Zoom(0.1F * Math.Sign(delta), this.InputHandler.MousePosition.ToVector2()); this.camera.Zoom(0.1F * Math.Sign(delta), this.InputHandler.ViewportMousePosition.ToVector2());
} }
/*if (Input.InputsDown.Length > 0) /*if (Input.InputsDown.Length > 0)