Merge pull request #815 from LilithSilver/rename-wasbutton

Improve naming of WasKeyJustDown/Up and WasButtonJustDown/Up
This commit is contained in:
Max Kopjev
2024-04-28 20:58:39 +03:00
committed by GitHub
2 changed files with 84 additions and 6 deletions
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using Microsoft.Xna.Framework.Input;
namespace MonoGame.Extended.Input
@@ -23,8 +24,37 @@ namespace MonoGame.Extended.Input
public bool IsKeyUp(Keys key) => _currentKeyboardState.IsKeyUp(key);
public Keys[] GetPressedKeys() => _currentKeyboardState.GetPressedKeys();
public void GetPressedKeys(Keys[] keys) => _currentKeyboardState.GetPressedKeys(keys);
/// <summary>
/// Gets whether the given key was down on the previous state, but is now up.
/// </summary>
/// <param name="key">The key to check.</param>
/// <returns>true if the key was released this state-change, otherwise false.</returns>
[Obsolete($"Deprecated in favor of {nameof(IsKeyReleased)}")]
public bool WasKeyJustDown(Keys key) => _previousKeyboardState.IsKeyDown(key) && _currentKeyboardState.IsKeyUp(key);
/// <summary>
/// Gets whether the given key was up on the previous state, but is now down.
/// </summary>
/// <param name="key">The key to check.</param>
/// <returns>true if the key was pressed this state-change, otherwise false.</returns>
[Obsolete($"Deprecated in favor of {nameof(IsKeyPressed)}")]
public bool WasKeyJustUp(Keys key) => _previousKeyboardState.IsKeyUp(key) && _currentKeyboardState.IsKeyDown(key);
/// <summary>
/// Gets whether the given key was down on the previous state, but is now up.
/// </summary>
/// <param name="key">The key to check.</param>
/// <returns>true if the key was released this state-change, otherwise false.</returns>
public readonly bool IsKeyReleased(Keys key) => _previousKeyboardState.IsKeyDown(key) && _currentKeyboardState.IsKeyUp(key);
/// <summary>
/// Gets whether the given key was up on the previous state, but is now down.
/// </summary>
/// <param name="key">The key to check.</param>
/// <returns>true if the key was pressed this state-change, otherwise false.</returns>
public readonly bool IsKeyPressed(Keys key) => _previousKeyboardState.IsKeyUp(key) && _currentKeyboardState.IsKeyDown(key);
public bool WasAnyKeyJustDown() => _previousKeyboardState.GetPressedKeyCount() > 0;
}
}
@@ -63,6 +63,13 @@ namespace MonoGame.Extended.Input
return false;
}
/// <summary>
/// Get the just-down state for the mouse on this state-change: true if the mouse button has just been pressed.
/// </summary>
/// <param name="button"></param>
/// <remarks>Deprecated because of inconsistency with <see cref="KeyboardStateExtended"/></remarks>
/// <returns>The just-down state for the mouse on this state-change.</returns>
[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;
}
/// <summary>
/// Get the just-up state for the mouse on this state-change: true if the mouse button has just been released.
/// </summary>
/// <param name="button"></param>
/// <remarks>Deprecated because of inconsistency with <see cref="KeyboardStateExtended"/></remarks>
/// <returns>The just-up state for the mouse on this state-change.</returns>
[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<MouseState, ButtonState> button) => button(_currentMouseState) == ButtonState.Pressed;
private bool IsReleased(Func<MouseState, ButtonState> button) => button(_currentMouseState) == ButtonState.Released;
private bool WasJustPressed(Func<MouseState, ButtonState> button) => button(_previousMouseState) == ButtonState.Released && button(_currentMouseState) == ButtonState.Pressed;
private bool WasJustReleased(Func<MouseState, ButtonState> button) => button(_previousMouseState) == ButtonState.Pressed && button(_currentMouseState) == ButtonState.Released;
/// <summary>
/// Get the pressed state of a mouse button, for this state-change.
/// </summary>
/// <param name="button">The button to check.</param>
/// <returns>true if the given mouse button was pressed this state-change, otherwise false</returns>
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,
};
/// <summary>
/// Get the released state of a mouse button, for this state-change.
/// </summary>
/// <param name="button">The button to check.</param>
/// <returns>true if the given mouse button was released this state-change, otherwise false</returns>
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<MouseState, ButtonState> button)
=> button(_currentMouseState) == ButtonState.Pressed;
private readonly bool IsReleased(Func<MouseState, ButtonState> button)
=> button(_currentMouseState) == ButtonState.Released;
private readonly bool WasJustPressed(Func<MouseState, ButtonState> button)
=> button(_previousMouseState) == ButtonState.Released && button(_currentMouseState) == ButtonState.Pressed;
private readonly bool WasJustReleased(Func<MouseState, ButtonState> button)
=> button(_previousMouseState) == ButtonState.Pressed && button(_currentMouseState) == ButtonState.Released;
}
}
}