From 136773678e1ce623bd23f170dca265d31030d200 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 9 Feb 2020 01:04:55 -0500 Subject: add helper.Input.GetStatus method --- src/SMAPI/Framework/Input/SInputState.cs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/SMAPI/Framework/Input/SInputState.cs') diff --git a/src/SMAPI/Framework/Input/SInputState.cs b/src/SMAPI/Framework/Input/SInputState.cs index 84cea36c..f54d3124 100644 --- a/src/SMAPI/Framework/Input/SInputState.cs +++ b/src/SMAPI/Framework/Input/SInputState.cs @@ -169,6 +169,13 @@ namespace StardewModdingAPI.Framework.Input return buttons.Any(button => this.IsDown(button.ToSButton())); } + /// Get the status of a button. + /// The button to check. + public InputStatus GetStatus(SButton button) + { + return this.GetStatus(this.ActiveButtons, button); + } + /********* ** Private methods -- cgit From ab90e2c890bf16674e0f5699729f114877a6f7a4 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 19 Feb 2020 23:28:37 -0500 Subject: rename InputStatus to SButtonState for consistency --- src/SMAPI/Framework/Input/SInputState.cs | 52 +++++++++++++-------------- src/SMAPI/Framework/ModHelpers/InputHelper.cs | 6 ++-- src/SMAPI/Framework/SGame.cs | 6 ++-- src/SMAPI/IInputHelper.cs | 4 +-- src/SMAPI/InputStatus.cs | 29 --------------- src/SMAPI/SButtonState.cs | 29 +++++++++++++++ 6 files changed, 63 insertions(+), 63 deletions(-) delete mode 100644 src/SMAPI/InputStatus.cs create mode 100644 src/SMAPI/SButtonState.cs (limited to 'src/SMAPI/Framework/Input/SInputState.cs') diff --git a/src/SMAPI/Framework/Input/SInputState.cs b/src/SMAPI/Framework/Input/SInputState.cs index f54d3124..4eaa9ca6 100644 --- a/src/SMAPI/Framework/Input/SInputState.cs +++ b/src/SMAPI/Framework/Input/SInputState.cs @@ -49,7 +49,7 @@ namespace StardewModdingAPI.Framework.Input public ICursorPosition CursorPosition => this.CursorPositionImpl; /// The buttons which were pressed, held, or released. - public IDictionary ActiveButtons { get; private set; } = new Dictionary(); + public IDictionary ActiveButtons { get; private set; } = new Dictionary(); /// The buttons to suppress when the game next handles input. Each button is suppressed until it's released. public HashSet SuppressButtons { get; } = new HashSet(); @@ -75,7 +75,7 @@ namespace StardewModdingAPI.Framework.Input [Obsolete("This method should only be called by the game itself.")] public override void Update() { } - /// Update the current button statuses for the given tick. + /// Update the current button states for the given tick. public void TrueUpdate() { try @@ -86,7 +86,7 @@ namespace StardewModdingAPI.Framework.Input GamePadState realController = GamePad.GetState(PlayerIndex.One); KeyboardState realKeyboard = Keyboard.GetState(); MouseState realMouse = Mouse.GetState(); - var activeButtons = this.DeriveStatuses(this.ActiveButtons, realKeyboard, realMouse, realController); + var activeButtons = this.DeriveStates(this.ActiveButtons, realKeyboard, realMouse, realController); Vector2 cursorAbsolutePos = new Vector2((realMouse.X * zoomMultiplier) + Game1.viewport.X, (realMouse.Y * zoomMultiplier) + Game1.viewport.Y); Vector2? playerTilePos = Context.IsPlayerFree ? Game1.player.getTileLocation() : (Vector2?)null; @@ -102,7 +102,7 @@ namespace StardewModdingAPI.Framework.Input } // update suppressed states - this.SuppressButtons.RemoveWhere(p => !this.GetStatus(activeButtons, p).IsDown()); + this.SuppressButtons.RemoveWhere(p => !this.GetState(activeButtons, p).IsDown()); this.UpdateSuppression(); } catch (InvalidOperationException) @@ -159,7 +159,7 @@ namespace StardewModdingAPI.Framework.Input /// The button to check. public bool IsDown(SButton button) { - return this.GetStatus(this.ActiveButtons, button).IsDown(); + return this.GetState(this.ActiveButtons, button).IsDown(); } /// Get whether any of the given buttons were pressed or held. @@ -169,11 +169,11 @@ namespace StardewModdingAPI.Framework.Input return buttons.Any(button => this.IsDown(button.ToSButton())); } - /// Get the status of a button. + /// Get the state of a button. /// The button to check. - public InputStatus GetStatus(SButton button) + public SButtonState GetState(SButton button) { - return this.GetStatus(this.ActiveButtons, button); + return this.GetState(this.ActiveButtons, button); } @@ -205,7 +205,7 @@ namespace StardewModdingAPI.Framework.Input /// The game's keyboard state for the current tick. /// The game's mouse state for the current tick. /// The game's controller state for the current tick. - private void SuppressGivenStates(IDictionary activeButtons, ref KeyboardState keyboardState, ref MouseState mouseState, ref GamePadState gamePadState) + private void SuppressGivenStates(IDictionary activeButtons, ref KeyboardState keyboardState, ref MouseState mouseState, ref GamePadState gamePadState) { if (this.SuppressButtons.Count == 0) return; @@ -252,48 +252,48 @@ namespace StardewModdingAPI.Framework.Input } } - /// Get the status of all pressed or released buttons relative to their previous status. - /// The previous button statuses. + /// Get the state of all pressed or released buttons relative to their previous state. + /// The previous button states. /// The keyboard state. /// The mouse state. /// The controller state. - private IDictionary DeriveStatuses(IDictionary previousStatuses, KeyboardState keyboard, MouseState mouse, GamePadState controller) + private IDictionary DeriveStates(IDictionary previousStates, KeyboardState keyboard, MouseState mouse, GamePadState controller) { - IDictionary activeButtons = new Dictionary(); + IDictionary activeButtons = new Dictionary(); // handle pressed keys SButton[] down = this.GetPressedButtons(keyboard, mouse, controller).ToArray(); foreach (SButton button in down) - activeButtons[button] = this.DeriveStatus(this.GetStatus(previousStatuses, button), isDown: true); + activeButtons[button] = this.DeriveState(this.GetState(previousStates, button), isDown: true); // handle released keys - foreach (KeyValuePair prev in previousStatuses) + foreach (KeyValuePair prev in previousStates) { if (prev.Value.IsDown() && !activeButtons.ContainsKey(prev.Key)) - activeButtons[prev.Key] = InputStatus.Released; + activeButtons[prev.Key] = SButtonState.Released; } return activeButtons; } - /// Get the status of a button relative to its previous status. - /// The previous button status. + /// Get the state of a button relative to its previous state. + /// The previous button state. /// Whether the button is currently down. - private InputStatus DeriveStatus(InputStatus oldStatus, bool isDown) + private SButtonState DeriveState(SButtonState oldState, bool isDown) { - if (isDown && oldStatus.IsDown()) - return InputStatus.Held; + if (isDown && oldState.IsDown()) + return SButtonState.Held; if (isDown) - return InputStatus.Pressed; - return InputStatus.Released; + return SButtonState.Pressed; + return SButtonState.Released; } - /// Get the status of a button. + /// Get the state of a button. /// The current button states to check. /// The button to check. - private InputStatus GetStatus(IDictionary activeButtons, SButton button) + private SButtonState GetState(IDictionary activeButtons, SButton button) { - return activeButtons.TryGetValue(button, out InputStatus status) ? status : InputStatus.None; + return activeButtons.TryGetValue(button, out SButtonState state) ? state : SButtonState.None; } /// Get the buttons pressed in the given stats. diff --git a/src/SMAPI/Framework/ModHelpers/InputHelper.cs b/src/SMAPI/Framework/ModHelpers/InputHelper.cs index 5858cddd..f8ff0355 100644 --- a/src/SMAPI/Framework/ModHelpers/InputHelper.cs +++ b/src/SMAPI/Framework/ModHelpers/InputHelper.cs @@ -51,11 +51,11 @@ namespace StardewModdingAPI.Framework.ModHelpers this.InputState.SuppressButtons.Add(button); } - /// Get the status of a button. + /// Get the state of a button. /// The button to check. - public InputStatus GetStatus(SButton button) + public SButtonState GetState(SButton button) { - return this.InputState.GetStatus(button); + return this.InputState.GetState(button); } } } diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 4b346059..6a472e3c 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -635,16 +635,16 @@ namespace StardewModdingAPI.Framework foreach (var pair in inputState.ActiveButtons) { SButton button = pair.Key; - InputStatus status = pair.Value; + SButtonState status = pair.Value; - if (status == InputStatus.Pressed) + if (status == SButtonState.Pressed) { if (this.Monitor.IsVerbose) this.Monitor.Log($"Events: button {button} pressed.", LogLevel.Trace); events.ButtonPressed.Raise(new ButtonPressedEventArgs(button, cursor, inputState)); } - else if (status == InputStatus.Released) + else if (status == SButtonState.Released) { if (this.Monitor.IsVerbose) this.Monitor.Log($"Events: button {button} released.", LogLevel.Trace); diff --git a/src/SMAPI/IInputHelper.cs b/src/SMAPI/IInputHelper.cs index ba26773d..e9768c24 100644 --- a/src/SMAPI/IInputHelper.cs +++ b/src/SMAPI/IInputHelper.cs @@ -18,8 +18,8 @@ namespace StardewModdingAPI /// The button to suppress. void Suppress(SButton button); - /// Get the status of a button. + /// Get the state of a button. /// The button to check. - InputStatus GetStatus(SButton button); + SButtonState GetState(SButton button); } } diff --git a/src/SMAPI/InputStatus.cs b/src/SMAPI/InputStatus.cs deleted file mode 100644 index d9cdd6b2..00000000 --- a/src/SMAPI/InputStatus.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace StardewModdingAPI -{ - /// The input status for a button during an update frame. - public enum InputStatus - { - /// The button was neither pressed, held, nor released. - None, - - /// The button was pressed in this frame. - Pressed, - - /// The button has been held since the last frame. - Held, - - /// The button was released in this frame. - Released - } - - /// Extension methods for . - internal static class InputStatusExtensions - { - /// Whether the button was pressed or held. - /// The button status. - public static bool IsDown(this InputStatus status) - { - return status == InputStatus.Held || status == InputStatus.Pressed; - } - } -} diff --git a/src/SMAPI/SButtonState.cs b/src/SMAPI/SButtonState.cs new file mode 100644 index 00000000..2b78da27 --- /dev/null +++ b/src/SMAPI/SButtonState.cs @@ -0,0 +1,29 @@ +namespace StardewModdingAPI +{ + /// The input state for a button during an update frame. + public enum SButtonState + { + /// The button was neither pressed, held, nor released. + None, + + /// The button was pressed in this frame. + Pressed, + + /// The button has been held since the last frame. + Held, + + /// The button was released in this frame. + Released + } + + /// Extension methods for . + internal static class InputStatusExtensions + { + /// Whether the button was pressed or held. + /// The button state. + public static bool IsDown(this SButtonState state) + { + return state == SButtonState.Held || state == SButtonState.Pressed; + } + } +} -- cgit