From 9b22f3e004b35f66d9be6af211f20fe126fae209 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 31 Jul 2017 23:48:53 -0400 Subject: fix GraphicsEvents.Resize being raised before the game updates its window data (#328) --- src/StardewModdingAPI/Events/GraphicsEvents.cs | 6 ++---- src/StardewModdingAPI/Framework/SGame.cs | 23 ++++++++++++++++++----- src/StardewModdingAPI/Program.cs | 1 - 3 files changed, 20 insertions(+), 10 deletions(-) (limited to 'src/StardewModdingAPI') diff --git a/src/StardewModdingAPI/Events/GraphicsEvents.cs b/src/StardewModdingAPI/Events/GraphicsEvents.cs index 25b976f1..fff51bed 100644 --- a/src/StardewModdingAPI/Events/GraphicsEvents.cs +++ b/src/StardewModdingAPI/Events/GraphicsEvents.cs @@ -51,11 +51,9 @@ namespace StardewModdingAPI.Events ****/ /// Raise a event. /// Encapsulates monitoring and logging. - /// The object which raised the event. - /// The event arguments. - internal static void InvokeResize(IMonitor monitor, object sender, EventArgs e) + internal static void InvokeResize(IMonitor monitor) { - monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.Resize)}", GraphicsEvents.Resize?.GetInvocationList(), sender, e); + monitor.SafelyRaisePlainEvent($"{nameof(GraphicsEvents)}.{nameof(GraphicsEvents.Resize)}", GraphicsEvents.Resize?.GetInvocationList()); } /**** diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs index bec6538b..65191931 100644 --- a/src/StardewModdingAPI/Framework/SGame.cs +++ b/src/StardewModdingAPI/Framework/SGame.cs @@ -53,10 +53,6 @@ namespace StardewModdingAPI.Framework /// Whether the game is saving and SMAPI has already raised . private bool IsBetweenSaveEvents; - /// Whether the game's zoom level is at 100% (i.e. nothing should be scaled). - public bool ZoomLevelIsOne => Game1.options.zoomLevel.Equals(1.0f); - - /**** ** Game state ****/ @@ -75,7 +71,10 @@ namespace StardewModdingAPI.Framework /// The previous mouse position on the screen adjusted for the zoom level. private Point PreviousMousePosition; - /// The previous save ID at last check. + /// The window size value at last check. + private Point PreviousWindowSize; + + /// The save ID at last check. private ulong PreviousSaveID; /// A hash of at last check. @@ -352,6 +351,20 @@ namespace StardewModdingAPI.Framework SaveEvents.InvokeAfterReturnToTitle(this.Monitor); } + /********* + ** Window events + *********/ + // Here we depend on the game's viewport instead of listening to the Window.Resize + // event because we need to notify mods after the game handles the resize, so the + // game's metadata (like Game1.viewport) are updated. That's a bit complicated + // since the game adds & removes its own handler on the fly. + if (Game1.viewport.Width != this.PreviousWindowSize.X || Game1.viewport.Height != this.PreviousWindowSize.Y) + { + Point size = new Point(Game1.viewport.Width, Game1.viewport.Height); + GraphicsEvents.InvokeResize(this.Monitor); + this.PreviousWindowSize = size; + } + /********* ** Input events (if window has focus) *********/ diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index b51917d9..0e1930ac 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -185,7 +185,6 @@ namespace StardewModdingAPI ((Form)Control.FromHandle(this.GameInstance.Window.Handle)).FormClosing += (sender, args) => this.Dispose(); #endif this.GameInstance.Exiting += (sender, e) => this.Dispose(); - this.GameInstance.Window.ClientSizeChanged += (sender, e) => GraphicsEvents.InvokeResize(this.Monitor, sender, e); GameEvents.InitializeInternal += (sender, e) => this.InitialiseAfterGameStart(); GameEvents.GameLoadedInternal += (sender, e) => this.CheckForUpdateAsync(); ContentEvents.AfterLocaleChanged += (sender, e) => this.OnLocaleChanged(); -- cgit