Skip to content

Commit

Permalink
Merge pull request #7105 from Jjagg/exit-cancel
Browse files Browse the repository at this point in the history
Support canceling game exit
  • Loading branch information
SimonDarksideJ committed May 19, 2024
2 parents 94c4ada + 59e0b71 commit 8d124c0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 33 deletions.
15 changes: 15 additions & 0 deletions MonoGame.Framework/ExitingEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Microsoft.Xna.Framework
{
/// <summary>
/// Event arguments for <see cref="Game.Exiting"/>.
/// </summary>
public class ExitingEventArgs : EventArgs
{
/// <summary>
/// Set to <c>true</c> to cancel closing the game.
/// </summary>
public bool Cancel { get; set; }
}
}
60 changes: 30 additions & 30 deletions MonoGame.Framework/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public Game()
PlatformConstruct();

}


/// <summary/>
~Game()
{
Expand All @@ -101,15 +101,15 @@ internal void Log(string Message)

#region IDisposable Implementation

private bool _isDisposed;
private bool _isDisposed;
/// <inheritdoc cref="IDisposable.Dispose()"/>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
EventHelpers.Raise(this, Disposed, EventArgs.Empty);
}


/// <summary/>
protected virtual void Dispose(bool disposing)
{
Expand Down Expand Up @@ -195,9 +195,9 @@ public GameComponentCollection Components
{
get { return _components; }
}

/// <summary>
/// Gets or sets time to sleep between frames when the game is not active

/// <summary>
/// Gets or sets time to sleep between frames when the game is not active
/// </summary>
public TimeSpan InactiveSleepTime
{
Expand Down Expand Up @@ -380,7 +380,7 @@ internal bool Initialized
/// <summary>
/// Raised when this game is exiting.
/// </summary>
public event EventHandler<EventArgs> Exiting;
public event EventHandler<ExitingEventArgs> Exiting;

#if WINDOWS_UAP
[CLSCompliant(false)]
Expand Down Expand Up @@ -408,11 +408,11 @@ public void Exit()
/// </summary>
public void ResetElapsedTime()
{
Platform.ResetElapsedTime();
if (_gameTimer != null)
{
_gameTimer.Reset();
_gameTimer.Start();
Platform.ResetElapsedTime();
if (_gameTimer != null)
{
_gameTimer.Reset();
_gameTimer.Start();
}

_accumulatedElapsedTime = TimeSpan.Zero;
Expand Down Expand Up @@ -495,8 +495,6 @@ public void Run(GameRunBehavior runBehavior)
DoUpdate(new GameTime());

Platform.RunLoop();
EndRun();
DoExiting();
break;
default:
throw new ArgumentException(string.Format(
Expand Down Expand Up @@ -540,11 +538,11 @@ public void Tick()
#endif
}

// Advance the accumulated elapsed time.
if (_gameTimer == null)
{
_gameTimer = new Stopwatch();
_gameTimer.Start();
// Advance the accumulated elapsed time.
if (_gameTimer == null)
{
_gameTimer = new Stopwatch();
_gameTimer.Start();
}
var currentTicks = _gameTimer.Elapsed.Ticks;
_accumulatedElapsedTime += TimeSpan.FromTicks(currentTicks - _previousTicks);
Expand Down Expand Up @@ -631,8 +629,18 @@ public void Tick()

if (_shouldExit)
{
Platform.Exit();
_shouldExit = false; //prevents perpetual exiting on platforms supporting resume.
var exitingEventArgs = new ExitingEventArgs();

OnExiting(this, exitingEventArgs);

if (!exitingEventArgs.Cancel)
{
Platform.Exit();
EndRun();
UnloadContent();
}

_shouldExit = false;
}
}

Expand Down Expand Up @@ -743,7 +751,7 @@ protected virtual void Update(GameTime gameTime)
/// </summary>
/// <param name="sender">This <see cref="Game"/>.</param>
/// <param name="args">The arguments to the <see cref="Exiting"/> event.</param>
protected virtual void OnExiting(object sender, EventArgs args)
protected virtual void OnExiting(object sender, ExitingEventArgs args)
{
EventHelpers.Raise(sender, Exiting, args);
}
Expand Down Expand Up @@ -795,8 +803,6 @@ private void Platform_AsyncRunLoopEnded(object sender, EventArgs e)

var platform = (GamePlatform)sender;
platform.AsyncRunLoopEnded -= Platform_AsyncRunLoopEnded;
EndRun();
DoExiting();
}

#endregion Event Handlers
Expand Down Expand Up @@ -871,12 +877,6 @@ internal void DoInitialize()
_components.ComponentRemoved += Components_ComponentRemoved;
}

internal void DoExiting()
{
OnExiting(this, EventArgs.Empty);
UnloadContent();
}

#endregion Internal Methods

internal GraphicsDeviceManager graphicsDeviceManager
Expand Down
4 changes: 2 additions & 2 deletions MonoGame.Framework/Platform/SDL/SDLGamePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void SdlRunLoop()
switch (ev.Type)
{
case Sdl.EventType.Quit:
_isExiting++;
Game.Exit();
break;
case Sdl.EventType.JoyDeviceAdded:
Joystick.AddDevices();
Expand Down Expand Up @@ -241,7 +241,7 @@ private void SdlRunLoop()
_view.Moved();
break;
case Sdl.Window.EventId.Close:
_isExiting++;
Game.Exit();
break;
}
break;
Expand Down
7 changes: 7 additions & 0 deletions MonoGame.Framework/Platform/Windows/WinFormsGameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ internal WinFormsGameWindow(WinFormsGamePlatform platform)
_resizeTickTimer = new System.Timers.Timer(1) { SynchronizingObject = Form, AutoReset = false };
_resizeTickTimer.Elapsed += OnResizeTick;

Form.FormClosing += OnFormClosing;
Form.Activated += OnActivated;
Form.Deactivate += OnDeactivate;
Form.Resize += OnResize;
Expand Down Expand Up @@ -237,6 +238,12 @@ private void UnregisterFromAllWindows()
}
}

private void OnFormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
_platform.Game.Exit();
}

private void OnActivated(object sender, EventArgs eventArgs)
{
_platform.IsActive = true;
Expand Down
2 changes: 1 addition & 1 deletion Tests/Framework/GameTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private class CountCallsGame : Game

protected override void OnActivated(object sender, EventArgs args) { ActivatedCount++; base.OnActivated(sender, args); }
protected override void OnDeactivated(object sender, EventArgs args) { DeactivatedCount++; base.OnDeactivated(sender, args); }
protected override void OnExiting(object sender, EventArgs args) { ExitingCount++; base.OnExiting(sender, args); }
protected override void OnExiting(object sender, ExitingEventArgs args) { ExitingCount++; base.OnExiting(sender, args); }
protected override void Dispose(bool disposing) { DisposeCount++; base.Dispose(disposing); }
}

Expand Down

0 comments on commit 8d124c0

Please sign in to comment.