rename InputStatus to SButtonState for consistency

This commit is contained in:
Jesse Plamondon-Willard 2020-02-19 23:28:37 -05:00
parent 2e9807a034
commit ab90e2c890
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
5 changed files with 40 additions and 40 deletions

View File

@ -49,7 +49,7 @@ namespace StardewModdingAPI.Framework.Input
public ICursorPosition CursorPosition => this.CursorPositionImpl; public ICursorPosition CursorPosition => this.CursorPositionImpl;
/// <summary>The buttons which were pressed, held, or released.</summary> /// <summary>The buttons which were pressed, held, or released.</summary>
public IDictionary<SButton, InputStatus> ActiveButtons { get; private set; } = new Dictionary<SButton, InputStatus>(); public IDictionary<SButton, SButtonState> ActiveButtons { get; private set; } = new Dictionary<SButton, SButtonState>();
/// <summary>The buttons to suppress when the game next handles input. Each button is suppressed until it's released.</summary> /// <summary>The buttons to suppress when the game next handles input. Each button is suppressed until it's released.</summary>
public HashSet<SButton> SuppressButtons { get; } = new HashSet<SButton>(); public HashSet<SButton> SuppressButtons { get; } = new HashSet<SButton>();
@ -75,7 +75,7 @@ namespace StardewModdingAPI.Framework.Input
[Obsolete("This method should only be called by the game itself.")] [Obsolete("This method should only be called by the game itself.")]
public override void Update() { } public override void Update() { }
/// <summary>Update the current button statuses for the given tick.</summary> /// <summary>Update the current button states for the given tick.</summary>
public void TrueUpdate() public void TrueUpdate()
{ {
try try
@ -86,7 +86,7 @@ namespace StardewModdingAPI.Framework.Input
GamePadState realController = GamePad.GetState(PlayerIndex.One); GamePadState realController = GamePad.GetState(PlayerIndex.One);
KeyboardState realKeyboard = Keyboard.GetState(); KeyboardState realKeyboard = Keyboard.GetState();
MouseState realMouse = Mouse.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 cursorAbsolutePos = new Vector2((realMouse.X * zoomMultiplier) + Game1.viewport.X, (realMouse.Y * zoomMultiplier) + Game1.viewport.Y);
Vector2? playerTilePos = Context.IsPlayerFree ? Game1.player.getTileLocation() : (Vector2?)null; Vector2? playerTilePos = Context.IsPlayerFree ? Game1.player.getTileLocation() : (Vector2?)null;
@ -102,7 +102,7 @@ namespace StardewModdingAPI.Framework.Input
} }
// update suppressed states // update suppressed states
this.SuppressButtons.RemoveWhere(p => !this.GetStatus(activeButtons, p).IsDown()); this.SuppressButtons.RemoveWhere(p => !this.GetState(activeButtons, p).IsDown());
this.UpdateSuppression(); this.UpdateSuppression();
} }
catch (InvalidOperationException) catch (InvalidOperationException)
@ -159,7 +159,7 @@ namespace StardewModdingAPI.Framework.Input
/// <param name="button">The button to check.</param> /// <param name="button">The button to check.</param>
public bool IsDown(SButton button) public bool IsDown(SButton button)
{ {
return this.GetStatus(this.ActiveButtons, button).IsDown(); return this.GetState(this.ActiveButtons, button).IsDown();
} }
/// <summary>Get whether any of the given buttons were pressed or held.</summary> /// <summary>Get whether any of the given buttons were pressed or held.</summary>
@ -169,11 +169,11 @@ namespace StardewModdingAPI.Framework.Input
return buttons.Any(button => this.IsDown(button.ToSButton())); return buttons.Any(button => this.IsDown(button.ToSButton()));
} }
/// <summary>Get the status of a button.</summary> /// <summary>Get the state of a button.</summary>
/// <param name="button">The button to check.</param> /// <param name="button">The button to check.</param>
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
/// <param name="keyboardState">The game's keyboard state for the current tick.</param> /// <param name="keyboardState">The game's keyboard state for the current tick.</param>
/// <param name="mouseState">The game's mouse state for the current tick.</param> /// <param name="mouseState">The game's mouse state for the current tick.</param>
/// <param name="gamePadState">The game's controller state for the current tick.</param> /// <param name="gamePadState">The game's controller state for the current tick.</param>
private void SuppressGivenStates(IDictionary<SButton, InputStatus> activeButtons, ref KeyboardState keyboardState, ref MouseState mouseState, ref GamePadState gamePadState) private void SuppressGivenStates(IDictionary<SButton, SButtonState> activeButtons, ref KeyboardState keyboardState, ref MouseState mouseState, ref GamePadState gamePadState)
{ {
if (this.SuppressButtons.Count == 0) if (this.SuppressButtons.Count == 0)
return; return;
@ -252,48 +252,48 @@ namespace StardewModdingAPI.Framework.Input
} }
} }
/// <summary>Get the status of all pressed or released buttons relative to their previous status.</summary> /// <summary>Get the state of all pressed or released buttons relative to their previous state.</summary>
/// <param name="previousStatuses">The previous button statuses.</param> /// <param name="previousStates">The previous button states.</param>
/// <param name="keyboard">The keyboard state.</param> /// <param name="keyboard">The keyboard state.</param>
/// <param name="mouse">The mouse state.</param> /// <param name="mouse">The mouse state.</param>
/// <param name="controller">The controller state.</param> /// <param name="controller">The controller state.</param>
private IDictionary<SButton, InputStatus> DeriveStatuses(IDictionary<SButton, InputStatus> previousStatuses, KeyboardState keyboard, MouseState mouse, GamePadState controller) private IDictionary<SButton, SButtonState> DeriveStates(IDictionary<SButton, SButtonState> previousStates, KeyboardState keyboard, MouseState mouse, GamePadState controller)
{ {
IDictionary<SButton, InputStatus> activeButtons = new Dictionary<SButton, InputStatus>(); IDictionary<SButton, SButtonState> activeButtons = new Dictionary<SButton, SButtonState>();
// handle pressed keys // handle pressed keys
SButton[] down = this.GetPressedButtons(keyboard, mouse, controller).ToArray(); SButton[] down = this.GetPressedButtons(keyboard, mouse, controller).ToArray();
foreach (SButton button in down) 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 // handle released keys
foreach (KeyValuePair<SButton, InputStatus> prev in previousStatuses) foreach (KeyValuePair<SButton, SButtonState> prev in previousStates)
{ {
if (prev.Value.IsDown() && !activeButtons.ContainsKey(prev.Key)) if (prev.Value.IsDown() && !activeButtons.ContainsKey(prev.Key))
activeButtons[prev.Key] = InputStatus.Released; activeButtons[prev.Key] = SButtonState.Released;
} }
return activeButtons; return activeButtons;
} }
/// <summary>Get the status of a button relative to its previous status.</summary> /// <summary>Get the state of a button relative to its previous state.</summary>
/// <param name="oldStatus">The previous button status.</param> /// <param name="oldState">The previous button state.</param>
/// <param name="isDown">Whether the button is currently down.</param> /// <param name="isDown">Whether the button is currently down.</param>
private InputStatus DeriveStatus(InputStatus oldStatus, bool isDown) private SButtonState DeriveState(SButtonState oldState, bool isDown)
{ {
if (isDown && oldStatus.IsDown()) if (isDown && oldState.IsDown())
return InputStatus.Held; return SButtonState.Held;
if (isDown) if (isDown)
return InputStatus.Pressed; return SButtonState.Pressed;
return InputStatus.Released; return SButtonState.Released;
} }
/// <summary>Get the status of a button.</summary> /// <summary>Get the state of a button.</summary>
/// <param name="activeButtons">The current button states to check.</param> /// <param name="activeButtons">The current button states to check.</param>
/// <param name="button">The button to check.</param> /// <param name="button">The button to check.</param>
private InputStatus GetStatus(IDictionary<SButton, InputStatus> activeButtons, SButton button) private SButtonState GetState(IDictionary<SButton, SButtonState> activeButtons, SButton button)
{ {
return activeButtons.TryGetValue(button, out InputStatus status) ? status : InputStatus.None; return activeButtons.TryGetValue(button, out SButtonState state) ? state : SButtonState.None;
} }
/// <summary>Get the buttons pressed in the given stats.</summary> /// <summary>Get the buttons pressed in the given stats.</summary>

View File

@ -51,11 +51,11 @@ namespace StardewModdingAPI.Framework.ModHelpers
this.InputState.SuppressButtons.Add(button); this.InputState.SuppressButtons.Add(button);
} }
/// <summary>Get the status of a button.</summary> /// <summary>Get the state of a button.</summary>
/// <param name="button">The button to check.</param> /// <param name="button">The button to check.</param>
public InputStatus GetStatus(SButton button) public SButtonState GetState(SButton button)
{ {
return this.InputState.GetStatus(button); return this.InputState.GetState(button);
} }
} }
} }

View File

@ -635,16 +635,16 @@ namespace StardewModdingAPI.Framework
foreach (var pair in inputState.ActiveButtons) foreach (var pair in inputState.ActiveButtons)
{ {
SButton button = pair.Key; SButton button = pair.Key;
InputStatus status = pair.Value; SButtonState status = pair.Value;
if (status == InputStatus.Pressed) if (status == SButtonState.Pressed)
{ {
if (this.Monitor.IsVerbose) if (this.Monitor.IsVerbose)
this.Monitor.Log($"Events: button {button} pressed.", LogLevel.Trace); this.Monitor.Log($"Events: button {button} pressed.", LogLevel.Trace);
events.ButtonPressed.Raise(new ButtonPressedEventArgs(button, cursor, inputState)); events.ButtonPressed.Raise(new ButtonPressedEventArgs(button, cursor, inputState));
} }
else if (status == InputStatus.Released) else if (status == SButtonState.Released)
{ {
if (this.Monitor.IsVerbose) if (this.Monitor.IsVerbose)
this.Monitor.Log($"Events: button {button} released.", LogLevel.Trace); this.Monitor.Log($"Events: button {button} released.", LogLevel.Trace);

View File

@ -18,8 +18,8 @@ namespace StardewModdingAPI
/// <param name="button">The button to suppress.</param> /// <param name="button">The button to suppress.</param>
void Suppress(SButton button); void Suppress(SButton button);
/// <summary>Get the status of a button.</summary> /// <summary>Get the state of a button.</summary>
/// <param name="button">The button to check.</param> /// <param name="button">The button to check.</param>
InputStatus GetStatus(SButton button); SButtonState GetState(SButton button);
} }
} }

View File

@ -1,7 +1,7 @@
namespace StardewModdingAPI namespace StardewModdingAPI
{ {
/// <summary>The input status for a button during an update frame.</summary> /// <summary>The input state for a button during an update frame.</summary>
public enum InputStatus public enum SButtonState
{ {
/// <summary>The button was neither pressed, held, nor released.</summary> /// <summary>The button was neither pressed, held, nor released.</summary>
None, None,
@ -16,14 +16,14 @@ namespace StardewModdingAPI
Released Released
} }
/// <summary>Extension methods for <see cref="InputStatus"/>.</summary> /// <summary>Extension methods for <see cref="SButtonState"/>.</summary>
internal static class InputStatusExtensions internal static class InputStatusExtensions
{ {
/// <summary>Whether the button was pressed or held.</summary> /// <summary>Whether the button was pressed or held.</summary>
/// <param name="status">The button status.</param> /// <param name="state">The button state.</param>
public static bool IsDown(this InputStatus status) public static bool IsDown(this SButtonState state)
{ {
return status == InputStatus.Held || status == InputStatus.Pressed; return state == SButtonState.Held || state == SButtonState.Pressed;
} }
} }
} }