From 8bb62a2ce5f37532c1395679e6496fc2b4f726c4 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 8 Nov 2022 17:43:34 +0100 Subject: [PATCH] also added WasModifierDown and WasDown to Keybind and Combination --- CHANGELOG.md | 2 +- MLEM/Input/InputHandler.cs | 42 +++++++++++++++++++++++++++ MLEM/Input/Keybind.cs | 59 +++++++++++++++++++++++++++++++++++++- 3 files changed, 101 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e97d705..d80b22b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ Jump to version: Additions - Added TokenizedString.Realign - Added GetFlags and GetUniqueFlags to EnumHelper -- Added GetDownTime, GetUpTime and GetTimeSincePress to Keybind and Combination +- Added GetDownTime, GetUpTime, GetTimeSincePress, WasModifierDown and WasDown to Keybind and Combination - **Added the ability to find paths to one of multiple goals using AStar** Improvements diff --git a/MLEM/Input/InputHandler.cs b/MLEM/Input/InputHandler.cs index 4cb6740..d32c305 100644 --- a/MLEM/Input/InputHandler.cs +++ b/MLEM/Input/InputHandler.cs @@ -757,6 +757,48 @@ namespace MLEM.Input { } } + /// + /// Returns if a given control of any kind was down in the last update call. + /// This is a helper function that can be passed a , or . + /// + /// The control whose down state to query + /// The index of the gamepad to query (if applicable), or -1 for any gamepad + /// Whether the given control was down + /// If the passed control isn't of a supported type + public bool WasDown(GenericInput control, int index = -1) { + switch (control.Type) { + case GenericInput.InputType.Keyboard: + return this.WasKeyDown(control); + case GenericInput.InputType.Gamepad: + return this.WasGamepadButtonDown(control, index); + case GenericInput.InputType.Mouse: + return this.WasMouseButtonDown(control); + default: + return false; + } + } + + /// + /// Returns if a given control of any kind was up in the last update call. + /// This is a helper function that can be passed a , or . + /// + /// The control whose up state to query + /// The index of the gamepad to query (if applicable), or -1 for any gamepad + /// Whether the given control was up. + /// If the passed control isn't of a supported type + public bool WasUp(GenericInput control, int index = -1) { + switch (control.Type) { + case GenericInput.InputType.Keyboard: + return this.WasKeyUp(control); + case GenericInput.InputType.Gamepad: + return this.WasGamepadButtonUp(control, index); + case GenericInput.InputType.Mouse: + return this.WasMouseButtonUp(control); + default: + return true; + } + } + /// /// Returns if a given control of any kind is pressed. /// This is a helper function that can be passed a , or . diff --git a/MLEM/Input/Keybind.cs b/MLEM/Input/Keybind.cs index ee501f2..4b5f91e 100644 --- a/MLEM/Input/Keybind.cs +++ b/MLEM/Input/Keybind.cs @@ -123,6 +123,21 @@ namespace MLEM.Input { return false; } + /// + /// Returns whether this keybind was considered to be down in the last update 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 + /// Whether this keybind was considered to be down + public bool WasDown(InputHandler handler, int gamepadIndex = -1) { + foreach (var combination in this.combinations) { + if (combination.WasDown(handler, gamepadIndex)) + return true; + } + return false; + } + /// /// Returns whether this keybind is considered to be pressed. /// See for more information. @@ -183,6 +198,21 @@ namespace MLEM.Input { return false; } + /// + /// Returns whether any of this keybind's modifier keys were down in the last update 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 + /// Whether any of this keyboard's modifier keys were down + public bool WasModifierDown(InputHandler handler, int gamepadIndex = -1) { + foreach (var combination in this.combinations) { + if (combination.WasModifierDown(handler, gamepadIndex)) + return true; + } + return false; + } + /// /// Returns the amount of time that this keybind has been held down for. /// If this input isn't currently down, this method returns . @@ -320,7 +350,7 @@ namespace MLEM.Input { } /// - /// Returns whether this combination is currently down + /// Returns whether this combination is currently down. /// See for more information. /// /// The input handler to query the keys with @@ -330,6 +360,17 @@ namespace MLEM.Input { return this.IsModifierDown(handler, gamepadIndex) && handler.IsDown(this.Key, gamepadIndex); } + /// + /// Returns whether this combination was down in the last upate 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 + /// Whether this combination was down + public bool WasDown(InputHandler handler, int gamepadIndex = -1) { + return this.WasModifierDown(handler, gamepadIndex) && handler.WasDown(this.Key, gamepadIndex); + } + /// /// Returns whether this combination is currently pressed. /// See for more information. @@ -380,6 +421,22 @@ namespace MLEM.Input { return false; } + /// + /// Returns whether this combination's modifier keys were down in the last update call. + /// + /// The input handler to query the keys with + /// The index of the gamepad to query, or -1 to query all gamepads + /// Whether this combination's modifiers were down + public bool WasModifierDown(InputHandler handler, int gamepadIndex = -1) { + if (this.Modifiers.Length <= 0) + return true; + foreach (var modifier in this.Modifiers) { + if (handler.WasDown(modifier, gamepadIndex)) + return true; + } + return false; + } + /// /// Returns the amount of time that this combination has been held down for. /// If this input isn't currently down, this method returns .