add Input.CursorMoved event (#310)

This commit is contained in:
Jesse Plamondon-Willard 2018-06-02 02:35:26 -04:00
parent 0df7a967a6
commit 6f931aa576
7 changed files with 81 additions and 23 deletions

View File

@ -24,14 +24,14 @@ namespace StardewModdingAPI.Events
remove => ControlEvents.EventManager.Legacy_Control_KeyboardChanged.Remove(value);
}
/// <summary>Raised when the player presses a keyboard key.</summary>
/// <summary>Raised after the player presses a keyboard key.</summary>
public static event EventHandler<EventArgsKeyPressed> KeyPressed
{
add => ControlEvents.EventManager.Legacy_Control_KeyPressed.Add(value);
remove => ControlEvents.EventManager.Legacy_Control_KeyPressed.Remove(value);
}
/// <summary>Raised when the player releases a keyboard key.</summary>
/// <summary>Raised after the player releases a keyboard key.</summary>
public static event EventHandler<EventArgsKeyPressed> KeyReleased
{
add => ControlEvents.EventManager.Legacy_Control_KeyReleased.Add(value);
@ -41,8 +41,8 @@ namespace StardewModdingAPI.Events
/// <summary>Raised when the <see cref="MouseState"/> changes. That happens when the player moves the mouse, scrolls the mouse wheel, or presses/releases a button.</summary>
public static event EventHandler<EventArgsMouseStateChanged> MouseChanged
{
add => ControlEvents.EventManager.Control_MouseChanged.Add(value);
remove => ControlEvents.EventManager.Control_MouseChanged.Remove(value);
add => ControlEvents.EventManager.Legacy_Control_MouseChanged.Add(value);
remove => ControlEvents.EventManager.Legacy_Control_MouseChanged.Remove(value);
}
/// <summary>The player pressed a controller button. This event isn't raised for trigger buttons.</summary>

View File

@ -5,10 +5,13 @@ namespace StardewModdingAPI.Events
/// <summary>Events raised when the player provides input using a controller, keyboard, or mouse.</summary>
public interface IInputEvents
{
/// <summary>Raised when the player presses a button on the keyboard, controller, or mouse.</summary>
/// <summary>Raised after the player presses a button on the keyboard, controller, or mouse.</summary>
event EventHandler<InputButtonPressedArgsInput> ButtonPressed;
/// <summary>Raised when the player releases a button on the keyboard, controller, or mouse.</summary>
/// <summary>Raised after the player releases a button on the keyboard, controller, or mouse.</summary>
event EventHandler<InputButtonReleasedArgsInput> ButtonReleased;
/// <summary>Raised after the player moves the in-game cursor.</summary>
event EventHandler<InputCursorMovedArgsInput> CursorMoved;
}
}

View File

@ -0,0 +1,30 @@
using System;
namespace StardewModdingAPI.Events
{
/// <summary>Event arguments when the in-game cursor is moved.</summary>
public class InputCursorMovedArgsInput : EventArgs
{
/*********
** Accessors
*********/
/// <summary>The previous cursor position.</summary>
public ICursorPosition OldPosition { get; }
/// <summary>The current cursor position.</summary>
public ICursorPosition NewPosition { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="oldPosition">The previous cursor position.</param>
/// <param name="newPosition">The new cursor position.</param>
public InputCursorMovedArgsInput(ICursorPosition oldPosition, ICursorPosition newPosition)
{
this.OldPosition = oldPosition;
this.NewPosition = newPosition;
}
}
}

View File

@ -35,12 +35,15 @@ namespace StardewModdingAPI.Framework.Events
/****
** Input
****/
/// <summary>Raised when the player presses a button on the keyboard, controller, or mouse.</summary>
/// <summary>Raised after the player presses a button on the keyboard, controller, or mouse.</summary>
public readonly ManagedEvent<InputButtonPressedArgsInput> Input_ButtonPressed;
/// <summary>Raised when the player released a button on the keyboard, controller, or mouse.</summary>
/// <summary>Raised after the player released a button on the keyboard, controller, or mouse.</summary>
public readonly ManagedEvent<InputButtonReleasedArgsInput> Input_ButtonReleased;
/// <summary>Raised after the player moves the in-game cursor.</summary>
public readonly ManagedEvent<InputCursorMovedArgsInput> Input_CursorMoved;
/*********
** Events (old)
@ -57,14 +60,14 @@ namespace StardewModdingAPI.Framework.Events
/// <summary>Raised when the <see cref="KeyboardState"/> changes. That happens when the player presses or releases a key.</summary>
public readonly ManagedEvent<EventArgsKeyboardStateChanged> Legacy_Control_KeyboardChanged;
/// <summary>Raised when the player presses a keyboard key.</summary>
/// <summary>Raised after the player presses a keyboard key.</summary>
public readonly ManagedEvent<EventArgsKeyPressed> Legacy_Control_KeyPressed;
/// <summary>Raised when the player releases a keyboard key.</summary>
/// <summary>Raised after the player releases a keyboard key.</summary>
public readonly ManagedEvent<EventArgsKeyPressed> Legacy_Control_KeyReleased;
/// <summary>Raised when the <see cref="MouseState"/> changes. That happens when the player moves the mouse, scrolls the mouse wheel, or presses/releases a button.</summary>
public readonly ManagedEvent<EventArgsMouseStateChanged> Control_MouseChanged;
public readonly ManagedEvent<EventArgsMouseStateChanged> Legacy_Control_MouseChanged;
/// <summary>The player pressed a controller button. This event isn't raised for trigger buttons.</summary>
public readonly ManagedEvent<EventArgsControllerButtonPressed> Legacy_Control_ControllerButtonPressed;
@ -132,10 +135,10 @@ namespace StardewModdingAPI.Framework.Events
/****
** InputEvents
****/
/// <summary>Raised when the player presses a button on the keyboard, controller, or mouse.</summary>
/// <summary>Raised after the player presses a button on the keyboard, controller, or mouse.</summary>
public readonly ManagedEvent<EventArgsInput> Legacy_Input_ButtonPressed;
/// <summary>Raised when the player releases a keyboard key on the keyboard, controller, or mouse.</summary>
/// <summary>Raised after the player releases a keyboard key on the keyboard, controller, or mouse.</summary>
public readonly ManagedEvent<EventArgsInput> Legacy_Input_ButtonReleased;
/****
@ -245,6 +248,7 @@ namespace StardewModdingAPI.Framework.Events
// init events (new)
this.Input_ButtonPressed = ManageEventOf<InputButtonPressedArgsInput>(nameof(IModEvents.Input), nameof(IInputEvents.ButtonPressed));
this.Input_ButtonReleased = ManageEventOf<InputButtonReleasedArgsInput>(nameof(IModEvents.Input), nameof(IInputEvents.ButtonReleased));
this.Input_CursorMoved = ManageEventOf<InputCursorMovedArgsInput>(nameof(IModEvents.Input), nameof(IInputEvents.CursorMoved));
this.World_BuildingListChanged = ManageEventOf<WorldBuildingListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.LocationListChanged));
this.World_LargeTerrainFeatureListChanged = ManageEventOf<WorldLargeTerrainFeatureListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.LargeTerrainFeatureListChanged));
@ -263,7 +267,7 @@ namespace StardewModdingAPI.Framework.Events
this.Legacy_Control_KeyboardChanged = ManageEventOf<EventArgsKeyboardStateChanged>(nameof(ControlEvents), nameof(ControlEvents.KeyboardChanged));
this.Legacy_Control_KeyPressed = ManageEventOf<EventArgsKeyPressed>(nameof(ControlEvents), nameof(ControlEvents.KeyPressed));
this.Legacy_Control_KeyReleased = ManageEventOf<EventArgsKeyPressed>(nameof(ControlEvents), nameof(ControlEvents.KeyReleased));
this.Control_MouseChanged = ManageEventOf<EventArgsMouseStateChanged>(nameof(ControlEvents), nameof(ControlEvents.MouseChanged));
this.Legacy_Control_MouseChanged = ManageEventOf<EventArgsMouseStateChanged>(nameof(ControlEvents), nameof(ControlEvents.MouseChanged));
this.Game_FirstUpdateTick = ManageEvent(nameof(GameEvents), nameof(GameEvents.FirstUpdateTick));
this.Game_UpdateTick = ManageEvent(nameof(GameEvents), nameof(GameEvents.UpdateTick));

View File

@ -9,20 +9,27 @@ namespace StardewModdingAPI.Framework.Events
/*********
** Accessors
*********/
/// <summary>Raised when the player presses a button on the keyboard, controller, or mouse.</summary>
/// <summary>Raised after the player presses a button on the keyboard, controller, or mouse.</summary>
public event EventHandler<InputButtonPressedArgsInput> ButtonPressed
{
add => this.EventManager.Input_ButtonPressed.Add(value);
remove => this.EventManager.Input_ButtonPressed.Remove(value);
}
/// <summary>Raised when the player releases a button on the keyboard, controller, or mouse.</summary>
/// <summary>Raised after the player releases a button on the keyboard, controller, or mouse.</summary>
public event EventHandler<InputButtonReleasedArgsInput> ButtonReleased
{
add => this.EventManager.Input_ButtonReleased.Add(value);
remove => this.EventManager.Input_ButtonReleased.Remove(value);
}
/// <summary>Raised after the player moves the in-game cursor.</summary>
public event EventHandler<InputCursorMovedArgsInput> CursorMoved
{
add => this.EventManager.Input_CursorMoved.Add(value);
remove => this.EventManager.Input_CursorMoved.Remove(value);
}
/*********
** Public methods

View File

@ -103,6 +103,9 @@ namespace StardewModdingAPI.Framework
/// <summary>The previous content locale.</summary>
private LocalizedContentManager.LanguageCode? PreviousLocale;
/// <summary>The previous cursor position.</summary>
private ICursorPosition PreviousCursorPosition;
/// <summary>An index incremented on every tick and reset every 60th tick (059).</summary>
private int CurrentUpdateTick;
@ -444,14 +447,24 @@ namespace StardewModdingAPI.Framework
{
// cursor position
Vector2 screenPixels = new Vector2(Game1.getMouseX(), Game1.getMouseY());
Vector2 tile = new Vector2((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();
cursor = new CursorPosition(screenPixels, tile, grabTile);
if (this.PreviousCursorPosition == null || screenPixels != this.PreviousCursorPosition.ScreenPixels)
{
Vector2 tile = new Vector2((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();
cursor = new CursorPosition(screenPixels, tile, grabTile);
}
else
cursor = this.PreviousCursorPosition;
}
// raise input events
// raise cursor moved event
if (this.PreviousCursorPosition != null && cursor.ScreenPixels != this.PreviousCursorPosition.ScreenPixels)
this.Events.Input_CursorMoved.Raise(new InputCursorMovedArgsInput(this.PreviousCursorPosition, cursor));
this.PreviousCursorPosition = cursor;
// raise input button events
foreach (var pair in inputState.ActiveButtons)
{
SButton button = pair.Key;
@ -501,7 +514,7 @@ namespace StardewModdingAPI.Framework
if (inputState.RealKeyboard != previousInputState.RealKeyboard)
this.Events.Legacy_Control_KeyboardChanged.Raise(new EventArgsKeyboardStateChanged(previousInputState.RealKeyboard, inputState.RealKeyboard));
if (inputState.RealMouse != previousInputState.RealMouse)
this.Events.Control_MouseChanged.Raise(new EventArgsMouseStateChanged(previousInputState.RealMouse, inputState.RealMouse, previousInputState.MousePosition, inputState.MousePosition));
this.Events.Legacy_Control_MouseChanged.Raise(new EventArgsMouseStateChanged(previousInputState.RealMouse, inputState.RealMouse, previousInputState.MousePosition, inputState.MousePosition));
}
}

View File

@ -85,6 +85,7 @@
<Compile Include="..\..\build\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Events\InputCursorMovedEventArgs.cs" />
<Compile Include="Events\InputButtonReleasedEventArgs.cs" />
<Compile Include="Events\InputButtonPressedEventArgs.cs" />
<Compile Include="Events\EventArgsLocationBuildingsChanged.cs" />