From 79a2eaa8d28da154c30149c60be07d24a5322f93 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 26 Dec 2022 18:44:46 +0100 Subject: [PATCH] Added InputHandler.WasPressedForLess and related methods --- CHANGELOG.md | 1 + MLEM/Input/InputHandler.cs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28a6756..0131c57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Additions - Added UniformTextureAtlas methods ToList and ToDictionary - Added SingleRandom and SeedSource - Added TokenizedString.GetArea +- Added InputHandler.WasPressedForLess and related methods - **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 6aff097..0280403 100644 --- a/MLEM/Input/InputHandler.cs +++ b/MLEM/Input/InputHandler.cs @@ -859,6 +859,43 @@ namespace MLEM.Input { } } + /// + /// Returns if a given control of any kind was just let go this frame, and had been down for less than the given before that. Essentially, this action signifies a short press action. + /// + /// The control whose pressed state to query. + /// The maximum time that the control should have been down for. + /// The index of the gamepad to query (if applicable), or -1 for any gamepad. + /// Whether the given control was pressed for less than the given time. + public bool WasPressedForLess(GenericInput control, TimeSpan time, int index = -1) { + return this.WasDown(control, index) && this.IsUp(control, index) && this.GetDownTime(control, index) < time; + } + + /// + /// Returns if a given control of any kind was just let go this frame, and had been down for less than the given before that, and if the press has not been consumed yet using . Essentially, this action signifies a short press action. + /// + /// The control whose pressed state to query. + /// The maximum time that the control should have been down for. + /// The index of the gamepad to query (if applicable), or -1 for any gamepad. + /// Whether the given control was pressed for less than the given time, and the press has not been consumed yet. + public bool WasPressedForLessAvailable(GenericInput control, TimeSpan time, int index = -1) { + return this.WasPressedForLess(control, time, index) && !this.IsPressConsumed(control, index); + } + + /// + /// Returns if a given control of any kind was just let go this frame, and had been down for less than the given before that, and if the press has not been consumed yet using , and marks the press as consumed if it is. Essentially, this action signifies a short press action. + /// + /// The control whose pressed state to query. + /// The maximum time that the control should have been down for. + /// The index of the gamepad to query (if applicable), or -1 for any gamepad. + /// Whether the given control was pressed for less than the given time, and the press was successfully consumed. + public bool TryConsumePressedForLess(GenericInput control, TimeSpan time, int index = -1) { + if (this.WasPressedForLessAvailable(control, time, index)) { + this.consumedPresses.Add((control, index)); + return true; + } + return false; + } + /// public bool IsAnyDown(params GenericInput[] controls) { foreach (var control in controls) {