summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Input
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-05-01 18:16:09 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-05-01 18:16:09 -0400
commitc8ad50dad1d706a1901798f9396f6becfea36c0e (patch)
tree28bd818a5db39ec5ece1bd141a28de955950463b /src/SMAPI/Framework/Input
parent451b70953ff4c0b1b27ae0de203ad99379b45b2a (diff)
parentf78093bdb58d477b400cde3f19b70ffd6ddf833d (diff)
downloadSMAPI-c8ad50dad1d706a1901798f9396f6becfea36c0e.tar.gz
SMAPI-c8ad50dad1d706a1901798f9396f6becfea36c0e.tar.bz2
SMAPI-c8ad50dad1d706a1901798f9396f6becfea36c0e.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Input')
-rw-r--r--src/SMAPI/Framework/Input/GamePadStateBuilder.cs22
-rw-r--r--src/SMAPI/Framework/Input/KeyboardStateBuilder.cs11
-rw-r--r--src/SMAPI/Framework/Input/MouseStateBuilder.cs7
-rw-r--r--src/SMAPI/Framework/Input/SInputState.cs18
4 files changed, 30 insertions, 28 deletions
diff --git a/src/SMAPI/Framework/Input/GamePadStateBuilder.cs b/src/SMAPI/Framework/Input/GamePadStateBuilder.cs
index b0bb7f80..4ac3332c 100644
--- a/src/SMAPI/Framework/Input/GamePadStateBuilder.cs
+++ b/src/SMAPI/Framework/Input/GamePadStateBuilder.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
@@ -21,7 +22,7 @@ namespace StardewModdingAPI.Framework.Input
private GamePadState? State;
/// <summary>The current button states.</summary>
- private readonly IDictionary<SButton, ButtonState> ButtonStates;
+ private readonly IDictionary<SButton, ButtonState>? ButtonStates;
/// <summary>The left trigger value.</summary>
private float LeftTrigger;
@@ -40,6 +41,7 @@ namespace StardewModdingAPI.Framework.Input
** Accessors
*********/
/// <summary>Whether the gamepad is currently connected.</summary>
+ [MemberNotNullWhen(true, nameof(GamePadStateBuilder.ButtonStates))]
public bool IsConnected { get; }
@@ -85,8 +87,7 @@ namespace StardewModdingAPI.Framework.Input
this.RightStickPos = sticks.Right;
}
- /// <summary>Override the states for a set of buttons.</summary>
- /// <param name="overrides">The button state overrides.</param>
+ /// <inheritdoc />
public GamePadStateBuilder OverrideButtons(IDictionary<SButton, SButtonState> overrides)
{
if (!this.IsConnected)
@@ -104,10 +105,10 @@ namespace StardewModdingAPI.Framework.Input
this.LeftStickPos.Y = isDown ? 1 : 0;
break;
case SButton.LeftThumbstickDown:
- this.LeftStickPos.Y = isDown ? 1 : 0;
+ this.LeftStickPos.Y = isDown ? -1 : 0;
break;
case SButton.LeftThumbstickLeft:
- this.LeftStickPos.X = isDown ? 1 : 0;
+ this.LeftStickPos.X = isDown ? -1 : 0;
break;
case SButton.LeftThumbstickRight:
this.LeftStickPos.X = isDown ? 1 : 0;
@@ -118,10 +119,10 @@ namespace StardewModdingAPI.Framework.Input
this.RightStickPos.Y = isDown ? 1 : 0;
break;
case SButton.RightThumbstickDown:
- this.RightStickPos.Y = isDown ? 1 : 0;
+ this.RightStickPos.Y = isDown ? -1 : 0;
break;
case SButton.RightThumbstickLeft:
- this.RightStickPos.X = isDown ? 1 : 0;
+ this.RightStickPos.X = isDown ? -1 : 0;
break;
case SButton.RightThumbstickRight:
this.RightStickPos.X = isDown ? 1 : 0;
@@ -151,7 +152,7 @@ namespace StardewModdingAPI.Framework.Input
return this;
}
- /// <summary>Get the currently pressed buttons.</summary>
+ /// <inheritdoc />
public IEnumerable<SButton> GetPressedButtons()
{
if (!this.IsConnected)
@@ -191,7 +192,7 @@ namespace StardewModdingAPI.Framework.Input
}
}
- /// <summary>Get the equivalent state.</summary>
+ /// <inheritdoc />
public GamePadState GetState()
{
this.State ??= new GamePadState(
@@ -212,6 +213,9 @@ namespace StardewModdingAPI.Framework.Input
/// <summary>Get the pressed gamepad buttons.</summary>
private IEnumerable<Buttons> GetPressedGamePadButtons()
{
+ if (!this.IsConnected)
+ yield break;
+
foreach (var pair in this.ButtonStates)
{
if (pair.Value == ButtonState.Pressed && pair.Key.TryGetController(out Buttons button))
diff --git a/src/SMAPI/Framework/Input/KeyboardStateBuilder.cs b/src/SMAPI/Framework/Input/KeyboardStateBuilder.cs
index 620ad442..f66fbd07 100644
--- a/src/SMAPI/Framework/Input/KeyboardStateBuilder.cs
+++ b/src/SMAPI/Framework/Input/KeyboardStateBuilder.cs
@@ -14,7 +14,7 @@ namespace StardewModdingAPI.Framework.Input
private KeyboardState? State;
/// <summary>The pressed buttons.</summary>
- private readonly HashSet<Keys> PressedButtons = new HashSet<Keys>();
+ private readonly HashSet<Keys> PressedButtons = new();
/*********
@@ -27,12 +27,11 @@ namespace StardewModdingAPI.Framework.Input
this.State = state;
this.PressedButtons.Clear();
- foreach (var button in state.GetPressedKeys())
+ foreach (Keys button in state.GetPressedKeys())
this.PressedButtons.Add(button);
}
- /// <summary>Override the states for a set of buttons.</summary>
- /// <param name="overrides">The button state overrides.</param>
+ /// <inheritdoc />
public KeyboardStateBuilder OverrideButtons(IDictionary<SButton, SButtonState> overrides)
{
foreach (var pair in overrides)
@@ -51,14 +50,14 @@ namespace StardewModdingAPI.Framework.Input
return this;
}
- /// <summary>Get the currently pressed buttons.</summary>
+ /// <inheritdoc />
public IEnumerable<SButton> GetPressedButtons()
{
foreach (Keys key in this.PressedButtons)
yield return key.ToSButton();
}
- /// <summary>Get the equivalent state.</summary>
+ /// <inheritdoc />
public KeyboardState GetState()
{
return
diff --git a/src/SMAPI/Framework/Input/MouseStateBuilder.cs b/src/SMAPI/Framework/Input/MouseStateBuilder.cs
index a1ac5492..c2a0891b 100644
--- a/src/SMAPI/Framework/Input/MouseStateBuilder.cs
+++ b/src/SMAPI/Framework/Input/MouseStateBuilder.cs
@@ -51,8 +51,7 @@ namespace StardewModdingAPI.Framework.Input
this.ScrollWheelValue = state.ScrollWheelValue;
}
- /// <summary>Override the states for a set of buttons.</summary>
- /// <param name="overrides">The button state overrides.</param>
+ /// <inheritdoc />
public MouseStateBuilder OverrideButtons(IDictionary<SButton, SButtonState> overrides)
{
foreach (var pair in overrides)
@@ -67,7 +66,7 @@ namespace StardewModdingAPI.Framework.Input
return this;
}
- /// <summary>Get the currently pressed buttons.</summary>
+ /// <inheritdoc />
public IEnumerable<SButton> GetPressedButtons()
{
foreach (var pair in this.ButtonStates)
@@ -77,7 +76,7 @@ namespace StardewModdingAPI.Framework.Input
}
}
- /// <summary>Get the equivalent state.</summary>
+ /// <inheritdoc />
public MouseState GetState()
{
this.State ??= new MouseState(
diff --git a/src/SMAPI/Framework/Input/SInputState.cs b/src/SMAPI/Framework/Input/SInputState.cs
index a8d1f371..fef83af7 100644
--- a/src/SMAPI/Framework/Input/SInputState.cs
+++ b/src/SMAPI/Framework/Input/SInputState.cs
@@ -15,16 +15,16 @@ namespace StardewModdingAPI.Framework.Input
** Accessors
*********/
/// <summary>The cursor position on the screen adjusted for the zoom level.</summary>
- private CursorPosition CursorPositionImpl;
+ private CursorPosition CursorPositionImpl = new(Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero);
/// <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>();
+ private readonly HashSet<SButton> CustomPressedKeys = new();
/// <summary>The buttons to consider released until the actual button is released.</summary>
- private readonly HashSet<SButton> CustomReleasedKeys = new HashSet<SButton>();
+ private readonly HashSet<SButton> CustomReleasedKeys = new();
/// <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;
@@ -72,8 +72,8 @@ namespace StardewModdingAPI.Framework.Input
var controller = new GamePadStateBuilder(base.GetGamePadState());
var keyboard = new KeyboardStateBuilder(base.GetKeyboardState());
var mouse = new MouseStateBuilder(base.GetMouseState());
- 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;
+ Vector2 cursorAbsolutePos = new((mouse.X * zoomMultiplier) + Game1.viewport.X, (mouse.Y * zoomMultiplier) + Game1.viewport.Y);
+ Vector2? playerTilePos = Context.IsPlayerFree ? Game1.player.getTileLocation() : null;
HashSet<SButton> reallyDown = new HashSet<SButton>(this.GetPressedButtons(keyboard, mouse, controller));
// apply overrides
@@ -104,7 +104,7 @@ namespace StardewModdingAPI.Framework.Input
this.KeyboardState = keyboard.GetState();
this.MouseState = mouse.GetState();
this.ButtonStates = activeButtons;
- if (cursorAbsolutePos != this.CursorPositionImpl?.AbsolutePixels || playerTilePos != this.LastPlayerTile)
+ if (cursorAbsolutePos != this.CursorPositionImpl.AbsolutePixels || playerTilePos != this.LastPlayerTile)
{
this.LastPlayerTile = playerTilePos;
this.CursorPositionImpl = this.GetCursorPosition(this.MouseState, cursorAbsolutePos, zoomMultiplier);
@@ -203,8 +203,8 @@ namespace StardewModdingAPI.Framework.Input
/// <param name="zoomMultiplier">The multiplier applied to pixel coordinates to adjust them for pixel zoom.</param>
private CursorPosition GetCursorPosition(MouseState mouseState, Vector2 absolutePixels, float zoomMultiplier)
{
- Vector2 screenPixels = new Vector2(mouseState.X * zoomMultiplier, mouseState.Y * zoomMultiplier);
- Vector2 tile = new Vector2((int)((Game1.viewport.X + screenPixels.X) / Game1.tileSize), (int)((Game1.viewport.Y + screenPixels.Y) / Game1.tileSize));
+ Vector2 screenPixels = new(mouseState.X * zoomMultiplier, mouseState.Y * zoomMultiplier);
+ Vector2 tile = new((int)((Game1.viewport.X + screenPixels.X) / Game1.tileSize), (int)((Game1.viewport.Y + screenPixels.Y) / Game1.tileSize));
Vector2 grabTile = (Game1.mouseCursorTransparency > 0 && Utility.tileWithinRadiusOfPlayer((int)tile.X, (int)tile.Y, 1, Game1.player)) // derived from Game1.pressActionButton
? tile
: Game1.player.GetGrabTile();
@@ -234,7 +234,7 @@ namespace StardewModdingAPI.Framework.Input
isDown: pressed.Contains(button)
);
- if (button == SButton.MouseLeft || button == SButton.MouseMiddle || button == SButton.MouseRight || button == SButton.MouseX1 || button == SButton.MouseX2)
+ if (button is SButton.MouseLeft or SButton.MouseMiddle or SButton.MouseRight or SButton.MouseX1 or SButton.MouseX2)
mouseOverrides[button] = newState;
else if (button.TryGetKeyboard(out Keys _))
keyboardOverrides[button] = newState;