Skip to content

Commit

Permalink
refactor GetAbsoluteOrientation(..) (#2221)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nkast authored Feb 17, 2025
1 parent bc86fe7 commit 698b3c4
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 97 deletions.
80 changes: 53 additions & 27 deletions Platforms/Game/.Android/AndroidCompatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
Expand All @@ -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);
}
}
}
20 changes: 12 additions & 8 deletions Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
80 changes: 53 additions & 27 deletions Platforms/Game/.CardboardLegacy/AndroidCompatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
Expand All @@ -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);
}
}
}
20 changes: 12 additions & 8 deletions Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
80 changes: 53 additions & 27 deletions Platforms/Game/.Oculus/AndroidCompatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
Expand All @@ -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);
}
}
}

0 comments on commit 698b3c4

Please sign in to comment.