Use initial orientation for device rotation instead of hardcoded values

This commit is contained in:
Benau 2021-05-01 16:39:06 +08:00
parent 3480a5da9b
commit 4081efbcaa
4 changed files with 26 additions and 8 deletions

View File

@ -65,6 +65,7 @@ public class SuperTuxKartActivity extends SDLActivity
private ImageView m_splash_screen; private ImageView m_splash_screen;
private STKEditText m_stk_edittext; private STKEditText m_stk_edittext;
private int m_bottom_y; private int m_bottom_y;
private int m_intial_orientation;
private float m_top_padding; private float m_top_padding;
private float m_bottom_padding; private float m_bottom_padding;
private float m_left_padding; private float m_left_padding;
@ -186,7 +187,7 @@ public class SuperTuxKartActivity extends SDLActivity
m_progress_dialog = null; m_progress_dialog = null;
m_progress_bar = null; m_progress_bar = null;
m_splash_screen = null; m_splash_screen = null;
m_bottom_y = 0; m_bottom_y = m_intial_orientation = 0;
m_top_padding = m_bottom_padding = m_left_padding = m_right_padding = m_top_padding = m_bottom_padding = m_left_padding = m_right_padding =
0.0f; 0.0f;
final View root = getWindow().getDecorView().findViewById( final View root = getWindow().getDecorView().findViewById(
@ -398,6 +399,9 @@ public class SuperTuxKartActivity extends SDLActivity
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
public float getRightPadding() { return m_right_padding; } public float getRightPadding() { return m_right_padding; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
public int getInitialOrientation() { return m_intial_orientation; }
// ------------------------------------------------------------------------
public void showExtractProgress(final int progress) public void showExtractProgress(final int progress)
{ {
runOnUiThread(new Runnable() runOnUiThread(new Runnable()
@ -485,6 +489,9 @@ public class SuperTuxKartActivity extends SDLActivity
m_bottom_padding = (float)dc.getBoundingRectBottom().height(); m_bottom_padding = (float)dc.getBoundingRectBottom().height();
m_left_padding = (float)dc.getBoundingRectLeft().width(); m_left_padding = (float)dc.getBoundingRectLeft().width();
m_right_padding = (float)dc.getBoundingRectRight().width(); m_right_padding = (float)dc.getBoundingRectRight().width();
// Left or right will depend on the device initial orientation
// So save it for dealing with device rotation later
m_intial_orientation = SDLActivity.getCurrentOrientation();
} }
} }
} }

View File

@ -62,7 +62,8 @@ ANDROID_HANDLE_PADDING_CALLBACK(ANDROID_PACKAGE_CALLBACK_NAME)
} }
extern "C" void Android_initDisplayCutout(float* top, float* bottom, extern "C" void Android_initDisplayCutout(float* top, float* bottom,
float* left, float* right) float* left, float* right,
int* initial_orientation)
{ {
JNIEnv* env = NULL; JNIEnv* env = NULL;
jobject activity = NULL; jobject activity = NULL;
@ -71,6 +72,7 @@ extern "C" void Android_initDisplayCutout(float* top, float* bottom,
jmethodID bottom_method = NULL; jmethodID bottom_method = NULL;
jmethodID left_method = NULL; jmethodID left_method = NULL;
jmethodID right_method = NULL; jmethodID right_method = NULL;
jmethodID initial_orientation_method = NULL;
env = (JNIEnv*)SDL_AndroidGetJNIEnv(); env = (JNIEnv*)SDL_AndroidGetJNIEnv();
if (!env) if (!env)
@ -104,6 +106,10 @@ extern "C" void Android_initDisplayCutout(float* top, float* bottom,
goto exit; goto exit;
*right = env->CallFloatMethod(activity, right_method); *right = env->CallFloatMethod(activity, right_method);
initial_orientation_method = env->GetMethodID(class_native_activity, "getInitialOrientation", "()I");
if (initial_orientation_method == NULL)
goto exit;
*initial_orientation = env->CallIntMethod(activity, initial_orientation_method);
exit: exit:
if (!env) if (!env)
return; return;

View File

@ -43,7 +43,7 @@ namespace irr
extern "C" void init_objc(SDL_SysWMinfo* info, float* top, float* bottom, float* left, float* right); extern "C" void init_objc(SDL_SysWMinfo* info, float* top, float* bottom, float* left, float* right);
extern "C" int handle_app_event(void* userdata, SDL_Event* event); extern "C" int handle_app_event(void* userdata, SDL_Event* event);
extern "C" void Android_initDisplayCutout(float* top, float* bottom, float* left, float* right); extern "C" void Android_initDisplayCutout(float* top, float* bottom, float* left, float* right, int* initial_orientation);
extern "C" int Android_disablePadding(); extern "C" int Android_disablePadding();
namespace irr namespace irr
@ -59,8 +59,8 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
MouseX(0), MouseY(0), MouseButtonStates(0), MouseX(0), MouseY(0), MouseButtonStates(0),
Width(param.WindowSize.Width), Height(param.WindowSize.Height), Width(param.WindowSize.Width), Height(param.WindowSize.Height),
TopPadding(0), BottomPadding(0), LeftPadding(0), RightPadding(0), TopPadding(0), BottomPadding(0), LeftPadding(0), RightPadding(0),
WindowHasFocus(false), WindowMinimized(false), Resizable(false), InitialOrientation(0), WindowHasFocus(false), WindowMinimized(false),
AccelerometerIndex(-1), AccelerometerInstance(-1), Resizable(false), AccelerometerIndex(-1), AccelerometerInstance(-1),
GyroscopeIndex(-1), GyroscopeInstance(-1) GyroscopeIndex(-1), GyroscopeInstance(-1)
{ {
#ifdef _DEBUG #ifdef _DEBUG
@ -117,7 +117,7 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
init_objc(&Info, &TopPadding, &BottomPadding, &LeftPadding, &RightPadding); init_objc(&Info, &TopPadding, &BottomPadding, &LeftPadding, &RightPadding);
#endif #endif
#ifdef ANDROID #ifdef ANDROID
Android_initDisplayCutout(&TopPadding, &BottomPadding, &LeftPadding, &RightPadding); Android_initDisplayCutout(&TopPadding, &BottomPadding, &LeftPadding, &RightPadding, &InitialOrientation);
#endif #endif
core::stringc sdlversion = "SDL Version "; core::stringc sdlversion = "SDL Version ";
sdlversion += Info.version.major; sdlversion += Info.version.major;
@ -1469,7 +1469,9 @@ s32 CIrrDeviceSDL::getLeftPadding()
if (Android_disablePadding() != 0) if (Android_disablePadding() != 0)
return 0; return 0;
#if SDL_VERSION_ATLEAST(2, 0, 9) #if SDL_VERSION_ATLEAST(2, 0, 9)
if (SDL_GetDisplayOrientation(0) == SDL_ORIENTATION_LANDSCAPE_FLIPPED) if ((InitialOrientation == SDL_ORIENTATION_LANDSCAPE ||
InitialOrientation == SDL_ORIENTATION_LANDSCAPE_FLIPPED) &&
SDL_GetDisplayOrientation(0) != InitialOrientation)
return RightPadding; return RightPadding;
#endif #endif
return LeftPadding; return LeftPadding;
@ -1485,7 +1487,9 @@ s32 CIrrDeviceSDL::getRightPadding()
#if SDL_VERSION_ATLEAST(2, 0, 9) #if SDL_VERSION_ATLEAST(2, 0, 9)
if (Android_disablePadding() != 0) if (Android_disablePadding() != 0)
return 0; return 0;
if (SDL_GetDisplayOrientation(0) == SDL_ORIENTATION_LANDSCAPE_FLIPPED) if ((InitialOrientation == SDL_ORIENTATION_LANDSCAPE ||
InitialOrientation == SDL_ORIENTATION_LANDSCAPE_FLIPPED) &&
SDL_GetDisplayOrientation(0) != InitialOrientation)
return LeftPadding; return LeftPadding;
#endif #endif
return RightPadding; return RightPadding;

View File

@ -291,6 +291,7 @@ namespace irr
f32 BottomPadding; f32 BottomPadding;
f32 LeftPadding; f32 LeftPadding;
f32 RightPadding; f32 RightPadding;
int InitialOrientation;
bool WindowHasFocus; bool WindowHasFocus;
bool WindowMinimized; bool WindowMinimized;