From 491425017781079785998c75db8172f784ee2140 Mon Sep 17 00:00:00 2001 From: bunnei Date: Mon, 22 Jul 2019 18:59:39 -0400 Subject: [PATCH] android: frontend: Add persistent changeable layout option for landscape mode. --- .../citra/citra_android/NativeLibrary.java | 7 +- .../activities/EmulationActivity.java | 105 ++++++++++++++---- .../citra_android/fragments/MenuFragment.java | 14 ++- .../citra_android/overlay/InputOverlay.java | 9 +- src/android/app/src/main/jni/native.cpp | 19 +--- src/android/app/src/main/jni/native.h | 5 +- .../app/src/main/res/menu/menu_emulation.xml | 22 +++- .../app/src/main/res/values/strings.xml | 6 +- 8 files changed, 128 insertions(+), 59 deletions(-) diff --git a/src/android/app/src/main/java/org/citra/citra_android/NativeLibrary.java b/src/android/app/src/main/java/org/citra/citra_android/NativeLibrary.java index a6f9ee3b3..2ec8554ef 100644 --- a/src/android/app/src/main/java/org/citra/citra_android/NativeLibrary.java +++ b/src/android/app/src/main/java/org/citra/citra_android/NativeLibrary.java @@ -275,15 +275,10 @@ public final class NativeLibrary { */ private static native void CacheClassesAndMethods(); - /** - * Switches the screen layout. - */ - public static native void SwitchScreenLayout(boolean is_portrait_mode); - /** * Notifies the core emulation that the orientation has changed. */ - public static native void NotifyOrientationChange(boolean is_portrait_mode); + public static native void NotifyOrientationChange(int layout_option, boolean is_portrait_mode); /** * Swaps the top and bottom screens. diff --git a/src/android/app/src/main/java/org/citra/citra_android/activities/EmulationActivity.java b/src/android/app/src/main/java/org/citra/citra_android/activities/EmulationActivity.java index 0811aa459..9a88ab123 100644 --- a/src/android/app/src/main/java/org/citra/citra_android/activities/EmulationActivity.java +++ b/src/android/app/src/main/java/org/citra/citra_android/activities/EmulationActivity.java @@ -57,9 +57,12 @@ public final class EmulationActivity extends AppCompatActivity { public static final int MENU_ACTION_ADJUST_SCALE = 2; public static final int MENU_ACTION_EXIT = 3; public static final int MENU_ACTION_TOGGLE_PREF_STATS = 4; - public static final int MENU_ACTION_SWITCH_SCREEN_LAYOUT = 5; - public static final int MENU_ACTION_SWAP_SCREENS = 6; - public static final int MENU_ACTION_RESET_OVERLAY = 7; + public static final int MENU_ACTION_SCREEN_LAYOUT_LANDSCAPE = 5; + public static final int MENU_ACTION_SCREEN_LAYOUT_PORTRAIT = 6; + public static final int MENU_ACTION_SCREEN_LAYOUT_SINGLE = 7; + public static final int MENU_ACTION_SCREEN_LAYOUT_SIDEBYSIDE = 8; + public static final int MENU_ACTION_SWAP_SCREENS = 9; + public static final int MENU_ACTION_RESET_OVERLAY = 10; private static final String BACKSTACK_NAME_MENU = "menu"; private static final String BACKSTACK_NAME_SUBMENU = "submenu"; private static SparseIntArray buttonsActionsMap = new SparseIntArray(); @@ -74,8 +77,14 @@ public final class EmulationActivity extends AppCompatActivity { buttonsActionsMap.append(R.id.menu_emulation_toggle_perf_stats, EmulationActivity.MENU_ACTION_TOGGLE_PREF_STATS); buttonsActionsMap.append(R.id.menu_exit, EmulationActivity.MENU_ACTION_EXIT); - buttonsActionsMap.append(R.id.menu_emulation_switch_screen_layout, - EmulationActivity.MENU_ACTION_SWITCH_SCREEN_LAYOUT); + buttonsActionsMap.append(R.id.menu_screen_layout_landscape, + EmulationActivity.MENU_ACTION_SCREEN_LAYOUT_LANDSCAPE); + buttonsActionsMap.append(R.id.menu_screen_layout_portrait, + EmulationActivity.MENU_ACTION_SCREEN_LAYOUT_PORTRAIT); + buttonsActionsMap.append(R.id.menu_screen_layout_single, + EmulationActivity.MENU_ACTION_SCREEN_LAYOUT_SINGLE); + buttonsActionsMap.append(R.id.menu_screen_layout_sidebyside, + EmulationActivity.MENU_ACTION_SCREEN_LAYOUT_SIDEBYSIDE); buttonsActionsMap.append(R.id.menu_emulation_swap_screens, EmulationActivity.MENU_ACTION_SWAP_SCREENS); buttonsActionsMap @@ -214,7 +223,6 @@ public final class EmulationActivity extends AppCompatActivity { } mPreferences = PreferenceManager.getDefaultSharedPreferences(this); - } @Override @@ -326,10 +334,38 @@ public final class EmulationActivity extends AppCompatActivity { .withEndAction(afterShowingScreenshot); } + // These must match what is defined in src/core/settings.h + private static final int LayoutOption_Default = 0; + private static final int LayoutOption_SingleScreen = 1; + private static final int LayoutOption_LargeScreen = 2; + private static final int LayoutOption_SideScreen = 3; + private static final int LayoutOption_MobilePortrait = 4; + private static final int LayoutOption_MobileLandscape = 5; + @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_emulation, menu); + + + // mPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); + int layoutOption = mPreferences.getInt("LandscapeScreenLayout", LayoutOption_MobileLandscape); + + int menuItemId = R.id.menu_screen_layout_landscape; + switch (layoutOption) { + case LayoutOption_SingleScreen: + menuItemId = R.id.menu_screen_layout_single; + break; + case LayoutOption_SideScreen: + menuItemId = R.id.menu_screen_layout_sidebyside; + break; + case LayoutOption_MobilePortrait: + menuItemId = R.id.menu_screen_layout_portrait; + break; + } + + menu.findItem(menuItemId).setChecked(true); + return true; } @@ -337,14 +373,8 @@ public final class EmulationActivity extends AppCompatActivity { @Override public boolean onOptionsItemSelected(MenuItem item) { int action = buttonsActionsMap.get(item.getItemId(), -1); - if (action >= 0) { - handleMenuAction(action); - } - return true; - } - public void handleMenuAction(@MenuAction int menuAction) { - switch (menuAction) { + switch (action) { // Edit the placement of the controls case MENU_ACTION_EDIT_CONTROLS_PLACEMENT: editControlsPlacement(); @@ -353,27 +383,42 @@ public final class EmulationActivity extends AppCompatActivity { // Enable/Disable specific buttons or the entire input overlay. case MENU_ACTION_TOGGLE_CONTROLS: toggleControls(); - return; + break; // Adjust the scale of the overlay controls. case MENU_ACTION_ADJUST_SCALE: adjustScale(); - return; + break; // Toggle the visibility of the Performance stats TextView case MENU_ACTION_TOGGLE_PREF_STATS: mEmulationFragment.togglePerfStatsVisibility(); - return; + break; - // Switch the layout of the screens - case MENU_ACTION_SWITCH_SCREEN_LAYOUT: - NativeLibrary.SwitchScreenLayout(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT); - return; + // Sets the screen layout to Landscape + case MENU_ACTION_SCREEN_LAYOUT_LANDSCAPE: + changeScreenOrientation(LayoutOption_MobileLandscape, item); + break; + + // Sets the screen layout to Portrait + case MENU_ACTION_SCREEN_LAYOUT_PORTRAIT: + changeScreenOrientation(LayoutOption_MobilePortrait, item); + break; + + // Sets the screen layout to Single + case MENU_ACTION_SCREEN_LAYOUT_SINGLE: + changeScreenOrientation(LayoutOption_SingleScreen, item); + break; + + // Sets the screen layout to Side by Side + case MENU_ACTION_SCREEN_LAYOUT_SIDEBYSIDE: + changeScreenOrientation(LayoutOption_SideScreen, item); + break; // Swap the top and bottom screen locations case MENU_ACTION_SWAP_SCREENS: NativeLibrary.SwapScreens(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT); - return; + break; // Reset overlay placement case MENU_ACTION_RESET_OVERLAY: @@ -384,8 +429,21 @@ public final class EmulationActivity extends AppCompatActivity { toggleMenu(); // Hide the menu (it will be showing since we just clicked it) mEmulationFragment.stopEmulation(); exitWithAnimation(); - return; + break; } + + return true; + } + + private void changeScreenOrientation(int layoutOption, MenuItem item) { + item.setChecked(true); + + NativeLibrary.NotifyOrientationChange(layoutOption, + getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT); + + final SharedPreferences.Editor editor = mPreferences.edit(); + editor.putInt("LandscapeScreenLayout", layoutOption); + editor.apply(); } private void editControlsPlacement() { @@ -560,7 +618,8 @@ public final class EmulationActivity extends AppCompatActivity { @Retention(SOURCE) @IntDef({MENU_ACTION_EDIT_CONTROLS_PLACEMENT, MENU_ACTION_TOGGLE_CONTROLS, MENU_ACTION_ADJUST_SCALE, - MENU_ACTION_EXIT, MENU_ACTION_TOGGLE_PREF_STATS, MENU_ACTION_SWITCH_SCREEN_LAYOUT, + MENU_ACTION_EXIT, MENU_ACTION_TOGGLE_PREF_STATS, MENU_ACTION_SCREEN_LAYOUT_LANDSCAPE, + MENU_ACTION_SCREEN_LAYOUT_PORTRAIT, MENU_ACTION_SCREEN_LAYOUT_SINGLE, MENU_ACTION_SCREEN_LAYOUT_SIDEBYSIDE, MENU_ACTION_SWAP_SCREENS, MENU_ACTION_RESET_OVERLAY}) public @interface MenuAction { } diff --git a/src/android/app/src/main/java/org/citra/citra_android/fragments/MenuFragment.java b/src/android/app/src/main/java/org/citra/citra_android/fragments/MenuFragment.java index b987527b8..1c6d0564c 100644 --- a/src/android/app/src/main/java/org/citra/citra_android/fragments/MenuFragment.java +++ b/src/android/app/src/main/java/org/citra/citra_android/fragments/MenuFragment.java @@ -22,8 +22,14 @@ public final class MenuFragment extends Fragment implements View.OnClickListener buttonsActionsMap.append(R.id.menu_emulation_toggle_perf_stats, EmulationActivity.MENU_ACTION_TOGGLE_PREF_STATS); buttonsActionsMap.append(R.id.menu_exit, EmulationActivity.MENU_ACTION_EXIT); - buttonsActionsMap.append(R.id.menu_emulation_switch_screen_layout, - EmulationActivity.MENU_ACTION_SWITCH_SCREEN_LAYOUT); + buttonsActionsMap.append(R.id.menu_screen_layout_landscape, + EmulationActivity.MENU_ACTION_SCREEN_LAYOUT_LANDSCAPE); + buttonsActionsMap.append(R.id.menu_screen_layout_portrait, + EmulationActivity.MENU_ACTION_SCREEN_LAYOUT_PORTRAIT); + buttonsActionsMap.append(R.id.menu_screen_layout_single, + EmulationActivity.MENU_ACTION_SCREEN_LAYOUT_SINGLE); + buttonsActionsMap.append(R.id.menu_screen_layout_sidebyside, + EmulationActivity.MENU_ACTION_SCREEN_LAYOUT_SIDEBYSIDE); buttonsActionsMap.append(R.id.menu_emulation_swap_screens, EmulationActivity.MENU_ACTION_SWAP_SCREENS); } @@ -62,9 +68,5 @@ public final class MenuFragment extends Fragment implements View.OnClickListener @SuppressWarnings("WrongConstant") @Override public void onClick(View button) { - int action = buttonsActionsMap.get(button.getId()); - if (action >= 0) { - ((EmulationActivity) getActivity()).handleMenuAction(action); - } } } diff --git a/src/android/app/src/main/java/org/citra/citra_android/overlay/InputOverlay.java b/src/android/app/src/main/java/org/citra/citra_android/overlay/InputOverlay.java index 375b11ddd..9ab60a7c0 100644 --- a/src/android/app/src/main/java/org/citra/citra_android/overlay/InputOverlay.java +++ b/src/android/app/src/main/java/org/citra/citra_android/overlay/InputOverlay.java @@ -344,7 +344,14 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener { joystick.draw(canvas); } - NativeLibrary.NotifyOrientationChange(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT); + // This is a convenient place to handle changes to orientation + HandleOrientationChange(); + } + + private void HandleOrientationChange() { + final int layoutOption = mPreferences.getInt("LandscapeScreenLayout",5 /*LayoutOption_MobileLandscape*/); + final boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT; + NativeLibrary.NotifyOrientationChange(layoutOption, isPortrait); } @Override diff --git a/src/android/app/src/main/jni/native.cpp b/src/android/app/src/main/jni/native.cpp index aefc0b322..b08237172 100644 --- a/src/android/app/src/main/jni/native.cpp +++ b/src/android/app/src/main/jni/native.cpp @@ -192,24 +192,9 @@ void Java_org_citra_citra_1android_NativeLibrary_CacheClassesAndMethods(JNIEnv* "(Ljava/lang/String;Ljava/lang/String;Z)Z"); } -void Java_org_citra_citra_1android_NativeLibrary_SwitchScreenLayout(JNIEnv* env, jobject obj, - jboolean is_portrait_mode) { - if (Settings::values.layout_option == Settings::LayoutOption::MobilePortrait) { - Settings::values.layout_option = Settings::LayoutOption::MobileLandscape; - } else if (Settings::values.layout_option == Settings::LayoutOption::MobileLandscape) { - Settings::values.layout_option = Settings::LayoutOption::SingleScreen; - } else if (Settings::values.layout_option == Settings::LayoutOption::SingleScreen) { - Settings::values.layout_option = Settings::LayoutOption::LargeScreen; - } else if (Settings::values.layout_option == Settings::LayoutOption::LargeScreen) { - Settings::values.layout_option = Settings::LayoutOption::SideScreen; - } else { - Settings::values.layout_option = Settings::LayoutOption::MobilePortrait; - } - VideoCore::g_renderer->UpdateCurrentFramebufferLayout(is_portrait_mode); -} - void Java_org_citra_citra_1android_NativeLibrary_NotifyOrientationChange( - JNIEnv* env, jobject obj, jboolean is_portrait_mode) { + JNIEnv* env, jobject obj, jint layout_option, jboolean is_portrait_mode) { + Settings::values.layout_option = static_cast(layout_option); VideoCore::g_renderer->UpdateCurrentFramebufferLayout(is_portrait_mode); } diff --git a/src/android/app/src/main/jni/native.h b/src/android/app/src/main/jni/native.h index 1cd72eba8..35917e367 100644 --- a/src/android/app/src/main/jni/native.h +++ b/src/android/app/src/main/jni/native.h @@ -139,11 +139,8 @@ JNIEXPORT void JNICALL Java_org_citra_citra_1android_NativeLibrary_WriteProfileR JNIEXPORT void JNICALL Java_org_citra_citra_1android_NativeLibrary_CacheClassesAndMethods(JNIEnv* env, jobject obj); -JNIEXPORT void JNICALL Java_org_citra_citra_1android_NativeLibrary_SwitchScreenLayout( - JNIEnv* env, jobject obj, jboolean is_portrait_mode); - JNIEXPORT void JNICALL Java_org_citra_citra_1android_NativeLibrary_NotifyOrientationChange( - JNIEnv* env, jobject obj, jboolean is_portrait_mode); + JNIEnv* env, jobject obj, jint layout_option, jboolean is_portrait_mode); JNIEXPORT void JNICALL Java_org_citra_citra_1android_NativeLibrary_SwapScreens( JNIEnv* env, jobject obj, jboolean is_portrait_mode); diff --git a/src/android/app/src/main/res/menu/menu_emulation.xml b/src/android/app/src/main/res/menu/menu_emulation.xml index d0442f6c8..d7e3581ff 100644 --- a/src/android/app/src/main/res/menu/menu_emulation.xml +++ b/src/android/app/src/main/res/menu/menu_emulation.xml @@ -33,7 +33,27 @@ + android:title="@string/emulation_switch_screen_layout"> + + + + + + + + + + + + Adjust Scale Choose Controller You may have to reload the game after changing extensions. - Switch Screen Layout + Landscape Screen Layout + Default + Portrait + Single Screen + Side by Side Screens Swap Screens Reset Overlay