Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor OrientationListener update #1570

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ enum AppState

bool _lostglContext;

DateTime _prevTickTime;

bool? _isCancellationRequested = null;
private int _frameRequests = 0;

Expand Down Expand Up @@ -135,7 +133,6 @@ internal void StartGameLoop()
// prepare gameLoop
Threading.MakeMainThread();

_prevTickTime = DateTime.Now;
Android.OS.Looper looper = Android.OS.Looper.MainLooper;
_handler = new Android.OS.Handler(looper); // why this.Handler is null? Do we initialize the game too soon?

Expand Down Expand Up @@ -351,21 +348,10 @@ void processStateRunning()
return;
}

AndroidGameWindow.Activity._orientationListener.Update();

try
{
DateTime currTickTime = DateTime.Now;
TimeSpan dt = TimeSpan.Zero;
if (_prevTickTime.Ticks != 0)
{
dt = (currTickTime - _prevTickTime);
if (dt.TotalMilliseconds < 0)
dt = TimeSpan.Zero;
}
_prevTickTime = currTickTime;

try { AndroidGameWindow.Activity._orientationListener.Update(dt); }
catch (Exception) { }

var handler = Tick;
if (handler != null)
handler(this, EventArgs.Empty);
Expand Down
35 changes: 25 additions & 10 deletions MonoGame.Framework/Platform/Android/OrientationListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Microsoft.Xna.Framework
internal class OrientationListener : OrientationEventListener
{
internal DisplayOrientation targetOrientation = DisplayOrientation.Unknown;
DateTime prevTickTime = DateTime.Now;
TimeSpan elapsed = TimeSpan.Zero;

/// <summary>
Expand Down Expand Up @@ -62,20 +63,34 @@ public override void OnOrientationChanged(int orientation)
return;
}

internal void Update(TimeSpan elapsedTime)
{
if (targetOrientation != DisplayOrientation.Unknown)
internal void Update()
{
DateTime currTickTime = DateTime.Now;
TimeSpan elapsedTime = TimeSpan.Zero;
if (prevTickTime.Ticks != 0)
{
elapsedTime = (currTickTime - prevTickTime);
if (elapsedTime.TotalMilliseconds < 0)
elapsedTime = TimeSpan.Zero;
}
prevTickTime = currTickTime;

try
{
elapsed += elapsedTime;
// orientation must be stable for 0.5 seconds before changing.
if (elapsed.TotalSeconds > 0.5)
if (targetOrientation != DisplayOrientation.Unknown)
{
AndroidGameWindow gameWindow = (AndroidGameWindow)ConcreteGame.GameConcreteInstance.Window;
gameWindow.SetOrientation(targetOrientation, true);
targetOrientation = DisplayOrientation.Unknown;
elapsed = TimeSpan.Zero;
elapsed += elapsedTime;
// orientation must be stable for 0.5 seconds before changing.
if (elapsed.TotalSeconds > 0.5)
{
AndroidGameWindow gameWindow = (AndroidGameWindow)ConcreteGame.GameConcreteInstance.Window;
gameWindow.SetOrientation(targetOrientation, true);
targetOrientation = DisplayOrientation.Unknown;
elapsed = TimeSpan.Zero;
}
}
}
catch (Exception) { /* ignore */ }
}
}
}
18 changes: 2 additions & 16 deletions MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ enum AppState

bool _lostglContext;

DateTime _prevTickTime;

bool? _isCancellationRequested = null;

private readonly AndroidGameWindow _gameWindow;
Expand Down Expand Up @@ -144,7 +142,6 @@ internal void StartGameLoop()

// prepare gameLoop
Threading.MakeMainThread();
_prevTickTime = DateTime.Now;
}

volatile bool _isStarted = false;
Expand Down Expand Up @@ -307,21 +304,10 @@ void processStateRunning()
return;
}

AndroidGameWindow.Activity._orientationListener.Update();

try
{
DateTime currTickTime = DateTime.Now;
TimeSpan dt = TimeSpan.Zero;
if (_prevTickTime.Ticks != 0)
{
dt = (currTickTime - _prevTickTime);
if (dt.TotalMilliseconds < 0)
dt = TimeSpan.Zero;
}
_prevTickTime = currTickTime;

try { AndroidGameWindow.Activity._orientationListener.Update(dt); }
catch (Exception) { }

var handler = Tick;
if (handler != null)
handler(this, EventArgs.Empty);
Expand Down
35 changes: 25 additions & 10 deletions MonoGame.Framework/Platform/Cardboard/OrientationListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Microsoft.Xna.Framework
internal class OrientationListener : OrientationEventListener
{
internal DisplayOrientation targetOrientation = DisplayOrientation.Unknown;
DateTime prevTickTime = DateTime.Now;
TimeSpan elapsed = TimeSpan.Zero;

/// <summary>
Expand Down Expand Up @@ -62,20 +63,34 @@ public override void OnOrientationChanged(int orientation)
return;
}

internal void Update(TimeSpan elapsedTime)
{
if (targetOrientation != DisplayOrientation.Unknown)
internal void Update()
{
DateTime currTickTime = DateTime.Now;
TimeSpan elapsedTime = TimeSpan.Zero;
if (prevTickTime.Ticks != 0)
{
elapsedTime = (currTickTime - prevTickTime);
if (elapsedTime.TotalMilliseconds < 0)
elapsedTime = TimeSpan.Zero;
}
prevTickTime = currTickTime;

try
{
elapsed += elapsedTime;
// orientation must be stable for 0.5 seconds before changing.
if (elapsed.TotalSeconds > 0.5)
if (targetOrientation != DisplayOrientation.Unknown)
{
AndroidGameWindow gameWindow = (AndroidGameWindow)ConcreteGame.GameConcreteInstance.Window;
gameWindow.SetOrientation(targetOrientation, true);
targetOrientation = DisplayOrientation.Unknown;
elapsed = TimeSpan.Zero;
elapsed += elapsedTime;
// orientation must be stable for 0.5 seconds before changing.
if (elapsed.TotalSeconds > 0.5)
{
AndroidGameWindow gameWindow = (AndroidGameWindow)ConcreteGame.GameConcreteInstance.Window;
gameWindow.SetOrientation(targetOrientation, true);
targetOrientation = DisplayOrientation.Unknown;
elapsed = TimeSpan.Zero;
}
}
}
catch (Exception) { /* ignore */ }
}
}
}
Loading