From e4c2d75198b10b6a0819d2f488dcd594df099acc Mon Sep 17 00:00:00 2001 From: Mike Harris Date: Tue, 26 Sep 2017 23:01:06 -0700 Subject: [PATCH] Combine SaveStateFragment and LoadStateFragment into one. Other than what action they send back to EmulationActivity.handleMenuAction(), they are the same. Change the menu-handling logic in EmulationActivity to keep track of a boolean for whether the submenu is visible, rather than keeping the fragment tag. There's only one fragment visible, so this makes more sense. --- .../activities/EmulationActivity.java | 81 +++++--------- .../fragments/LoadStateFragment.java | 69 ------------ .../fragments/SaveLoadStateFragment.java | 100 ++++++++++++++++++ .../fragments/SaveStateFragment.java | 69 ------------ ...e_load.xml => fragment_saveload_state.xml} | 14 +-- .../main/res/layout/fragment_state_save.xml | 60 ----------- 6 files changed, 135 insertions(+), 258 deletions(-) delete mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/LoadStateFragment.java create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/SaveLoadStateFragment.java delete mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/SaveStateFragment.java rename Source/Android/app/src/main/res/layout/{fragment_state_load.xml => fragment_saveload_state.xml} (84%) delete mode 100644 Source/Android/app/src/main/res/layout/fragment_state_save.xml diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java index b1e53ebb6a..3162b650d3 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/activities/EmulationActivity.java @@ -37,9 +37,8 @@ import com.squareup.picasso.Picasso; import org.dolphinemu.dolphinemu.NativeLibrary; import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.fragments.EmulationFragment; -import org.dolphinemu.dolphinemu.fragments.LoadStateFragment; import org.dolphinemu.dolphinemu.fragments.MenuFragment; -import org.dolphinemu.dolphinemu.fragments.SaveStateFragment; +import org.dolphinemu.dolphinemu.fragments.SaveLoadStateFragment; import org.dolphinemu.dolphinemu.ui.main.MainPresenter; import org.dolphinemu.dolphinemu.ui.platform.Platform; import org.dolphinemu.dolphinemu.utils.Animations; @@ -54,14 +53,13 @@ import static java.lang.annotation.RetentionPolicy.SOURCE; public final class EmulationActivity extends AppCompatActivity { + private static final String FRAGMENT_SUBMENU_TAG = "submenu"; private View mDecorView; private ImageView mImageView; private FrameLayout mFrameEmulation; private LinearLayout mMenuLayout; - private String mSubmenuFragmentTag; - private SharedPreferences mPreferences; // So that MainActivity knows which view to invalidate before the return animation. @@ -70,6 +68,7 @@ public final class EmulationActivity extends AppCompatActivity private boolean mDeviceHasTouchScreen; private boolean mSystemUiVisible; private boolean mMenuVisible; + private boolean mSubMenuVisible = false; private static boolean mIsGameCubeGame; @@ -371,7 +370,7 @@ public final class EmulationActivity extends AppCompatActivity { if (!mDeviceHasTouchScreen) { - if (mSubmenuFragmentTag != null) + if (mSubMenuVisible) { removeSubMenu(); } @@ -543,14 +542,14 @@ public final class EmulationActivity extends AppCompatActivity case MENU_ACTION_SAVE_ROOT: if (!mDeviceHasTouchScreen) { - showMenu(MenuType.SAVE); + showMenu(SaveLoadStateFragment.SaveOrLoad.SAVE); } return; case MENU_ACTION_LOAD_ROOT: if (!mDeviceHasTouchScreen) { - showMenu(MenuType.LOAD); + showMenu(SaveLoadStateFragment.SaveOrLoad.LOAD); } return; @@ -883,68 +882,44 @@ public final class EmulationActivity extends AppCompatActivity }); } - private void showMenu(MenuType menuId) + private void showMenu(SaveLoadStateFragment.SaveOrLoad saveOrLoad) { - Fragment fragment; - - switch (menuId) - { - case SAVE: - fragment = SaveStateFragment.newInstance(); - mSubmenuFragmentTag = SaveStateFragment.FRAGMENT_TAG; - break; - - case LOAD: - fragment = LoadStateFragment.newInstance(); - mSubmenuFragmentTag = LoadStateFragment.FRAGMENT_TAG; - break; - - default: - return; - } - + Fragment fragment = SaveLoadStateFragment.newInstance(saveOrLoad); getFragmentManager().beginTransaction() .setCustomAnimations(R.animator.menu_slide_in, R.animator.menu_slide_out) - .replace(R.id.frame_submenu, fragment, mSubmenuFragmentTag) + .replace(R.id.frame_submenu, fragment, FRAGMENT_SUBMENU_TAG) .commit(); + mSubMenuVisible = true; } private void removeSubMenu() { - if (mSubmenuFragmentTag != null) + final Fragment fragment = getFragmentManager().findFragmentByTag(FRAGMENT_SUBMENU_TAG); + + if (fragment != null) { - final Fragment fragment = getFragmentManager().findFragmentByTag(mSubmenuFragmentTag); - - if (fragment != null) - { - // When removing a fragment without replacement, its animation must be done - // manually beforehand. - Animations.fadeViewOutToRight(fragment.getView()) - .withEndAction(new Runnable() + // When removing a fragment without replacement, its animation must be done + // manually beforehand. + Animations.fadeViewOutToRight(fragment.getView()) + .withEndAction(new Runnable() + { + @Override + public void run() { - @Override - public void run() + if (mMenuVisible) { - if (mMenuVisible) - { - getFragmentManager().beginTransaction() - .remove(fragment) - .commit(); - } + getFragmentManager().beginTransaction() + .remove(fragment) + .commit(); } - }); - } - else - { - Log.error("[EmulationActivity] Fragment not found, can't remove."); - } - - mSubmenuFragmentTag = null; + } + }); } else { - Log.error("[EmulationActivity] Fragment Tag empty."); + Log.error("[EmulationActivity] Fragment not found, can't remove."); } + mSubMenuVisible = false; } public String getSelectedTitle() diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/LoadStateFragment.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/LoadStateFragment.java deleted file mode 100644 index 41b24f929a..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/LoadStateFragment.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.dolphinemu.dolphinemu.fragments; - -import android.app.Fragment; -import android.os.Bundle; -import android.support.annotation.Nullable; -import android.util.SparseIntArray; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.GridLayout; - -import org.dolphinemu.dolphinemu.R; -import org.dolphinemu.dolphinemu.activities.EmulationActivity; - -public final class LoadStateFragment extends Fragment implements View.OnClickListener -{ - public static final String FRAGMENT_TAG = "load_state"; - - private static SparseIntArray buttonsActionsMap = new SparseIntArray(); - static { - buttonsActionsMap.append(R.id.menu_emulation_load_1, EmulationActivity.MENU_ACTION_LOAD_SLOT1); - buttonsActionsMap.append(R.id.menu_emulation_load_2, EmulationActivity.MENU_ACTION_LOAD_SLOT2); - buttonsActionsMap.append(R.id.menu_emulation_load_3, EmulationActivity.MENU_ACTION_LOAD_SLOT3); - buttonsActionsMap.append(R.id.menu_emulation_load_4, EmulationActivity.MENU_ACTION_LOAD_SLOT4); - buttonsActionsMap.append(R.id.menu_emulation_load_5, EmulationActivity.MENU_ACTION_LOAD_SLOT5); - buttonsActionsMap.append(R.id.menu_emulation_load_6, EmulationActivity.MENU_ACTION_LOAD_SLOT6); - } - - public static LoadStateFragment newInstance() - { - LoadStateFragment fragment = new LoadStateFragment(); - - // TODO Add any appropriate arguments to this fragment. - - return fragment; - } - - @Nullable - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) - { - View rootView = inflater.inflate(R.layout.fragment_state_load, container, false); - - GridLayout grid = (GridLayout) rootView.findViewById(R.id.grid_state_slots); - for (int childIndex = 0; childIndex < grid.getChildCount(); childIndex++) - { - Button button = (Button) grid.getChildAt(childIndex); - - button.setOnClickListener(this); - } - - // So that item clicked to start this Fragment is no longer the focused item. - grid.requestFocus(); - - return rootView; - } - - @SuppressWarnings("WrongConstant") - @Override - public void onClick(View button) - { - int action = buttonsActionsMap.get(button.getId(), -1); - if (action >= 0) - { - ((EmulationActivity) getActivity()).handleMenuAction(action); - } - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/SaveLoadStateFragment.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/SaveLoadStateFragment.java new file mode 100644 index 0000000000..2752a8d4b3 --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/SaveLoadStateFragment.java @@ -0,0 +1,100 @@ +package org.dolphinemu.dolphinemu.fragments; + +import android.app.Fragment; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.util.SparseIntArray; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.GridLayout; + +import org.dolphinemu.dolphinemu.R; +import org.dolphinemu.dolphinemu.activities.EmulationActivity; + +public final class SaveLoadStateFragment extends Fragment implements View.OnClickListener +{ + public enum SaveOrLoad + { + SAVE, LOAD + } + + private static final String KEY_SAVEORLOAD = "saveorload"; + private static SparseIntArray saveButtonsActionsMap = new SparseIntArray(); + static { + saveButtonsActionsMap.append(R.id.loadsave_state_button_1, EmulationActivity.MENU_ACTION_SAVE_SLOT1); + saveButtonsActionsMap.append(R.id.loadsave_state_button_2, EmulationActivity.MENU_ACTION_SAVE_SLOT2); + saveButtonsActionsMap.append(R.id.loadsave_state_button_3, EmulationActivity.MENU_ACTION_SAVE_SLOT3); + saveButtonsActionsMap.append(R.id.loadsave_state_button_4, EmulationActivity.MENU_ACTION_SAVE_SLOT4); + saveButtonsActionsMap.append(R.id.loadsave_state_button_5, EmulationActivity.MENU_ACTION_SAVE_SLOT5); + saveButtonsActionsMap.append(R.id.loadsave_state_button_6, EmulationActivity.MENU_ACTION_SAVE_SLOT6); + } + private static SparseIntArray loadButtonsActionsMap = new SparseIntArray(); + static { + loadButtonsActionsMap.append(R.id.loadsave_state_button_1, EmulationActivity.MENU_ACTION_LOAD_SLOT1); + loadButtonsActionsMap.append(R.id.loadsave_state_button_2, EmulationActivity.MENU_ACTION_LOAD_SLOT2); + loadButtonsActionsMap.append(R.id.loadsave_state_button_3, EmulationActivity.MENU_ACTION_LOAD_SLOT3); + loadButtonsActionsMap.append(R.id.loadsave_state_button_4, EmulationActivity.MENU_ACTION_LOAD_SLOT4); + loadButtonsActionsMap.append(R.id.loadsave_state_button_5, EmulationActivity.MENU_ACTION_LOAD_SLOT5); + loadButtonsActionsMap.append(R.id.loadsave_state_button_6, EmulationActivity.MENU_ACTION_LOAD_SLOT6); + } + private SaveOrLoad mSaveOrLoad; + + public static SaveLoadStateFragment newInstance(SaveOrLoad saveOrLoad) + { + SaveLoadStateFragment fragment = new SaveLoadStateFragment(); + + Bundle arguments = new Bundle(); + arguments.putSerializable(KEY_SAVEORLOAD, saveOrLoad); + fragment.setArguments(arguments); + + return fragment; + } + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + mSaveOrLoad = (SaveOrLoad) getArguments().getSerializable(KEY_SAVEORLOAD); + } + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) + { + View rootView = inflater.inflate(R.layout.fragment_saveload_state, container, false); + + GridLayout grid = (GridLayout) rootView.findViewById(R.id.grid_state_slots); + for (int childIndex = 0; childIndex < grid.getChildCount(); childIndex++) + { + Button button = (Button) grid.getChildAt(childIndex); + button.setOnClickListener(this); + } + + // So that item clicked to start this Fragment is no longer the focused item. + grid.requestFocus(); + + return rootView; + } + + @SuppressWarnings("WrongConstant") + @Override + public void onClick(View button) + { + int action = 0; + switch(mSaveOrLoad) + { + case SAVE: + action = saveButtonsActionsMap.get(button.getId(), -1); + break; + case LOAD: + action = loadButtonsActionsMap.get(button.getId(), -1); + } + if (action >= 0) + { + ((EmulationActivity) getActivity()).handleMenuAction(action); + } + } +} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/SaveStateFragment.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/SaveStateFragment.java deleted file mode 100644 index b0ae7d9818..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/SaveStateFragment.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.dolphinemu.dolphinemu.fragments; - -import android.app.Fragment; -import android.os.Bundle; -import android.support.annotation.Nullable; -import android.util.SparseIntArray; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.GridLayout; - -import org.dolphinemu.dolphinemu.R; -import org.dolphinemu.dolphinemu.activities.EmulationActivity; - -public final class SaveStateFragment extends Fragment implements View.OnClickListener -{ - public static final String FRAGMENT_TAG = "save_state"; - - private static SparseIntArray buttonsActionsMap = new SparseIntArray(); - static { - buttonsActionsMap.append(R.id.menu_emulation_save_1, EmulationActivity.MENU_ACTION_SAVE_SLOT1); - buttonsActionsMap.append(R.id.menu_emulation_save_2, EmulationActivity.MENU_ACTION_SAVE_SLOT2); - buttonsActionsMap.append(R.id.menu_emulation_save_3, EmulationActivity.MENU_ACTION_SAVE_SLOT3); - buttonsActionsMap.append(R.id.menu_emulation_save_4, EmulationActivity.MENU_ACTION_SAVE_SLOT4); - buttonsActionsMap.append(R.id.menu_emulation_save_5, EmulationActivity.MENU_ACTION_SAVE_SLOT5); - buttonsActionsMap.append(R.id.menu_emulation_save_6, EmulationActivity.MENU_ACTION_SAVE_SLOT6); - } - - public static SaveStateFragment newInstance() - { - SaveStateFragment fragment = new SaveStateFragment(); - - // TODO Add any appropriate arguments to this fragment. - - return fragment; - } - - @Nullable - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) - { - View rootView = inflater.inflate(R.layout.fragment_state_save, container, false); - - GridLayout grid = (GridLayout) rootView.findViewById(R.id.grid_state_slots); - for (int childIndex = 0; childIndex < grid.getChildCount(); childIndex++) - { - Button button = (Button) grid.getChildAt(childIndex); - - button.setOnClickListener(this); - } - - // So that item clicked to start this Fragment is no longer the focused item. - grid.requestFocus(); - - return rootView; - } - - @SuppressWarnings("WrongConstant") - @Override - public void onClick(View button) - { - int action = buttonsActionsMap.get(button.getId(), -1); - if (action >= 0) - { - ((EmulationActivity) getActivity()).handleMenuAction(action); - } - } -} diff --git a/Source/Android/app/src/main/res/layout/fragment_state_load.xml b/Source/Android/app/src/main/res/layout/fragment_saveload_state.xml similarity index 84% rename from Source/Android/app/src/main/res/layout/fragment_state_load.xml rename to Source/Android/app/src/main/res/layout/fragment_saveload_state.xml index ec8d80def1..64ba331553 100644 --- a/Source/Android/app/src/main/res/layout/fragment_state_load.xml +++ b/Source/Android/app/src/main/res/layout/fragment_saveload_state.xml @@ -14,42 +14,42 @@ android:layout_gravity="center">