1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-25 01:43:38 +02:00

fixed the input handler querying input when the window is inactive

This commit is contained in:
Ell 2021-02-18 18:36:29 +01:00
parent 69d81da70c
commit 01b6168259
4 changed files with 19 additions and 17 deletions

View file

@ -70,9 +70,9 @@ namespace MLEM.Startup {
/// <inheritdoc />
protected override void LoadContent() {
this.SpriteBatch = new SpriteBatch(this.GraphicsDevice);
this.InputHandler = new InputHandler();
this.InputHandler = new InputHandler(this);
this.Components.Add(this.InputHandler);
this.UiSystem = new UiSystem(this.Window, this.GraphicsDevice, new UntexturedStyle(this.SpriteBatch), this.InputHandler);
this.UiSystem = new UiSystem(this, this.GraphicsDevice, new UntexturedStyle(this.SpriteBatch), this.InputHandler);
this.Components.Add(this.UiSystem);
this.OnLoadContent?.Invoke(this);
}

View file

@ -123,7 +123,7 @@ namespace MLEM.Ui {
/// <param name="inputHandler">The input handler to use for controlling, or null to create a new one.</param>
public UiControls(UiSystem system, InputHandler inputHandler = null) {
this.System = system;
this.Input = inputHandler ?? new InputHandler();
this.Input = inputHandler ?? new InputHandler(system.Game);
this.IsInputOurs = inputHandler == null;
this.Keybinds = typeof(UiControls).GetFields()
.Where(f => f.FieldType == typeof(Keybind))

View file

@ -180,26 +180,26 @@ namespace MLEM.Ui {
/// <summary>
/// Creates a new ui system with the given settings.
/// </summary>
/// <param name="window">The game's window</param>
/// <param name="game">The game</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="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(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) : base(null) {
public UiSystem(Game game, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) : base(game) {
this.Controls = new UiControls(this, inputHandler);
this.GraphicsDevice = device;
this.Window = window;
this.Window = game.Window;
this.style = style;
this.Viewport = new Rectangle(Point.Zero, window.ClientBounds.Size);
this.Viewport = new Rectangle(Point.Zero, this.Window.ClientBounds.Size);
this.AutoScaleReferenceSize = this.Viewport.Size;
window.ClientSizeChanged += (sender, args) => {
this.Viewport = new Rectangle(Point.Zero, window.ClientBounds.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 (TextInputWrapper.Current != null)
TextInputWrapper.Current.AddListener(window, (sender, key, character) => this.ApplyToAll(e => e.OnTextInput?.Invoke(e, key, character)));
TextInputWrapper.Current.AddListener(this.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));

View file

@ -123,11 +123,12 @@ namespace MLEM.Input {
/// <summary>
/// Creates a new input handler with optional initial values.
/// </summary>
/// <param name="game">The game instance that this input handler belongs to</param>
/// <param name="handleKeyboard">If keyboard input should be handled</param>
/// <param name="handleMouse">If mouse input should be handled</param>
/// <param name="handleGamepads">If gamepad input should be handled</param>
/// <param name="handleTouch">If touch input should be handled</param>
public InputHandler(bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true, bool handleTouch = true) : base(null) {
public InputHandler(Game game, bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true, bool handleTouch = true) : base(game) {
this.HandleKeyboard = handleKeyboard;
this.HandleMouse = handleMouse;
this.HandleGamepads = handleGamepads;
@ -140,9 +141,10 @@ namespace MLEM.Input {
/// Call this in your <see cref="Game.Update"/> method.
/// </summary>
public void Update() {
var active = this.Game.IsActive;
if (this.HandleKeyboard) {
this.LastKeyboardState = this.KeyboardState;
this.KeyboardState = Keyboard.GetState();
this.KeyboardState = active ? Keyboard.GetState() : default;
this.PressedKeys = this.KeyboardState.GetPressedKeys();
if (this.HandleKeyboardRepeats) {
@ -179,15 +181,15 @@ namespace MLEM.Input {
if (this.HandleMouse) {
this.LastMouseState = this.MouseState;
this.MouseState = Mouse.GetState();
this.MouseState = active ? Mouse.GetState() : default;
}
if (this.HandleGamepads) {
this.ConnectedGamepads = GamePad.MaximumGamePadCount;
for (var i = 0; i < GamePad.MaximumGamePadCount; i++) {
this.lastGamepads[i] = this.gamepads[i];
this.gamepads[i] = GamePad.GetState(i);
if (this.ConnectedGamepads > i && !this.gamepads[i].IsConnected)
this.gamepads[i] = active ? GamePad.GetState(i) : default;
if (this.ConnectedGamepads > i && !GamePad.GetCapabilities(i).IsConnected)
this.ConnectedGamepads = i;
}
@ -224,10 +226,10 @@ namespace MLEM.Input {
if (this.HandleTouch) {
this.LastTouchState = this.TouchState;
this.TouchState = TouchPanel.GetState();
this.TouchState = active ? TouchPanel.GetState() : default;
this.gestures.Clear();
while (TouchPanel.IsGestureAvailable)
while (active && TouchPanel.IsGestureAvailable)
this.gestures.Add(TouchPanel.ReadGesture());
}
}