From 7d0cd22f1ec0465c666cf0c69cbfa4da5fd3fe1a Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 31 Oct 2016 17:01:16 -0400 Subject: add zoom-adjusted mouse position to mouse-changed event arguments (#129) --- src/StardewModdingAPI/Events/ControlEvents.cs | 6 ++++-- .../Events/EventArgsMouseStateChanged.cs | 18 +++++++++++++++--- src/StardewModdingAPI/Inheritance/SGame.cs | 14 +++++++++++--- 3 files changed, 30 insertions(+), 8 deletions(-) (limited to 'src/StardewModdingAPI') diff --git a/src/StardewModdingAPI/Events/ControlEvents.cs b/src/StardewModdingAPI/Events/ControlEvents.cs index 8fb9061d..1b1e0b78 100644 --- a/src/StardewModdingAPI/Events/ControlEvents.cs +++ b/src/StardewModdingAPI/Events/ControlEvents.cs @@ -49,9 +49,11 @@ namespace StardewModdingAPI.Events /// Raise a event. /// The previous mouse state. /// The current mouse state. - internal static void InvokeMouseChanged(MouseState priorState, MouseState newState) + /// The previous mouse position on the screen adjusted for the zoom level. + /// The current mouse position on the screen adjusted for the zoom level. + internal static void InvokeMouseChanged(MouseState priorState, MouseState newState, Point priorPosition, Point newPosition) { - ControlEvents.MouseChanged.Invoke(null, new EventArgsMouseStateChanged(priorState, newState)); + ControlEvents.MouseChanged.Invoke(null, new EventArgsMouseStateChanged(priorState, newState, priorPosition, newPosition)); } /// Raise a event. diff --git a/src/StardewModdingAPI/Events/EventArgsMouseStateChanged.cs b/src/StardewModdingAPI/Events/EventArgsMouseStateChanged.cs index 9117a0c2..0997ad18 100644 --- a/src/StardewModdingAPI/Events/EventArgsMouseStateChanged.cs +++ b/src/StardewModdingAPI/Events/EventArgsMouseStateChanged.cs @@ -1,5 +1,7 @@ using System; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; +using StardewValley; namespace StardewModdingAPI.Events { @@ -10,10 +12,16 @@ namespace StardewModdingAPI.Events ** Accessors *********/ /// The previous mouse state. - public MouseState NewState { get; private set; } + public MouseState PriorState { get; private set; } /// The current mouse state. - public MouseState PriorState { get; private set; } + public MouseState NewState { get; private set; } + + /// The previous mouse position on the screen adjusted for the zoom level. + public Point PriorPosition { get; private set; } + + /// The current mouse position on the screen adjusted for the zoom level. + public Point NewPosition { get; private set; } /********* @@ -22,10 +30,14 @@ namespace StardewModdingAPI.Events /// Construct an instance. /// The previous mouse state. /// The current mouse state. - public EventArgsMouseStateChanged(MouseState priorState, MouseState newState) + /// The previous mouse position on the screen adjusted for the zoom level. + /// The current mouse position on the screen adjusted for the zoom level. + public EventArgsMouseStateChanged(MouseState priorState, MouseState newState, Point priorPosition, Point newPosition) { this.PriorState = priorState; this.NewState = newState; + this.PriorPosition = priorPosition; + this.NewPosition = newPosition; } } } diff --git a/src/StardewModdingAPI/Inheritance/SGame.cs b/src/StardewModdingAPI/Inheritance/SGame.cs index c595fae3..b0e6bebc 100644 --- a/src/StardewModdingAPI/Inheritance/SGame.cs +++ b/src/StardewModdingAPI/Inheritance/SGame.cs @@ -54,6 +54,12 @@ namespace StardewModdingAPI.Inheritance /// public MouseState MStatePrior { get; private set; } + /// The current mouse position on the screen adjusted for the zoom level. + public Point MPositionNow { get; private set; } + + /// The previous mouse position on the screen adjusted for the zoom level. + public Point MPositionPrior { get; private set; } + /// /// All keys pressed on the current frame /// @@ -261,7 +267,7 @@ namespace StardewModdingAPI.Inheritance { return WasButtonJustPressed(button, value > 0.2f ? ButtonState.Pressed : ButtonState.Released, stateIndex); } - + /// /// Whether or not an analog button was just released on the controller /// @@ -767,7 +773,7 @@ namespace StardewModdingAPI.Inheritance //typeof (Game).GetMethod("Update", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(this, new object[] {gameTime}); //base.Update(gameTime); - + #endregion } else @@ -1403,6 +1409,7 @@ namespace StardewModdingAPI.Inheritance KStateNow = Keyboard.GetState(); MStateNow = Mouse.GetState(); + MPositionNow = new Point(Game1.getMouseX(), Game1.getMouseY()); foreach (var k in FramePressedKeys) ControlEvents.InvokeKeyPressed(k); @@ -1449,8 +1456,9 @@ namespace StardewModdingAPI.Inheritance if (MStateNow != MStatePrior) { - ControlEvents.InvokeMouseChanged(MStatePrior, MStateNow); + ControlEvents.InvokeMouseChanged(MStatePrior, MStateNow, MPositionPrior, MPositionNow); MStatePrior = MStateNow; + MPositionPrior = MPositionPrior; } if (activeClickableMenu != null && activeClickableMenu != PreviousActiveMenu) -- cgit