Skip to content

Commit

Permalink
refactor GetDeviceNaturalOrientation (#2236)
Browse files Browse the repository at this point in the history
* types

* move check

* move GetAbsoluteOrientation(Activity)

* switch (orientation)

* simplify

* switch (rotation)
  • Loading branch information
nkast authored Feb 25, 2025
1 parent 00a0f6b commit 82729f7
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 114 deletions.
123 changes: 85 additions & 38 deletions Platforms/Game/.Android/AndroidCompatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,53 +54,69 @@ private AndroidCompatibility(Activity activity)

private Orientation GetDeviceNaturalOrientation(Activity activity)
{
var orientation = activity.Resources.Configuration.Orientation;
SurfaceOrientation rotation = activity.WindowManager.DefaultDisplay.Rotation;

// check if MainActivity setup is correct.
var screenOrientation = activity.RequestedOrientation;
Android.Content.PM.ScreenOrientation screenOrientation = activity.RequestedOrientation;
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))
{
return Orientation.Landscape;
}
else
Orientation orientation = activity.Resources.Configuration.Orientation;
SurfaceOrientation rotation = activity.WindowManager.DefaultDisplay.Rotation;

switch (orientation)
{
return Orientation.Portrait;
}
}
case Orientation.Portrait:
{
switch (rotation)
{
case SurfaceOrientation.Rotation0:
case SurfaceOrientation.Rotation180:
return Orientation.Portrait;

internal DisplayOrientation GetAbsoluteOrientation(int degrees)
{
// Orientation is reported by the device in degrees compared to the natural orientation
// Some tablets have a natural landscape orientation, which we need to account for
if (NaturalOrientation == Orientation.Landscape)
degrees += 270;
case SurfaceOrientation.Rotation90:
case SurfaceOrientation.Rotation270:
return Orientation.Landscape;

// Round orientation into one of 8 positions, either 0, 45, 90, 135, 180, 225, 270, 315.
degrees = ( ((degrees + 22) / 45) * 45) % 360;
default:
return Orientation.Portrait;
}
}
break;

// Surprisingly 90 degree is landscape right, except on Kindle devices
switch (degrees)
{
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;
case Orientation.Landscape:
{
switch (rotation)
{
case SurfaceOrientation.Rotation0:
case SurfaceOrientation.Rotation180:
return Orientation.Landscape;

case SurfaceOrientation.Rotation90:
case SurfaceOrientation.Rotation270:
return Orientation.Portrait;

default:
return Orientation.Portrait;
}
}
break;

default:
return DisplayOrientation.Unknown;
{
switch (rotation)
{
case SurfaceOrientation.Rotation0:
case SurfaceOrientation.Rotation180:
return Orientation.Portrait;

case SurfaceOrientation.Rotation90:
case SurfaceOrientation.Rotation270:
return Orientation.Portrait;

default:
return Orientation.Portrait;
}
}
break;
}
}

Expand Down Expand Up @@ -137,7 +153,7 @@ public DisplayOrientation GetAbsoluteOrientation(Activity activity)
}
break;

case Android.Content.Res.Orientation.Landscape:
case Orientation.Landscape:
default:
{
switch (rotation)
Expand Down Expand Up @@ -168,5 +184,36 @@ public DisplayOrientation GetAbsoluteOrientation(Activity activity)
break;
}
}

internal DisplayOrientation GetAbsoluteOrientation(int degrees)
{
// Orientation is reported by the device in degrees compared to the natural orientation
// Some tablets have a natural landscape orientation, which we need to account for
if (NaturalOrientation == Orientation.Landscape)
degrees += 270;

// Round orientation into one of 8 positions, either 0, 45, 90, 135, 180, 225, 270, 315.
degrees = ( ((degrees + 22) / 45) * 45) % 360;

// Surprisingly 90 degree is landscape right, except on Kindle devices
switch (degrees)
{
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;
}
}
}
}
123 changes: 85 additions & 38 deletions Platforms/Game/.CardboardLegacy/AndroidCompatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,53 +54,69 @@ private AndroidCompatibility(Activity activity)

private Orientation GetDeviceNaturalOrientation(Activity activity)
{
var orientation = activity.Resources.Configuration.Orientation;
SurfaceOrientation rotation = activity.WindowManager.DefaultDisplay.Rotation;

// check if MainActivity setup is correct.
var screenOrientation = activity.RequestedOrientation;
Android.Content.PM.ScreenOrientation screenOrientation = activity.RequestedOrientation;
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))
{
return Orientation.Landscape;
}
else
Orientation orientation = activity.Resources.Configuration.Orientation;
SurfaceOrientation rotation = activity.WindowManager.DefaultDisplay.Rotation;

switch (orientation)
{
return Orientation.Portrait;
}
}
case Orientation.Portrait:
{
switch (rotation)
{
case SurfaceOrientation.Rotation0:
case SurfaceOrientation.Rotation180:
return Orientation.Portrait;

internal DisplayOrientation GetAbsoluteOrientation(int degrees)
{
// Orientation is reported by the device in degrees compared to the natural orientation
// Some tablets have a natural landscape orientation, which we need to account for
if (NaturalOrientation == Orientation.Landscape)
degrees += 270;
case SurfaceOrientation.Rotation90:
case SurfaceOrientation.Rotation270:
return Orientation.Landscape;

// Round orientation into one of 8 positions, either 0, 45, 90, 135, 180, 225, 270, 315.
degrees = ( ((degrees + 22) / 45) * 45) % 360;
default:
return Orientation.Portrait;
}
}
break;

// Surprisingly 90 degree is landscape right, except on Kindle devices
switch (degrees)
{
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;
case Orientation.Landscape:
{
switch (rotation)
{
case SurfaceOrientation.Rotation0:
case SurfaceOrientation.Rotation180:
return Orientation.Landscape;

case SurfaceOrientation.Rotation90:
case SurfaceOrientation.Rotation270:
return Orientation.Portrait;

default:
return Orientation.Portrait;
}
}
break;

default:
return DisplayOrientation.Unknown;
{
switch (rotation)
{
case SurfaceOrientation.Rotation0:
case SurfaceOrientation.Rotation180:
return Orientation.Portrait;

case SurfaceOrientation.Rotation90:
case SurfaceOrientation.Rotation270:
return Orientation.Portrait;

default:
return Orientation.Portrait;
}
}
break;
}
}

Expand Down Expand Up @@ -137,7 +153,7 @@ public DisplayOrientation GetAbsoluteOrientation(Activity activity)
}
break;

case Android.Content.Res.Orientation.Landscape:
case Orientation.Landscape:
default:
{
switch (rotation)
Expand Down Expand Up @@ -168,5 +184,36 @@ public DisplayOrientation GetAbsoluteOrientation(Activity activity)
break;
}
}

internal DisplayOrientation GetAbsoluteOrientation(int degrees)
{
// Orientation is reported by the device in degrees compared to the natural orientation
// Some tablets have a natural landscape orientation, which we need to account for
if (NaturalOrientation == Orientation.Landscape)
degrees += 270;

// Round orientation into one of 8 positions, either 0, 45, 90, 135, 180, 225, 270, 315.
degrees = ( ((degrees + 22) / 45) * 45) % 360;

// Surprisingly 90 degree is landscape right, except on Kindle devices
switch (degrees)
{
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;
}
}
}
}
Loading

0 comments on commit 82729f7

Please sign in to comment.