diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ff0af7..b667234 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ Improvements Fixes - Fixed TokenizedString handling trailing spaces incorrectly in the last line of non-left aligned text - Fixed some TokenizedString tokens starting with a line break not being split correctly +- Fixed InputHandler maintaining old input states when input types are toggled off ### MLEM.Ui Additions @@ -50,6 +51,7 @@ Fixes - Fixed Element.OnChildAdded and Element.OnChildRemoved being called for grandchildren when a child is added - Fixed an exception when trying to force-update the area of an element without a ui system - Fixed the scroll bar of an empty panel being positioned incorrectly +- Fixed UiControls maintaining old input states when input types are toggled off ### MLEM.Data Additions diff --git a/MLEM.Ui/UiControls.cs b/MLEM.Ui/UiControls.cs index d02a413..e2d7a25 100644 --- a/MLEM.Ui/UiControls.cs +++ b/MLEM.Ui/UiControls.cs @@ -161,7 +161,6 @@ namespace MLEM.Ui { this.Input.Update(); this.ActiveRoot = this.System.GetRootElements().FirstOrDefault(root => root.CanBeActive); - // MOUSE INPUT if (this.HandleMouse) { var mousedNow = this.GetElementUnderPos(new Vector2(this.Input.ViewportMousePosition.X, this.Input.ViewportMousePosition.Y)); this.SetMousedElement(mousedNow); @@ -181,9 +180,10 @@ namespace MLEM.Ui { this.Input.TryConsumeMouseButtonPressed(MouseButton.Right); } } + } else { + this.SetMousedElement(null); } - // KEYBOARD INPUT if (this.HandleKeyboard) { if (this.KeyboardButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) { if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) { @@ -210,7 +210,6 @@ namespace MLEM.Ui { } } - // TOUCH INPUT if (this.HandleTouch) { if (this.Input.GetViewportGesture(GestureType.Tap, out var tap)) { this.IsAutoNavMode = false; @@ -238,9 +237,10 @@ namespace MLEM.Ui { } } } + } else { + this.SetTouchedElement(null); } - // GAMEPAD INPUT if (this.HandleGamepad) { if (this.GamepadButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) { if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) { diff --git a/MLEM/Input/InputHandler.cs b/MLEM/Input/InputHandler.cs index 20e2bc5..db3c046 100644 --- a/MLEM/Input/InputHandler.cs +++ b/MLEM/Input/InputHandler.cs @@ -108,8 +108,8 @@ namespace MLEM.Input { /// public IList ViewportTouchState { get; private set; } /// - /// Contains the amount of gamepads that are currently connected. - /// This field is automatically updated in + /// Contains the amount of gamepads that are currently connected. Note that this value will be set to 0 if is false. + /// This field is automatically updated in . /// public int ConnectedGamepads { get; private set; } /// @@ -196,15 +196,15 @@ namespace MLEM.Input { this.consumedPresses.Clear(); + this.LastKeyboardState = this.KeyboardState; if (this.HandleKeyboard) { - this.LastKeyboardState = this.KeyboardState; this.KeyboardState = active ? Keyboard.GetState() : default; var pressedKeys = this.KeyboardState.GetPressedKeys(); foreach (var pressed in pressedKeys) this.AccumulateDown(pressed, -1); + this.triggerKeyRepeat = false; if (this.HandleKeyboardRepeats) { - this.triggerKeyRepeat = false; // the key that started being held most recently should be the one being repeated this.heldKey = pressedKeys.OrderBy(k => this.GetDownTime(k)).FirstOrDefault(); if (this.TryGetDownTime(this.heldKey, out var heldTime)) { @@ -219,11 +219,17 @@ namespace MLEM.Input { } } } + } else { + this.heldKey = Keys.None; } + } else { + this.KeyboardState = default; + this.triggerKeyRepeat = false; + this.heldKey = Keys.None; } + this.LastMouseState = this.MouseState; if (this.HandleMouse) { - this.LastMouseState = this.MouseState; var state = Mouse.GetState(); if (active && this.Game.GraphicsDevice.Viewport.Bounds.Contains(state.X, state.Y)) { this.MouseState = state; @@ -239,6 +245,8 @@ namespace MLEM.Input { this.MouseState = new MouseState(state.X, state.Y, state.ScrollWheelValue, 0, 0, 0, 0, 0, state.HorizontalScrollWheelValue); #endif } + } else { + this.MouseState = default; } if (this.HandleGamepads) { @@ -259,9 +267,9 @@ namespace MLEM.Input { } } - if (this.HandleGamepadRepeats) { - for (var i = 0; i < this.ConnectedGamepads; i++) { - this.triggerGamepadButtonRepeat[i] = false; + for (var i = 0; i < this.ConnectedGamepads; i++) { + this.triggerGamepadButtonRepeat[i] = false; + if (this.HandleGamepadRepeats) { this.heldGamepadButtons[i] = EnumHelper.Buttons .Where(b => this.IsGamepadButtonDown(b, i)) .OrderBy(b => this.GetDownTime(b, i)) @@ -275,14 +283,23 @@ namespace MLEM.Input { } } } + } else { + this.heldGamepadButtons[i] = null; } } + } else { + this.ConnectedGamepads = 0; + for (var i = 0; i < InputHandler.MaximumGamePadCount; i++) { + this.lastGamepads[i] = this.gamepads[i]; + this.gamepads[i] = default; + this.triggerGamepadButtonRepeat[i] = false; + this.heldGamepadButtons[i] = null; + } } + this.LastTouchState = this.TouchState; + this.LastViewportTouchState = this.ViewportTouchState; if (this.HandleTouch) { - this.LastTouchState = this.TouchState; - this.LastViewportTouchState = this.ViewportTouchState; - this.TouchState = active ? TouchPanel.GetState() : new TouchCollection(InputHandler.EmptyTouchLocations); if (this.TouchState.Count > 0 && this.ViewportOffset != Point.Zero) { this.ViewportTouchState = new List(); @@ -301,6 +318,9 @@ namespace MLEM.Input { while (TouchPanel.IsGestureAvailable) this.gestures.Add(TouchPanel.ReadGesture()); } + } else { + this.TouchState = new TouchCollection(InputHandler.EmptyTouchLocations); + this.gestures.Clear(); } if (this.inputsDownAccum.Count <= 0 && this.inputsDown.Count <= 0) {