1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 22:18:34 +01:00

- Added InputHandler.IsPressedIgnoreRepeats

- Marked non-GenericInput versions of IsDown, IsUp, IsPressed and related methods as obsolete in favor of GenericInput ones
This commit is contained in:
Ell 2022-12-27 22:57:35 +01:00
parent 79a2eaa8d2
commit ef0499958d
6 changed files with 145 additions and 86 deletions

View file

@ -20,7 +20,7 @@ Additions
- Added UniformTextureAtlas methods ToList and ToDictionary - Added UniformTextureAtlas methods ToList and ToDictionary
- Added SingleRandom and SeedSource - Added SingleRandom and SeedSource
- Added TokenizedString.GetArea - Added TokenizedString.GetArea
- Added InputHandler.WasPressedForLess and related methods - Added InputHandler.WasPressedForLess and related methods as well as InputHandler.IsPressedIgnoreRepeats
- **Added the ability to find paths to one of multiple goals using AStar** - **Added the ability to find paths to one of multiple goals using AStar**
Improvements Improvements
@ -49,6 +49,7 @@ Removals
- Marked EnumHelper as obsolete due to its reimplementation in [DynamicEnums](https://www.nuget.org/packages/DynamicEnums) - Marked EnumHelper as obsolete due to its reimplementation in [DynamicEnums](https://www.nuget.org/packages/DynamicEnums)
- Marked Code.GetReplacementString as obsolete - Marked Code.GetReplacementString as obsolete
- Marked TokenizedString.Measure as obsolete in favor of GetArea - Marked TokenizedString.Measure as obsolete in favor of GetArea
- Marked non-GenericInput versions of IsDown, IsUp, IsPressed and related methods as obsolete in favor of GenericInput ones
### MLEM.Ui ### MLEM.Ui
Additions Additions

View file

@ -147,8 +147,8 @@ namespace MLEM.Ui.Elements {
// MOUSE INPUT // MOUSE INPUT
var moused = this.Controls.MousedElement; var moused = this.Controls.MousedElement;
var wasMouseUp = this.Input.WasMouseButtonUp(MouseButton.Left); var wasMouseUp = this.Input.WasUp(MouseButton.Left);
var isMouseDown = this.Input.IsMouseButtonDown(MouseButton.Left); var isMouseDown = this.Input.IsDown(MouseButton.Left);
if (moused == this && wasMouseUp && isMouseDown) { if (moused == this && wasMouseUp && isMouseDown) {
this.isMouseScrolling = true; this.isMouseScrolling = true;
this.scrollStartOffset = this.TransformInverseAll(this.Input.ViewportMousePosition.ToVector2()) - this.ScrollerPosition; this.scrollStartOffset = this.TransformInverseAll(this.Input.ViewportMousePosition.ToVector2()) - this.ScrollerPosition;

View file

@ -165,19 +165,19 @@ namespace MLEM.Ui {
var mousedNow = this.GetElementUnderPos(new Vector2(this.Input.ViewportMousePosition.X, this.Input.ViewportMousePosition.Y)); var mousedNow = this.GetElementUnderPos(new Vector2(this.Input.ViewportMousePosition.X, this.Input.ViewportMousePosition.Y));
this.SetMousedElement(mousedNow); this.SetMousedElement(mousedNow);
if (this.Input.IsMouseButtonPressedAvailable(MouseButton.Left)) { if (this.Input.IsPressedAvailable(MouseButton.Left)) {
this.IsAutoNavMode = false; this.IsAutoNavMode = false;
var selectedNow = mousedNow != null && mousedNow.CanBeSelected ? mousedNow : null; var selectedNow = mousedNow != null && mousedNow.CanBeSelected ? mousedNow : null;
this.SelectElement(this.ActiveRoot, selectedNow); this.SelectElement(this.ActiveRoot, selectedNow);
if (mousedNow != null && mousedNow.CanBePressed) { if (mousedNow != null && mousedNow.CanBePressed) {
this.System.InvokeOnElementPressed(mousedNow); this.System.InvokeOnElementPressed(mousedNow);
this.Input.TryConsumeMouseButtonPressed(MouseButton.Left); this.Input.TryConsumePressed(MouseButton.Left);
} }
} else if (this.Input.IsMouseButtonPressedAvailable(MouseButton.Right)) { } else if (this.Input.IsPressedAvailable(MouseButton.Right)) {
this.IsAutoNavMode = false; this.IsAutoNavMode = false;
if (mousedNow != null && mousedNow.CanBePressed) { if (mousedNow != null && mousedNow.CanBePressed) {
this.System.InvokeOnElementSecondaryPressed(mousedNow); this.System.InvokeOnElementSecondaryPressed(mousedNow);
this.Input.TryConsumeMouseButtonPressed(MouseButton.Right); this.Input.TryConsumePressed(MouseButton.Right);
} }
} }
} else { } else {
@ -196,7 +196,7 @@ namespace MLEM.Ui {
} }
this.KeyboardButtons.TryConsumePressed(this.Input, this.GamepadIndex); this.KeyboardButtons.TryConsumePressed(this.Input, this.GamepadIndex);
} }
} else if (this.Input.IsKeyPressedAvailable(Keys.Tab)) { } else if (this.Input.IsPressedAvailable(Keys.Tab)) {
this.IsAutoNavMode = true; this.IsAutoNavMode = true;
// tab or shift-tab to next or previous element // tab or shift-tab to next or previous element
var backward = this.Input.IsModifierKeyDown(ModifierKey.Shift); var backward = this.Input.IsModifierKeyDown(ModifierKey.Shift);
@ -205,7 +205,7 @@ namespace MLEM.Ui {
next = this.SelectedElement.GetTabNextElement(backward, next); next = this.SelectedElement.GetTabNextElement(backward, next);
if (next != this.SelectedElement) { if (next != this.SelectedElement) {
this.SelectElement(this.ActiveRoot, next); this.SelectElement(this.ActiveRoot, next);
this.Input.TryConsumeKeyPressed(Keys.Tab); this.Input.TryConsumePressed(Keys.Tab);
} }
} }
} }

View file

@ -6,11 +6,12 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch; using Microsoft.Xna.Framework.Input.Touch;
using static MLEM.Input.GenericInput;
namespace MLEM.Input { namespace MLEM.Input {
/// <summary> /// <summary>
/// An input handler is a more advanced wrapper around MonoGame's default input system. /// An input handler is a more advanced wrapper around MonoGame's default input system.
/// It includes keyboard, mouse, gamepad and touch states, as well as a new "pressed" state for keys and the ability for keyboard and gamepad repeat events. /// It includes keyboard, mouse, gamepad and touch handling through the <see cref="GenericInput"/> wrapper, as well as a new "pressed" state for inputs, the ability for keyboard and gamepad repeat events, and the ability to track down, up and press times for inputs.
/// </summary> /// </summary>
public class InputHandler : GameComponent { public class InputHandler : GameComponent {
@ -277,7 +278,7 @@ namespace MLEM.Input {
if (active) { if (active) {
this.gamepads[i] = GamePad.GetState((PlayerIndex) i); this.gamepads[i] = GamePad.GetState((PlayerIndex) i);
foreach (var button in InputHandler.AllButtons) { foreach (var button in InputHandler.AllButtons) {
if (this.IsGamepadButtonDown(button, i)) if (this.IsDown(button, i))
this.AccumulateDown(button, i); this.AccumulateDown(button, i);
} }
} }
@ -290,7 +291,7 @@ namespace MLEM.Input {
this.triggerGamepadButtonRepeat[i] = false; this.triggerGamepadButtonRepeat[i] = false;
if (this.HandleGamepadRepeats) { if (this.HandleGamepadRepeats) {
this.heldGamepadButtons[i] = InputHandler.AllButtons this.heldGamepadButtons[i] = InputHandler.AllButtons
.Where(b => this.IsGamepadButtonDown(b, i)) .Where(b => this.IsDown(b, i))
.OrderBy(b => this.GetDownTime(b, i)) .OrderBy(b => this.GetDownTime(b, i))
.Cast<Buttons?>().FirstOrDefault(); .Cast<Buttons?>().FirstOrDefault();
if (this.heldGamepadButtons[i].HasValue && this.TryGetDownTime(this.heldGamepadButtons[i].Value, out var heldTime, i)) { if (this.heldGamepadButtons[i].HasValue && this.TryGetDownTime(this.heldGamepadButtons[i].Value, out var heldTime, i)) {
@ -398,22 +399,39 @@ namespace MLEM.Input {
return this.gamepads[index]; return this.gamepads[index];
} }
/// <summary>
/// Returns whether the given modifier key is down.
/// </summary>
/// <param name="modifier">The modifier key</param>
/// <returns>If the modifier key is down</returns>
public bool IsModifierKeyDown(ModifierKey modifier) {
foreach (var key in modifier.GetKeys()) {
if (this.IsDown(key))
return true;
}
return false;
}
/// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyDown"/> /// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyDown"/>
[Obsolete("This method is deprecated. Use the GenericInput version IsDown instead.")]
public bool IsKeyDown(Keys key) { public bool IsKeyDown(Keys key) {
return this.KeyboardState.IsKeyDown(key); return this.KeyboardState.IsKeyDown(key);
} }
/// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyUp"/> /// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyUp"/>
[Obsolete("This method is deprecated. Use the GenericInput version IsUp instead.")]
public bool IsKeyUp(Keys key) { public bool IsKeyUp(Keys key) {
return this.KeyboardState.IsKeyUp(key); return this.KeyboardState.IsKeyUp(key);
} }
/// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyDown"/> /// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyDown"/>
[Obsolete("This method is deprecated. Use the GenericInput version WasDown instead.")]
public bool WasKeyDown(Keys key) { public bool WasKeyDown(Keys key) {
return this.LastKeyboardState.IsKeyDown(key); return this.LastKeyboardState.IsKeyDown(key);
} }
/// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyUp"/> /// <inheritdoc cref="Microsoft.Xna.Framework.Input.KeyboardState.IsKeyUp"/>
[Obsolete("This method is deprecated. Use the GenericInput version WasUp instead.")]
public bool WasKeyUp(Keys key) { public bool WasKeyUp(Keys key) {
return this.LastKeyboardState.IsKeyUp(key); return this.LastKeyboardState.IsKeyUp(key);
} }
@ -425,6 +443,7 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="key">The key to query</param> /// <param name="key">The key to query</param>
/// <returns>If the key is pressed</returns> /// <returns>If the key is pressed</returns>
[Obsolete("This method is deprecated. Use the GenericInput version IsPressed instead.")]
public bool IsKeyPressed(Keys key) { public bool IsKeyPressed(Keys key) {
// if the queried key is the held key and a repeat should be triggered, return true // if the queried key is the held key and a repeat should be triggered, return true
if (this.HandleKeyboardRepeats && key == this.heldKey && this.triggerKeyRepeat) if (this.HandleKeyboardRepeats && key == this.heldKey && this.triggerKeyRepeat)
@ -440,6 +459,7 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="key">The key to query</param> /// <param name="key">The key to query</param>
/// <returns>If the key is pressed</returns> /// <returns>If the key is pressed</returns>
[Obsolete("This method is deprecated. Use the GenericInput version IsPressedIgnoreRepeats instead.")]
public bool IsKeyPressedIgnoreRepeats(Keys key) { public bool IsKeyPressedIgnoreRepeats(Keys key) {
if (this.InvertPressBehavior) if (this.InvertPressBehavior)
return this.WasKeyDown(key) && this.IsKeyUp(key); return this.WasKeyDown(key) && this.IsKeyUp(key);
@ -451,6 +471,7 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="key">The key to query.</param> /// <param name="key">The key to query.</param>
/// <returns>If the key is pressed and the press is not consumed yet.</returns> /// <returns>If the key is pressed and the press is not consumed yet.</returns>
[Obsolete("This method is deprecated. Use the GenericInput version IsPressedAvailable instead.")]
public bool IsKeyPressedAvailable(Keys key) { public bool IsKeyPressedAvailable(Keys key) {
return this.IsKeyPressed(key) && !this.IsPressConsumed(key); return this.IsKeyPressed(key) && !this.IsPressConsumed(key);
} }
@ -463,6 +484,7 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="key">The key to query.</param> /// <param name="key">The key to query.</param>
/// <returns>If the key is pressed and the press is not consumed yet.</returns> /// <returns>If the key is pressed and the press is not consumed yet.</returns>
[Obsolete("This method is deprecated. Use the GenericInput version TryConsumePressed instead.")]
public bool TryConsumeKeyPressed(Keys key) { public bool TryConsumeKeyPressed(Keys key) {
if (this.IsKeyPressedAvailable(key)) { if (this.IsKeyPressedAvailable(key)) {
this.consumedPresses.Add((key, -1)); this.consumedPresses.Add((key, -1));
@ -471,24 +493,12 @@ namespace MLEM.Input {
return false; return false;
} }
/// <summary>
/// Returns whether the given modifier key is down.
/// </summary>
/// <param name="modifier">The modifier key</param>
/// <returns>If the modifier key is down</returns>
public bool IsModifierKeyDown(ModifierKey modifier) {
foreach (var key in modifier.GetKeys()) {
if (this.IsKeyDown(key))
return true;
}
return false;
}
/// <summary> /// <summary>
/// Returns whether the given mouse button is currently down. /// Returns whether the given mouse button is currently down.
/// </summary> /// </summary>
/// <param name="button">The button to query</param> /// <param name="button">The button to query</param>
/// <returns>Whether or not the queried button is down</returns> /// <returns>Whether or not the queried button is down</returns>
[Obsolete("This method is deprecated. Use the GenericInput version IsDown instead.")]
public bool IsMouseButtonDown(MouseButton button) { public bool IsMouseButtonDown(MouseButton button) {
return this.MouseState.GetState(button) == ButtonState.Pressed; return this.MouseState.GetState(button) == ButtonState.Pressed;
} }
@ -498,6 +508,7 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="button">The button to query</param> /// <param name="button">The button to query</param>
/// <returns>Whether or not the queried button is up</returns> /// <returns>Whether or not the queried button is up</returns>
[Obsolete("This method is deprecated. Use the GenericInput version IsUp instead.")]
public bool IsMouseButtonUp(MouseButton button) { public bool IsMouseButtonUp(MouseButton button) {
return this.MouseState.GetState(button) == ButtonState.Released; return this.MouseState.GetState(button) == ButtonState.Released;
} }
@ -507,6 +518,7 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="button">The button to query</param> /// <param name="button">The button to query</param>
/// <returns>Whether or not the queried button was down</returns> /// <returns>Whether or not the queried button was down</returns>
[Obsolete("This method is deprecated. Use the GenericInput version WasDown instead.")]
public bool WasMouseButtonDown(MouseButton button) { public bool WasMouseButtonDown(MouseButton button) {
return this.LastMouseState.GetState(button) == ButtonState.Pressed; return this.LastMouseState.GetState(button) == ButtonState.Pressed;
} }
@ -516,6 +528,7 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="button">The button to query</param> /// <param name="button">The button to query</param>
/// <returns>Whether or not the queried button was up</returns> /// <returns>Whether or not the queried button was up</returns>
[Obsolete("This method is deprecated. Use the GenericInput version WasUp instead.")]
public bool WasMouseButtonUp(MouseButton button) { public bool WasMouseButtonUp(MouseButton button) {
return this.LastMouseState.GetState(button) == ButtonState.Released; return this.LastMouseState.GetState(button) == ButtonState.Released;
} }
@ -526,6 +539,7 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="button">The button to query</param> /// <param name="button">The button to query</param>
/// <returns>Whether the button is pressed</returns> /// <returns>Whether the button is pressed</returns>
[Obsolete("This method is deprecated. Use the GenericInput version IsPressed instead.")]
public bool IsMouseButtonPressed(MouseButton button) { public bool IsMouseButtonPressed(MouseButton button) {
if (this.InvertPressBehavior) if (this.InvertPressBehavior)
return this.WasMouseButtonDown(button) && this.IsMouseButtonUp(button); return this.WasMouseButtonDown(button) && this.IsMouseButtonUp(button);
@ -537,6 +551,7 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="button">The button to query.</param> /// <param name="button">The button to query.</param>
/// <returns>If the button is pressed and the press is not consumed yet.</returns> /// <returns>If the button is pressed and the press is not consumed yet.</returns>
[Obsolete("This method is deprecated. Use the GenericInput version IsPressedAvailable instead.")]
public bool IsMouseButtonPressedAvailable(MouseButton button) { public bool IsMouseButtonPressedAvailable(MouseButton button) {
return this.IsMouseButtonPressed(button) && !this.IsPressConsumed(button); return this.IsMouseButtonPressed(button) && !this.IsPressConsumed(button);
} }
@ -548,6 +563,7 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="button">The button to query.</param> /// <param name="button">The button to query.</param>
/// <returns>If the button is pressed and the press is not consumed yet.</returns> /// <returns>If the button is pressed and the press is not consumed yet.</returns>
[Obsolete("This method is deprecated. Use the GenericInput version TryConsumePressed instead.")]
public bool TryConsumeMouseButtonPressed(MouseButton button) { public bool TryConsumeMouseButtonPressed(MouseButton button) {
if (this.IsMouseButtonPressedAvailable(button)) { if (this.IsMouseButtonPressedAvailable(button)) {
this.consumedPresses.Add((button, -1)); this.consumedPresses.Add((button, -1));
@ -557,6 +573,7 @@ namespace MLEM.Input {
} }
/// <inheritdoc cref="GamePadState.IsButtonDown"/> /// <inheritdoc cref="GamePadState.IsButtonDown"/>
[Obsolete("This method is deprecated. Use the GenericInput version IsDown instead.")]
public bool IsGamepadButtonDown(Buttons button, int index = -1) { public bool IsGamepadButtonDown(Buttons button, int index = -1) {
if (index < 0) { if (index < 0) {
for (var i = 0; i < this.ConnectedGamepads; i++) { for (var i = 0; i < this.ConnectedGamepads; i++) {
@ -569,6 +586,7 @@ namespace MLEM.Input {
} }
/// <inheritdoc cref="GamePadState.IsButtonUp"/> /// <inheritdoc cref="GamePadState.IsButtonUp"/>
[Obsolete("This method is deprecated. Use the GenericInput version IsUp instead.")]
public bool IsGamepadButtonUp(Buttons button, int index = -1) { public bool IsGamepadButtonUp(Buttons button, int index = -1) {
if (index < 0) { if (index < 0) {
for (var i = 0; i < this.ConnectedGamepads; i++) { for (var i = 0; i < this.ConnectedGamepads; i++) {
@ -581,6 +599,7 @@ namespace MLEM.Input {
} }
/// <inheritdoc cref="GamePadState.IsButtonDown"/> /// <inheritdoc cref="GamePadState.IsButtonDown"/>
[Obsolete("This method is deprecated. Use the GenericInput version WasDown instead.")]
public bool WasGamepadButtonDown(Buttons button, int index = -1) { public bool WasGamepadButtonDown(Buttons button, int index = -1) {
if (index < 0) { if (index < 0) {
for (var i = 0; i < this.ConnectedGamepads; i++) { for (var i = 0; i < this.ConnectedGamepads; i++) {
@ -593,6 +612,7 @@ namespace MLEM.Input {
} }
/// <inheritdoc cref="GamePadState.IsButtonUp"/> /// <inheritdoc cref="GamePadState.IsButtonUp"/>
[Obsolete("This method is deprecated. Use the GenericInput version WasUp instead.")]
public bool WasGamepadButtonUp(Buttons button, int index = -1) { public bool WasGamepadButtonUp(Buttons button, int index = -1) {
if (index < 0) { if (index < 0) {
for (var i = 0; i < this.ConnectedGamepads; i++) { for (var i = 0; i < this.ConnectedGamepads; i++) {
@ -612,6 +632,7 @@ namespace MLEM.Input {
/// <param name="button">The button to query</param> /// <param name="button">The button to query</param>
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad</param> /// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad</param>
/// <returns>Whether the given button is pressed</returns> /// <returns>Whether the given button is pressed</returns>
[Obsolete("This method is deprecated. Use the GenericInput version IsPressed instead.")]
public bool IsGamepadButtonPressed(Buttons button, int index = -1) { public bool IsGamepadButtonPressed(Buttons button, int index = -1) {
if (this.HandleGamepadRepeats) { if (this.HandleGamepadRepeats) {
if (index < 0) { if (index < 0) {
@ -635,6 +656,7 @@ namespace MLEM.Input {
/// <param name="button">The button to query</param> /// <param name="button">The button to query</param>
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad</param> /// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad</param>
/// <returns>Whether the given button is pressed</returns> /// <returns>Whether the given button is pressed</returns>
[Obsolete("This method is deprecated. Use the GenericInput version IsPressedIgnoreRepeats instead.")]
public bool IsGamepadButtonPressedIgnoreRepeats(Buttons button, int index = -1) { public bool IsGamepadButtonPressedIgnoreRepeats(Buttons button, int index = -1) {
if (this.InvertPressBehavior) if (this.InvertPressBehavior)
return this.WasGamepadButtonDown(button, index) && this.IsGamepadButtonUp(button, index); return this.WasGamepadButtonDown(button, index) && this.IsGamepadButtonUp(button, index);
@ -647,6 +669,7 @@ namespace MLEM.Input {
/// <param name="button">The button to query.</param> /// <param name="button">The button to query.</param>
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad.</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> /// <returns>Whether the given button is pressed and the press is not consumed yet.</returns>
[Obsolete("This method is deprecated. Use the GenericInput version IsPressedAvailable instead.")]
public bool IsGamepadButtonPressedAvailable(Buttons button, int index = -1) { public bool IsGamepadButtonPressedAvailable(Buttons button, int index = -1) {
return this.IsGamepadButtonPressed(button) && !this.IsPressConsumed(button, index) && (index < 0 || !this.IsPressConsumed(button)); return this.IsGamepadButtonPressed(button) && !this.IsPressConsumed(button, index) && (index < 0 || !this.IsPressConsumed(button));
} }
@ -660,6 +683,7 @@ namespace MLEM.Input {
/// <param name="button">The button to query.</param> /// <param name="button">The button to query.</param>
/// <param name="index">The zero-based index of the gamepad, or -1 for any gamepad.</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> /// <returns>Whether the given button is pressed and the press is not consumed yet.</returns>
[Obsolete("This method is deprecated. Use the GenericInput version TryConsumePressed instead.")]
public bool TryConsumeGamepadButtonPressed(Buttons button, int index = -1) { public bool TryConsumeGamepadButtonPressed(Buttons button, int index = -1) {
if (this.IsGamepadButtonPressedAvailable(button, index)) { if (this.IsGamepadButtonPressedAvailable(button, index)) {
this.consumedPresses.Add((button, index)); this.consumedPresses.Add((button, index));
@ -722,15 +746,21 @@ namespace MLEM.Input {
/// <param name="control">The control whose down state to query</param> /// <param name="control">The control whose down state to query</param>
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</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 down</returns> /// <returns>Whether the given control is down</returns>
/// <exception cref="ArgumentException">If the passed control isn't of a supported type</exception>
public bool IsDown(GenericInput control, int index = -1) { public bool IsDown(GenericInput control, int index = -1) {
switch (control.Type) { switch (control.Type) {
case GenericInput.InputType.Keyboard: case InputType.Keyboard:
return this.IsKeyDown(control); return this.KeyboardState.IsKeyDown(control);
case GenericInput.InputType.Gamepad: case InputType.Gamepad:
return this.IsGamepadButtonDown(control, index); if (index < 0) {
case GenericInput.InputType.Mouse: for (var i = 0; i < this.ConnectedGamepads; i++) {
return this.IsMouseButtonDown(control); if (this.GetGamepadState(i).GetAnalogValue(control) > this.GamepadButtonDeadzone)
return true;
}
return false;
}
return this.GetGamepadState(index).GetAnalogValue(control) > this.GamepadButtonDeadzone;
case InputType.Mouse:
return this.MouseState.GetState(control) == ButtonState.Pressed;
default: default:
return false; return false;
} }
@ -743,15 +773,21 @@ namespace MLEM.Input {
/// <param name="control">The control whose up state to query</param> /// <param name="control">The control whose up state to query</param>
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</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 up.</returns> /// <returns>Whether the given control is up.</returns>
/// <exception cref="ArgumentException">If the passed control isn't of a supported type</exception>
public bool IsUp(GenericInput control, int index = -1) { public bool IsUp(GenericInput control, int index = -1) {
switch (control.Type) { switch (control.Type) {
case GenericInput.InputType.Keyboard: case InputType.Keyboard:
return this.IsKeyUp(control); return this.KeyboardState.IsKeyUp(control);
case GenericInput.InputType.Gamepad: case InputType.Gamepad:
return this.IsGamepadButtonUp(control, index); if (index < 0) {
case GenericInput.InputType.Mouse: for (var i = 0; i < this.ConnectedGamepads; i++) {
return this.IsMouseButtonUp(control); if (this.GetGamepadState(i).GetAnalogValue(control) <= this.GamepadButtonDeadzone)
return true;
}
return false;
}
return this.GetGamepadState(index).GetAnalogValue(control) <= this.GamepadButtonDeadzone;
case InputType.Mouse:
return this.MouseState.GetState(control) == ButtonState.Released;
default: default:
return true; return true;
} }
@ -764,15 +800,21 @@ namespace MLEM.Input {
/// <param name="control">The control whose down state to query</param> /// <param name="control">The control whose down state to query</param>
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param> /// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param>
/// <returns>Whether the given control was down</returns> /// <returns>Whether the given control was down</returns>
/// <exception cref="ArgumentException">If the passed control isn't of a supported type</exception>
public bool WasDown(GenericInput control, int index = -1) { public bool WasDown(GenericInput control, int index = -1) {
switch (control.Type) { switch (control.Type) {
case GenericInput.InputType.Keyboard: case InputType.Keyboard:
return this.WasKeyDown(control); return this.LastKeyboardState.IsKeyDown(control);
case GenericInput.InputType.Gamepad: case InputType.Gamepad:
return this.WasGamepadButtonDown(control, index); if (index < 0) {
case GenericInput.InputType.Mouse: for (var i = 0; i < this.ConnectedGamepads; i++) {
return this.WasMouseButtonDown(control); if (this.GetLastGamepadState(i).GetAnalogValue(control) > this.GamepadButtonDeadzone)
return true;
}
return false;
}
return this.GetLastGamepadState(index).GetAnalogValue(control) > this.GamepadButtonDeadzone;
case InputType.Mouse:
return this.LastMouseState.GetState(control) == ButtonState.Pressed;
default: default:
return false; return false;
} }
@ -785,15 +827,21 @@ namespace MLEM.Input {
/// <param name="control">The control whose up state to query</param> /// <param name="control">The control whose up state to query</param>
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param> /// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad</param>
/// <returns>Whether the given control was up.</returns> /// <returns>Whether the given control was up.</returns>
/// <exception cref="ArgumentException">If the passed control isn't of a supported type</exception>
public bool WasUp(GenericInput control, int index = -1) { public bool WasUp(GenericInput control, int index = -1) {
switch (control.Type) { switch (control.Type) {
case GenericInput.InputType.Keyboard: case InputType.Keyboard:
return this.WasKeyUp(control); return this.LastKeyboardState.IsKeyUp(control);
case GenericInput.InputType.Gamepad: case InputType.Gamepad:
return this.WasGamepadButtonUp(control, index); if (index < 0) {
case GenericInput.InputType.Mouse: for (var i = 0; i < this.ConnectedGamepads; i++) {
return this.WasMouseButtonUp(control); if (this.GetLastGamepadState(i).GetAnalogValue(control) <= this.GamepadButtonDeadzone)
return true;
}
return false;
}
return this.GetLastGamepadState(index).GetAnalogValue(control) <= this.GamepadButtonDeadzone;
case InputType.Mouse:
return this.LastMouseState.GetState(control) == ButtonState.Released;
default: default:
return true; return true;
} }
@ -801,23 +849,47 @@ namespace MLEM.Input {
/// <summary> /// <summary>
/// Returns if a given control of any kind is pressed. /// Returns if a given control of any kind is pressed.
/// This is a helper function that can be passed a <see cref="Keys"/>, <see cref="Buttons"/> or <see cref="MouseButton"/>. /// If <see cref="HandleKeyboardRepeats"/> or <see cref="HandleGamepadRepeats"/> are true, this method will also return true to signify a key or gamepad button repeat.
/// An input is considered pressed if it was not down the last update call, but is down the current update call. If <see cref="InvertPressBehavior"/> is true, this behavior is inverted.
/// </summary> /// </summary>
/// <param name="control">The control whose pressed state to query</param> /// <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> /// <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.</returns> /// <returns>Whether the given control is pressed.</returns>
/// <exception cref="ArgumentException">If the passed control isn't of a supported type</exception>
public bool IsPressed(GenericInput control, int index = -1) { public bool IsPressed(GenericInput control, int index = -1) {
// handle repeat events for specific inputs, and delegate to default "ignore repeats" behavior otherwise
switch (control.Type) { switch (control.Type) {
case GenericInput.InputType.Keyboard: case InputType.Keyboard:
return this.IsKeyPressed(control); if (this.HandleKeyboardRepeats && (Keys) control == this.heldKey && this.triggerKeyRepeat)
case GenericInput.InputType.Gamepad: return true;
return this.IsGamepadButtonPressed(control, index); break;
case GenericInput.InputType.Mouse: case InputType.Gamepad:
return this.IsMouseButtonPressed(control); if (this.HandleGamepadRepeats) {
default: if (index < 0) {
return false; for (var i = 0; i < this.ConnectedGamepads; i++) {
if (this.heldGamepadButtons[i] == (Buttons) control && this.triggerGamepadButtonRepeat[i])
return true;
} }
} else if (this.heldGamepadButtons[index] == (Buttons) control && this.triggerGamepadButtonRepeat[index]) {
return true;
}
}
break;
}
return this.IsPressedIgnoreRepeats(control, index);
}
/// <summary>
/// An input is considered pressed if it was not down the last update call, but is down the current update call. If <see cref="InvertPressBehavior"/> is true, this behavior is inverted.
/// This has the same behavior as <see cref="IsPressed"/>, but ignores keyboard and gamepad repeat events.
/// If <see cref="HandleKeyboardRepeats"/> and <see cref="HandleGamepadRepeats"/> are false, this method does the same as <see cref="IsPressed"/>.
/// </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, ignoring repeat events.</returns>
public bool IsPressedIgnoreRepeats(GenericInput control, int index = -1) {
if (this.InvertPressBehavior)
return this.WasDown(control, index) && this.IsUp(control, index);
return this.WasUp(control, index) && this.IsDown(control, index);
} }
/// <summary> /// <summary>
@ -827,16 +899,7 @@ namespace MLEM.Input {
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</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> /// <returns>Whether the given control is pressed and the press is not consumed yet.</returns>
public bool IsPressedAvailable(GenericInput control, int index = -1) { public bool IsPressedAvailable(GenericInput control, int index = -1) {
switch (control.Type) { return this.IsPressed(control, index) && !this.IsPressConsumed(control, index);
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> /// <summary>
@ -847,16 +910,11 @@ namespace MLEM.Input {
/// <param name="index">The index of the gamepad to query (if applicable), or -1 for any gamepad.</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> /// <returns>Whether the given control is pressed and the press is not consumed yet.</returns>
public bool TryConsumePressed(GenericInput control, int index = -1) { public bool TryConsumePressed(GenericInput control, int index = -1) {
switch (control.Type) { if (this.IsPressedAvailable(control, index)) {
case GenericInput.InputType.Keyboard: this.consumedPresses.Add((control, index));
return this.TryConsumeKeyPressed(control); return true;
case GenericInput.InputType.Gamepad:
return this.TryConsumeGamepadButtonPressed(control, index);
case GenericInput.InputType.Mouse:
return this.TryConsumeMouseButtonPressed(control);
default:
return false;
} }
return false;
} }
/// <summary> /// <summary>

View file

@ -289,16 +289,16 @@ namespace MLEM.Input {
this.CaretPos--; this.CaretPos--;
} else if (this.CaretPos < this.text.Length && input.TryConsumePressed(Keys.Right)) { } else if (this.CaretPos < this.text.Length && input.TryConsumePressed(Keys.Right)) {
this.CaretPos++; this.CaretPos++;
} else if (this.Multiline && input.IsKeyPressedAvailable(Keys.Up) && this.MoveCaretToLine(this.CaretLine - 1)) { } else if (this.Multiline && input.IsPressedAvailable(Keys.Up) && this.MoveCaretToLine(this.CaretLine - 1)) {
input.TryConsumePressed(Keys.Up); input.TryConsumePressed(Keys.Up);
} else if (this.Multiline && input.IsKeyPressedAvailable(Keys.Down) && this.MoveCaretToLine(this.CaretLine + 1)) { } else if (this.Multiline && input.IsPressedAvailable(Keys.Down) && this.MoveCaretToLine(this.CaretLine + 1)) {
input.TryConsumePressed(Keys.Down); input.TryConsumePressed(Keys.Down);
} else if (this.CaretPos != 0 && input.TryConsumePressed(Keys.Home)) { } else if (this.CaretPos != 0 && input.TryConsumePressed(Keys.Home)) {
this.CaretPos = 0; this.CaretPos = 0;
} else if (this.CaretPos != this.text.Length && input.TryConsumePressed(Keys.End)) { } else if (this.CaretPos != this.text.Length && input.TryConsumePressed(Keys.End)) {
this.CaretPos = this.text.Length; this.CaretPos = this.text.Length;
} else if (input.IsModifierKeyDown(ModifierKey.Control)) { } else if (input.IsModifierKeyDown(ModifierKey.Control)) {
if (input.IsKeyPressedAvailable(Keys.V)) { if (input.IsPressedAvailable(Keys.V)) {
var clip = this.PasteFromClipboardFunction?.Invoke(); var clip = this.PasteFromClipboardFunction?.Invoke();
if (clip != null) { if (clip != null) {
this.InsertText(clip, true); this.InsertText(clip, true);

View file

@ -387,7 +387,7 @@ public class GameImpl : MlemGame {
protected override void DoUpdate(GameTime gameTime) { protected override void DoUpdate(GameTime gameTime) {
base.DoUpdate(gameTime); base.DoUpdate(gameTime);
if (this.InputHandler.IsKeyPressed(Keys.F11)) if (this.InputHandler.IsPressed(Keys.F11))
this.GraphicsDeviceManager.SetFullscreen(!this.GraphicsDeviceManager.IsFullScreen); this.GraphicsDeviceManager.SetFullscreen(!this.GraphicsDeviceManager.IsFullScreen);
var delta = this.InputHandler.ScrollWheel - this.InputHandler.LastScrollWheel; var delta = this.InputHandler.ScrollWheel - this.InputHandler.LastScrollWheel;