From 111aec2d1e1dc92de67d1aefcd95341481f0d495 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 22 Feb 2025 16:24:25 +0200 Subject: [PATCH 01/17] inline GetEffectiveSupportedOrientations() --- Platforms/Game/.Android/AndroidGameWindow.cs | 12 ++++++++++-- Platforms/Game/.Android/OrientationListener.cs | 12 +++++++++++- Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs | 12 ++++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Platforms/Game/.Android/AndroidGameWindow.cs b/Platforms/Game/.Android/AndroidGameWindow.cs index fab677b2dd6..04454b24523 100644 --- a/Platforms/Game/.Android/AndroidGameWindow.cs +++ b/Platforms/Game/.Android/AndroidGameWindow.cs @@ -44,7 +44,7 @@ internal static AndroidGameWindow FromHandle(IntPtr windowHandle) internal RunnableObject _runner; internal AndroidGameActivity _activity; - private readonly Game _game; + internal readonly Game _game; private bool _isActivated = false; private AndroidGameWindow.AppState _appState = AndroidGameWindow.AppState.Exited; MediaState _mediaPlayer_PrevState = MediaState.Stopped; @@ -279,7 +279,15 @@ internal DisplayOrientation GetEffectiveSupportedOrientations() /// internal void SetOrientation(DisplayOrientation newOrientation, bool applyGraphicsChanges) { - DisplayOrientation supported = GetEffectiveSupportedOrientations(); + DisplayOrientation supported = this._supportedOrientations; + if (supported == DisplayOrientation.Default) + { + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + GraphicsDeviceManager deviceManager = (_game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } // If the new orientation is not supported, force a supported orientation if ((supported & newOrientation) == 0) diff --git a/Platforms/Game/.Android/OrientationListener.cs b/Platforms/Game/.Android/OrientationListener.cs index e8d2b17ef94..8a5099020f8 100644 --- a/Platforms/Game/.Android/OrientationListener.cs +++ b/Platforms/Game/.Android/OrientationListener.cs @@ -46,7 +46,17 @@ public override void OnOrientationChanged(int orientation) DisplayOrientation absOrientation = AndroidCompatibility.Current.GetAbsoluteOrientation(orientation); - if ((_gameWindow.GetEffectiveSupportedOrientations() & absOrientation) == 0 + DisplayOrientation supported = _gameWindow._supportedOrientations; + if (supported == DisplayOrientation.Default) + { + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + GraphicsDeviceManager deviceManager = (_gameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } + + if ((supported & absOrientation) == 0 || absOrientation == _gameWindow.CurrentOrientation || absOrientation == DisplayOrientation.Unknown ) diff --git a/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs b/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs index 5821253c95b..73f3f750cff 100644 --- a/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs +++ b/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs @@ -48,7 +48,7 @@ internal static AndroidGameWindow FromHandle(IntPtr windowHandle) internal AndroidSurfaceView GameView { get; private set; } internal AndroidGameActivity _activity; - private readonly Game _game; + internal readonly Game _game; private bool _isActivated = false; private AndroidGameWindow.AppState _appState = AndroidGameWindow.AppState.Exited; MediaState _mediaPlayer_PrevState = MediaState.Stopped; @@ -279,7 +279,15 @@ internal DisplayOrientation GetEffectiveSupportedOrientations() /// internal void SetOrientation(DisplayOrientation newOrientation, bool applyGraphicsChanges) { - DisplayOrientation supported = GetEffectiveSupportedOrientations(); + DisplayOrientation supported = this._supportedOrientations; + if (supported == DisplayOrientation.Default) + { + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + GraphicsDeviceManager deviceManager = (_game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } // If the new orientation is not supported, force a supported orientation if ((supported & newOrientation) == 0) From efcff6af047943fd1f5be3e0f5a745d3a09397b7 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 22 Feb 2025 16:25:41 +0200 Subject: [PATCH 02/17] remove GetEffectiveSupportedOrientations() --- Platforms/Game/.Android/AndroidGameWindow.cs | 25 ------------------- .../.CardboardLegacy/AndroidGameWindow.cs | 25 ------------------- 2 files changed, 50 deletions(-) diff --git a/Platforms/Game/.Android/AndroidGameWindow.cs b/Platforms/Game/.Android/AndroidGameWindow.cs index 04454b24523..b9746cdcaa5 100644 --- a/Platforms/Game/.Android/AndroidGameWindow.cs +++ b/Platforms/Game/.Android/AndroidGameWindow.cs @@ -249,31 +249,6 @@ internal void ForceSetFullScreen(bool _isFullScreen) } } - /// - /// In Xna, setting SupportedOrientations = DisplayOrientation.Default (which is the default value) - /// has the effect of setting SupportedOrientations to landscape only or portrait only, based on the - /// aspect ratio of PreferredBackBufferWidth / PreferredBackBufferHeight - /// - /// - internal DisplayOrientation GetEffectiveSupportedOrientations() - { - if (_supportedOrientations == DisplayOrientation.Default) - { - GraphicsDeviceManager deviceManager = (_game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); - if (deviceManager != null) - { - if (deviceManager.PreferredBackBufferWidth > deviceManager.PreferredBackBufferHeight) - return (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - else - return (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); - } - - return (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - } - - return _supportedOrientations; - } - /// /// Updates the screen orientation. Filters out requests for unsupported orientations. /// diff --git a/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs b/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs index 73f3f750cff..936a26d6055 100644 --- a/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs +++ b/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs @@ -249,31 +249,6 @@ internal void ForceSetFullScreen(bool _isFullScreen) } } - /// - /// In Xna, setting SupportedOrientations = DisplayOrientation.Default (which is the default value) - /// has the effect of setting SupportedOrientations to landscape only or portrait only, based on the - /// aspect ratio of PreferredBackBufferWidth / PreferredBackBufferHeight - /// - /// - internal DisplayOrientation GetEffectiveSupportedOrientations() - { - if (_supportedOrientations == DisplayOrientation.Default) - { - GraphicsDeviceManager deviceManager = (_game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); - if (deviceManager != null) - { - if (deviceManager.PreferredBackBufferWidth > deviceManager.PreferredBackBufferHeight) - return (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - else - return (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); - } - - return (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - } - - return _supportedOrientations; - } - /// /// Updates the screen orientation. Filters out requests for unsupported orientations. /// From b2eb3a3cafcb8968b93fbb6cba6311003563156b Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 22 Feb 2025 16:34:59 +0200 Subject: [PATCH 03/17] move supported --- Platforms/Game/.Android/AndroidGameWindow.cs | 12 +------ .../.Android/ConcreteGraphicsDeviceManager.cs | 33 +++++++++++++++++-- .../Game/.Android/OrientationListener.cs | 12 ++++++- .../.CardboardLegacy/AndroidGameWindow.cs | 12 +------ .../ConcreteGraphicsDeviceManager.cs | 33 +++++++++++++++++-- 5 files changed, 73 insertions(+), 29 deletions(-) diff --git a/Platforms/Game/.Android/AndroidGameWindow.cs b/Platforms/Game/.Android/AndroidGameWindow.cs index b9746cdcaa5..8c47ba901d2 100644 --- a/Platforms/Game/.Android/AndroidGameWindow.cs +++ b/Platforms/Game/.Android/AndroidGameWindow.cs @@ -252,18 +252,8 @@ internal void ForceSetFullScreen(bool _isFullScreen) /// /// Updates the screen orientation. Filters out requests for unsupported orientations. /// - internal void SetOrientation(DisplayOrientation newOrientation, bool applyGraphicsChanges) + internal void SetOrientation(DisplayOrientation newOrientation, DisplayOrientation supported, bool applyGraphicsChanges) { - DisplayOrientation supported = this._supportedOrientations; - if (supported == DisplayOrientation.Default) - { - supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - - GraphicsDeviceManager deviceManager = (_game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) - supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); - } - // If the new orientation is not supported, force a supported orientation if ((supported & newOrientation) == 0) { diff --git a/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs index df4bbf676f5..2c19f7f6b34 100644 --- a/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs @@ -111,7 +111,16 @@ public override void CreateDevice() // ApplyChanges { // Trigger a change in orientation in case the supported orientations have changed - androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, false); + DisplayOrientation supported = androidGameWindow._supportedOrientations; + if (supported == DisplayOrientation.Default) + { + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + GraphicsDeviceManager deviceManager = (androidGameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } + androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, supported, false); base.GraphicsDevice.PresentationParameters.DisplayOrientation = base.Game.Window.CurrentOrientation; @@ -136,7 +145,16 @@ public override void CreateDevice() Android.App.Activity activity = AndroidGameWindow.Activity; DisplayOrientation currentOrientation = AndroidCompatibility.Current.GetAbsoluteOrientation(activity); - androidGameWindow.SetOrientation(currentOrientation, false); + DisplayOrientation supported2 = androidGameWindow._supportedOrientations; + if (supported2 == DisplayOrientation.Default) + { + supported2 = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + GraphicsDeviceManager deviceManager = (androidGameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported2 = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } + androidGameWindow.SetOrientation(currentOrientation, supported2, false); // TODO: check if the PreferredBackBufferWidth/Hight is supported and throw an error similar to fullscreen Windows Desktop. base.GraphicsDevice.PresentationParameters.BackBufferWidth = surfaceView.Width; @@ -165,7 +183,16 @@ public override void ApplyChanges() View surfaceView = androidGameWindow.GameView; // Trigger a change in orientation in case the supported orientations have changed - androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, false); + DisplayOrientation supported = androidGameWindow._supportedOrientations; + if (supported == DisplayOrientation.Default) + { + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + GraphicsDeviceManager deviceManager = (androidGameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } + androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, supported, false); base.GraphicsDevice.PresentationParameters.DisplayOrientation = base.Game.Window.CurrentOrientation; diff --git a/Platforms/Game/.Android/OrientationListener.cs b/Platforms/Game/.Android/OrientationListener.cs index 8a5099020f8..58807322e66 100644 --- a/Platforms/Game/.Android/OrientationListener.cs +++ b/Platforms/Game/.Android/OrientationListener.cs @@ -96,7 +96,17 @@ internal void Update() // orientation must be stable for 0.5 seconds before changing. if (elapsed.TotalSeconds > 0.5) { - _gameWindow.SetOrientation(targetOrientation, true); + DisplayOrientation supported = _gameWindow._supportedOrientations; + if (supported == DisplayOrientation.Default) + { + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + GraphicsDeviceManager deviceManager = (_gameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } + _gameWindow.SetOrientation(targetOrientation, supported, true); + targetOrientation = DisplayOrientation.Unknown; elapsed = TimeSpan.Zero; } diff --git a/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs b/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs index 936a26d6055..5a741330d76 100644 --- a/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs +++ b/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs @@ -252,18 +252,8 @@ internal void ForceSetFullScreen(bool _isFullScreen) /// /// Updates the screen orientation. Filters out requests for unsupported orientations. /// - internal void SetOrientation(DisplayOrientation newOrientation, bool applyGraphicsChanges) + internal void SetOrientation(DisplayOrientation newOrientation, DisplayOrientation supported, bool applyGraphicsChanges) { - DisplayOrientation supported = this._supportedOrientations; - if (supported == DisplayOrientation.Default) - { - supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - - GraphicsDeviceManager deviceManager = (_game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) - supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); - } - // If the new orientation is not supported, force a supported orientation if ((supported & newOrientation) == 0) { diff --git a/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs index e379d2652f7..eec4564ebf1 100644 --- a/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs @@ -111,7 +111,16 @@ public override void CreateDevice() // ApplyChanges { // Trigger a change in orientation in case the supported orientations have changed - androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, false); + DisplayOrientation supported = androidGameWindow._supportedOrientations; + if (supported == DisplayOrientation.Default) + { + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + GraphicsDeviceManager deviceManager = (androidGameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } + androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, supported, false); base.GraphicsDevice.PresentationParameters.DisplayOrientation = base.Game.Window.CurrentOrientation; @@ -136,7 +145,16 @@ public override void CreateDevice() Android.App.Activity activity = AndroidGameWindow.Activity; DisplayOrientation currentOrientation = AndroidCompatibility.Current.GetAbsoluteOrientation(activity); - androidGameWindow.SetOrientation(currentOrientation, false); + DisplayOrientation supported2 = androidGameWindow._supportedOrientations; + if (supported2 == DisplayOrientation.Default) + { + supported2 = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + GraphicsDeviceManager deviceManager = (androidGameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported2 = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } + androidGameWindow.SetOrientation(currentOrientation, supported2, false); // TODO: check if the PreferredBackBufferWidth/Hight is supported and throw an error similar to fullscreen Windows Desktop. base.GraphicsDevice.PresentationParameters.BackBufferWidth = surfaceView.Width; @@ -165,7 +183,16 @@ public override void ApplyChanges() View surfaceView = androidGameWindow.GameView; // Trigger a change in orientation in case the supported orientations have changed - androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, false); + DisplayOrientation supported = androidGameWindow._supportedOrientations; + if (supported == DisplayOrientation.Default) + { + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + GraphicsDeviceManager deviceManager = (androidGameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } + androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, supported, false); base.GraphicsDevice.PresentationParameters.DisplayOrientation = base.Game.Window.CurrentOrientation; From 4a9e97502e9d560f727e17a1833f412fa8b0a1b5 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 22 Feb 2025 16:46:35 +0200 Subject: [PATCH 04/17] simplify supported --- .../.Android/ConcreteGraphicsDeviceManager.cs | 15 ++++++--------- .../ConcreteGraphicsDeviceManager.cs | 15 ++++++--------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs index 2c19f7f6b34..ffe28278e92 100644 --- a/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs @@ -111,13 +111,12 @@ public override void CreateDevice() // ApplyChanges { // Trigger a change in orientation in case the supported orientations have changed - DisplayOrientation supported = androidGameWindow._supportedOrientations; + DisplayOrientation supported = this.SupportedOrientations; if (supported == DisplayOrientation.Default) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - GraphicsDeviceManager deviceManager = (androidGameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + if (this.PreferredBackBufferWidth <= this.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, supported, false); @@ -145,13 +144,12 @@ public override void CreateDevice() Android.App.Activity activity = AndroidGameWindow.Activity; DisplayOrientation currentOrientation = AndroidCompatibility.Current.GetAbsoluteOrientation(activity); - DisplayOrientation supported2 = androidGameWindow._supportedOrientations; + DisplayOrientation supported2 = this.SupportedOrientations; if (supported2 == DisplayOrientation.Default) { supported2 = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - GraphicsDeviceManager deviceManager = (androidGameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + if (this.PreferredBackBufferWidth <= this.PreferredBackBufferHeight) supported2 = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } androidGameWindow.SetOrientation(currentOrientation, supported2, false); @@ -183,13 +181,12 @@ public override void ApplyChanges() View surfaceView = androidGameWindow.GameView; // Trigger a change in orientation in case the supported orientations have changed - DisplayOrientation supported = androidGameWindow._supportedOrientations; + DisplayOrientation supported = this.SupportedOrientations; if (supported == DisplayOrientation.Default) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - GraphicsDeviceManager deviceManager = (androidGameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + if (this.PreferredBackBufferWidth <= this.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, supported, false); diff --git a/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs index eec4564ebf1..6627ba48711 100644 --- a/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs @@ -111,13 +111,12 @@ public override void CreateDevice() // ApplyChanges { // Trigger a change in orientation in case the supported orientations have changed - DisplayOrientation supported = androidGameWindow._supportedOrientations; + DisplayOrientation supported = this.SupportedOrientations; if (supported == DisplayOrientation.Default) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - GraphicsDeviceManager deviceManager = (androidGameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + if (this.PreferredBackBufferWidth <= this.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, supported, false); @@ -145,13 +144,12 @@ public override void CreateDevice() Android.App.Activity activity = AndroidGameWindow.Activity; DisplayOrientation currentOrientation = AndroidCompatibility.Current.GetAbsoluteOrientation(activity); - DisplayOrientation supported2 = androidGameWindow._supportedOrientations; + DisplayOrientation supported2 = this.SupportedOrientations; if (supported2 == DisplayOrientation.Default) { supported2 = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - GraphicsDeviceManager deviceManager = (androidGameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + if (this.PreferredBackBufferWidth <= this.PreferredBackBufferHeight) supported2 = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } androidGameWindow.SetOrientation(currentOrientation, supported2, false); @@ -183,13 +181,12 @@ public override void ApplyChanges() View surfaceView = androidGameWindow.GameView; // Trigger a change in orientation in case the supported orientations have changed - DisplayOrientation supported = androidGameWindow._supportedOrientations; + DisplayOrientation supported = this.SupportedOrientations; if (supported == DisplayOrientation.Default) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - GraphicsDeviceManager deviceManager = (androidGameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + if (this.PreferredBackBufferWidth <= this.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, supported, false); From aaa9091dc33cc0c045237e96ba055f050a686077 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 22 Feb 2025 17:04:18 +0200 Subject: [PATCH 05/17] split supported check --- Platforms/Game/.Android/OrientationListener.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Platforms/Game/.Android/OrientationListener.cs b/Platforms/Game/.Android/OrientationListener.cs index 58807322e66..e6796e5f6c9 100644 --- a/Platforms/Game/.Android/OrientationListener.cs +++ b/Platforms/Game/.Android/OrientationListener.cs @@ -46,6 +46,7 @@ public override void OnOrientationChanged(int orientation) DisplayOrientation absOrientation = AndroidCompatibility.Current.GetAbsoluteOrientation(orientation); + DisplayOrientation supported = _gameWindow._supportedOrientations; if (supported == DisplayOrientation.Default) { @@ -56,8 +57,14 @@ public override void OnOrientationChanged(int orientation) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } - if ((supported & absOrientation) == 0 - || absOrientation == _gameWindow.CurrentOrientation + if ((supported & absOrientation) == 0) + { + targetOrientation = DisplayOrientation.Unknown; + elapsed = TimeSpan.Zero; + return; + } + + if (absOrientation == _gameWindow.CurrentOrientation || absOrientation == DisplayOrientation.Unknown ) { From 2c6324d4e89e6e2f4f8610fcbfb0e150418c8f19 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sun, 23 Feb 2025 10:27:17 +0200 Subject: [PATCH 06/17] move deviceManager --- Platforms/Game/.Android/OrientationListener.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Platforms/Game/.Android/OrientationListener.cs b/Platforms/Game/.Android/OrientationListener.cs index e6796e5f6c9..dd707bafe53 100644 --- a/Platforms/Game/.Android/OrientationListener.cs +++ b/Platforms/Game/.Android/OrientationListener.cs @@ -46,13 +46,12 @@ public override void OnOrientationChanged(int orientation) DisplayOrientation absOrientation = AndroidCompatibility.Current.GetAbsoluteOrientation(orientation); - + GraphicsDeviceManager deviceManager = (_gameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); DisplayOrientation supported = _gameWindow._supportedOrientations; if (supported == DisplayOrientation.Default) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - GraphicsDeviceManager deviceManager = (_gameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } @@ -103,12 +102,12 @@ internal void Update() // orientation must be stable for 0.5 seconds before changing. if (elapsed.TotalSeconds > 0.5) { + GraphicsDeviceManager deviceManager = (_gameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); DisplayOrientation supported = _gameWindow._supportedOrientations; if (supported == DisplayOrientation.Default) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - GraphicsDeviceManager deviceManager = (_gameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } From 5867142331a210d870b7ff9150fdde2a0dd018d1 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sun, 23 Feb 2025 10:29:33 +0200 Subject: [PATCH 07/17] duplicate code --- .../Game/.Android/OrientationListener.cs | 79 ++++++++++++++----- 1 file changed, 59 insertions(+), 20 deletions(-) diff --git a/Platforms/Game/.Android/OrientationListener.cs b/Platforms/Game/.Android/OrientationListener.cs index dd707bafe53..6e890280aee 100644 --- a/Platforms/Game/.Android/OrientationListener.cs +++ b/Platforms/Game/.Android/OrientationListener.cs @@ -47,20 +47,41 @@ public override void OnOrientationChanged(int orientation) DisplayOrientation absOrientation = AndroidCompatibility.Current.GetAbsoluteOrientation(orientation); GraphicsDeviceManager deviceManager = (_gameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); - DisplayOrientation supported = _gameWindow._supportedOrientations; - if (supported == DisplayOrientation.Default) + if (deviceManager != null) { - supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + DisplayOrientation supported = _gameWindow._supportedOrientations; + if (supported == DisplayOrientation.Default) + { + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) - supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); - } + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } - if ((supported & absOrientation) == 0) + if ((supported & absOrientation) == 0) + { + targetOrientation = DisplayOrientation.Unknown; + elapsed = TimeSpan.Zero; + return; + } + } + else { - targetOrientation = DisplayOrientation.Unknown; - elapsed = TimeSpan.Zero; - return; + DisplayOrientation supported = _gameWindow._supportedOrientations; + if (supported == DisplayOrientation.Default) + { + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } + + if ((supported & absOrientation) == 0) + { + targetOrientation = DisplayOrientation.Unknown; + elapsed = TimeSpan.Zero; + return; + } } if (absOrientation == _gameWindow.CurrentOrientation @@ -103,18 +124,36 @@ internal void Update() if (elapsed.TotalSeconds > 0.5) { GraphicsDeviceManager deviceManager = (_gameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); - DisplayOrientation supported = _gameWindow._supportedOrientations; - if (supported == DisplayOrientation.Default) + if (deviceManager != null) { - supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) - supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + DisplayOrientation supported = _gameWindow._supportedOrientations; + if (supported == DisplayOrientation.Default) + { + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } + _gameWindow.SetOrientation(targetOrientation, supported, true); + + targetOrientation = DisplayOrientation.Unknown; + elapsed = TimeSpan.Zero; + } + else + { + DisplayOrientation supported = _gameWindow._supportedOrientations; + if (supported == DisplayOrientation.Default) + { + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); + + if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + } + _gameWindow.SetOrientation(targetOrientation, supported, true); + + targetOrientation = DisplayOrientation.Unknown; + elapsed = TimeSpan.Zero; } - _gameWindow.SetOrientation(targetOrientation, supported, true); - - targetOrientation = DisplayOrientation.Unknown; - elapsed = TimeSpan.Zero; } } } From 5837d576d760601d3e2fe4be769df47ece85ef36 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sun, 23 Feb 2025 10:31:12 +0200 Subject: [PATCH 08/17] simplify deviceManager null check --- Platforms/Game/.Android/OrientationListener.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Platforms/Game/.Android/OrientationListener.cs b/Platforms/Game/.Android/OrientationListener.cs index 6e890280aee..eab6114c84f 100644 --- a/Platforms/Game/.Android/OrientationListener.cs +++ b/Platforms/Game/.Android/OrientationListener.cs @@ -54,7 +54,7 @@ public override void OnOrientationChanged(int orientation) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + if (deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } @@ -71,9 +71,6 @@ public override void OnOrientationChanged(int orientation) if (supported == DisplayOrientation.Default) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) - supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } if ((supported & absOrientation) == 0) @@ -131,7 +128,7 @@ internal void Update() { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) + if (deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } _gameWindow.SetOrientation(targetOrientation, supported, true); @@ -145,9 +142,6 @@ internal void Update() if (supported == DisplayOrientation.Default) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - - if (deviceManager != null && deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) - supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); } _gameWindow.SetOrientation(targetOrientation, supported, true); From a24fbc83744e364cd1891e220f98add996ae9172 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sun, 23 Feb 2025 10:33:49 +0200 Subject: [PATCH 09/17] default supported --- Platforms/Game/.Android/OrientationListener.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Platforms/Game/.Android/OrientationListener.cs b/Platforms/Game/.Android/OrientationListener.cs index eab6114c84f..5e3a534129e 100644 --- a/Platforms/Game/.Android/OrientationListener.cs +++ b/Platforms/Game/.Android/OrientationListener.cs @@ -67,7 +67,7 @@ public override void OnOrientationChanged(int orientation) } else { - DisplayOrientation supported = _gameWindow._supportedOrientations; + DisplayOrientation supported = DisplayOrientation.Default; if (supported == DisplayOrientation.Default) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); @@ -138,7 +138,7 @@ internal void Update() } else { - DisplayOrientation supported = _gameWindow._supportedOrientations; + DisplayOrientation supported = DisplayOrientation.Default; if (supported == DisplayOrientation.Default) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); From 2618492190de01fd02ce96e2ee53535a36fc3928 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sun, 23 Feb 2025 10:35:04 +0200 Subject: [PATCH 10/17] use deviceManager.SupportedOrientations --- Platforms/Game/.Android/OrientationListener.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Platforms/Game/.Android/OrientationListener.cs b/Platforms/Game/.Android/OrientationListener.cs index 5e3a534129e..a928acf568a 100644 --- a/Platforms/Game/.Android/OrientationListener.cs +++ b/Platforms/Game/.Android/OrientationListener.cs @@ -49,7 +49,7 @@ public override void OnOrientationChanged(int orientation) GraphicsDeviceManager deviceManager = (_gameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); if (deviceManager != null) { - DisplayOrientation supported = _gameWindow._supportedOrientations; + DisplayOrientation supported = deviceManager.SupportedOrientations; if (supported == DisplayOrientation.Default) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); @@ -123,7 +123,7 @@ internal void Update() GraphicsDeviceManager deviceManager = (_gameWindow._game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager); if (deviceManager != null) { - DisplayOrientation supported = _gameWindow._supportedOrientations; + DisplayOrientation supported = deviceManager.SupportedOrientations; if (supported == DisplayOrientation.Default) { supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); From 5c18eb5bb3df0623648337c5ca456a6d46848f8d Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sun, 23 Feb 2025 10:37:42 +0200 Subject: [PATCH 11/17] remove _supportedOrientations --- Platforms/Game/.Android/AndroidGameWindow.cs | 1 - Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs | 3 --- Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs | 1 - .../Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs | 3 --- 4 files changed, 8 deletions(-) diff --git a/Platforms/Game/.Android/AndroidGameWindow.cs b/Platforms/Game/.Android/AndroidGameWindow.cs index 8c47ba901d2..15ec81e4adc 100644 --- a/Platforms/Game/.Android/AndroidGameWindow.cs +++ b/Platforms/Game/.Android/AndroidGameWindow.cs @@ -50,7 +50,6 @@ internal static AndroidGameWindow FromHandle(IntPtr windowHandle) MediaState _mediaPlayer_PrevState = MediaState.Stopped; private Rectangle _clientBounds; - internal DisplayOrientation _supportedOrientations = DisplayOrientation.Default; private DisplayOrientation _currentOrientation; private OrientationListener _orientationListener; diff --git a/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs index ffe28278e92..434f2b39c7f 100644 --- a/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs @@ -66,9 +66,6 @@ public override DisplayOrientation SupportedOrientations set { base.SupportedOrientations = value; - - if (base.Game.Window != null) - ((AndroidGameWindow)base.Game.Window)._supportedOrientations = base.SupportedOrientations; } } diff --git a/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs b/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs index 5a741330d76..1c83265d39d 100644 --- a/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs +++ b/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs @@ -54,7 +54,6 @@ internal static AndroidGameWindow FromHandle(IntPtr windowHandle) MediaState _mediaPlayer_PrevState = MediaState.Stopped; private Rectangle _clientBounds; - internal DisplayOrientation _supportedOrientations = DisplayOrientation.Default; private DisplayOrientation _currentOrientation; private OrientationListener _orientationListener; diff --git a/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs index 6627ba48711..57be1049231 100644 --- a/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs @@ -66,9 +66,6 @@ public override DisplayOrientation SupportedOrientations set { base.SupportedOrientations = value; - - if (base.Game.Window != null) - ((AndroidGameWindow)base.Game.Window)._supportedOrientations = base.SupportedOrientations; } } From f4b07788f353a815cfef0aa434c1b313fca3f2f3 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sun, 23 Feb 2025 10:43:08 +0200 Subject: [PATCH 12/17] simplify default supported --- Platforms/Game/.Android/OrientationListener.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Platforms/Game/.Android/OrientationListener.cs b/Platforms/Game/.Android/OrientationListener.cs index a928acf568a..3cba0e621a3 100644 --- a/Platforms/Game/.Android/OrientationListener.cs +++ b/Platforms/Game/.Android/OrientationListener.cs @@ -67,11 +67,7 @@ public override void OnOrientationChanged(int orientation) } else { - DisplayOrientation supported = DisplayOrientation.Default; - if (supported == DisplayOrientation.Default) - { - supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - } + DisplayOrientation supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); if ((supported & absOrientation) == 0) { @@ -138,11 +134,7 @@ internal void Update() } else { - DisplayOrientation supported = DisplayOrientation.Default; - if (supported == DisplayOrientation.Default) - { - supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - } + DisplayOrientation supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); _gameWindow.SetOrientation(targetOrientation, supported, true); targetOrientation = DisplayOrientation.Unknown; From 2218b0084ef29b613a69d0cdfc5011ae2e558be0 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sun, 23 Feb 2025 10:52:36 +0200 Subject: [PATCH 13/17] refactor ratio check --- Platforms/Game/.Android/OrientationListener.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Platforms/Game/.Android/OrientationListener.cs b/Platforms/Game/.Android/OrientationListener.cs index 3cba0e621a3..d30ca3e149a 100644 --- a/Platforms/Game/.Android/OrientationListener.cs +++ b/Platforms/Game/.Android/OrientationListener.cs @@ -52,10 +52,10 @@ public override void OnOrientationChanged(int orientation) DisplayOrientation supported = deviceManager.SupportedOrientations; if (supported == DisplayOrientation.Default) { - supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - if (deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + else + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); } if ((supported & absOrientation) == 0) @@ -122,10 +122,10 @@ internal void Update() DisplayOrientation supported = deviceManager.SupportedOrientations; if (supported == DisplayOrientation.Default) { - supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - if (deviceManager.PreferredBackBufferWidth <= deviceManager.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + else + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); } _gameWindow.SetOrientation(targetOrientation, supported, true); From d984692f46fd7c34516f6f3c6dc8e66c9068647c Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sun, 23 Feb 2025 11:03:12 +0200 Subject: [PATCH 14/17] refactor SetOrientation(...) --- Platforms/Game/.Android/AndroidGameWindow.cs | 6 +++--- Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Platforms/Game/.Android/AndroidGameWindow.cs b/Platforms/Game/.Android/AndroidGameWindow.cs index 15ec81e4adc..bf4a45626f0 100644 --- a/Platforms/Game/.Android/AndroidGameWindow.cs +++ b/Platforms/Game/.Android/AndroidGameWindow.cs @@ -254,7 +254,7 @@ internal void ForceSetFullScreen(bool _isFullScreen) internal void SetOrientation(DisplayOrientation newOrientation, DisplayOrientation supported, bool applyGraphicsChanges) { // If the new orientation is not supported, force a supported orientation - if ((supported & newOrientation) == 0) + if ((newOrientation & supported) == 0) { if ((supported & DisplayOrientation.LandscapeLeft) != 0) newOrientation = DisplayOrientation.LandscapeLeft; @@ -278,7 +278,7 @@ internal void SetOrientation(DisplayOrientation newOrientation, DisplayOrientati newOrientation = DisplayOrientation.PortraitDown; } - if ((supported & newOrientation) != 0) + if ((newOrientation & supported) != 0) { DisplayOrientation oldOrientation = CurrentOrientation; _currentOrientation = newOrientation; @@ -288,7 +288,7 @@ internal void SetOrientation(DisplayOrientation newOrientation, DisplayOrientati TouchPanel.DisplayOrientation = newOrientation; - if (applyGraphicsChanges && (oldOrientation != newOrientation)) + if ((newOrientation != oldOrientation) && applyGraphicsChanges) { GraphicsDeviceManager gdm = ((IPlatformGame)_game).GetStrategy().GraphicsDeviceManager; if (gdm != null) diff --git a/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs b/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs index 1c83265d39d..7dff6ed1935 100644 --- a/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs +++ b/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs @@ -254,7 +254,7 @@ internal void ForceSetFullScreen(bool _isFullScreen) internal void SetOrientation(DisplayOrientation newOrientation, DisplayOrientation supported, bool applyGraphicsChanges) { // If the new orientation is not supported, force a supported orientation - if ((supported & newOrientation) == 0) + if ((newOrientation & supported) == 0) { if ((supported & DisplayOrientation.LandscapeLeft) != 0) newOrientation = DisplayOrientation.LandscapeLeft; @@ -278,7 +278,7 @@ internal void SetOrientation(DisplayOrientation newOrientation, DisplayOrientati newOrientation = DisplayOrientation.PortraitDown; } - if ((supported & newOrientation) != 0) + if ((newOrientation & supported) != 0) { DisplayOrientation oldOrientation = CurrentOrientation; _currentOrientation = newOrientation; @@ -288,7 +288,7 @@ internal void SetOrientation(DisplayOrientation newOrientation, DisplayOrientati TouchPanel.DisplayOrientation = newOrientation; - if (applyGraphicsChanges && (oldOrientation != newOrientation)) + if ((newOrientation != oldOrientation) && applyGraphicsChanges) { GraphicsDeviceManager gdm = ((IPlatformGame)_game).GetStrategy().GraphicsDeviceManager; if (gdm != null) From f95f4e52cad24a0d0f07045b97f09477fbbd70e4 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sun, 23 Feb 2025 11:14:27 +0200 Subject: [PATCH 15/17] refactor ratio check --- .../Game/.Android/ConcreteGraphicsDeviceManager.cs | 12 ++++++------ .../ConcreteGraphicsDeviceManager.cs | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs index 434f2b39c7f..a22dc3e49f8 100644 --- a/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs @@ -111,10 +111,10 @@ public override void CreateDevice() DisplayOrientation supported = this.SupportedOrientations; if (supported == DisplayOrientation.Default) { - supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - if (this.PreferredBackBufferWidth <= this.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + else + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); } androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, supported, false); @@ -144,10 +144,10 @@ public override void CreateDevice() DisplayOrientation supported2 = this.SupportedOrientations; if (supported2 == DisplayOrientation.Default) { - supported2 = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - if (this.PreferredBackBufferWidth <= this.PreferredBackBufferHeight) supported2 = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + else + supported2 = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); } androidGameWindow.SetOrientation(currentOrientation, supported2, false); @@ -181,10 +181,10 @@ public override void ApplyChanges() DisplayOrientation supported = this.SupportedOrientations; if (supported == DisplayOrientation.Default) { - supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - if (this.PreferredBackBufferWidth <= this.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + else + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); } androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, supported, false); diff --git a/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs index 57be1049231..8f9f0b56527 100644 --- a/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs @@ -111,10 +111,10 @@ public override void CreateDevice() DisplayOrientation supported = this.SupportedOrientations; if (supported == DisplayOrientation.Default) { - supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - if (this.PreferredBackBufferWidth <= this.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + else + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); } androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, supported, false); @@ -144,10 +144,10 @@ public override void CreateDevice() DisplayOrientation supported2 = this.SupportedOrientations; if (supported2 == DisplayOrientation.Default) { - supported2 = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - if (this.PreferredBackBufferWidth <= this.PreferredBackBufferHeight) supported2 = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + else + supported2 = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); } androidGameWindow.SetOrientation(currentOrientation, supported2, false); @@ -181,10 +181,10 @@ public override void ApplyChanges() DisplayOrientation supported = this.SupportedOrientations; if (supported == DisplayOrientation.Default) { - supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); - if (this.PreferredBackBufferWidth <= this.PreferredBackBufferHeight) supported = (DisplayOrientation.Portrait | DisplayOrientation.PortraitDown); + else + supported = (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight); } androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, supported, false); From 52f72b5fd5bdc26a9c3b06bf73f4b970ecf9ddf7 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sun, 23 Feb 2025 12:13:48 +0200 Subject: [PATCH 16/17] WindowOrientationChanged event --- .../.Android/AndroidConfigChangedEventArgs.cs | 30 +++++++++++++++++++ .../Game/.Android/AndroidGameActivity.cs | 13 +++++++- .../.CardboardLegacy/AndroidGameActivity.cs | 13 +++++++- .../Kni.Platform.Android.GL.Xamarin.csproj | 1 + Platforms/Kni.Platform.Android.GL.csproj | 1 + Platforms/Kni.Platform.Cardboard.GL.csproj | 1 + Platforms/Kni.Platform.Oculus.GL.csproj | 1 + 7 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 Platforms/Game/.Android/AndroidConfigChangedEventArgs.cs diff --git a/Platforms/Game/.Android/AndroidConfigChangedEventArgs.cs b/Platforms/Game/.Android/AndroidConfigChangedEventArgs.cs new file mode 100644 index 00000000000..aa6a547e666 --- /dev/null +++ b/Platforms/Game/.Android/AndroidConfigChangedEventArgs.cs @@ -0,0 +1,30 @@ +// Copyright (C)2025 Nick Kastellanos + +using System; +using Microsoft.Xna.Framework; + + +namespace Microsoft.Xna.Platform +{ + internal class AndroidConfigChangedEventArgs : EventArgs + { + public readonly Android.Content.Res.Configuration NewConfig; + + public AndroidConfigChangedEventArgs(Android.Content.Res.Configuration newConfig) + { + NewConfig = newConfig; + } + } + + internal class AndroidConfigChangedOrientationEventArgs : AndroidConfigChangedEventArgs + { + public readonly Android.Content.Res.Orientation NewOrientation; + + public AndroidConfigChangedOrientationEventArgs(Android.Content.Res.Configuration newConfig, Android.Content.Res.Orientation newOrientation) + : base(newConfig) + { + this.NewOrientation = newOrientation; + } + } + +} diff --git a/Platforms/Game/.Android/AndroidGameActivity.cs b/Platforms/Game/.Android/AndroidGameActivity.cs index 87aef83b438..185aa1bdaf6 100644 --- a/Platforms/Game/.Android/AndroidGameActivity.cs +++ b/Platforms/Game/.Android/AndroidGameActivity.cs @@ -20,6 +20,7 @@ public class AndroidGameActivity : Activity internal event EventHandler WindowFocused; internal event EventHandler WindowUnfocused; + internal event EventHandler WindowOrientationChanged; public event EventHandler Paused; public event EventHandler Resumed; @@ -49,8 +50,18 @@ protected override void OnCreate(Bundle savedInstanceState) public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig) { - // we need to refresh the viewport here. base.OnConfigurationChanged(newConfig); + + Android.Content.PM.ConfigChanges changes = (Android.Content.PM.ConfigChanges)newConfig.UpdateFrom(Resources.Configuration); + + if ((changes & Android.Content.PM.ConfigChanges.Orientation) != 0) + { + Android.Content.Res.Orientation newOrientation = newConfig.Orientation; + + var handler = WindowOrientationChanged; + if (handler != null) + handler(this, new AndroidConfigChangedOrientationEventArgs(newConfig, newOrientation)); + } } protected override void OnPause() diff --git a/Platforms/Game/.CardboardLegacy/AndroidGameActivity.cs b/Platforms/Game/.CardboardLegacy/AndroidGameActivity.cs index e953b638b2f..81e4f285d77 100644 --- a/Platforms/Game/.CardboardLegacy/AndroidGameActivity.cs +++ b/Platforms/Game/.CardboardLegacy/AndroidGameActivity.cs @@ -21,6 +21,7 @@ public class AndroidGameActivity : VRCardboard.CardboardActivity internal event EventHandler WindowFocused; internal event EventHandler WindowUnfocused; + internal event EventHandler WindowOrientationChanged; public event EventHandler Paused; public event EventHandler Resumed; @@ -57,8 +58,18 @@ protected override void OnCreate(Bundle savedInstanceState) public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig) { - // we need to refresh the viewport here. base.OnConfigurationChanged(newConfig); + + Android.Content.PM.ConfigChanges changes = (Android.Content.PM.ConfigChanges)newConfig.UpdateFrom(Resources.Configuration); + + if ((changes & Android.Content.PM.ConfigChanges.Orientation) != 0) + { + Android.Content.Res.Orientation newOrientation = newConfig.Orientation; + + var handler = WindowOrientationChanged; + if (handler != null) + handler(this, new AndroidConfigChangedOrientationEventArgs(newConfig, newOrientation)); + } } protected override void OnPause() diff --git a/Platforms/Kni.Platform.Android.GL.Xamarin.csproj b/Platforms/Kni.Platform.Android.GL.Xamarin.csproj index dc5910a584b..c255bb98259 100644 --- a/Platforms/Kni.Platform.Android.GL.Xamarin.csproj +++ b/Platforms/Kni.Platform.Android.GL.Xamarin.csproj @@ -197,6 +197,7 @@ + diff --git a/Platforms/Kni.Platform.Android.GL.csproj b/Platforms/Kni.Platform.Android.GL.csproj index 5246f664ce9..3857ad3348c 100644 --- a/Platforms/Kni.Platform.Android.GL.csproj +++ b/Platforms/Kni.Platform.Android.GL.csproj @@ -118,6 +118,7 @@ + diff --git a/Platforms/Kni.Platform.Cardboard.GL.csproj b/Platforms/Kni.Platform.Cardboard.GL.csproj index 327dd58f056..4a9da31b463 100644 --- a/Platforms/Kni.Platform.Cardboard.GL.csproj +++ b/Platforms/Kni.Platform.Cardboard.GL.csproj @@ -162,6 +162,7 @@ + diff --git a/Platforms/Kni.Platform.Oculus.GL.csproj b/Platforms/Kni.Platform.Oculus.GL.csproj index 57e01b93158..6740f1ebeec 100644 --- a/Platforms/Kni.Platform.Oculus.GL.csproj +++ b/Platforms/Kni.Platform.Oculus.GL.csproj @@ -118,6 +118,7 @@ + From 1a006130126d663e026034c523288a43deec6365 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Tue, 25 Feb 2025 02:08:03 +0200 Subject: [PATCH 17/17] subscribe to WindowOrientationChanged --- Platforms/Game/.Android/AndroidGameWindow.cs | 8 ++++++++ Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/Platforms/Game/.Android/AndroidGameWindow.cs b/Platforms/Game/.Android/AndroidGameWindow.cs index bf4a45626f0..547372b21c7 100644 --- a/Platforms/Game/.Android/AndroidGameWindow.cs +++ b/Platforms/Game/.Android/AndroidGameWindow.cs @@ -69,6 +69,7 @@ public AndroidGameWindow(AndroidGameActivity activity, Game game) _activity.WindowFocused += Activity_WindowFocused; _activity.WindowUnfocused += Activity_WindowUnfocused; + _activity.WindowOrientationChanged += Activity_WindowOrientationChanged; Point size; // GetRealSize() was defined in JellyBeanMr1 / API 17 / Android 4.2 @@ -235,6 +236,12 @@ private void Activity_WindowUnfocused(object sender, EventArgs e) } } + private void Activity_WindowOrientationChanged(object sender, AndroidConfigChangedOrientationEventArgs e) + { + Android.Content.Res.Orientation NewOrientation = e.NewOrientation; + + } + internal void ForceSetFullScreen(bool _isFullScreen) { if (_isFullScreen) @@ -396,6 +403,7 @@ public void Dispose() _activity.WindowFocused += Activity_WindowFocused; _activity.WindowUnfocused += Activity_WindowUnfocused; + _activity.WindowOrientationChanged += Activity_WindowOrientationChanged; _activity = null; } diff --git a/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs b/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs index 7dff6ed1935..1bcf9cd70b0 100644 --- a/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs +++ b/Platforms/Game/.CardboardLegacy/AndroidGameWindow.cs @@ -73,6 +73,7 @@ public AndroidGameWindow(AndroidGameActivity activity, Game game) _activity.WindowFocused += Activity_WindowFocused; _activity.WindowUnfocused += Activity_WindowUnfocused; + _activity.WindowOrientationChanged += Activity_WindowOrientationChanged; Point size; // GetRealSize() was defined in JellyBeanMr1 / API 17 / Android 4.2 @@ -235,6 +236,12 @@ private void Activity_WindowUnfocused(object sender, EventArgs e) } } + private void Activity_WindowOrientationChanged(object sender, AndroidConfigChangedOrientationEventArgs e) + { + Android.Content.Res.Orientation NewOrientation = e.NewOrientation; + + } + internal void ForceSetFullScreen(bool _isFullScreen) { if (_isFullScreen) @@ -396,6 +403,7 @@ public void Dispose() _activity.WindowFocused += Activity_WindowFocused; _activity.WindowUnfocused += Activity_WindowUnfocused; + _activity.WindowOrientationChanged += Activity_WindowOrientationChanged; _activity = null; }