From 5d5fc88a7010e8f6598cb9e44ee746cdfca07314 Mon Sep 17 00:00:00 2001 From: sigmabeta Date: Tue, 9 Jun 2015 10:31:15 -0400 Subject: [PATCH] Android: Persist previously-set controller bindings on the Settings menu. --- .../activities/EmulationActivity.java | 5 +- .../dolphinemu/dialogs/MotionAlertDialog.java | 82 +- .../utils/InputBindingPreference.java | 12 +- .../dolphinemu/utils/InputConfigFragment.java | 103 -- .../app/src/main/res/xml/cpu_prefs.xml | 21 - .../app/src/main/res/xml/input_prefs.xml | 1057 ----------------- .../app/src/main/res/xml/preferences.xml | 92 +- .../app/src/main/res/xml/video_prefs.xml | 178 --- 8 files changed, 162 insertions(+), 1388 deletions(-) delete mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/InputConfigFragment.java delete mode 100644 Source/Android/app/src/main/res/xml/cpu_prefs.xml delete mode 100644 Source/Android/app/src/main/res/xml/input_prefs.xml delete mode 100644 Source/Android/app/src/main/res/xml/video_prefs.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 14ca650dd4..e1804abf96 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 @@ -18,7 +18,6 @@ import android.view.View; import org.dolphinemu.dolphinemu.NativeLibrary; import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.fragments.EmulationFragment; -import org.dolphinemu.dolphinemu.utils.InputConfigFragment; import java.util.List; @@ -274,7 +273,7 @@ public final class EmulationActivity extends Activity return false; } InputDevice input = event.getDevice(); - boolean handled = NativeLibrary.onGamePadEvent(InputConfigFragment.getInputDesc(input), event.getKeyCode(), action); + boolean handled = NativeLibrary.onGamePadEvent(input.getDescriptor(), event.getKeyCode(), action); return handled; } @@ -295,7 +294,7 @@ public final class EmulationActivity extends Activity for (InputDevice.MotionRange range : motions) { - NativeLibrary.onGamePadMoveEvent(InputConfigFragment.getInputDesc(input), range.getAxis(), event.getAxisValue(range.getAxis())); + NativeLibrary.onGamePadMoveEvent(input.getDescriptor(), range.getAxis(), event.getAxisValue(range.getAxis())); } return true; diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/MotionAlertDialog.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/MotionAlertDialog.java index 4f9b7de0a3..aea1f39df0 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/MotionAlertDialog.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/MotionAlertDialog.java @@ -2,14 +2,15 @@ package org.dolphinemu.dolphinemu.dialogs; import android.app.AlertDialog; import android.content.Context; +import android.content.SharedPreferences; import android.preference.Preference; +import android.preference.PreferenceManager; import android.util.Log; import android.view.InputDevice; import android.view.KeyEvent; import android.view.MotionEvent; import org.dolphinemu.dolphinemu.NativeLibrary; -import org.dolphinemu.dolphinemu.utils.InputConfigFragment; import java.util.ArrayList; import java.util.List; @@ -47,11 +48,10 @@ public final class MotionAlertDialog extends AlertDialog { case KeyEvent.ACTION_DOWN: case KeyEvent.ACTION_UP: + InputDevice input = event.getDevice(); - String bindStr = "Device '" + InputConfigFragment.getInputDesc(input) + "'-Button " + event.getKeyCode(); - NativeLibrary.SetConfig("Dolphin.ini", "Android", inputPref.getKey(), bindStr); - inputPref.setSummary(bindStr); - dismiss(); + saveInput(input, event, null, false); + return true; default: @@ -90,17 +90,11 @@ public final class MotionAlertDialog extends AlertDialog if (m_values.get(a) > (event.getAxisValue(range.getAxis()) + 0.5f)) { - String bindStr = "Device '" + InputConfigFragment.getInputDesc(input) + "'-Axis " + range.getAxis() + "-"; - NativeLibrary.SetConfig("Dolphin.ini", "Android", inputPref.getKey(), bindStr); - inputPref.setSummary(bindStr); - dismiss(); + saveInput(input, null, range, false); } else if (m_values.get(a) < (event.getAxisValue(range.getAxis()) - 0.5f)) { - String bindStr = "Device '" + InputConfigFragment.getInputDesc(input) + "'-Axis " + range.getAxis() + "+"; - NativeLibrary.SetConfig("Dolphin.ini", "Android", inputPref.getKey(), bindStr); - inputPref.setSummary(bindStr); - dismiss(); + saveInput(input, null, range, true); } } } @@ -125,4 +119,66 @@ public final class MotionAlertDialog extends AlertDialog return super.dispatchGenericMotionEvent(event); } + + /** + * Saves the provided input setting both to the INI file (so native code can use it) and as an + * Android preference (so it persists correctly, and is human-readable.) + * + * @param device Required; the InputDevice from which the input event originated. + * @param keyEvent If the event was a button push, this KeyEvent represents it and is required. + * @param motionRange If the event was an axis movement, this MotionRange represents it and is required. + * @param axisPositive If the event was an axis movement, this boolean indicates the direction and is required. + */ + private void saveInput(InputDevice device, KeyEvent keyEvent, InputDevice.MotionRange motionRange, boolean axisPositive) + { + String bindStr = null; + String uiString = null; + + if (keyEvent != null) + { + bindStr = "Device '" + device.getDescriptor() + "'-Button " + keyEvent.getKeyCode(); + uiString = device.getName() + ": Button " + keyEvent.getKeyCode(); + } + + if (motionRange != null) + { + if (axisPositive) + { + bindStr = "Device '" + device.getDescriptor() + "'-Axis " + motionRange.getAxis() + "+"; + uiString = device.getName() + ": Axis " + motionRange.getAxis() + "+"; + } + else + { + bindStr = "Device '" + device.getDescriptor() + "'-Axis " + motionRange.getAxis() + "-"; + uiString = device.getName() + ": Axis " + motionRange.getAxis() + "-"; + } + } + + if (bindStr != null) + { + NativeLibrary.SetConfig("Dolphin.ini", "Android", inputPref.getKey(), bindStr); + } + else + { + Log.e("DolphinEmu", "Failed to save input to INI."); + } + + + if (uiString != null) + { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext()); + SharedPreferences.Editor editor = preferences.edit(); + + editor.putString(inputPref.getKey(), uiString); + editor.apply(); + + inputPref.setSummary(uiString); + } + else + { + Log.e("DolphinEmu", "Failed to save input to preference."); + } + + dismiss(); + } } \ No newline at end of file diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/InputBindingPreference.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/InputBindingPreference.java index cb67b90a18..9e651fc102 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/InputBindingPreference.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/InputBindingPreference.java @@ -3,6 +3,7 @@ package org.dolphinemu.dolphinemu.utils; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; +import android.preference.EditTextPreference; import android.preference.Preference; import android.util.AttributeSet; @@ -13,7 +14,7 @@ import org.dolphinemu.dolphinemu.dialogs.MotionAlertDialog; * {@link Preference} subclass that represents a preference * used for assigning a key bind. */ -public final class InputBindingPreference extends Preference +public final class InputBindingPreference extends EditTextPreference { /** * Constructor that is called when inflating an InputBindingPreference from XML. @@ -53,4 +54,13 @@ public final class InputBindingPreference extends Preference // Everything is set, show the dialog. dialog.show(); } + + @Override + public CharSequence getSummary() + { + String summary = super.getSummary().toString(); + return String.format(summary, getText()); + } + + } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/InputConfigFragment.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/InputConfigFragment.java deleted file mode 100644 index ce1e4a2145..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/InputConfigFragment.java +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Copyright 2013 Dolphin Emulator Project - * Licensed under GPLv2+ - * Refer to the license.txt file included. - */ - -package org.dolphinemu.dolphinemu.utils; - -import android.app.Fragment; -import android.os.Build; -import android.os.Bundle; -import android.preference.Preference; -import android.preference.PreferenceFragment; -import android.view.InputDevice; - -import org.dolphinemu.dolphinemu.NativeLibrary; -import org.dolphinemu.dolphinemu.R; - -import java.util.List; - -/** - * The {@link Fragment} responsible for implementing the functionality - * within the input control mapping config. - */ -public final class InputConfigFragment extends PreferenceFragment -{ - @Override - public void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - - // Expand the preferences from the XML. - addPreferencesFromResource(R.xml.input_prefs); - - // Set the summary messages of the preferences to whatever binding - // is currently set within the Dolphin config. - final String[] gamecubeKeys = - { - "InputA", "InputB", "InputX", "InputY", "InputZ", "InputStart", - "DPadUp", "DPadDown", "DPadLeft", "DPadRight", - "MainUp", "MainDown", "MainLeft", "MainRight", - "CStickUp", "CStickDown", "CStickLeft", "CStickRight", - "InputL", "InputR" - }; - - final String[] wiimoteKeys = - { - "WiimoteInputA", "WiimoteInputB", "WiimoteInputOne", "WiimoteInputTwo", "WiimoteInputPlus", "WiimoteInputMinus", "WiimoteInputHome", - "WiimoteIRUp", "WiimoteIRDown", "WiimoteIRLeft", "WiimoteIRRight", "WiimoteIRForward", "WiimoteIRBackward", - "WiimoteSwingUp", "WiimoteSwingDown", "WiimoteSwingLeft", "WiimoteSwingRight", - "WiimoteTiltForward", "WiimoteTiltBackward", "WiimoteTiltLeft", "WiimoteTiltRight", - "WiimoteShakeX", "WiimoteShakeY", "WiimoteShakeZ", - "WiimoteDPadUp", "WiimoteDPadDown", "WiimoteDPadLeft", "WiimoteDPadRight" - }; - - for (int i = 0; i < 4; i++) - { - // Loop through the keys for all 4 GameCube controllers. - for (String key : gamecubeKeys) - { - final String binding = NativeLibrary.GetConfig("Dolphin.ini", "Android", key+"_"+i, "None"); - final Preference pref = findPreference(key+"_"+i); - pref.setSummary(binding); - } - - // Loop through the keys for the Wiimote - /*for (String key : wiimoteKeys) - { - final String binding = NativeLibrary.GetConfig("Dolphin.ini", "Android", key+"_"+i, "None"); - final Preference pref = findPreference(key+"_"+i); - pref.setSummary(binding); - }*/ - } - } - - /** - * Gets the descriptor for the given {@link InputDevice}. - * - * @param input The {@link InputDevice} to get the descriptor of. - * - * @return the descriptor for the given {@link InputDevice}. - */ - public static String getInputDesc(InputDevice input) - { - if (input == null) - return "null"; // Happens when the InputDevice is from an unknown source - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) - { - return input.getDescriptor(); - } - else - { - List motions = input.getMotionRanges(); - StringBuilder fakeid = new StringBuilder(); - - for (InputDevice.MotionRange range : motions) - fakeid.append(range.getAxis()); - - return fakeid.toString(); - } - } -} diff --git a/Source/Android/app/src/main/res/xml/cpu_prefs.xml b/Source/Android/app/src/main/res/xml/cpu_prefs.xml deleted file mode 100644 index 59ef76ed46..0000000000 --- a/Source/Android/app/src/main/res/xml/cpu_prefs.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/Source/Android/app/src/main/res/xml/input_prefs.xml b/Source/Android/app/src/main/res/xml/input_prefs.xml deleted file mode 100644 index 4ce2373af0..0000000000 --- a/Source/Android/app/src/main/res/xml/input_prefs.xml +++ /dev/null @@ -1,1057 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Source/Android/app/src/main/res/xml/preferences.xml b/Source/Android/app/src/main/res/xml/preferences.xml index a05cc7c14d..7e6a9f09ce 100644 --- a/Source/Android/app/src/main/res/xml/preferences.xml +++ b/Source/Android/app/src/main/res/xml/preferences.xml @@ -59,11 +59,9 @@ - - @@ -71,109 +69,126 @@ - - - @@ -181,105 +196,123 @@ - - @@ -290,109 +323,126 @@ - - - @@ -400,105 +450,123 @@ - - diff --git a/Source/Android/app/src/main/res/xml/video_prefs.xml b/Source/Android/app/src/main/res/xml/video_prefs.xml deleted file mode 100644 index 57c02e5944..0000000000 --- a/Source/Android/app/src/main/res/xml/video_prefs.xml +++ /dev/null @@ -1,178 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file