From 610527374ec9ebfcf1cffbe3a227fefe441c0206 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 30 Apr 2022 12:14:08 +0200 Subject: [PATCH] Make use of the new consuming variants in InputHandler and Keybind to consume UiControls inputs --- CHANGELOG.md | 1 + MLEM.Ui/UiControls.cs | 54 ++++++++++++++++++++++++------------- MLEM/Input/InputHandler.cs | 55 +++++++++++++++++++++++++++++++++++--- MLEM/Input/Keybind.cs | 33 +++++++++++++++++++++-- 4 files changed, 118 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3badedf..f6e7e9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Additions Improvements - Ensure that Element.IsMouseOver is always accurate by making it an auto-property - Started using SpriteBatchContext for Draw and DrawTransformed methods +- Make use of the new consuming variants in InputHandler and Keybind to consume UiControls inputs Fixes - Fixed auto-nav tooltip displaying on the selected element even when not in auto-nav mode diff --git a/MLEM.Ui/UiControls.cs b/MLEM.Ui/UiControls.cs index 8c9c4f1..a53b8cf 100644 --- a/MLEM.Ui/UiControls.cs +++ b/MLEM.Ui/UiControls.cs @@ -162,22 +162,26 @@ namespace MLEM.Ui { var mousedNow = this.GetElementUnderPos(this.Input.ViewportMousePosition.ToVector2()); this.SetMousedElement(mousedNow); - if (this.Input.IsMouseButtonPressed(MouseButton.Left)) { + if (this.Input.IsMouseButtonPressedAvailable(MouseButton.Left)) { this.IsAutoNavMode = false; var selectedNow = mousedNow != null && mousedNow.CanBeSelected ? mousedNow : null; this.SelectElement(this.ActiveRoot, selectedNow); - if (mousedNow != null && mousedNow.CanBePressed) + if (mousedNow != null && mousedNow.CanBePressed) { this.System.InvokeOnElementPressed(mousedNow); - } else if (this.Input.IsMouseButtonPressed(MouseButton.Right)) { + this.Input.TryConsumeMouseButtonPressed(MouseButton.Left); + } + } else if (this.Input.IsMouseButtonPressedAvailable(MouseButton.Right)) { this.IsAutoNavMode = false; - if (mousedNow != null && mousedNow.CanBePressed) + if (mousedNow != null && mousedNow.CanBePressed) { this.System.InvokeOnElementSecondaryPressed(mousedNow); + this.Input.TryConsumeMouseButtonPressed(MouseButton.Right); + } } } // KEYBOARD INPUT if (this.HandleKeyboard) { - if (this.KeyboardButtons.IsPressed(this.Input, this.GamepadIndex)) { + if (this.KeyboardButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) { if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) { if (this.Input.IsModifierKeyDown(ModifierKey.Shift)) { // secondary action on element using space or enter @@ -186,6 +190,7 @@ namespace MLEM.Ui { // first action on element using space or enter this.System.InvokeOnElementPressed(this.SelectedElement); } + this.KeyboardButtons.TryConsumePressed(this.Input, this.GamepadIndex); } } else if (this.Input.IsKeyPressed(Keys.Tab)) { this.IsAutoNavMode = true; @@ -230,20 +235,28 @@ namespace MLEM.Ui { // GAMEPAD INPUT if (this.HandleGamepad) { - if (this.GamepadButtons.IsPressed(this.Input, this.GamepadIndex)) { - if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) + if (this.GamepadButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) { + if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) { this.System.InvokeOnElementPressed(this.SelectedElement); - } else if (this.SecondaryGamepadButtons.IsPressed(this.Input, this.GamepadIndex)) { - if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) + this.GamepadButtons.TryConsumePressed(this.Input, this.GamepadIndex); + } + } else if (this.SecondaryGamepadButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) { + if (this.SelectedElement?.Root != null && this.SelectedElement.CanBePressed) { this.System.InvokeOnElementSecondaryPressed(this.SelectedElement); - } else if (this.DownButtons.IsPressed(this.Input, this.GamepadIndex)) { - this.HandleGamepadNextElement(Direction2.Down); - } else if (this.LeftButtons.IsPressed(this.Input, this.GamepadIndex)) { - this.HandleGamepadNextElement(Direction2.Left); - } else if (this.RightButtons.IsPressed(this.Input, this.GamepadIndex)) { - this.HandleGamepadNextElement(Direction2.Right); - } else if (this.UpButtons.IsPressed(this.Input, this.GamepadIndex)) { - this.HandleGamepadNextElement(Direction2.Up); + this.SecondaryGamepadButtons.TryConsumePressed(this.Input, this.GamepadIndex); + } + } else if (this.DownButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) { + if (this.HandleGamepadNextElement(Direction2.Down)) + this.DownButtons.TryConsumePressed(this.Input, this.GamepadIndex); + } else if (this.LeftButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) { + if (this.HandleGamepadNextElement(Direction2.Left)) + this.LeftButtons.TryConsumePressed(this.Input, this.GamepadIndex); + } else if (this.RightButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) { + if (this.HandleGamepadNextElement(Direction2.Right)) + this.RightButtons.TryConsumePressed(this.Input, this.GamepadIndex); + } else if (this.UpButtons.IsPressedAvailable(this.Input, this.GamepadIndex)) { + if (this.HandleGamepadNextElement(Direction2.Up)) + this.UpButtons.TryConsumePressed(this.Input, this.GamepadIndex); } } } @@ -413,13 +426,16 @@ namespace MLEM.Ui { } } - private void HandleGamepadNextElement(Direction2 dir) { + private bool HandleGamepadNextElement(Direction2 dir) { this.IsAutoNavMode = true; var next = this.GetGamepadNextElement(dir); if (this.SelectedElement != null) next = this.SelectedElement.GetGamepadNextElement(dir, next); - if (next != null) + if (next != null) { this.SelectElement(this.ActiveRoot, next); + return true; + } + return false; } } diff --git a/MLEM/Input/InputHandler.cs b/MLEM/Input/InputHandler.cs index fa7f18e..f930fcf 100644 --- a/MLEM/Input/InputHandler.cs +++ b/MLEM/Input/InputHandler.cs @@ -365,6 +365,15 @@ namespace MLEM.Input { return this.WasKeyUp(key) && this.IsKeyDown(key); } + /// + /// Returns if the given key is considered pressed, and if the press has not been consumed yet using . + /// + /// The key to query. + /// If the key is pressed and the press is not consumed yet. + public bool IsKeyPressedAvailable(Keys key) { + return this.IsKeyPressed(key) && !this.consumedPresses.Contains((key, -1)); + } + /// /// Returns whether the given key is considered pressed, and marks the press as consumed if it is. /// A key is considered pressed if it was not down the last update call, but is down the current update call. @@ -374,7 +383,7 @@ namespace MLEM.Input { /// The key to query. /// If the key is pressed and the press is not consumed yet. public bool TryConsumeKeyPressed(Keys key) { - if (this.IsKeyPressed(key) && !this.consumedPresses.Contains((key, -1))) { + if (this.IsKeyPressedAvailable(key)) { this.consumedPresses.Add((key, -1)); return true; } @@ -442,6 +451,15 @@ namespace MLEM.Input { return this.WasMouseButtonUp(button) && this.IsMouseButtonDown(button); } + /// + /// Returns if the given mouse button is considered pressed, and if the press has not been consumed yet using . + /// + /// The button to query. + /// If the button is pressed and the press is not consumed yet. + public bool IsMouseButtonPressedAvailable(MouseButton button) { + return this.IsMouseButtonPressed(button) && !this.consumedPresses.Contains((button, -1)); + } + /// /// Returns whether the given mouse button is considered pressed, and marks the press as consumed if it is. /// A mouse button is considered pressed if it was up the last update call, and is down the current update call. @@ -450,7 +468,7 @@ namespace MLEM.Input { /// The button to query. /// If the button is pressed and the press is not consumed yet. public bool TryConsumeMouseButtonPressed(MouseButton button) { - if (this.IsMouseButtonPressed(button) && !this.consumedPresses.Contains((button, -1))) { + if (this.IsMouseButtonPressedAvailable(button)) { this.consumedPresses.Add((button, -1)); return true; } @@ -542,6 +560,16 @@ namespace MLEM.Input { return this.WasGamepadButtonUp(button, index) && this.IsGamepadButtonDown(button, index); } + /// + /// Returns if the given gamepad button is considered pressed, and if the press has not been consumed yet using . + /// + /// The button to query. + /// The zero-based index of the gamepad, or -1 for any gamepad. + /// Whether the given button is pressed and the press is not consumed yet. + public bool IsGamepadButtonPressedAvailable(Buttons button, int index = -1) { + return this.IsGamepadButtonPressed(button) && !this.consumedPresses.Contains((button, index)) && (index < 0 || !this.consumedPresses.Contains((button, -1))); + } + /// /// Returns whether the given gamepad button on the given index is considered pressed, and marks the press as consumed if it is. /// A gamepad button is considered pressed if it was down the last update call, and is up the current update call. @@ -552,7 +580,7 @@ namespace MLEM.Input { /// The zero-based index of the gamepad, or -1 for any gamepad. /// Whether the given button is pressed and the press is not consumed yet. public bool TryConsumeGamepadButtonPressed(Buttons button, int index = -1) { - if (this.IsGamepadButtonPressed(button) && !this.consumedPresses.Contains((button, index))) { + if (this.IsGamepadButtonPressedAvailable(button, index)) { this.consumedPresses.Add((button, index)); return true; } @@ -656,7 +684,26 @@ namespace MLEM.Input { } /// - /// Returns if a given control of any kind is pressed, and marks the press as consumed if it is. + /// Returns if a given control of any kind is pressed, and if the press has not been consumed yet using . + /// + /// The control whose pressed state to query. + /// The index of the gamepad to query (if applicable), or -1 for any gamepad. + /// Whether the given control is pressed and the press is not consumed yet. + public bool IsPressedAvailable(GenericInput control, int index = -1) { + switch (control.Type) { + case GenericInput.InputType.Keyboard: + return this.IsKeyPressedAvailable(control); + case GenericInput.InputType.Gamepad: + return this.IsGamepadButtonPressedAvailable(control, index); + case GenericInput.InputType.Mouse: + return this.IsMouseButtonPressedAvailable(control); + default: + return false; + } + } + + /// + /// Returns if a given control of any kind is pressed and available, and marks the press as consumed if it is. /// This is a helper function that can be passed a , or . /// /// The control whose pressed state to query. diff --git a/MLEM/Input/Keybind.cs b/MLEM/Input/Keybind.cs index ddde6aa..9b920e7 100644 --- a/MLEM/Input/Keybind.cs +++ b/MLEM/Input/Keybind.cs @@ -131,9 +131,24 @@ namespace MLEM.Input { return false; } + /// + /// Returns whether this keybind is considered to be pressed and has not been consumed yet using. + /// See for more information. + /// + /// The input handler to query the keys with + /// The index of the gamepad to query, or -1 to query all gamepads + /// Whether this keybind is considered to be pressed + public bool IsPressedAvailable(InputHandler handler, int gamepadIndex = -1) { + foreach (var combination in this.combinations) { + if (combination.IsPressedAvailable(handler, gamepadIndex)) + return true; + } + return false; + } + /// /// Returns whether this keybind is considered to be pressed. - /// See for more information. + /// See for more information. /// /// The input handler to query the keys with /// The index of the gamepad to query, or -1 to query all gamepads @@ -239,6 +254,7 @@ namespace MLEM.Input { /// /// Returns whether this combination is currently down + /// See for more information. /// /// The input handler to query the keys with /// The index of the gamepad to query, or -1 to query all gamepads @@ -248,7 +264,8 @@ namespace MLEM.Input { } /// - /// Returns whether this combination is currently pressed + /// Returns whether this combination is currently pressed. + /// See for more information. /// /// The input handler to query the keys with /// The index of the gamepad to query, or -1 to query all gamepads @@ -257,9 +274,21 @@ namespace MLEM.Input { return this.IsModifierDown(handler, gamepadIndex) && handler.IsPressed(this.Key, gamepadIndex); } + /// + /// Returns whether this combination is currently pressed and has not been consumed yet using . + /// See for more information. + /// + /// The input handler to query the keys with + /// The index of the gamepad to query, or -1 to query all gamepads + /// Whether this combination is pressed + public bool IsPressedAvailable(InputHandler handler, int gamepadIndex = -1) { + return this.IsModifierDown(handler, gamepadIndex) && handler.IsPressedAvailable(this.Key, gamepadIndex); + } + /// /// Returns whether this combination is currently pressed and the press has not been consumed yet. /// A combination is considered consumed if this method has already returned true previously since the last call. + /// See for more information. /// /// The input handler to query the keys with /// The index of the gamepad to query, or -1 to query all gamepads