From d56f27857b76c9c7bc00a4f5e9de38e597fa7fa6 Mon Sep 17 00:00:00 2001 From: sigmabeta Date: Mon, 8 Jun 2015 20:07:43 -0400 Subject: [PATCH] Android: Don't remove input configuration related files from old UI. --- .../dolphinemu/dialogs/MotionAlertDialog.java | 128 ++++++ .../utils/InputBindingPreference.java | 56 +++ .../app/src/main/res/xml/input_prefs.xml | 384 +++++++++--------- .../app/src/main/res/xml/preferences.xml | 160 ++++---- 4 files changed, 456 insertions(+), 272 deletions(-) create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/MotionAlertDialog.java create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/InputBindingPreference.java 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 new file mode 100644 index 0000000000..4f9b7de0a3 --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/MotionAlertDialog.java @@ -0,0 +1,128 @@ +package org.dolphinemu.dolphinemu.dialogs; + +import android.app.AlertDialog; +import android.content.Context; +import android.preference.Preference; +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; + +/** + * {@link AlertDialog} derivative that listens for + * motion events from controllers and joysticks. + */ +public final class MotionAlertDialog extends AlertDialog +{ + // The selected input preference + private final Preference inputPref; + + private boolean firstEvent = true; + private final ArrayList m_values = new ArrayList(); + + /** + * Constructor + * + * @param ctx The current {@link Context}. + * @param inputPref The Preference to show this dialog for. + */ + public MotionAlertDialog(Context ctx, Preference inputPref) + { + super(ctx); + + this.inputPref = inputPref; + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) + { + Log.d("InputConfigFragment", "Received key event: " + event.getAction()); + switch (event.getAction()) + { + 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(); + return true; + + default: + return false; + } + } + + + // Method that will be called within dispatchGenericMotionEvent + // that handles joystick/controller movements. + private boolean onMotionEvent(MotionEvent event) + { + if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == 0) + return false; + + Log.d("InputConfigFragment", "Received motion event: " + event.getAction()); + + InputDevice input = event.getDevice(); + List motions = input.getMotionRanges(); + if (firstEvent) + { + m_values.clear(); + + for (InputDevice.MotionRange range : motions) + { + m_values.add(event.getAxisValue(range.getAxis())); + } + + firstEvent = false; + } + else + { + for (int a = 0; a < motions.size(); ++a) + { + InputDevice.MotionRange range = motions.get(a); + + 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(); + } + 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(); + } + } + } + + return true; + } + + @Override + public boolean dispatchKeyEvent(KeyEvent event) + { + if (onKeyDown(event.getKeyCode(), event)) + return true; + + return super.dispatchKeyEvent(event); + } + + @Override + public boolean dispatchGenericMotionEvent(MotionEvent event) + { + if (onMotionEvent(event)) + return true; + + return super.dispatchGenericMotionEvent(event); + } +} \ 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 new file mode 100644 index 0000000000..cb67b90a18 --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/InputBindingPreference.java @@ -0,0 +1,56 @@ +package org.dolphinemu.dolphinemu.utils; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.preference.Preference; +import android.util.AttributeSet; + +import org.dolphinemu.dolphinemu.R; +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 +{ + /** + * Constructor that is called when inflating an InputBindingPreference from XML. + * + * @param context The current {@link Context}. + * @param attrs The attributes of the XML tag that is inflating the preference. + */ + public InputBindingPreference(Context context, AttributeSet attrs) + { + super(context, attrs); + } + + @Override + protected void onClick() + { + // Begin the creation of the input alert. + final MotionAlertDialog dialog = new MotionAlertDialog(getContext(), this); + + // Set the cancel button. + dialog.setButton(AlertDialog.BUTTON_NEGATIVE, getContext().getString(R.string.cancel), new AlertDialog.OnClickListener() + { + @Override + public void onClick(DialogInterface dialog, int which) + { + // Do nothing. Just makes the cancel button show up. + } + }); + + // Set the title and description message. + dialog.setTitle(R.string.input_binding); + dialog.setMessage(String.format(getContext().getString(R.string.input_binding_descrip), getTitle())); + + // Don't allow the dialog to close when a user taps + // outside of it. They must press cancel or provide an input. + dialog.setCanceledOnTouchOutside(false); + + // Everything is set, show the dialog. + dialog.show(); + } +} diff --git a/Source/Android/app/src/main/res/xml/input_prefs.xml b/Source/Android/app/src/main/res/xml/input_prefs.xml index f6d8842f89..4ce2373af0 100644 --- a/Source/Android/app/src/main/res/xml/input_prefs.xml +++ b/Source/Android/app/src/main/res/xml/input_prefs.xml @@ -32,102 +32,102 @@ android:key="enableController1" android:title="@string/enable_controller"/> - - - - - - - - - - - - - - - - - - - - @@ -139,102 +139,102 @@ android:key="enableController2" android:title="@string/enable_controller"/> - - - - - - - - - - - - - - - - - - - - @@ -246,102 +246,102 @@ android:key="enableController3" android:title="@string/enable_controller"/> - - - - - - - - - - - - - - - - - - - - @@ -353,102 +353,102 @@ android:key="enableController4" android:title="@string/enable_controller"/> - - - - - - - - - - - - - - - - - - - - @@ -469,142 +469,142 @@ android:key="enableWiimote1" android:title="@string/enable_wiimote"/> - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -617,142 +617,142 @@ android:key="enableWiimote2" android:title="@string/enable_wiimote"/> - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -765,142 +765,142 @@ android:key="enableWiimote3" android:title="@string/enable_wiimote"/> - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -913,142 +913,142 @@ android:key="enableWiimote4" android:title="@string/enable_wiimote"/> - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Source/Android/app/src/main/res/xml/preferences.xml b/Source/Android/app/src/main/res/xml/preferences.xml index b2af687783..a05cc7c14d 100644 --- a/Source/Android/app/src/main/res/xml/preferences.xml +++ b/Source/Android/app/src/main/res/xml/preferences.xml @@ -68,102 +68,102 @@ android:key="enableController1" android:title="@string/enable_controller"/> - - - - - - - - - - - - - - - - - - - - @@ -178,102 +178,102 @@ android:key="enableController2" android:title="@string/enable_controller"/> - - - - - - - - - - - - - - - - - - - - @@ -287,102 +287,102 @@ android:key="enableController3" android:title="@string/enable_controller"/> - - - - - - - - - - - - - - - - - - - - @@ -397,102 +397,102 @@ android:key="enableController4" android:title="@string/enable_controller"/> - - - - - - - - - - - - - - - - - - - -