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 GetAbsoluteOrientation(Activity) #2229

Merged
merged 1 commit into from
Feb 22, 2025
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
91 changes: 58 additions & 33 deletions Platforms/Game/.Android/AndroidCompatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ private Orientation GetDeviceNaturalOrientation(Activity activity)
if (screenOrientation != Android.Content.PM.ScreenOrientation.FullSensor)
throw new InvalidOperationException("NaturalOrientation detection failed. Set ScreenOrientation in MainActivity to FullSensor.");

if (((rotation == SurfaceOrientation.Rotation0 || rotation == SurfaceOrientation.Rotation180) &&
orientation == Orientation.Landscape)
|| ((rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270) &&
orientation == Orientation.Portrait))
if (((rotation == SurfaceOrientation.Rotation0 || rotation == SurfaceOrientation.Rotation180) && orientation == Orientation.Landscape)
|| ((rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270) && orientation == Orientation.Portrait))
{
return Orientation.Landscape;
}
Expand All @@ -88,14 +86,18 @@ internal DisplayOrientation GetAbsoluteOrientation(int degrees)
// Surprisingly 90 degree is landscape right, except on Kindle devices
switch (degrees)
{
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;
case 90:
if (FlipLandscape)
return DisplayOrientation.LandscapeLeft;
return DisplayOrientation.LandscapeRight;
case 270:
if (FlipLandscape)
return DisplayOrientation.LandscapeRight;
return DisplayOrientation.LandscapeLeft;

default:
return DisplayOrientation.Unknown;
Expand All @@ -109,38 +111,61 @@ internal DisplayOrientation GetAbsoluteOrientation(int degrees)
[CLSCompliant(false)]
public DisplayOrientation GetAbsoluteOrientation(Activity activity)
{
SurfaceOrientation orientation = activity.WindowManager.DefaultDisplay.Rotation;
Orientation orientation = activity.Resources.Configuration.Orientation;
SurfaceOrientation rotation = activity.WindowManager.DefaultDisplay.Rotation;

switch (orientation)
{
case SurfaceOrientation.Rotation90:
case Orientation.Portrait:
{
if (NaturalOrientation == Orientation.Landscape)
return DisplayOrientation.PortraitDown;
else
return (FlipLandscape) ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft;
switch (rotation)
{
case SurfaceOrientation.Rotation0:
default:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation != Orientation.Landscape);
return DisplayOrientation.Portrait;
case SurfaceOrientation.Rotation180:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation != Orientation.Landscape);
return DisplayOrientation.PortraitDown;
case SurfaceOrientation.Rotation90:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation == Orientation.Landscape);
return DisplayOrientation.PortraitDown;
case SurfaceOrientation.Rotation270:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation == Orientation.Landscape);
return DisplayOrientation.Portrait;
}
}
case SurfaceOrientation.Rotation180:
{
if (NaturalOrientation == Orientation.Landscape)
return (FlipLandscape) ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight;
else
return DisplayOrientation.PortraitDown;
}
case SurfaceOrientation.Rotation270:
{
if (NaturalOrientation == Orientation.Landscape)
return DisplayOrientation.Portrait;
else
return (FlipLandscape) ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight;
}
case SurfaceOrientation.Rotation0:
break;

case Android.Content.Res.Orientation.Landscape:
default:
{
if (NaturalOrientation == Orientation.Landscape)
return (FlipLandscape) ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft;
else
return DisplayOrientation.Portrait;
switch (rotation)
{
case SurfaceOrientation.Rotation0:
default:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation == Orientation.Landscape);
if (AndroidCompatibility.Current.FlipLandscape)
return DisplayOrientation.LandscapeRight;
return DisplayOrientation.LandscapeLeft;
case SurfaceOrientation.Rotation180:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation == Orientation.Landscape);
if (AndroidCompatibility.Current.FlipLandscape)
return DisplayOrientation.LandscapeLeft;
return DisplayOrientation.LandscapeRight;
case SurfaceOrientation.Rotation90:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation != Orientation.Landscape);
if (AndroidCompatibility.Current.FlipLandscape)
return DisplayOrientation.LandscapeRight;
return DisplayOrientation.LandscapeLeft;
case SurfaceOrientation.Rotation270:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation != Orientation.Landscape);
if (AndroidCompatibility.Current.FlipLandscape)
return DisplayOrientation.LandscapeLeft;
return DisplayOrientation.LandscapeRight;
}
}
break;
}
}
}
Expand Down
20 changes: 1 addition & 19 deletions Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,7 @@ public override void CreateDevice()

Android.App.Activity activity = AndroidGameWindow.Activity;
DisplayOrientation currentOrientation = AndroidCompatibility.Current.GetAbsoluteOrientation(activity);
switch (activity.Resources.Configuration.Orientation)
{
case Android.Content.Res.Orientation.Portrait:
{
DisplayOrientation orientation = (currentOrientation == DisplayOrientation.PortraitDown)
? DisplayOrientation.PortraitDown
: DisplayOrientation.Portrait;
gameWindow.SetOrientation(orientation, false);
}
break;
default:
{
DisplayOrientation orientation = (currentOrientation == DisplayOrientation.LandscapeRight)
? DisplayOrientation.LandscapeRight
: DisplayOrientation.LandscapeLeft;
gameWindow.SetOrientation(orientation, false);
}
break;
}
gameWindow.SetOrientation(currentOrientation, false);

// ResetClientBounds
{
Expand Down
91 changes: 58 additions & 33 deletions Platforms/Game/.CardboardLegacy/AndroidCompatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ private Orientation GetDeviceNaturalOrientation(Activity activity)
if (screenOrientation != Android.Content.PM.ScreenOrientation.Landscape)
throw new InvalidOperationException("NaturalOrientation detection failed. Set ScreenOrientation in MainActivity to Landscape.");

if (((rotation == SurfaceOrientation.Rotation0 || rotation == SurfaceOrientation.Rotation180) &&
orientation == Orientation.Landscape)
|| ((rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270) &&
orientation == Orientation.Portrait))
if (((rotation == SurfaceOrientation.Rotation0 || rotation == SurfaceOrientation.Rotation180) && orientation == Orientation.Landscape)
|| ((rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270) && orientation == Orientation.Portrait))
{
return Orientation.Landscape;
}
Expand All @@ -88,14 +86,18 @@ internal DisplayOrientation GetAbsoluteOrientation(int degrees)
// Surprisingly 90 degree is landscape right, except on Kindle devices
switch (degrees)
{
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;
case 90:
if (FlipLandscape)
return DisplayOrientation.LandscapeLeft;
return DisplayOrientation.LandscapeRight;
case 270:
if (FlipLandscape)
return DisplayOrientation.LandscapeRight;
return DisplayOrientation.LandscapeLeft;

default:
return DisplayOrientation.Unknown;
Expand All @@ -109,38 +111,61 @@ internal DisplayOrientation GetAbsoluteOrientation(int degrees)
[CLSCompliant(false)]
public DisplayOrientation GetAbsoluteOrientation(Activity activity)
{
SurfaceOrientation orientation = activity.WindowManager.DefaultDisplay.Rotation;
Orientation orientation = activity.Resources.Configuration.Orientation;
SurfaceOrientation rotation = activity.WindowManager.DefaultDisplay.Rotation;

switch (orientation)
{
case SurfaceOrientation.Rotation90:
case Orientation.Portrait:
{
if (NaturalOrientation == Orientation.Landscape)
return DisplayOrientation.PortraitDown;
else
return (FlipLandscape) ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft;
switch (rotation)
{
case SurfaceOrientation.Rotation0:
default:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation != Orientation.Landscape);
return DisplayOrientation.Portrait;
case SurfaceOrientation.Rotation180:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation != Orientation.Landscape);
return DisplayOrientation.PortraitDown;
case SurfaceOrientation.Rotation90:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation == Orientation.Landscape);
return DisplayOrientation.PortraitDown;
case SurfaceOrientation.Rotation270:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation == Orientation.Landscape);
return DisplayOrientation.Portrait;
}
}
case SurfaceOrientation.Rotation180:
{
if (NaturalOrientation == Orientation.Landscape)
return (FlipLandscape) ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight;
else
return DisplayOrientation.PortraitDown;
}
case SurfaceOrientation.Rotation270:
{
if (NaturalOrientation == Orientation.Landscape)
return DisplayOrientation.Portrait;
else
return (FlipLandscape) ? DisplayOrientation.LandscapeLeft : DisplayOrientation.LandscapeRight;
}
case SurfaceOrientation.Rotation0:
break;

case Android.Content.Res.Orientation.Landscape:
default:
{
if (NaturalOrientation == Orientation.Landscape)
return (FlipLandscape) ? DisplayOrientation.LandscapeRight : DisplayOrientation.LandscapeLeft;
else
return DisplayOrientation.Portrait;
switch (rotation)
{
case SurfaceOrientation.Rotation0:
default:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation == Orientation.Landscape);
if (AndroidCompatibility.Current.FlipLandscape)
return DisplayOrientation.LandscapeRight;
return DisplayOrientation.LandscapeLeft;
case SurfaceOrientation.Rotation180:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation == Orientation.Landscape);
if (AndroidCompatibility.Current.FlipLandscape)
return DisplayOrientation.LandscapeLeft;
return DisplayOrientation.LandscapeRight;
case SurfaceOrientation.Rotation90:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation != Orientation.Landscape);
if (AndroidCompatibility.Current.FlipLandscape)
return DisplayOrientation.LandscapeRight;
return DisplayOrientation.LandscapeLeft;
case SurfaceOrientation.Rotation270:
System.Diagnostics.Debug.Assert(AndroidCompatibility.Current.NaturalOrientation != Orientation.Landscape);
if (AndroidCompatibility.Current.FlipLandscape)
return DisplayOrientation.LandscapeLeft;
return DisplayOrientation.LandscapeRight;
}
}
break;
}
}
}
Expand Down
20 changes: 1 addition & 19 deletions Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,7 @@ public override void CreateDevice()

Android.App.Activity activity = AndroidGameWindow.Activity;
DisplayOrientation currentOrientation = AndroidCompatibility.Current.GetAbsoluteOrientation(activity);
switch (activity.Resources.Configuration.Orientation)
{
case Android.Content.Res.Orientation.Portrait:
{
DisplayOrientation orientation = (currentOrientation == DisplayOrientation.PortraitDown)
? DisplayOrientation.PortraitDown
: DisplayOrientation.Portrait;
gameWindow.SetOrientation(orientation, false);
}
break;
default:
{
DisplayOrientation orientation = (currentOrientation == DisplayOrientation.LandscapeRight)
? DisplayOrientation.LandscapeRight
: DisplayOrientation.LandscapeLeft;
gameWindow.SetOrientation(orientation, false);
}
break;
}
gameWindow.SetOrientation(currentOrientation, false);

// ResetClientBounds
{
Expand Down
Loading