From 7b8be4cee8d0420b121757b6f24980b07c6b50fe Mon Sep 17 00:00:00 2001 From: Lilith Silver Date: Sun, 3 Sep 2023 23:57:26 -0700 Subject: [PATCH] make WasKey/ButtonJustDown/Up obsolete and replace with clarified versions --- .../KeyboardStateExtended.cs | 33 ++++++++++- .../MouseStateExtended.cs | 58 +++++++++++++++++-- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/src/cs/MonoGame.Extended.Input/KeyboardStateExtended.cs b/src/cs/MonoGame.Extended.Input/KeyboardStateExtended.cs index 01464893..9c136851 100644 --- a/src/cs/MonoGame.Extended.Input/KeyboardStateExtended.cs +++ b/src/cs/MonoGame.Extended.Input/KeyboardStateExtended.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using Microsoft.Xna.Framework.Input; namespace MonoGame.Extended.Input @@ -23,8 +24,36 @@ namespace MonoGame.Extended.Input public bool IsKeyUp(Keys key) => _currentKeyboardState.IsKeyUp(key); public Keys[] GetPressedKeys() => _currentKeyboardState.GetPressedKeys(); + /// + /// Gets whether the given key was down on the previous state, but is now up. + /// + /// The key to check. + /// true if the key was released this state-change, otherwise false. + [Obsolete($"Deprecated in favor of {nameof(IsKeyReleased)}")] public bool WasKeyJustDown(Keys key) => _previousKeyboardState.IsKeyDown(key) && _currentKeyboardState.IsKeyUp(key); + + /// + /// Gets whether the given key was up on the previous state, but is now down. + /// + /// The key to check. + /// true if the key was pressed this state-change, otherwise false. + [Obsolete($"Deprecated in favor of {nameof(IsKeyPressed)}")] public bool WasKeyJustUp(Keys key) => _previousKeyboardState.IsKeyUp(key) && _currentKeyboardState.IsKeyDown(key); + + /// + /// Gets whether the given key was down on the previous state, but is now up. + /// + /// The key to check. + /// true if the key was released this state-change, otherwise false. + public readonly bool IsKeyReleased(Keys key) => _previousKeyboardState.IsKeyDown(key) && _currentKeyboardState.IsKeyUp(key); + + /// + /// Gets whether the given key was up on the previous state, but is now down. + /// + /// The key to check. + /// true if the key was pressed this state-change, otherwise false. + public readonly bool IsKeyPressed(Keys key) => _previousKeyboardState.IsKeyUp(key) && _currentKeyboardState.IsKeyDown(key); + public bool WasAnyKeyJustDown() => _previousKeyboardState.GetPressedKeys().Any(); } -} \ No newline at end of file +} diff --git a/src/cs/MonoGame.Extended.Input/MouseStateExtended.cs b/src/cs/MonoGame.Extended.Input/MouseStateExtended.cs index d9379d83..0ec6943f 100644 --- a/src/cs/MonoGame.Extended.Input/MouseStateExtended.cs +++ b/src/cs/MonoGame.Extended.Input/MouseStateExtended.cs @@ -63,6 +63,13 @@ namespace MonoGame.Extended.Input return false; } + /// + /// Get the just-down state for the mouse on this state-change: true if the mouse button has just been pressed. + /// + /// + /// Deprecated because of inconsistency with + /// The just-down state for the mouse on this state-change. + [Obsolete($"Deprecated in favor of {nameof(IsButtonPressed)}")] public bool WasButtonJustDown(MouseButton button) { // ReSharper disable once SwitchStatementMissingSomeCases @@ -78,6 +85,13 @@ namespace MonoGame.Extended.Input return false; } + /// + /// Get the just-up state for the mouse on this state-change: true if the mouse button has just been released. + /// + /// + /// Deprecated because of inconsistency with + /// The just-up state for the mouse on this state-change. + [Obsolete($"Deprecated in favor of {nameof(IsButtonReleased)}")] public bool WasButtonJustUp(MouseButton button) { // ReSharper disable once SwitchStatementMissingSomeCases @@ -93,9 +107,43 @@ namespace MonoGame.Extended.Input return false; } - private bool IsPressed(Func button) => button(_currentMouseState) == ButtonState.Pressed; - private bool IsReleased(Func button) => button(_currentMouseState) == ButtonState.Released; - private bool WasJustPressed(Func button) => button(_previousMouseState) == ButtonState.Released && button(_currentMouseState) == ButtonState.Pressed; - private bool WasJustReleased(Func button) => button(_previousMouseState) == ButtonState.Pressed && button(_currentMouseState) == ButtonState.Released; + /// + /// Get the pressed state of a mouse button, for this state-change. + /// + /// The button to check. + /// true if the given mouse button was pressed this state-change, otherwise false + public readonly bool IsButtonPressed(MouseButton button) => button switch + { + MouseButton.Left => WasJustPressed(m => m.LeftButton), + MouseButton.Middle => WasJustPressed(m => m.MiddleButton), + MouseButton.Right => WasJustPressed(m => m.RightButton), + MouseButton.XButton1 => WasJustPressed(m => m.XButton1), + MouseButton.XButton2 => WasJustPressed(m => m.XButton2), + _ => false, + }; + + /// + /// Get the released state of a mouse button, for this state-change. + /// + /// The button to check. + /// true if the given mouse button was released this state-change, otherwise false + public readonly bool IsButtonReleased(MouseButton button) => button switch + { + MouseButton.Left => WasJustReleased(m => m.LeftButton), + MouseButton.Middle => WasJustReleased(m => m.MiddleButton), + MouseButton.Right => WasJustReleased(m => m.RightButton), + MouseButton.XButton1 => WasJustReleased(m => m.XButton1), + MouseButton.XButton2 => WasJustReleased(m => m.XButton2), + _ => false, + }; + + private readonly bool IsPressed(Func button) + => button(_currentMouseState) == ButtonState.Pressed; + private readonly bool IsReleased(Func button) + => button(_currentMouseState) == ButtonState.Released; + private readonly bool WasJustPressed(Func button) + => button(_previousMouseState) == ButtonState.Released && button(_currentMouseState) == ButtonState.Pressed; + private readonly bool WasJustReleased(Func button) + => button(_previousMouseState) == ButtonState.Pressed && button(_currentMouseState) == ButtonState.Released; } -} \ No newline at end of file +}