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

Make use of the new consuming variants in InputHandler and Keybind to consume UiControls inputs

This commit is contained in:
Ell 2022-04-30 12:14:08 +02:00
parent 4a88cca8bf
commit 610527374e
4 changed files with 118 additions and 25 deletions

View file

@ -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

View file

@ -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;
}
}

View file

@ -365,6 +365,15 @@ namespace MLEM.Input {
return this.WasKeyUp(key) && this.IsKeyDown(key);
}
/// <summary>
/// Returns if the given key is considered pressed, and if the press has not been consumed yet using <see cref="TryConsumeKeyPressed"/>.
/// </summary>
/// <param name="key">The key to query.</param>
/// <returns>If the key is pressed and the press is not consumed yet.</returns>
public bool IsKeyPressedAvailable(Keys key) {
return this.IsKeyPressed(key) && !this.consumedPresses.Contains((key, -1));
}
/// <summary>
/// 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 {
/// <param name="key">The key to query.</param>
/// <returns>If the key is pressed and the press is not consumed yet.</returns>
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);
}
/// <summary>
/// Returns if the given mouse button is considered pressed, and if the press has not been consumed yet using <see cref="TryConsumeMouseButtonPressed"/>.
/// </summary>
/// <param name="button">The button to query.</param>
/// <returns>If the button is pressed and the press is not consumed yet.</returns>
public bool IsMouseButtonPressedAvailable(MouseButton button) {
return this.IsMouseButtonPressed(button) && !this.consumedPresses.Contains((button, -1));
}
/// <summary>
/// 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 {
/// <param name="button">The button to query.</param>
/// <returns>If the button is pressed and the press is not consumed yet.</returns>
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);
}
/// <summary>
/// Returns if the given gamepad button is considered pressed, and if the press has not been consumed yet using <see cref="TryConsumeMouseButtonPressed"/>.
/// </summary>
/// <param name="button">The button to query.</param>
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad.</param>
/// <returns>Whether the given button is pressed and the press is not consumed yet.</returns>
public bool IsGamepadButtonPressedAvailable(Buttons button, int index = -1) {
return this.IsGamepadButtonPressed(button) && !this.consumedPresses.Contains((button, index)) && (index < 0 || !this.consumedPresses.Contains((button, -1)));
}
/// <summary>
/// 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 {
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad.</param>
/// <returns>Whether the given button is pressed and the press is not consumed yet.</returns>
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 {
}
/// <summary>
/// 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 <see cref="TryConsumePressed"/>.
/// </summary>
/// <param name="control">The control whose pressed state to query.</param>
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</param>
/// <returns>Whether the given control is pressed and the press is not consumed yet.</returns>
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;
}
}
/// <summary>
/// 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 <see cref="Keys"/>, <see cref="Buttons"/> or <see cref="MouseButton"/>.
/// </summary>
/// <param name="control">The control whose pressed state to query.</param>

View file

@ -131,9 +131,24 @@ namespace MLEM.Input {
return false;
}
/// <summary>
/// Returns whether this keybind is considered to be pressed and has not been consumed yet using<see cref="TryConsumePressed"/>.
/// See <see cref="InputHandler.IsPressedAvailable"/> for more information.
/// </summary>
/// <param name="handler">The input handler to query the keys with</param>
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
/// <returns>Whether this keybind is considered to be pressed</returns>
public bool IsPressedAvailable(InputHandler handler, int gamepadIndex = -1) {
foreach (var combination in this.combinations) {
if (combination.IsPressedAvailable(handler, gamepadIndex))
return true;
}
return false;
}
/// <summary>
/// Returns whether this keybind is considered to be pressed.
/// See <see cref="InputHandler.IsPressed"/> for more information.
/// See <see cref="InputHandler.TryConsumePressed"/> for more information.
/// </summary>
/// <param name="handler">The input handler to query the keys with</param>
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
@ -239,6 +254,7 @@ namespace MLEM.Input {
/// <summary>
/// Returns whether this combination is currently down
/// See <see cref="InputHandler.IsDown"/> for more information.
/// </summary>
/// <param name="handler">The input handler to query the keys with</param>
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
@ -248,7 +264,8 @@ namespace MLEM.Input {
}
/// <summary>
/// Returns whether this combination is currently pressed
/// Returns whether this combination is currently pressed.
/// See <see cref="InputHandler.IsPressed"/> for more information.
/// </summary>
/// <param name="handler">The input handler to query the keys with</param>
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
@ -257,9 +274,21 @@ namespace MLEM.Input {
return this.IsModifierDown(handler, gamepadIndex) && handler.IsPressed(this.Key, gamepadIndex);
}
/// <summary>
/// Returns whether this combination is currently pressed and has not been consumed yet using <see cref="TryConsumePressed"/>.
/// See <see cref="InputHandler.IsPressedAvailable"/> for more information.
/// </summary>
/// <param name="handler">The input handler to query the keys with</param>
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>
/// <returns>Whether this combination is pressed</returns>
public bool IsPressedAvailable(InputHandler handler, int gamepadIndex = -1) {
return this.IsModifierDown(handler, gamepadIndex) && handler.IsPressedAvailable(this.Key, gamepadIndex);
}
/// <summary>
/// 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 <see cref="InputHandler.Update()"/> call.
/// See <see cref="InputHandler.TryConsumePressed"/> for more information.
/// </summary>
/// <param name="handler">The input handler to query the keys with</param>
/// <param name="gamepadIndex">The index of the gamepad to query, or -1 to query all gamepads</param>