From 46c77d24447ee3c69288a8cbca93d4fc2fb4372b Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 29 Apr 2022 15:34:04 +0200 Subject: [PATCH] Added InputHandler.InvertPressBehavior --- CHANGELOG.md | 1 + MLEM/Input/InputHandler.cs | 27 +++++++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aff1632..d392208 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Jump to version: Additions - Added consuming variants of IsPressed methods to InputHandler - Added SpriteBatchContext struct and extensions +- Added InputHandler.InvertPressBehavior ### MLEM.Ui Additions diff --git a/MLEM/Input/InputHandler.cs b/MLEM/Input/InputHandler.cs index d275656..fa7f18e 100644 --- a/MLEM/Input/InputHandler.cs +++ b/MLEM/Input/InputHandler.cs @@ -61,6 +61,11 @@ namespace MLEM.Input { /// Querying of analog values is done using . /// public float GamepadButtonDeadzone; + /// + /// Set this field to true to invert the press behavior of , , and . + /// Inverted behavior means that, instead of an input counting as pressed when it was up in the last frame and is now down, it will be counted as pressed when it was down in the last frame and is now up. + /// + public bool InvertPressBehavior; /// /// An array of all , and values that are currently down. @@ -274,12 +279,14 @@ namespace MLEM.Input { this.gestures.Add(TouchPanel.ReadGesture()); } - if (this.inputsDownAccum.Count <= 0) { + if (this.inputsDownAccum.Count <= 0 && this.inputsDown.Count <= 0) { this.InputsPressed = Array.Empty(); this.InputsDown = Array.Empty(); this.inputsDown.Clear(); } else { - this.InputsPressed = this.inputsDownAccum.Keys.Where(kv => this.IsPressed(kv.Item1, kv.Item2)).Select(kv => kv.Item1).ToArray(); + // if we're inverting press behavior, we need to check the press state for the inputs that were down in the last frame + var down = this.InvertPressBehavior ? this.inputsDown : this.inputsDownAccum; + this.InputsPressed = down.Keys.Where(kv => this.IsPressed(kv.Item1, kv.Item2)).Select(kv => kv.Item1).ToArray(); this.InputsDown = this.inputsDownAccum.Keys.Select(kv => kv.Item1).ToArray(); // swapping these collections means that we don't have to keep moving entries between them (this.inputsDown, this.inputsDownAccum) = (this.inputsDownAccum, this.inputsDown); @@ -332,7 +339,7 @@ namespace MLEM.Input { /// /// Returns whether the given key is considered pressed. - /// A key is considered pressed if it was not down the last update call, but is down the current update call. + /// A key is considered pressed if it was not down the last update call, but is down the current update call. If is true, this behavior is inverted. /// If is true, this method will also return true to signify a key repeat. /// /// The key to query @@ -346,12 +353,15 @@ namespace MLEM.Input { /// /// Returns whether the given key is considered pressed. + /// A key is considered pressed if it was not down the last update call, but is down the current update call. If is true, this behavior is inverted. /// This has the same behavior as , but ignores keyboard repeat events. /// If is false, this method does the same as . /// /// The key to query /// If the key is pressed public bool IsKeyPressedIgnoreRepeats(Keys key) { + if (this.InvertPressBehavior) + return this.WasKeyDown(key) && this.IsKeyUp(key); return this.WasKeyUp(key) && this.IsKeyDown(key); } @@ -422,11 +432,13 @@ namespace MLEM.Input { /// /// Returns whether the given mouse button is considered pressed. - /// A mouse button is considered pressed if it was up the last update call, and is down the current update call. + /// A mouse button is considered pressed if it was up the last update call, and is down the current update call. If is true, this behavior is inverted. /// /// The button to query /// Whether the button is pressed public bool IsMouseButtonPressed(MouseButton button) { + if (this.InvertPressBehavior) + return this.WasMouseButtonDown(button) && this.IsMouseButtonUp(button); return this.WasMouseButtonUp(button) && this.IsMouseButtonDown(button); } @@ -495,7 +507,7 @@ namespace MLEM.Input { /// /// Returns whether the given gamepad button on the given index is considered pressed. - /// A gamepad button is considered pressed if it was down the last update call, and is up the current update call. + /// A gamepad button is considered pressed if it was down the last update call, and is up the current update call. If is true, this behavior is inverted. /// If is true, this method will also return true to signify a gamepad button repeat. /// /// The button to query @@ -517,13 +529,16 @@ namespace MLEM.Input { /// /// Returns whether the given key is considered pressed. - /// This has the same behavior as , but ignores gamepad repeat events. + /// A gamepad button is considered pressed if it was down the last update call, and is up the current update call. If is true, this behavior is inverted. + /// This has the same behavior as , but ignores gamepad repeat events. /// If is false, this method does the same as . /// /// The button to query /// The zero-based index of the gamepad, or -1 for any gamepad /// Whether the given button is pressed public bool IsGamepadButtonPressedIgnoreRepeats(Buttons button, int index = -1) { + if (this.InvertPressBehavior) + return this.WasGamepadButtonDown(button, index) && this.IsGamepadButtonUp(button, index); return this.WasGamepadButtonUp(button, index) && this.IsGamepadButtonDown(button, index); }