From 74971f532822b44fbffc99810afd2c0d8a0d424d Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 2 Jun 2018 03:00:39 -0400 Subject: [PATCH] use value watcher for cursor position (#310) --- src/SMAPI/Framework/SGame.cs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index f4e0d3c5..894a771f 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -100,6 +100,9 @@ namespace StardewModdingAPI.Framework /// Tracks changes to . private readonly IValueWatcher ActiveMenuWatcher; + /// Tracks changes to the cursor position. + private readonly IValueWatcher CursorWatcher; + /// The previous content locale. private LocalizedContentManager.LanguageCode? PreviousLocale; @@ -168,6 +171,7 @@ namespace StardewModdingAPI.Framework // init watchers Game1.locations = new ObservableCollection(); + this.CursorWatcher = WatcherFactory.ForEquatable(() => this.Input.MousePosition); 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); @@ -175,6 +179,7 @@ namespace StardewModdingAPI.Framework this.LocationsWatcher = new WorldLocationsTracker((ObservableCollection)Game1.locations); this.Watchers.AddRange(new IWatcher[] { + this.CursorWatcher, this.SaveIdWatcher, this.WindowSizeWatcher, this.TimeWatcher, @@ -443,25 +448,24 @@ namespace StardewModdingAPI.Framework if (!isChatInput) { // get cursor position - ICursorPosition cursor; + ICursorPosition cursor = this.PreviousCursorPosition; + if (this.CursorWatcher.IsChanged) { // cursor position - Vector2 screenPixels = new Vector2(Game1.getMouseX(), Game1.getMouseY()); - 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; + Vector2 screenPixels = new Vector2(this.CursorWatcher.CurrentValue.X, this.CursorWatcher.CurrentValue.Y); + 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); } // raise cursor moved event - if (this.PreviousCursorPosition != null && cursor.ScreenPixels != this.PreviousCursorPosition.ScreenPixels) + if (this.CursorWatcher.IsChanged && this.PreviousCursorPosition != null) + { + this.CursorWatcher.Reset(); this.Events.Input_CursorMoved.Raise(new InputCursorMovedArgsInput(this.PreviousCursorPosition, cursor)); + } this.PreviousCursorPosition = cursor; // raise input button events