From 90f55a6921ac798e03d6f81240d3a9899544c031 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 2 Jun 2018 12:14:15 -0400 Subject: [PATCH] add mouse scroll event (#310) --- src/SMAPI/Events/IInputEvents.cs | 3 ++ .../InputMouseWheelScrolledEventArgs.cs | 38 +++++++++++++++++++ src/SMAPI/Framework/Events/EventManager.cs | 4 ++ src/SMAPI/Framework/Events/ModInputEvents.cs | 7 ++++ src/SMAPI/Framework/SGame.cs | 15 ++++++++ src/SMAPI/StardewModdingAPI.csproj | 1 + 6 files changed, 68 insertions(+) create mode 100644 src/SMAPI/Events/InputMouseWheelScrolledEventArgs.cs diff --git a/src/SMAPI/Events/IInputEvents.cs b/src/SMAPI/Events/IInputEvents.cs index 938c772b..64d82c57 100644 --- a/src/SMAPI/Events/IInputEvents.cs +++ b/src/SMAPI/Events/IInputEvents.cs @@ -13,5 +13,8 @@ namespace StardewModdingAPI.Events /// Raised after the player moves the in-game cursor. event EventHandler CursorMoved; + + /// Raised after the player scrolls the mouse wheel. + event EventHandler MouseWheelScrolled; } } diff --git a/src/SMAPI/Events/InputMouseWheelScrolledEventArgs.cs b/src/SMAPI/Events/InputMouseWheelScrolledEventArgs.cs new file mode 100644 index 00000000..9afab9cc --- /dev/null +++ b/src/SMAPI/Events/InputMouseWheelScrolledEventArgs.cs @@ -0,0 +1,38 @@ +using System; + +namespace StardewModdingAPI.Events +{ + /// Event arguments when the player scrolls the mouse wheel. + public class InputMouseWheelScrolledEventArgs : EventArgs + { + /********* + ** Accessors + *********/ + /// The cursor position. + public ICursorPosition Position { get; } + + /// The old scroll value. + public int OldValue { get; } + + /// The new scroll value. + public int NewValue { get; } + + /// The amount by which the scroll value changed. + public int Delta => this.NewValue - this.OldValue; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The cursor position. + /// The old scroll value. + /// The new scroll value. + public InputMouseWheelScrolledEventArgs(ICursorPosition position, int oldValue, int newValue) + { + this.Position = position; + this.OldValue = oldValue; + this.NewValue = newValue; + } + } +} diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index eea74587..9f67244a 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -44,6 +44,9 @@ namespace StardewModdingAPI.Framework.Events /// Raised after the player moves the in-game cursor. public readonly ManagedEvent Input_CursorMoved; + /// Raised after the player scrolls the mouse wheel. + public readonly ManagedEvent Input_MouseWheelScrolled; + /********* ** Events (old) @@ -249,6 +252,7 @@ namespace StardewModdingAPI.Framework.Events this.Input_ButtonPressed = ManageEventOf(nameof(IModEvents.Input), nameof(IInputEvents.ButtonPressed)); this.Input_ButtonReleased = ManageEventOf(nameof(IModEvents.Input), nameof(IInputEvents.ButtonReleased)); this.Input_CursorMoved = ManageEventOf(nameof(IModEvents.Input), nameof(IInputEvents.CursorMoved)); + this.Input_MouseWheelScrolled = ManageEventOf(nameof(IModEvents.Input), nameof(IInputEvents.MouseWheelScrolled)); this.World_BuildingListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.LocationListChanged)); this.World_LargeTerrainFeatureListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.LargeTerrainFeatureListChanged)); diff --git a/src/SMAPI/Framework/Events/ModInputEvents.cs b/src/SMAPI/Framework/Events/ModInputEvents.cs index 48dd0369..387ea87a 100644 --- a/src/SMAPI/Framework/Events/ModInputEvents.cs +++ b/src/SMAPI/Framework/Events/ModInputEvents.cs @@ -30,6 +30,13 @@ namespace StardewModdingAPI.Framework.Events remove => this.EventManager.Input_CursorMoved.Remove(value); } + /// Raised after the player scrolls the mouse wheel. + public event EventHandler MouseWheelScrolled + { + add => this.EventManager.Input_MouseWheelScrolled.Add(value); + remove => this.EventManager.Input_MouseWheelScrolled.Remove(value); + } + /********* ** Public methods diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 894a771f..18529728 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -103,6 +103,9 @@ namespace StardewModdingAPI.Framework /// Tracks changes to the cursor position. private readonly IValueWatcher CursorWatcher; + /// Tracks changes to the mouse wheel scroll. + private readonly IValueWatcher MouseWheelScrollWatcher; + /// The previous content locale. private LocalizedContentManager.LanguageCode? PreviousLocale; @@ -172,6 +175,7 @@ namespace StardewModdingAPI.Framework // init watchers Game1.locations = new ObservableCollection(); this.CursorWatcher = WatcherFactory.ForEquatable(() => this.Input.MousePosition); + this.MouseWheelScrollWatcher = WatcherFactory.ForEquatable(() => this.Input.RealMouse.ScrollWheelValue); this.SaveIdWatcher = WatcherFactory.ForEquatable(() => Game1.hasLoadedGame ? Game1.uniqueIDForThisGame : 0); this.WindowSizeWatcher = WatcherFactory.ForEquatable(() => new Point(Game1.viewport.Width, Game1.viewport.Height)); this.TimeWatcher = WatcherFactory.ForEquatable(() => Game1.timeOfDay); @@ -180,6 +184,7 @@ namespace StardewModdingAPI.Framework this.Watchers.AddRange(new IWatcher[] { this.CursorWatcher, + this.MouseWheelScrollWatcher, this.SaveIdWatcher, this.WindowSizeWatcher, this.TimeWatcher, @@ -468,6 +473,16 @@ namespace StardewModdingAPI.Framework } this.PreviousCursorPosition = cursor; + // raise mouse wheel scrolled + if (this.MouseWheelScrollWatcher.IsChanged) + { + int oldValue = this.MouseWheelScrollWatcher.PreviousValue; + int newValue = this.MouseWheelScrollWatcher.CurrentValue; + this.MouseWheelScrollWatcher.Reset(); + + this.Events.Input_MouseWheelScrolled.Raise(new InputMouseWheelScrolledEventArgs(cursor, oldValue, newValue)); + } + // raise input button events foreach (var pair in inputState.ActiveButtons) { diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj index 604ad64f..b81f1359 100644 --- a/src/SMAPI/StardewModdingAPI.csproj +++ b/src/SMAPI/StardewModdingAPI.csproj @@ -85,6 +85,7 @@ Properties\GlobalAssemblyInfo.cs +