From 698b3c405918666ad1d1e0cb9b780caae0776b4a Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Mon, 17 Feb 2025 15:39:45 +0200 Subject: [PATCH] refactor GetAbsoluteOrientation(..) (#2221) * refactor GetAbsoluteOrientation(Activity) * refactor GetAbsoluteOrientation(int degrees) * inline GetAbsoluteOrientation() * expand Landscape check * simplify code * move degrees * add + 270 * sum degrees * skip rounding * wrap * inline degrees * remove invalid cases * remove switches * remove old comments * add Rotation0 case * return Portrait * types * refactor CreateDevice() * sync Cardboard * sync Oculus --- .../Game/.Android/AndroidCompatibility.cs | 80 ++++++++++++------- .../.Android/ConcreteGraphicsDeviceManager.cs | 20 +++-- .../.CardboardLegacy/AndroidCompatibility.cs | 80 ++++++++++++------- .../ConcreteGraphicsDeviceManager.cs | 20 +++-- .../Game/.Oculus/AndroidCompatibility.cs | 80 ++++++++++++------- 5 files changed, 183 insertions(+), 97 deletions(-) diff --git a/Platforms/Game/.Android/AndroidCompatibility.cs b/Platforms/Game/.Android/AndroidCompatibility.cs index 133cace2196..d20afc8c2e5 100644 --- a/Platforms/Game/.Android/AndroidCompatibility.cs +++ b/Platforms/Game/.Android/AndroidCompatibility.cs @@ -86,23 +86,20 @@ internal DisplayOrientation GetAbsoluteOrientation(int degrees) degrees = ( ((degrees + 22) / 45) * 45) % 360; // Surprisingly 90 degree is landscape right, except on Kindle devices - var disporientation = DisplayOrientation.Unknown; switch (degrees) { - case 90: disporientation = FlipLandscape ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight; - break; - case 270: disporientation = FlipLandscape ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft; - break; - case 0: disporientation = DisplayOrientation.Portrait; - break; - case 180: disporientation = DisplayOrientation.PortraitDown; - break; + case 90: + return FlipLandscape ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight; + case 270: + return FlipLandscape ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft; + case 0: + return DisplayOrientation.Portrait; + case 180: + return DisplayOrientation.PortraitDown; + default: - disporientation = DisplayOrientation.Unknown; - break; + return DisplayOrientation.Unknown; } - - return disporientation; } /// @@ -112,28 +109,57 @@ internal DisplayOrientation GetAbsoluteOrientation(int degrees) [CLSCompliant(false)] public DisplayOrientation GetAbsoluteOrientation(Activity activity) { - var orientation = activity.WindowManager.DefaultDisplay.Rotation; + SurfaceOrientation orientation = activity.WindowManager.DefaultDisplay.Rotation; - // Landscape degrees (provided by the OrientationListener) are swapped by default - // Since we use the code used by OrientationListener, we have to swap manually - int degrees; switch (orientation) { case SurfaceOrientation.Rotation90: - degrees = 270; - break; + { + if (NaturalOrientation == Orientation.Landscape) + { + return DisplayOrientation.PortraitDown; + } + else + { + return FlipLandscape ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft; + } + } case SurfaceOrientation.Rotation180: - degrees = 180; - break; + { + if (NaturalOrientation == Orientation.Landscape) + { + return FlipLandscape ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight; + } + else + { + return DisplayOrientation.PortraitDown; + } + } case SurfaceOrientation.Rotation270: - degrees = 90; - break; + { + if (NaturalOrientation == Orientation.Landscape) + { + return DisplayOrientation.Portrait; + } + else + { + return FlipLandscape ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight; + } + } + + case SurfaceOrientation.Rotation0: default: - degrees = 0; - break; + { + if (NaturalOrientation == Orientation.Landscape) + { + return FlipLandscape ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft; + } + else + { + return DisplayOrientation.Portrait; + } + } } - - return GetAbsoluteOrientation(degrees); } } } diff --git a/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs index f0759f4ac95..6bf5514cba6 100644 --- a/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs @@ -123,16 +123,20 @@ public override void CreateDevice() switch (activity.Resources.Configuration.Orientation) { case Android.Content.Res.Orientation.Portrait: - gameWindow.SetOrientation((currentOrientation == DisplayOrientation.PortraitDown) - ? DisplayOrientation.PortraitDown - : DisplayOrientation.Portrait, - false); + { + DisplayOrientation orientation = (currentOrientation == DisplayOrientation.PortraitDown) + ? DisplayOrientation.PortraitDown + : DisplayOrientation.Portrait; + gameWindow.SetOrientation(orientation, false); + } break; default: - gameWindow.SetOrientation((currentOrientation == DisplayOrientation.LandscapeRight) - ? DisplayOrientation.LandscapeRight - : DisplayOrientation.LandscapeLeft, - false); + { + DisplayOrientation orientation = (currentOrientation == DisplayOrientation.LandscapeRight) + ? DisplayOrientation.LandscapeRight + : DisplayOrientation.LandscapeLeft; + gameWindow.SetOrientation(orientation, false); + } break; } diff --git a/Platforms/Game/.CardboardLegacy/AndroidCompatibility.cs b/Platforms/Game/.CardboardLegacy/AndroidCompatibility.cs index 46d5d6ddb40..dfd4b1beef8 100644 --- a/Platforms/Game/.CardboardLegacy/AndroidCompatibility.cs +++ b/Platforms/Game/.CardboardLegacy/AndroidCompatibility.cs @@ -86,23 +86,20 @@ internal DisplayOrientation GetAbsoluteOrientation(int degrees) degrees = ( ((degrees + 22) / 45) * 45) % 360; // Surprisingly 90 degree is landscape right, except on Kindle devices - var disporientation = DisplayOrientation.Unknown; switch (degrees) { - case 90: disporientation = FlipLandscape ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight; - break; - case 270: disporientation = FlipLandscape ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft; - break; - case 0: disporientation = DisplayOrientation.Portrait; - break; - case 180: disporientation = DisplayOrientation.PortraitDown; - break; + case 90: + return FlipLandscape ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight; + case 270: + return FlipLandscape ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft; + case 0: + return DisplayOrientation.Portrait; + case 180: + return DisplayOrientation.PortraitDown; + default: - disporientation = DisplayOrientation.Unknown; - break; + return DisplayOrientation.Unknown; } - - return disporientation; } /// @@ -112,28 +109,57 @@ internal DisplayOrientation GetAbsoluteOrientation(int degrees) [CLSCompliant(false)] public DisplayOrientation GetAbsoluteOrientation(Activity activity) { - var orientation = activity.WindowManager.DefaultDisplay.Rotation; + SurfaceOrientation orientation = activity.WindowManager.DefaultDisplay.Rotation; - // Landscape degrees (provided by the OrientationListener) are swapped by default - // Since we use the code used by OrientationListener, we have to swap manually - int degrees; switch (orientation) { case SurfaceOrientation.Rotation90: - degrees = 270; - break; + { + if (NaturalOrientation == Orientation.Landscape) + { + return DisplayOrientation.PortraitDown; + } + else + { + return FlipLandscape ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft; + } + } case SurfaceOrientation.Rotation180: - degrees = 180; - break; + { + if (NaturalOrientation == Orientation.Landscape) + { + return FlipLandscape ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight; + } + else + { + return DisplayOrientation.PortraitDown; + } + } case SurfaceOrientation.Rotation270: - degrees = 90; - break; + { + if (NaturalOrientation == Orientation.Landscape) + { + return DisplayOrientation.Portrait; + } + else + { + return FlipLandscape ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight; + } + } + + case SurfaceOrientation.Rotation0: default: - degrees = 0; - break; + { + if (NaturalOrientation == Orientation.Landscape) + { + return FlipLandscape ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft; + } + else + { + return DisplayOrientation.Portrait; + } + } } - - return GetAbsoluteOrientation(degrees); } } } diff --git a/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs index 1ae8f6415a8..a8485f16c5a 100644 --- a/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs @@ -123,16 +123,20 @@ public override void CreateDevice() switch (activity.Resources.Configuration.Orientation) { case Android.Content.Res.Orientation.Portrait: - gameWindow.SetOrientation((currentOrientation == DisplayOrientation.PortraitDown) - ? DisplayOrientation.PortraitDown - : DisplayOrientation.Portrait, - false); + { + DisplayOrientation orientation = (currentOrientation == DisplayOrientation.PortraitDown) + ? DisplayOrientation.PortraitDown + : DisplayOrientation.Portrait; + gameWindow.SetOrientation(orientation, false); + } break; default: - gameWindow.SetOrientation((currentOrientation == DisplayOrientation.LandscapeRight) - ? DisplayOrientation.LandscapeRight - : DisplayOrientation.LandscapeLeft, - false); + { + DisplayOrientation orientation = (currentOrientation == DisplayOrientation.LandscapeRight) + ? DisplayOrientation.LandscapeRight + : DisplayOrientation.LandscapeLeft; + gameWindow.SetOrientation(orientation, false); + } break; } diff --git a/Platforms/Game/.Oculus/AndroidCompatibility.cs b/Platforms/Game/.Oculus/AndroidCompatibility.cs index 6f798c7008c..a602134cffa 100644 --- a/Platforms/Game/.Oculus/AndroidCompatibility.cs +++ b/Platforms/Game/.Oculus/AndroidCompatibility.cs @@ -86,23 +86,20 @@ internal DisplayOrientation GetAbsoluteOrientation(int degrees) degrees = ( ((degrees + 22) / 45) * 45) % 360; // Surprisingly 90 degree is landscape right, except on Kindle devices - var disporientation = DisplayOrientation.Unknown; switch (degrees) { - case 90: disporientation = FlipLandscape ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight; - break; - case 270: disporientation = FlipLandscape ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft; - break; - case 0: disporientation = DisplayOrientation.Portrait; - break; - case 180: disporientation = DisplayOrientation.PortraitDown; - break; + case 90: + return FlipLandscape ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight; + case 270: + return FlipLandscape ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft; + case 0: + return DisplayOrientation.Portrait; + case 180: + return DisplayOrientation.PortraitDown; + default: - disporientation = DisplayOrientation.Unknown; - break; + return DisplayOrientation.Unknown; } - - return disporientation; } /// @@ -112,28 +109,57 @@ internal DisplayOrientation GetAbsoluteOrientation(int degrees) [CLSCompliant(false)] public DisplayOrientation GetAbsoluteOrientation(Activity activity) { - var orientation = activity.WindowManager.DefaultDisplay.Rotation; + SurfaceOrientation orientation = activity.WindowManager.DefaultDisplay.Rotation; - // Landscape degrees (provided by the OrientationListener) are swapped by default - // Since we use the code used by OrientationListener, we have to swap manually - int degrees; switch (orientation) { case SurfaceOrientation.Rotation90: - degrees = 270; - break; + { + if (NaturalOrientation == Orientation.Landscape) + { + return DisplayOrientation.PortraitDown; + } + else + { + return FlipLandscape ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft; + } + } case SurfaceOrientation.Rotation180: - degrees = 180; - break; + { + if (NaturalOrientation == Orientation.Landscape) + { + return FlipLandscape ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight; + } + else + { + return DisplayOrientation.PortraitDown; + } + } case SurfaceOrientation.Rotation270: - degrees = 90; - break; + { + if (NaturalOrientation == Orientation.Landscape) + { + return DisplayOrientation.Portrait; + } + else + { + return FlipLandscape ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight; + } + } + + case SurfaceOrientation.Rotation0: default: - degrees = 0; - break; + { + if (NaturalOrientation == Orientation.Landscape) + { + return FlipLandscape ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft; + } + else + { + return DisplayOrientation.Portrait; + } + } } - - return GetAbsoluteOrientation(degrees); } } }