summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Input/SInputState.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-03-22 19:52:42 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-03-22 19:52:42 -0400
commit7ca5efbbc576f3c6c43493654b2a0ac040fd4f31 (patch)
treefae7a4e06a14ff7f8d709e2f4d5b8b92b8784a37 /src/SMAPI/Framework/Input/SInputState.cs
parent5ae640dc91adff8dfb0827e2a3c3f6b54be7c612 (diff)
parent6d1494a56c5d04e7bc1ee406810a5a53dea2229a (diff)
downloadSMAPI-7ca5efbbc576f3c6c43493654b2a0ac040fd4f31.tar.gz
SMAPI-7ca5efbbc576f3c6c43493654b2a0ac040fd4f31.tar.bz2
SMAPI-7ca5efbbc576f3c6c43493654b2a0ac040fd4f31.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Input/SInputState.cs')
-rw-r--r--src/SMAPI/Framework/Input/SInputState.cs347
1 files changed, 135 insertions, 212 deletions
diff --git a/src/SMAPI/Framework/Input/SInputState.cs b/src/SMAPI/Framework/Input/SInputState.cs
index 4eaa9ca6..333f5726 100644
--- a/src/SMAPI/Framework/Input/SInputState.cs
+++ b/src/SMAPI/Framework/Input/SInputState.cs
@@ -14,46 +14,40 @@ namespace StardewModdingAPI.Framework.Input
/*********
** Accessors
*********/
- /// <summary>The maximum amount of direction to ignore for the left thumbstick.</summary>
- private const float LeftThumbstickDeadZone = 0.2f;
-
/// <summary>The cursor position on the screen adjusted for the zoom level.</summary>
private CursorPosition CursorPositionImpl;
/// <summary>The player's last known tile position.</summary>
private Vector2? LastPlayerTile;
+ /// <summary>The buttons to press until the game next handles input.</summary>
+ private readonly HashSet<SButton> CustomPressedKeys = new HashSet<SButton>();
+
+ /// <summary>The buttons to consider released until the actual button is released.</summary>
+ private readonly HashSet<SButton> CustomReleasedKeys = new HashSet<SButton>();
+
+ /// <summary>Whether there are new overrides in <see cref="CustomPressedKeys"/> or <see cref="CustomReleasedKeys"/> that haven't been applied to the previous state.</summary>
+ private bool HasNewOverrides;
+
/*********
** Accessors
*********/
/// <summary>The controller state as of the last update.</summary>
- public GamePadState RealController { get; private set; }
+ public GamePadState LastController { get; private set; }
/// <summary>The keyboard state as of the last update.</summary>
- public KeyboardState RealKeyboard { get; private set; }
+ public KeyboardState LastKeyboard { get; private set; }
/// <summary>The mouse state as of the last update.</summary>
- public MouseState RealMouse { get; private set; }
+ public MouseState LastMouse { get; private set; }
- /// <summary>A derivative of <see cref="RealController"/> which suppresses the buttons in <see cref="SuppressButtons"/>.</summary>
- public GamePadState SuppressedController { get; private set; }
-
- /// <summary>A derivative of <see cref="RealKeyboard"/> which suppresses the buttons in <see cref="SuppressButtons"/>.</summary>
- public KeyboardState SuppressedKeyboard { get; private set; }
-
- /// <summary>A derivative of <see cref="RealMouse"/> which suppresses the buttons in <see cref="SuppressButtons"/>.</summary>
- public MouseState SuppressedMouse { get; private set; }
+ /// <summary>The buttons which were pressed, held, or released as of the last update.</summary>
+ public IDictionary<SButton, SButtonState> LastButtonStates { get; private set; } = new Dictionary<SButton, SButtonState>();
/// <summary>The cursor position on the screen adjusted for the zoom level.</summary>
public ICursorPosition CursorPosition => this.CursorPositionImpl;
- /// <summary>The buttons which were pressed, held, or released.</summary>
- 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>
- public HashSet<SButton> SuppressButtons { get; } = new HashSet<SButton>();
-
/*********
** Public methods
@@ -63,14 +57,34 @@ namespace StardewModdingAPI.Framework.Input
{
return new SInputState
{
- ActiveButtons = this.ActiveButtons,
- RealController = this.RealController,
- RealKeyboard = this.RealKeyboard,
- RealMouse = this.RealMouse,
+ LastButtonStates = this.LastButtonStates,
+ LastController = this.LastController,
+ LastKeyboard = this.LastKeyboard,
+ LastMouse = this.LastMouse,
CursorPositionImpl = this.CursorPositionImpl
};
}
+ /// <summary>Override the state for a button.</summary>
+ /// <param name="button">The button to override.</param>
+ /// <param name="setDown">Whether to mark it pressed; else mark it released.</param>
+ public void OverrideButton(SButton button, bool setDown)
+ {
+ bool changed = setDown
+ ? this.CustomPressedKeys.Add(button) | this.CustomReleasedKeys.Remove(button)
+ : this.CustomPressedKeys.Remove(button) | this.CustomReleasedKeys.Add(button);
+
+ if (changed)
+ this.HasNewOverrides = true;
+ }
+
+ /// <summary>Get whether a mod has indicated the key was already handled, so the game shouldn't handle it.</summary>
+ /// <param name="button">The button to check.</param>
+ public bool IsSuppressed(SButton button)
+ {
+ return this.CustomReleasedKeys.Contains(button);
+ }
+
/// <summary>This method is called by the game, and does nothing since SMAPI will already have updated by that point.</summary>
[Obsolete("This method should only be called by the game itself.")]
public override void Update() { }
@@ -82,28 +96,47 @@ namespace StardewModdingAPI.Framework.Input
{
float zoomMultiplier = (1f / Game1.options.zoomLevel);
- // get new states
- GamePadState realController = GamePad.GetState(PlayerIndex.One);
- KeyboardState realKeyboard = Keyboard.GetState();
- MouseState realMouse = Mouse.GetState();
- 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);
+ // get real values
+ var controller = new GamePadStateBuilder();
+ var keyboard = new KeyboardStateBuilder();
+ var mouse = new MouseStateBuilder();
+ Vector2 cursorAbsolutePos = new Vector2((mouse.X * zoomMultiplier) + Game1.viewport.X, (mouse.Y * zoomMultiplier) + Game1.viewport.Y);
Vector2? playerTilePos = Context.IsPlayerFree ? Game1.player.getTileLocation() : (Vector2?)null;
+ HashSet<SButton> reallyDown = new HashSet<SButton>(this.GetPressedButtons(keyboard, mouse, controller));
+
+ // apply overrides
+ bool hasOverrides = false;
+ if (this.CustomPressedKeys.Count > 0 || this.CustomReleasedKeys.Count > 0)
+ {
+ // reset overrides that no longer apply
+ this.CustomPressedKeys.RemoveWhere(key => reallyDown.Contains(key));
+ this.CustomReleasedKeys.RemoveWhere(key => !reallyDown.Contains(key));
+
+ // apply overrides
+ if (this.ApplyOverrides(this.CustomPressedKeys, this.CustomReleasedKeys, controller, keyboard, mouse))
+ hasOverrides = true;
- // update real states
- this.ActiveButtons = activeButtons;
- this.RealController = realController;
- this.RealKeyboard = realKeyboard;
- this.RealMouse = realMouse;
+ // remove pressed keys
+ this.CustomPressedKeys.Clear();
+ }
+
+ // get button states
+ var pressedButtons = hasOverrides
+ ? new HashSet<SButton>(this.GetPressedButtons(keyboard, mouse, controller))
+ : reallyDown;
+ var activeButtons = this.DeriveStates(this.LastButtonStates, pressedButtons);
+
+ // update
+ this.HasNewOverrides = false;
+ this.LastController = controller.GetState();
+ this.LastKeyboard = keyboard.GetState();
+ this.LastMouse = mouse.GetState();
+ this.LastButtonStates = activeButtons;
if (cursorAbsolutePos != this.CursorPositionImpl?.AbsolutePixels || playerTilePos != this.LastPlayerTile)
{
this.LastPlayerTile = playerTilePos;
- this.CursorPositionImpl = this.GetCursorPosition(realMouse, cursorAbsolutePos, zoomMultiplier);
+ this.CursorPositionImpl = this.GetCursorPosition(this.LastMouse, cursorAbsolutePos, zoomMultiplier);
}
-
- // update suppressed states
- this.SuppressButtons.RemoveWhere(p => !this.GetState(activeButtons, p).IsDown());
- this.UpdateSuppression();
}
catch (InvalidOperationException)
{
@@ -111,18 +144,22 @@ namespace StardewModdingAPI.Framework.Input
}
}
- /// <summary>Apply input suppression to current input.</summary>
- public void UpdateSuppression()
+ /// <summary>Apply input overrides to the current state.</summary>
+ public void ApplyOverrides()
{
- GamePadState suppressedController = this.RealController;
- KeyboardState suppressedKeyboard = this.RealKeyboard;
- MouseState suppressedMouse = this.RealMouse;
-
- this.SuppressGivenStates(this.ActiveButtons, ref suppressedKeyboard, ref suppressedMouse, ref suppressedController);
+ if (this.HasNewOverrides)
+ {
+ var controller = new GamePadStateBuilder(this.LastController);
+ var keyboard = new KeyboardStateBuilder(this.LastKeyboard);
+ var mouse = new MouseStateBuilder(this.LastMouse);
- this.SuppressedController = suppressedController;
- this.SuppressedKeyboard = suppressedKeyboard;
- this.SuppressedMouse = suppressedMouse;
+ if (this.ApplyOverrides(pressed: this.CustomPressedKeys, released: this.CustomReleasedKeys, controller, keyboard, mouse))
+ {
+ this.LastController = controller.GetState();
+ this.LastKeyboard = keyboard.GetState();
+ this.LastMouse = mouse.GetState();
+ }
+ }
}
/// <summary>Get the gamepad state visible to the game.</summary>
@@ -130,36 +167,30 @@ namespace StardewModdingAPI.Framework.Input
public override GamePadState GetGamePadState()
{
if (Game1.options.gamepadMode == Options.GamepadModes.ForceOff)
- return base.GetGamePadState();
+ return new GamePadState();
- return this.ShouldSuppressNow()
- ? this.SuppressedController
- : this.RealController;
+ return this.LastController;
}
/// <summary>Get the keyboard state visible to the game.</summary>
[Obsolete("This method should only be called by the game itself.")]
public override KeyboardState GetKeyboardState()
{
- return this.ShouldSuppressNow()
- ? this.SuppressedKeyboard
- : this.RealKeyboard;
+ return this.LastKeyboard;
}
/// <summary>Get the keyboard state visible to the game.</summary>
[Obsolete("This method should only be called by the game itself.")]
public override MouseState GetMouseState()
{
- return this.ShouldSuppressNow()
- ? this.SuppressedMouse
- : this.RealMouse;
+ return this.LastMouse;
}
/// <summary>Get whether a given button was pressed or held.</summary>
/// <param name="button">The button to check.</param>
public bool IsDown(SButton button)
{
- return this.GetState(this.ActiveButtons, button).IsDown();
+ return this.GetState(this.LastButtonStates, button).IsDown();
}
/// <summary>Get whether any of the given buttons were pressed or held.</summary>
@@ -173,7 +204,7 @@ namespace StardewModdingAPI.Framework.Input
/// <param name="button">The button to check.</param>
public SButtonState GetState(SButton button)
{
- return this.GetState(this.ActiveButtons, button);
+ return this.GetState(this.LastButtonStates, button);
}
@@ -194,76 +225,57 @@ namespace StardewModdingAPI.Framework.Input
return new CursorPosition(absolutePixels, screenPixels, tile, grabTile);
}
- /// <summary>Whether input should be suppressed in the current context.</summary>
- private bool ShouldSuppressNow()
- {
- return Game1.chatBox == null || !Game1.chatBox.isActive();
- }
-
- /// <summary>Apply input suppression to the given input states.</summary>
- /// <param name="activeButtons">The current button states to check.</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="gamePadState">The game's controller state for the current tick.</param>
- private void SuppressGivenStates(IDictionary<SButton, SButtonState> activeButtons, ref KeyboardState keyboardState, ref MouseState mouseState, ref GamePadState gamePadState)
+ /// <summary>Apply input overrides to the given states.</summary>
+ /// <param name="pressed">The buttons to mark pressed.</param>
+ /// <param name="released">The buttons to mark released.</param>
+ /// <param name="controller">The game's controller state for the current tick.</param>
+ /// <param name="keyboard">The game's keyboard state for the current tick.</param>
+ /// <param name="mouse">The game's mouse state for the current tick.</param>
+ /// <returns>Returns whether any overrides were applied.</returns>
+ private bool ApplyOverrides(ISet<SButton> pressed, ISet<SButton> released, GamePadStateBuilder controller, KeyboardStateBuilder keyboard, MouseStateBuilder mouse)
{
- if (this.SuppressButtons.Count == 0)
- return;
-
- // gather info
- HashSet<Keys> suppressKeys = new HashSet<Keys>();
- HashSet<SButton> suppressButtons = new HashSet<SButton>();
- HashSet<SButton> suppressMouse = new HashSet<SButton>();
- foreach (SButton button in this.SuppressButtons)
+ if (pressed.Count == 0 && released.Count == 0)
+ return false;
+
+ // group keys by type
+ IDictionary<SButton, SButtonState> keyboardOverrides = new Dictionary<SButton, SButtonState>();
+ IDictionary<SButton, SButtonState> controllerOverrides = new Dictionary<SButton, SButtonState>();
+ IDictionary<SButton, SButtonState> mouseOverrides = new Dictionary<SButton, SButtonState>();
+ foreach (var button in pressed.Concat(released))
{
+ var newState = this.DeriveState(
+ oldState: this.GetState(button),
+ isDown: pressed.Contains(button)
+ );
+
if (button == SButton.MouseLeft || button == SButton.MouseMiddle || button == SButton.MouseRight || button == SButton.MouseX1 || button == SButton.MouseX2)
- suppressMouse.Add(button);
- else if (button.TryGetKeyboard(out Keys key))
- suppressKeys.Add(key);
- else if (gamePadState.IsConnected && button.TryGetController(out Buttons _))
- suppressButtons.Add(button);
+ mouseOverrides[button] = newState;
+ else if (button.TryGetKeyboard(out Keys _))
+ keyboardOverrides[button] = newState;
+ else if (controller.IsConnected && button.TryGetController(out Buttons _))
+ controllerOverrides[button] = newState;
}
- // suppress keyboard keys
- if (keyboardState.GetPressedKeys().Any() && suppressKeys.Any())
- keyboardState = new KeyboardState(keyboardState.GetPressedKeys().Except(suppressKeys).ToArray());
-
- // suppress controller keys
- if (gamePadState.IsConnected && suppressButtons.Any())
- {
- GamePadStateBuilder builder = new GamePadStateBuilder(gamePadState);
- builder.SuppressButtons(suppressButtons);
- gamePadState = builder.ToGamePadState();
- }
+ // override states
+ if (keyboardOverrides.Any())
+ keyboard.OverrideButtons(keyboardOverrides);
+ if (controller.IsConnected && controllerOverrides.Any())
+ controller.OverrideButtons(controllerOverrides);
+ if (mouseOverrides.Any())
+ mouse.OverrideButtons(mouseOverrides);
- // suppress mouse buttons
- if (suppressMouse.Any())
- {
- mouseState = new MouseState(
- x: mouseState.X,
- y: mouseState.Y,
- scrollWheel: mouseState.ScrollWheelValue,
- leftButton: suppressMouse.Contains(SButton.MouseLeft) ? ButtonState.Released : mouseState.LeftButton,
- middleButton: suppressMouse.Contains(SButton.MouseMiddle) ? ButtonState.Released : mouseState.MiddleButton,
- rightButton: suppressMouse.Contains(SButton.MouseRight) ? ButtonState.Released : mouseState.RightButton,
- xButton1: suppressMouse.Contains(SButton.MouseX1) ? ButtonState.Released : mouseState.XButton1,
- xButton2: suppressMouse.Contains(SButton.MouseX2) ? ButtonState.Released : mouseState.XButton2
- );
- }
+ return true;
}
/// <summary>Get the state of all pressed or released buttons relative to their previous state.</summary>
/// <param name="previousStates">The previous button states.</param>
- /// <param name="keyboard">The keyboard state.</param>
- /// <param name="mouse">The mouse state.</param>
- /// <param name="controller">The controller state.</param>
- private IDictionary<SButton, SButtonState> DeriveStates(IDictionary<SButton, SButtonState> previousStates, KeyboardState keyboard, MouseState mouse, GamePadState controller)
+ /// <param name="pressedButtons">The currently pressed buttons.</param>
+ private IDictionary<SButton, SButtonState> DeriveStates(IDictionary<SButton, SButtonState> previousStates, HashSet<SButton> pressedButtons)
{
IDictionary<SButton, SButtonState> activeButtons = new Dictionary<SButton, SButtonState>();
// handle pressed keys
- SButton[] down = this.GetPressedButtons(keyboard, mouse, controller).ToArray();
- foreach (SButton button in down)
+ foreach (SButton button in pressedButtons)
activeButtons[button] = this.DeriveState(this.GetState(previousStates, button), isDown: true);
// handle released keys
@@ -301,101 +313,12 @@ namespace StardewModdingAPI.Framework.Input
/// <param name="mouse">The mouse state.</param>
/// <param name="controller">The controller state.</param>
/// <remarks>Thumbstick direction logic derived from <see cref="ButtonCollection"/>.</remarks>
- private IEnumerable<SButton> GetPressedButtons(KeyboardState keyboard, MouseState mouse, GamePadState controller)
- {
- // keyboard
- foreach (Keys key in keyboard.GetPressedKeys())
- yield return key.ToSButton();
-
- // mouse
- if (mouse.LeftButton == ButtonState.Pressed)
- yield return SButton.MouseLeft;
- if (mouse.RightButton == ButtonState.Pressed)
- yield return SButton.MouseRight;
- if (mouse.MiddleButton == ButtonState.Pressed)
- yield return SButton.MouseMiddle;
- if (mouse.XButton1 == ButtonState.Pressed)
- yield return SButton.MouseX1;
- if (mouse.XButton2 == ButtonState.Pressed)
- yield return SButton.MouseX2;
-
- // controller
- if (controller.IsConnected)
- {
- // main buttons
- if (controller.Buttons.A == ButtonState.Pressed)
- yield return SButton.ControllerA;
- if (controller.Buttons.B == ButtonState.Pressed)
- yield return SButton.ControllerB;
- if (controller.Buttons.X == ButtonState.Pressed)
- yield return SButton.ControllerX;
- if (controller.Buttons.Y == ButtonState.Pressed)
- yield return SButton.ControllerY;
- if (controller.Buttons.LeftStick == ButtonState.Pressed)
- yield return SButton.LeftStick;
- if (controller.Buttons.RightStick == ButtonState.Pressed)
- yield return SButton.RightStick;
- if (controller.Buttons.Start == ButtonState.Pressed)
- yield return SButton.ControllerStart;
-
- // directional pad
- if (controller.DPad.Up == ButtonState.Pressed)
- yield return SButton.DPadUp;
- if (controller.DPad.Down == ButtonState.Pressed)
- yield return SButton.DPadDown;
- if (controller.DPad.Left == ButtonState.Pressed)
- yield return SButton.DPadLeft;
- if (controller.DPad.Right == ButtonState.Pressed)
- yield return SButton.DPadRight;
-
- // secondary buttons
- if (controller.Buttons.Back == ButtonState.Pressed)
- yield return SButton.ControllerBack;
- if (controller.Buttons.BigButton == ButtonState.Pressed)
- yield return SButton.BigButton;
-
- // shoulders
- if (controller.Buttons.LeftShoulder == ButtonState.Pressed)
- yield return SButton.LeftShoulder;
- if (controller.Buttons.RightShoulder == ButtonState.Pressed)
- yield return SButton.RightShoulder;
-
- // triggers
- if (controller.Triggers.Left > 0.2f)
- yield return SButton.LeftTrigger;
- if (controller.Triggers.Right > 0.2f)
- yield return SButton.RightTrigger;
-
- // left thumbstick direction
- if (controller.ThumbSticks.Left.Y > SInputState.LeftThumbstickDeadZone)
- yield return SButton.LeftThumbstickUp;
- if (controller.ThumbSticks.Left.Y < -SInputState.LeftThumbstickDeadZone)
- yield return SButton.LeftThumbstickDown;
- if (controller.ThumbSticks.Left.X > SInputState.LeftThumbstickDeadZone)
- yield return SButton.LeftThumbstickRight;
- if (controller.ThumbSticks.Left.X < -SInputState.LeftThumbstickDeadZone)
- yield return SButton.LeftThumbstickLeft;
-
- // right thumbstick direction
- if (this.IsRightThumbstickOutsideDeadZone(controller.ThumbSticks.Right))
- {
- if (controller.ThumbSticks.Right.Y > 0)
- yield return SButton.RightThumbstickUp;
- if (controller.ThumbSticks.Right.Y < 0)
- yield return SButton.RightThumbstickDown;
- if (controller.ThumbSticks.Right.X > 0)
- yield return SButton.RightThumbstickRight;
- if (controller.ThumbSticks.Right.X < 0)
- yield return SButton.RightThumbstickLeft;
- }
- }
- }
-
- /// <summary>Get whether the right thumbstick should be considered outside the dead zone.</summary>
- /// <param name="direction">The right thumbstick value.</param>
- private bool IsRightThumbstickOutsideDeadZone(Vector2 direction)
+ private IEnumerable<SButton> GetPressedButtons(KeyboardStateBuilder keyboard, MouseStateBuilder mouse, GamePadStateBuilder controller)
{
- return direction.Length() > 0.9f;
+ return keyboard
+ .GetPressedButtons()
+ .Concat(mouse.GetPressedButtons())
+ .Concat(controller.GetPressedButtons());
}
}
}