1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 04:53:29 +01:00

Added InputHandler mouse and touch position querying that preserves the game's viewport and fixed the graphics device's viewport being ignored for mouse and touch queries

Closes #1
This commit is contained in:
Ell 2022-02-06 22:07:33 +01:00
parent ad2784a67e
commit 48b96a10a4
6 changed files with 69 additions and 17 deletions

View file

@ -17,6 +17,7 @@ Additions
Improvements
- Generify GenericFont's string drawing
- Added InputHandler mouse and touch position querying that preserves the game's viewport
Fixes
- Fixed StaticSpriteBatch handling rotated sprites incorrectly
@ -40,6 +41,7 @@ Improvements
Fixes
- 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
- Marked StyleProp equality members as obsolete

View file

@ -137,14 +137,14 @@ namespace MLEM.Ui.Elements {
// MOUSE INPUT
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.scrollStartOffset = this.TransformInverseAll(this.Input.MousePosition.ToVector2()) - this.ScrollerPosition;
} else if (this.isMouseHeld && !this.Controls.Input.IsMouseButtonDown(MouseButton.Left)) {
this.scrollStartOffset = this.TransformInverseAll(this.Input.ViewportMousePosition.ToVector2()) - this.ScrollerPosition;
} else if (this.isMouseHeld && !this.Input.IsMouseButtonDown(MouseButton.Left)) {
this.isMouseHeld = false;
}
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))) {
var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel;
if (scroll != 0)
@ -154,7 +154,7 @@ namespace MLEM.Ui.Elements {
// TOUCH INPUT
if (!this.Horizontal) {
// 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
var touched = this.Parent.GetElementUnderPos(this.TransformInverseAll(drag.Position));
if (touched != null && touched != this)
@ -167,11 +167,11 @@ namespace MLEM.Ui.Elements {
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
this.isTouchHeld = false;
} else {
foreach (var loc in this.Input.TouchState) {
foreach (var loc in this.Input.ViewportTouchState) {
var pos = this.TransformInverseAll(loc.Position);
// 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 _)) {

View file

@ -91,7 +91,7 @@ namespace MLEM.Ui.Elements {
/// </summary>
public void SnapPositionToMouse() {
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)
offset.X = viewport.X;
if (offset.Y < viewport.Y)

View file

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

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using MLEM.Misc;
@ -16,7 +17,7 @@ namespace MLEM.Input {
/// <summary>
/// Contains all of the gestures that have finished during the last update call.
/// To easily query these gestures, use <see cref="GetGesture"/>
/// To easily query these gestures, use <see cref="GetGesture"/> or <see cref="GetViewportGesture"/>.
/// </summary>
public readonly ReadOnlyCollection<GestureSample> Gestures;
@ -79,6 +80,14 @@ namespace MLEM.Input {
/// </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>
@ -92,13 +101,21 @@ namespace MLEM.Input {
/// </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 position of the mouse from the last update call, extracted from <see cref="LastMouseState"/>
/// Contains the <see cref="MousePosition"/>, but with the <see cref="GraphicsDevice.Viewport"/> taken into account.
/// </summary>
public Point LastMousePosition => this.LastMouseState.Position;
public Point ViewportMousePosition => this.MousePosition + this.ViewportOffset;
/// <summary>
/// Contains the current scroll wheel value, in increments of 120
/// </summary>
@ -125,6 +142,7 @@ namespace MLEM.Input {
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;
@ -267,7 +285,17 @@ namespace MLEM.Input {
if (this.HandleTouch) {
this.LastTouchState = this.TouchState;
this.LastViewportTouchState = this.ViewportTouchState;
this.TouchState = active ? TouchPanel.GetState() : default;
this.ViewportTouchState = this.TouchState;
if (this.ViewportTouchState.Count > 0 && this.ViewportOffset != Point.Zero) {
for (var i = 0; i < this.ViewportTouchState.Count; i++) {
var touch = this.ViewportTouchState[i];
touch.TryGetPreviousLocation(out var previous);
this.ViewportTouchState[i] = new TouchLocation(touch.Id, touch.State, touch.Position + this.ViewportOffset.ToVector2(), previous.State, previous.Position + this.ViewportOffset.ToVector2());
}
}
this.gestures.Clear();
while (active && TouchPanel.IsGestureAvailable)
@ -512,6 +540,22 @@ namespace MLEM.Input {
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>
/// 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"/>.

View file

@ -22,6 +22,7 @@ using MLEM.Ui;
using MLEM.Ui.Elements;
using MLEM.Ui.Style;
using MonoGame.Extended.Tiled;
using MonoGame.Extended.ViewportAdapters;
namespace Sandbox {
public class GameImpl : MlemGame {
@ -298,6 +299,11 @@ namespace Sandbox {
}
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) {
@ -307,7 +313,7 @@ namespace Sandbox {
var delta = this.InputHandler.ScrollWheel - this.InputHandler.LastScrollWheel;
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)