use value watcher for cursor position (#310)

This commit is contained in:
Jesse Plamondon-Willard 2018-06-02 03:00:39 -04:00
parent 6f931aa576
commit 74971f5328
1 changed files with 17 additions and 13 deletions

View File

@ -100,6 +100,9 @@ namespace StardewModdingAPI.Framework
/// <summary>Tracks changes to <see cref="Game1.activeClickableMenu"/>.</summary> /// <summary>Tracks changes to <see cref="Game1.activeClickableMenu"/>.</summary>
private readonly IValueWatcher<IClickableMenu> ActiveMenuWatcher; private readonly IValueWatcher<IClickableMenu> ActiveMenuWatcher;
/// <summary>Tracks changes to the cursor position.</summary>
private readonly IValueWatcher<Point> CursorWatcher;
/// <summary>The previous content locale.</summary> /// <summary>The previous content locale.</summary>
private LocalizedContentManager.LanguageCode? PreviousLocale; private LocalizedContentManager.LanguageCode? PreviousLocale;
@ -168,6 +171,7 @@ namespace StardewModdingAPI.Framework
// init watchers // init watchers
Game1.locations = new ObservableCollection<GameLocation>(); Game1.locations = new ObservableCollection<GameLocation>();
this.CursorWatcher = WatcherFactory.ForEquatable(() => this.Input.MousePosition);
this.SaveIdWatcher = WatcherFactory.ForEquatable(() => Game1.hasLoadedGame ? Game1.uniqueIDForThisGame : 0); this.SaveIdWatcher = WatcherFactory.ForEquatable(() => Game1.hasLoadedGame ? Game1.uniqueIDForThisGame : 0);
this.WindowSizeWatcher = WatcherFactory.ForEquatable(() => new Point(Game1.viewport.Width, Game1.viewport.Height)); this.WindowSizeWatcher = WatcherFactory.ForEquatable(() => new Point(Game1.viewport.Width, Game1.viewport.Height));
this.TimeWatcher = WatcherFactory.ForEquatable(() => Game1.timeOfDay); this.TimeWatcher = WatcherFactory.ForEquatable(() => Game1.timeOfDay);
@ -175,6 +179,7 @@ namespace StardewModdingAPI.Framework
this.LocationsWatcher = new WorldLocationsTracker((ObservableCollection<GameLocation>)Game1.locations); this.LocationsWatcher = new WorldLocationsTracker((ObservableCollection<GameLocation>)Game1.locations);
this.Watchers.AddRange(new IWatcher[] this.Watchers.AddRange(new IWatcher[]
{ {
this.CursorWatcher,
this.SaveIdWatcher, this.SaveIdWatcher,
this.WindowSizeWatcher, this.WindowSizeWatcher,
this.TimeWatcher, this.TimeWatcher,
@ -443,25 +448,24 @@ namespace StardewModdingAPI.Framework
if (!isChatInput) if (!isChatInput)
{ {
// get cursor position // get cursor position
ICursorPosition cursor; ICursorPosition cursor = this.PreviousCursorPosition;
if (this.CursorWatcher.IsChanged)
{ {
// cursor position // cursor position
Vector2 screenPixels = new Vector2(Game1.getMouseX(), Game1.getMouseY()); Vector2 screenPixels = new Vector2(this.CursorWatcher.CurrentValue.X, this.CursorWatcher.CurrentValue.Y);
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
Vector2 tile = new Vector2((int)((Game1.viewport.X + screenPixels.X) / Game1.tileSize), (int)((Game1.viewport.Y + screenPixels.Y) / Game1.tileSize)); ? tile
Vector2 grabTile = (Game1.mouseCursorTransparency > 0 && Utility.tileWithinRadiusOfPlayer((int)tile.X, (int)tile.Y, 1, Game1.player)) // derived from Game1.pressActionButton : Game1.player.GetGrabTile();
? tile cursor = new CursorPosition(screenPixels, tile, grabTile);
: Game1.player.GetGrabTile();
cursor = new CursorPosition(screenPixels, tile, grabTile);
}
else
cursor = this.PreviousCursorPosition;
} }
// raise cursor moved event // 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.Events.Input_CursorMoved.Raise(new InputCursorMovedArgsInput(this.PreviousCursorPosition, cursor));
}
this.PreviousCursorPosition = cursor; this.PreviousCursorPosition = cursor;
// raise input button events // raise input button events