summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/StardewModdingAPI/Events/EventArgsInput.cs38
-rw-r--r--src/StardewModdingAPI/Events/InputEvents.cs45
-rw-r--r--src/StardewModdingAPI/Framework/CursorPosition.cs37
-rw-r--r--src/StardewModdingAPI/Framework/SGame.cs268
-rw-r--r--src/StardewModdingAPI/ICursorPosition.cs19
-rw-r--r--src/StardewModdingAPI/StardewModdingAPI.csproj5
-rw-r--r--src/StardewModdingAPI/Utilities/SButton.cs659
7 files changed, 928 insertions, 143 deletions
diff --git a/src/StardewModdingAPI/Events/EventArgsInput.cs b/src/StardewModdingAPI/Events/EventArgsInput.cs
new file mode 100644
index 00000000..1d5e6fde
--- /dev/null
+++ b/src/StardewModdingAPI/Events/EventArgsInput.cs
@@ -0,0 +1,38 @@
+#if SMAPI_2_0
+using System;
+using StardewModdingAPI.Utilities;
+
+namespace StardewModdingAPI.Events
+{
+ /// <summary>Event arguments when a button is pressed or released.</summary>
+ public class EventArgsInput : EventArgs
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The button on the controller, keyboard, or mouse.</summary>
+ public SButton Button { get; }
+
+ /// <summary>The current cursor position.</summary>
+ public ICursorPosition Cursor { get; set; }
+
+ /// <summary>Whether the input is considered a 'click' by the game for enabling action.</summary>
+ public bool IsClick { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="button">The button on the controller, keyboard, or mouse.</param>
+ /// <param name="cursor">The cursor position.</param>
+ /// <param name="isClick">Whether the input is considered a 'click' by the game for enabling action.</param>
+ public EventArgsInput(SButton button, ICursorPosition cursor, bool isClick)
+ {
+ this.Button = button;
+ this.Cursor = cursor;
+ this.IsClick = isClick;
+ }
+ }
+}
+#endif
diff --git a/src/StardewModdingAPI/Events/InputEvents.cs b/src/StardewModdingAPI/Events/InputEvents.cs
new file mode 100644
index 00000000..285487af
--- /dev/null
+++ b/src/StardewModdingAPI/Events/InputEvents.cs
@@ -0,0 +1,45 @@
+#if SMAPI_2_0
+using System;
+using StardewModdingAPI.Framework;
+using StardewModdingAPI.Utilities;
+
+namespace StardewModdingAPI.Events
+{
+ /// <summary>Events raised when the player uses a controller, keyboard, or mouse button.</summary>
+ public static class InputEvents
+ {
+ /*********
+ ** Events
+ *********/
+ /// <summary>Raised when the player presses a button on the keyboard, controller, or mouse.</summary>
+ public static event EventHandler<EventArgsInput> ButtonPressed;
+
+ /// <summary>Raised when the player releases a keyboard key on the keyboard, controller, or mouse.</summary>
+ public static event EventHandler<EventArgsInput> ButtonReleased;
+
+
+ /*********
+ ** Internal methods
+ *********/
+ /// <summary>Raise a <see cref="ButtonPressed"/> event.</summary>
+ /// <param name="monitor">Encapsulates monitoring and logging.</param>
+ /// <param name="button">The button on the controller, keyboard, or mouse.</param>
+ /// <param name="cursor">The cursor position.</param>
+ /// <param name="isClick">Whether the input is considered a 'click' by the game for enabling action.</param>
+ internal static void InvokeButtonPressed(IMonitor monitor, SButton button, ICursorPosition cursor, bool isClick)
+ {
+ monitor.SafelyRaiseGenericEvent($"{nameof(InputEvents)}.{nameof(InputEvents.ButtonPressed)}", InputEvents.ButtonPressed?.GetInvocationList(), null, new EventArgsInput(button, cursor, isClick));
+ }
+
+ /// <summary>Raise a <see cref="ButtonReleased"/> event.</summary>
+ /// <param name="monitor">Encapsulates monitoring and logging.</param>
+ /// <param name="button">The button on the controller, keyboard, or mouse.</param>
+ /// <param name="cursor">The cursor position.</param>
+ /// <param name="isClick">Whether the input is considered a 'click' by the game for enabling action.</param>
+ internal static void InvokeButtonReleased(IMonitor monitor, SButton button, ICursorPosition cursor, bool isClick)
+ {
+ monitor.SafelyRaiseGenericEvent($"{nameof(InputEvents)}.{nameof(InputEvents.ButtonReleased)}", InputEvents.ButtonReleased?.GetInvocationList(), null, new EventArgsInput(button, cursor, isClick));
+ }
+ }
+}
+#endif \ No newline at end of file
diff --git a/src/StardewModdingAPI/Framework/CursorPosition.cs b/src/StardewModdingAPI/Framework/CursorPosition.cs
new file mode 100644
index 00000000..4f256da5
--- /dev/null
+++ b/src/StardewModdingAPI/Framework/CursorPosition.cs
@@ -0,0 +1,37 @@
+#if SMAPI_2_0
+using Microsoft.Xna.Framework;
+
+namespace StardewModdingAPI.Framework
+{
+ /// <summary>Defines a position on a given map at different reference points.</summary>
+ internal class CursorPosition : ICursorPosition
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The pixel position relative to the top-left corner of the visible screen.</summary>
+ public Vector2 ScreenPixels { get; }
+
+ /// <summary>The tile position under the cursor relative to the top-left corner of the map.</summary>
+ public Vector2 Tile { get; }
+
+ /// <summary>The tile position that the game considers under the cursor for purposes of clicking actions. This may be different than <see cref="Tile"/> if that's too far from the player.</summary>
+ public Vector2 GrabTile { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="screenPixels">The pixel position relative to the top-left corner of the visible screen.</param>
+ /// <param name="tile">The tile position relative to the top-left corner of the map.</param>
+ /// <param name="grabTile">The tile position that the game considers under the cursor for purposes of clicking actions.</param>
+ public CursorPosition(Vector2 screenPixels, Vector2 tile, Vector2 grabTile)
+ {
+ this.ScreenPixels = screenPixels;
+ this.Tile = tile;
+ this.GrabTile = grabTile;
+ }
+ }
+}
+#endif
diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs
index 678dcf3a..f2c5c0c9 100644
--- a/src/StardewModdingAPI/Framework/SGame.cs
+++ b/src/StardewModdingAPI/Framework/SGame.cs
@@ -10,6 +10,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using StardewModdingAPI.Events;
+using StardewModdingAPI.Utilities;
using StardewValley;
using StardewValley.BellsAndWhistles;
using StardewValley.Locations;
@@ -59,12 +60,15 @@ namespace StardewModdingAPI.Framework
/****
** Game state
****/
- /// <summary>Arrays of pressed controller buttons indexed by <see cref="PlayerIndex"/>.</summary>
- private Buttons[] PreviousPressedButtons = new Buttons[0];
+ /// <summary>A record of the buttons pressed as of the previous tick.</summary>
+ private SButton[] PreviousPressedButtons = new SButton[0];
/// <summary>A record of the keyboard state (i.e. the up/down state for each button) as of the previous tick.</summary>
private KeyboardState PreviousKeyState;
+ /// <summary>A record of the controller state (i.e. the up/down state for each button) as of the previous tick.</summary>
+ private GamePadState PreviousControllerState;
+
/// <summary>A record of the mouse state (i.e. the cursor position, scroll amount, and the up/down state for each button) as of the previous tick.</summary>
private MouseState PreviousMouseState;
@@ -351,64 +355,95 @@ namespace StardewModdingAPI.Framework
{
// get latest state
KeyboardState keyState;
+ GamePadState controllerState;
MouseState mouseState;
Point mousePosition;
try
{
keyState = Keyboard.GetState();
+ controllerState = GamePad.GetState(PlayerIndex.One);
mouseState = Mouse.GetState();
mousePosition = new Point(Game1.getMouseX(), Game1.getMouseY());
}
catch (InvalidOperationException) // GetState() may crash for some players if window doesn't have focus but game1.IsActive == true
{
keyState = this.PreviousKeyState;
+ controllerState = this.PreviousControllerState;
mouseState = this.PreviousMouseState;
mousePosition = this.PreviousMousePosition;
}
// analyse state
- Keys[] currentlyPressedKeys = keyState.GetPressedKeys();
- Keys[] previousPressedKeys = this.PreviousKeyState.GetPressedKeys();
- Keys[] framePressedKeys = currentlyPressedKeys.Except(previousPressedKeys).ToArray();
- Keys[] frameReleasedKeys = previousPressedKeys.Except(currentlyPressedKeys).ToArray();
-
- // raise key pressed
- foreach (Keys key in framePressedKeys)
- ControlEvents.InvokeKeyPressed(this.Monitor, key);
-
- // raise key released
- foreach (Keys key in frameReleasedKeys)
- ControlEvents.InvokeKeyReleased(this.Monitor, key);
+ SButton[] currentlyPressedKeys = this.GetPressedButtons(keyState, mouseState, controllerState).ToArray();
+ SButton[] previousPressedKeys = this.PreviousPressedButtons;
+ SButton[] framePressedKeys = currentlyPressedKeys.Except(previousPressedKeys).ToArray();
+ SButton[] frameReleasedKeys = previousPressedKeys.Except(currentlyPressedKeys).ToArray();
+ bool isClick = framePressedKeys.Contains(SButton.MouseLeft) || (framePressedKeys.Contains(SButton.ControllerA) && !currentlyPressedKeys.Contains(SButton.ControllerX));
+
+ // get cursor position
+#if SMAPI_2_0
+ ICursorPosition cursor;
+ {
+ // cursor position
+ Vector2 screenPixels = new Vector2(Game1.getMouseX(), Game1.getMouseY());
+ Vector2 tile = new Vector2((Game1.viewport.X + screenPixels.X) / Game1.tileSize, (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();
+ cursor = new CursorPosition(screenPixels, tile, grabTile);
+ }
+#endif
- // raise controller button pressed
- foreach (Buttons button in this.GetFramePressedButtons())
+ // raise button pressed
+ foreach (SButton button in framePressedKeys)
{
- if (button == Buttons.LeftTrigger || button == Buttons.RightTrigger)
+#if SMAPI_2_0
+ InputEvents.InvokeButtonPressed(this.Monitor, button, cursor, isClick);
+#endif
+
+ // legacy events
+ if (button.TryGetKeyboard(out Keys key))
{
- var triggers = GamePad.GetState(PlayerIndex.One).Triggers;
- ControlEvents.InvokeTriggerPressed(this.Monitor, button, button == Buttons.LeftTrigger ? triggers.Left : triggers.Right);
+ if (key != Keys.None)
+ ControlEvents.InvokeKeyPressed(this.Monitor, key);
+ }
+ else if (button.TryGetController(out Buttons controllerButton))
+ {
+ if (controllerButton == Buttons.LeftTrigger || controllerButton == Buttons.RightTrigger)
+ ControlEvents.InvokeTriggerPressed(this.Monitor, controllerButton, controllerButton == Buttons.LeftTrigger ? controllerState.Triggers.Left : controllerState.Triggers.Right);
+ else
+ ControlEvents.InvokeButtonPressed(this.Monitor, controllerButton);
}
- else
- ControlEvents.InvokeButtonPressed(this.Monitor, button);
}
- // raise controller button released
- foreach (Buttons button in this.GetFrameReleasedButtons())
+ // raise button released
+ foreach (SButton button in frameReleasedKeys)
{
- if (button == Buttons.LeftTrigger || button == Buttons.RightTrigger)
+#if SMAPI_2_0
+ bool wasClick =
+ (button == SButton.MouseLeft && previousPressedKeys.Contains(SButton.MouseLeft)) // released left click
+ || (button == SButton.ControllerA && previousPressedKeys.Contains(SButton.ControllerA) && !previousPressedKeys.Contains(SButton.ControllerX));
+ InputEvents.InvokeButtonReleased(this.Monitor, button, cursor, wasClick);
+#endif
+
+ // legacy events
+ if (button.TryGetKeyboard(out Keys key))
{
- var triggers = GamePad.GetState(PlayerIndex.One).Triggers;
- ControlEvents.InvokeTriggerReleased(this.Monitor, button, button == Buttons.LeftTrigger ? triggers.Left : triggers.Right);
+ if (key != Keys.None)
+ ControlEvents.InvokeKeyReleased(this.Monitor, key);
+ }
+ else if (button.TryGetController(out Buttons controllerButton))
+ {
+ if (controllerButton == Buttons.LeftTrigger || controllerButton == Buttons.RightTrigger)
+ ControlEvents.InvokeTriggerReleased(this.Monitor, controllerButton, controllerButton == Buttons.LeftTrigger ? controllerState.Triggers.Left : controllerState.Triggers.Right);
+ else
+ ControlEvents.InvokeButtonReleased(this.Monitor, controllerButton);
}
- else
- ControlEvents.InvokeButtonReleased(this.Monitor, button);
}
- // raise keyboard state changed
+ // raise legacy state-changed events
if (keyState != this.PreviousKeyState)
ControlEvents.InvokeKeyboardChanged(this.Monitor, this.PreviousKeyState, keyState);
-
- // raise mouse state changed
if (mouseState != this.PreviousMouseState)
ControlEvents.InvokeMouseChanged(this.Monitor, this.PreviousMouseState, mouseState, this.PreviousMousePosition, mousePosition);
@@ -416,7 +451,8 @@ namespace StardewModdingAPI.Framework
this.PreviousMouseState = mouseState;
this.PreviousMousePosition = mousePosition;
this.PreviousKeyState = keyState;
- this.PreviousPressedButtons = this.GetButtonsDown();
+ this.PreviousControllerState = controllerState;
+ this.PreviousPressedButtons = currentlyPressedKeys;
}
/*********
@@ -1308,120 +1344,66 @@ namespace StardewModdingAPI.Framework
this.PreviousSaveID = 0;
}
- /// <summary>Get the controller buttons which are currently pressed.</summary>
- private Buttons[] GetButtonsDown()
+ /// <summary>Get the buttons pressed in the given stats.</summary>
+ /// <param name="keyboard">The keyboard state.</param>
+ /// <param name="mouse">The mouse state.</param>
+ /// <param name="controller">The controller state.</param>
+ private IEnumerable<SButton> GetPressedButtons(KeyboardState keyboard, MouseState mouse, GamePadState controller)
{
- var state = GamePad.GetState(PlayerIndex.One);
- var buttons = new List<Buttons>();
- if (state.IsConnected)
+ // 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)
{
- if (state.Buttons.A == ButtonState.Pressed) buttons.Add(Buttons.A);
- if (state.Buttons.B == ButtonState.Pressed) buttons.Add(Buttons.B);
- if (state.Buttons.Back == ButtonState.Pressed) buttons.Add(Buttons.Back);
- if (state.Buttons.BigButton == ButtonState.Pressed) buttons.Add(Buttons.BigButton);
- if (state.Buttons.LeftShoulder == ButtonState.Pressed) buttons.Add(Buttons.LeftShoulder);
- if (state.Buttons.LeftStick == ButtonState.Pressed) buttons.Add(Buttons.LeftStick);
- if (state.Buttons.RightShoulder == ButtonState.Pressed) buttons.Add(Buttons.RightShoulder);
- if (state.Buttons.RightStick == ButtonState.Pressed) buttons.Add(Buttons.RightStick);
- if (state.Buttons.Start == ButtonState.Pressed) buttons.Add(Buttons.Start);
- if (state.Buttons.X == ButtonState.Pressed) buttons.Add(Buttons.X);
- if (state.Buttons.Y == ButtonState.Pressed) buttons.Add(Buttons.Y);
- if (state.DPad.Up == ButtonState.Pressed) buttons.Add(Buttons.DPadUp);
- if (state.DPad.Down == ButtonState.Pressed) buttons.Add(Buttons.DPadDown);
- if (state.DPad.Left == ButtonState.Pressed) buttons.Add(Buttons.DPadLeft);
- if (state.DPad.Right == ButtonState.Pressed) buttons.Add(Buttons.DPadRight);
- if (state.Triggers.Left > 0.2f) buttons.Add(Buttons.LeftTrigger);
- if (state.Triggers.Right > 0.2f) buttons.Add(Buttons.RightTrigger);
+ if (controller.Buttons.A == ButtonState.Pressed)
+ yield return SButton.ControllerA;
+ if (controller.Buttons.B == ButtonState.Pressed)
+ yield return SButton.ControllerB;
+ if (controller.Buttons.Back == ButtonState.Pressed)
+ yield return SButton.ControllerBack;
+ if (controller.Buttons.BigButton == ButtonState.Pressed)
+ yield return SButton.BigButton;
+ if (controller.Buttons.LeftShoulder == ButtonState.Pressed)
+ yield return SButton.LeftShoulder;
+ if (controller.Buttons.LeftStick == ButtonState.Pressed)
+ yield return SButton.LeftStick;
+ if (controller.Buttons.RightShoulder == ButtonState.Pressed)
+ yield return SButton.RightShoulder;
+ if (controller.Buttons.RightStick == ButtonState.Pressed)
+ yield return SButton.RightStick;
+ if (controller.Buttons.Start == ButtonState.Pressed)
+ yield return SButton.ControllerStart;
+ if (controller.Buttons.X == ButtonState.Pressed)
+ yield return SButton.ControllerX;
+ if (controller.Buttons.Y == ButtonState.Pressed)
+ yield return SButton.ControllerY;
+ 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;
+ if (controller.Triggers.Left > 0.2f)
+ yield return SButton.LeftTrigger;
+ if (controller.Triggers.Right > 0.2f)
+ yield return SButton.RightTrigger;
}
- return buttons.ToArray();
- }
-
- /// <summary>Get the controller buttons which were pressed after the last update.</summary>
- private Buttons[] GetFramePressedButtons()
- {
- var state = GamePad.GetState(PlayerIndex.One);
- var buttons = new List<Buttons>();
- if (state.IsConnected)
- {
- if (this.WasButtonJustPressed(Buttons.A, state.Buttons.A)) buttons.Add(Buttons.A);
- if (this.WasButtonJustPressed(Buttons.B, state.Buttons.B)) buttons.Add(Buttons.B);
- if (this.WasButtonJustPressed(Buttons.Back, state.Buttons.Back)) buttons.Add(Buttons.Back);
- if (this.WasButtonJustPressed(Buttons.BigButton, state.Buttons.BigButton)) buttons.Add(Buttons.BigButton);
- if (this.WasButtonJustPressed(Buttons.LeftShoulder, state.Buttons.LeftShoulder)) buttons.Add(Buttons.LeftShoulder);
- if (this.WasButtonJustPressed(Buttons.LeftStick, state.Buttons.LeftStick)) buttons.Add(Buttons.LeftStick);
- if (this.WasButtonJustPressed(Buttons.RightShoulder, state.Buttons.RightShoulder)) buttons.Add(Buttons.RightShoulder);
- if (this.WasButtonJustPressed(Buttons.RightStick, state.Buttons.RightStick)) buttons.Add(Buttons.RightStick);
- if (this.WasButtonJustPressed(Buttons.Start, state.Buttons.Start)) buttons.Add(Buttons.Start);
- if (this.WasButtonJustPressed(Buttons.X, state.Buttons.X)) buttons.Add(Buttons.X);
- if (this.WasButtonJustPressed(Buttons.Y, state.Buttons.Y)) buttons.Add(Buttons.Y);
- if (this.WasButtonJustPressed(Buttons.DPadUp, state.DPad.Up)) buttons.Add(Buttons.DPadUp);
- if (this.WasButtonJustPressed(Buttons.DPadDown, state.DPad.Down)) buttons.Add(Buttons.DPadDown);
- if (this.WasButtonJustPressed(Buttons.DPadLeft, state.DPad.Left)) buttons.Add(Buttons.DPadLeft);
- if (this.WasButtonJustPressed(Buttons.DPadRight, state.DPad.Right)) buttons.Add(Buttons.DPadRight);
- if (this.WasButtonJustPressed(Buttons.LeftTrigger, state.Triggers.Left)) buttons.Add(Buttons.LeftTrigger);
- if (this.WasButtonJustPressed(Buttons.RightTrigger, state.Triggers.Right)) buttons.Add(Buttons.RightTrigger);
- }
- return buttons.ToArray();
- }
-
- /// <summary>Get the controller buttons which were released after the last update.</summary>
- private Buttons[] GetFrameReleasedButtons()
- {
- var state = GamePad.GetState(PlayerIndex.One);
- var buttons = new List<Buttons>();
- if (state.IsConnected)
- {
- if (this.WasButtonJustReleased(Buttons.A, state.Buttons.A)) buttons.Add(Buttons.A);
- if (this.WasButtonJustReleased(Buttons.B, state.Buttons.B)) buttons.Add(Buttons.B);
- if (this.WasButtonJustReleased(Buttons.Back, state.Buttons.Back)) buttons.Add(Buttons.Back);
- if (this.WasButtonJustReleased(Buttons.BigButton, state.Buttons.BigButton)) buttons.Add(Buttons.BigButton);
- if (this.WasButtonJustReleased(Buttons.LeftShoulder, state.Buttons.LeftShoulder)) buttons.Add(Buttons.LeftShoulder);
- if (this.WasButtonJustReleased(Buttons.LeftStick, state.Buttons.LeftStick)) buttons.Add(Buttons.LeftStick);
- if (this.WasButtonJustReleased(Buttons.RightShoulder, state.Buttons.RightShoulder)) buttons.Add(Buttons.RightShoulder);
- if (this.WasButtonJustReleased(Buttons.RightStick, state.Buttons.RightStick)) buttons.Add(Buttons.RightStick);
- if (this.WasButtonJustReleased(Buttons.Start, state.Buttons.Start)) buttons.Add(Buttons.Start);
- if (this.WasButtonJustReleased(Buttons.X, state.Buttons.X)) buttons.Add(Buttons.X);
- if (this.WasButtonJustReleased(Buttons.Y, state.Buttons.Y)) buttons.Add(Buttons.Y);
- if (this.WasButtonJustReleased(Buttons.DPadUp, state.DPad.Up)) buttons.Add(Buttons.DPadUp);
- if (this.WasButtonJustReleased(Buttons.DPadDown, state.DPad.Down)) buttons.Add(Buttons.DPadDown);
- if (this.WasButtonJustReleased(Buttons.DPadLeft, state.DPad.Left)) buttons.Add(Buttons.DPadLeft);
- if (this.WasButtonJustReleased(Buttons.DPadRight, state.DPad.Right)) buttons.Add(Buttons.DPadRight);
- if (this.WasButtonJustReleased(Buttons.LeftTrigger, state.Triggers.Left)) buttons.Add(Buttons.LeftTrigger);
- if (this.WasButtonJustReleased(Buttons.RightTrigger, state.Triggers.Right)) buttons.Add(Buttons.RightTrigger);
- }
- return buttons.ToArray();
- }
-
- /// <summary>Get whether a controller button was pressed since the last check.</summary>
- /// <param name="button">The controller button to check.</param>
- /// <param name="buttonState">The last known state.</param>
- private bool WasButtonJustPressed(Buttons button, ButtonState buttonState)
- {
- return buttonState == ButtonState.Pressed && !this.PreviousPressedButtons.Contains(button);
- }
-
- /// <summary>Get whether a controller button was released since the last check.</summary>
- /// <param name="button">The controller button to check.</param>
- /// <param name="buttonState">The last known state.</param>
- private bool WasButtonJustReleased(Buttons button, ButtonState buttonState)
- {
- return buttonState == ButtonState.Released && this.PreviousPressedButtons.Contains(button);
- }
-
- /// <summary>Get whether an analogue controller button was pressed since the last check.</summary>
- /// <param name="button">The controller button to check.</param>
- /// <param name="value">The last known value.</param>
- private bool WasButtonJustPressed(Buttons button, float value)
- {
- return this.WasButtonJustPressed(button, value > 0.2f ? ButtonState.Pressed : ButtonState.Released);
- }
-
- /// <summary>Get whether an analogue controller button was released since the last check.</summary>
- /// <param name="button">The controller button to check.</param>
- /// <param name="value">The last known value.</param>
- private bool WasButtonJustReleased(Buttons button, float value)
- {
- return this.WasButtonJustReleased(button, value > 0.2f ? ButtonState.Pressed : ButtonState.Released);
}
/// <summary>Get the player inventory changes between two states.</summary>
diff --git a/src/StardewModdingAPI/ICursorPosition.cs b/src/StardewModdingAPI/ICursorPosition.cs
new file mode 100644
index 00000000..d03cda71
--- /dev/null
+++ b/src/StardewModdingAPI/ICursorPosition.cs
@@ -0,0 +1,19 @@
+#if SMAPI_2_0
+using Microsoft.Xna.Framework;
+
+namespace StardewModdingAPI
+{
+ /// <summary>Represents a cursor position in the different coordinate systems.</summary>
+ public interface ICursorPosition
+ {
+ /// <summary>The pixel position relative to the top-left corner of the visible screen.</summary>
+ Vector2 ScreenPixels { get; }
+
+ /// <summary>The tile position under the cursor relative to the top-left corner of the map.</summary>
+ Vector2 Tile { get; }
+
+ /// <summary>The tile position that the game considers under the cursor for purposes of clicking actions. This may be different than <see cref="Tile"/> if that's too far from the player.</summary>
+ Vector2 GrabTile { get; }
+ }
+}
+#endif
diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj
index bf1c43d1..c442cc8a 100644
--- a/src/StardewModdingAPI/StardewModdingAPI.csproj
+++ b/src/StardewModdingAPI/StardewModdingAPI.csproj
@@ -93,7 +93,9 @@
<Compile Include="Command.cs" />
<Compile Include="ContentSource.cs" />
<Compile Include="Events\ContentEvents.cs" />
+ <Compile Include="Events\EventArgsInput.cs" />
<Compile Include="Events\EventArgsValueChanged.cs" />
+ <Compile Include="Events\InputEvents.cs" />
<Compile Include="Framework\Content\AssetInfo.cs" />
<Compile Include="Framework\Exceptions\SContentLoadException.cs" />
<Compile Include="Framework\Command.cs" />
@@ -207,7 +209,10 @@
<Compile Include="IReflectionHelper.cs" />
<Compile Include="SemanticVersion.cs" />
<Compile Include="Translation.cs" />
+ <Compile Include="ICursorPosition.cs" />
<Compile Include="Utilities\SDate.cs" />
+ <Compile Include="Utilities\SButton.cs" />
+ <Compile Include="Framework\CursorPosition.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config">
diff --git a/src/StardewModdingAPI/Utilities/SButton.cs b/src/StardewModdingAPI/Utilities/SButton.cs
new file mode 100644
index 00000000..f4fccfff
--- /dev/null
+++ b/src/StardewModdingAPI/Utilities/SButton.cs
@@ -0,0 +1,659 @@
+using System;
+using Microsoft.Xna.Framework.Input;
+
+namespace StardewModdingAPI.Utilities
+{
+ /// <summary>A unified button constant which includes all controller, keyboard, and mouse buttons.</summary>
+ /// <remarks>Derived from <see cref="Keys"/>, <see cref="Buttons"/>, and <see cref="System.Windows.Forms.MouseButtons"/>.</remarks>
+#if SMAPI_2_0
+ public
+#else
+ internal
+#endif
+ enum SButton
+ {
+ /// <summary>No valid key.</summary>
+ None = 0,
+
+ /*********
+ ** Mouse
+ *********/
+ /// <summary>The left mouse button.</summary>
+ MouseLeft = 1000,
+
+ /// <summary>The right mouse button.</summary>
+ MouseRight = 1001,
+
+ /// <summary>The middle mouse button.</summary>
+ MouseMiddle = 1002,
+
+ /// <summary>The first mouse XButton.</summary>
+ MouseX1 = 1003,
+
+ /// <summary>The second mouse XButton.</summary>
+ MouseX2 = 1004,
+
+ /*********
+ ** Controller
+ *********/
+ /// <summary>The 'A' button on a controller.</summary>
+ ControllerA = SButtonExtensions.ControllerOffset + Buttons.A,
+
+ /// <summary>The 'B' button on a controller.</summary>
+ ControllerB = SButtonExtensions.ControllerOffset + Buttons.B,
+
+ /// <summary>The 'X' button on a controller.</summary>
+ ControllerX = SButtonExtensions.ControllerOffset + Buttons.X,
+
+ /// <summary>The 'Y' button on a controller.</summary>
+ ControllerY = SButtonExtensions.ControllerOffset + Buttons.Y,
+
+ /// <summary>The back button on a controller.</summary>
+ ControllerBack = SButtonExtensions.ControllerOffset + Buttons.Back,
+
+ /// <summary>The start button on a controller.</summary>
+ ControllerStart = SButtonExtensions.ControllerOffset + Buttons.Start,
+
+ /// <summary>The up button on the directional pad of a controller.</summary>
+ DPadUp = SButtonExtensions.ControllerOffset + Buttons.DPadUp,
+
+ /// <summary>The down button on the directional pad of a controller.</summary>
+ DPadDown = SButtonExtensions.ControllerOffset + Buttons.DPadDown,
+
+ /// <summary>The left button on the directional pad of a controller.</summary>
+ DPadLeft = SButtonExtensions.ControllerOffset + Buttons.DPadLeft,
+
+ /// <summary>The right button on the directional pad of a controller.</summary>
+ DPadRight = SButtonExtensions.ControllerOffset + Buttons.DPadRight,
+
+ /// <summary>The left bumper (shoulder) button on a controller.</summary>
+ LeftShoulder = SButtonExtensions.ControllerOffset + Buttons.LeftShoulder,
+
+ /// <summary>The right bumper (shoulder) button on a controller.</summary>
+ RightShoulder = SButtonExtensions.ControllerOffset + Buttons.RightShoulder,
+
+ /// <summary>The left trigger on a controller.</summary>
+ LeftTrigger = SButtonExtensions.ControllerOffset + Buttons.LeftTrigger,
+
+ /// <summary>The right trigger on a controller.</summary>
+ RightTrigger = SButtonExtensions.ControllerOffset + Buttons.RightTrigger,
+
+ /// <summary>The left analog stick on a controller (when pressed).</summary>
+ LeftStick = SButtonExtensions.ControllerOffset + Buttons.LeftStick,
+
+ /// <summary>The right analog stick on a controller (when pressed).</summary>
+ RightStick = SButtonExtensions.ControllerOffset + Buttons.RightStick,
+
+ /// <summary>The 'big button' on a controller.</summary>
+ BigButton = SButtonExtensions.ControllerOffset + Buttons.BigButton,
+
+ /// <summary>The left analog stick on a controller (when pushed left).</summary>
+ LeftThumbstickLeft = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickLeft,
+
+ /// <summary>The left analog stick on a controller (when pushed right).</summary>
+ LeftThumbstickRight = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickRight,
+
+ /// <summary>The left analog stick on a controller (when pushed down).</summary>
+ LeftThumbstickDown = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickDown,
+
+ /// <summary>The left analog stick on a controller (when pushed up).</summary>
+ LeftThumbstickUp = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickUp,
+
+ /// <summary>The right analog stick on a controller (when pushed left).</summary>
+ RightThumbstickLeft = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickLeft,
+
+ /// <summary>The right analog stick on a controller (when pushed right).</summary>
+ RightThumbstickRight = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickRight,
+
+ /// <summary>The right analog stick on a controller (when pushed down).</summary>
+ RightThumbstickDown = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickDown,
+
+ /// <summary>The right analog stick on a controller (when pushed up).</summary>
+ RightThumbstickUp = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickUp,
+
+ /*********
+ ** Keyboard
+ *********/
+ /// <summary>The A button on a keyboard.</summary>
+ A = Keys.A,
+
+ /// <summary>The Add button on a keyboard.</summary>
+ Add = Keys.Add,
+
+ /// <summary>The Applications button on a keyboard.</summary>
+ Apps = Keys.Apps,
+
+ /// <summary>The Attn button on a keyboard.</summary>
+ Attn = Keys.Attn,
+
+ /// <summary>The B button on a keyboard.</summary>
+ B = Keys.B,
+
+ /// <summary>The Backspace button on a keyboard.</summary>
+ Back = Keys.Back,
+
+ /// <summary>The Browser Back button on a keyboard in Windows 2000/XP.</summary>
+ BrowserBack = Keys.BrowserBack,
+
+ /// <summary>The Browser Favorites button on a keyboard in Windows 2000/XP.</summary>
+ BrowserFavorites = Keys.BrowserFavorites,
+
+ /// <summary>The Browser Favorites button on a keyboard in Windows 2000/XP.</summary>
+ BrowserForward = Keys.BrowserForward,
+
+ /// <summary>The Browser Home button on a keyboard in Windows 2000/XP.</summary>
+ BrowserHome = Keys.BrowserHome,
+
+ /// <summary>The Browser Refresh button on a keyboard in Windows 2000/XP.</summary>
+ BrowserRefresh = Keys.BrowserRefresh,
+
+ /// <summary>The Browser Search button on a keyboard in Windows 2000/XP.</summary>
+ BrowserSearch = Keys.BrowserSearch,
+
+ /// <summary>The Browser Stop button on a keyboard in Windows 2000/XP.</summary>
+ BrowserStop = Keys.BrowserStop,
+
+ /// <summary>The C button on a keyboard.</summary>
+ C = Keys.C,
+
+ /// <summary>The Caps Lock button on a keyboard.</summary>
+ CapsLock = Keys.CapsLock,
+
+ /// <summary>The Green ChatPad button on a keyboard.</summary>
+ ChatPadGreen = Keys.ChatPadGreen,
+
+ /// <summary>The Orange ChatPad button on a keyboard.</summary>
+ ChatPadOrange = Keys.ChatPadOrange,
+
+ /// <summary>The CrSel button on a keyboard.</summary>
+ Crsel = Keys.Crsel,
+
+ /// <summary>The D button on a keyboard.</summary>
+ D = Keys.D,
+
+ /// <summary>A miscellaneous button on a keyboard; can vary by keyboard.</summary>
+ D0 = Keys.D0,
+
+ /// <summary>A miscellaneous button on a keyboard; can vary by keyboard.</summary>
+ D1 = Keys.D1,
+
+ /// <summary>A miscellaneous button on a keyboard; can vary by keyboard.</summary>
+ D2 = Keys.D2,
+
+ /// <summary>A miscellaneous button on a keyboard; can vary by keyboard.</summary>
+ D3 = Keys.D3,
+
+ /// <summary>A miscellaneous button on a keyboard; can vary by keyboard.</summary>
+ D4 = Keys.D4,
+
+ /// <summary>A miscellaneous button on a keyboard; can vary by keyboard.</summary>
+ D5 = Keys.D5,
+
+ /// <summary>A miscellaneous button on a keyboard; can vary by keyboard.</summary>
+ D6 = Keys.D6,
+
+ /// <summary>A miscellaneous button on a keyboard; can vary by keyboard.</summary>
+ D7 = Keys.D7,
+
+ /// <summary>A miscellaneous button on a keyboard; can vary by keyboard.</summary>
+ D8 = Keys.D8,
+
+ /// <summary>A miscellaneo