Android: Option to toggle on/off individual buttons

This commit is contained in:
Sean Maas 2016-09-22 18:25:23 -04:00
parent 50b44fa337
commit 313be655f4
5 changed files with 249 additions and 45 deletions

View File

@ -2,13 +2,17 @@ package org.dolphinemu.dolphinemu.activities;
import android.app.Activity;
import android.app.ActivityOptions;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.InputDevice;
import android.view.KeyEvent;
@ -48,6 +52,8 @@ public final class EmulationActivity extends AppCompatActivity
private String mSubmenuFragmentTag;
private SharedPreferences mPreferences;
// So that MainActivity knows which view to invalidate before the return animation.
private int mPosition;
@ -212,6 +218,8 @@ public final class EmulationActivity extends AppCompatActivity
}
}
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mIsGameCubeGame = (NativeLibrary.GetPlatform(path) == 0);
}
@ -387,13 +395,92 @@ public final class EmulationActivity extends AppCompatActivity
{
switch (id)
{
// Enable/Disable input overlay.
// Enable/Disable specific buttons or the entire input overlay.
case R.id.menu_emulation_input_overlay:
{
boolean[] enabledButtons = new boolean[11];
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.emulation_toggle_input);
if (mIsGameCubeGame)
{
for (int i = 0; i < enabledButtons.length; i++)
{
enabledButtons[i] = mPreferences.getBoolean("buttonToggleGc" + i, true);
}
builder.setMultiChoiceItems(R.array.gcpadButtons, enabledButtons,
new DialogInterface.OnMultiChoiceClickListener()
{
@Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked)
{
SharedPreferences.Editor editor = mPreferences.edit();
if (mPreferences.getBoolean("buttonToggleGc" + indexSelected, true))
{
// If the button is enabled, disable it.
editor.putBoolean("buttonToggleGc" + indexSelected, false);
}
else
{
// If the button is disabled, enable it.
editor.putBoolean("buttonToggleGc" + indexSelected, true);
}
editor.apply();
}
});
}
else
{
for (int i = 0; i < enabledButtons.length; i++)
{
enabledButtons[i] = mPreferences.getBoolean("buttonToggleWii" + i, true);
}
builder.setMultiChoiceItems(R.array.wiimoteButtons, enabledButtons,
new DialogInterface.OnMultiChoiceClickListener()
{
@Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked)
{
SharedPreferences.Editor editor = mPreferences.edit();
if (mPreferences.getBoolean("buttonToggleWii" + indexSelected, true))
{
// If the button is enabled, disable it.
editor.putBoolean("buttonToggleWii" + indexSelected, false);
}
else
{
// If the button is disabled, enable it.
editor.putBoolean("buttonToggleWii" + indexSelected, true);
}
editor.apply();
}
});
}
builder.setNeutralButton(getString(R.string.emulation_toggle_all), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
EmulationFragment emulationFragment = (EmulationFragment) getFragmentManager()
.findFragmentByTag(EmulationFragment.FRAGMENT_TAG);
emulationFragment.toggleInputOverlayVisibility();
}
});
builder.setPositiveButton(getString(R.string.emulation_done), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
EmulationFragment emulationFragment = (EmulationFragment) getFragmentManager()
.findFragmentByTag(EmulationFragment.FRAGMENT_TAG);
emulationFragment.refreshInputOverlay();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
return;
}

View File

@ -162,6 +162,11 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
editor.apply();
}
public void refreshInputOverlay()
{
mInputOverlay.refreshControls();
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{

View File

@ -46,6 +46,8 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
private InputOverlayDrawableDpad mDpadBeingConfigured;
private InputOverlayDrawableJoystick mJoystickBeingConfigured;
private SharedPreferences mPreferences;
/**
* Resizes a {@link Bitmap} by a given scale factor
*
@ -78,47 +80,10 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
{
super(context, attrs);
// Add all the overlay items to the HashSet.
if (EmulationActivity.isGameCubeGame())
{
// GameCube
overlayButtons.add(initializeOverlayButton(context, R.drawable.gcpad_a, ButtonType.BUTTON_A));
overlayButtons.add(initializeOverlayButton(context, R.drawable.gcpad_b, ButtonType.BUTTON_B));
overlayButtons.add(initializeOverlayButton(context, R.drawable.gcpad_x, ButtonType.BUTTON_X));
overlayButtons.add(initializeOverlayButton(context, R.drawable.gcpad_y, ButtonType.BUTTON_Y));
overlayButtons.add(initializeOverlayButton(context, R.drawable.gcpad_z, ButtonType.BUTTON_Z));
overlayButtons.add(initializeOverlayButton(context, R.drawable.gcpad_start, ButtonType.BUTTON_START));
overlayButtons.add(initializeOverlayButton(context, R.drawable.gcpad_l, ButtonType.TRIGGER_L));
overlayButtons.add(initializeOverlayButton(context, R.drawable.gcpad_r, ButtonType.TRIGGER_R));
overlayDpads.add(initializeOverlayDpad(context, R.drawable.gcwii_dpad,
ButtonType.BUTTON_UP, ButtonType.BUTTON_DOWN,
ButtonType.BUTTON_LEFT, ButtonType.BUTTON_RIGHT));
overlayJoysticks.add(initializeOverlayJoystick(context,
R.drawable.gcwii_joystick_range, R.drawable.gcwii_joystick,
ButtonType.STICK_MAIN));
overlayJoysticks.add(initializeOverlayJoystick(context,
R.drawable.gcwii_joystick_range, R.drawable.gcpad_c,
ButtonType.STICK_C));
}
else
{
// Wiimote + Nunchuk
overlayButtons.add(initializeOverlayButton(context, R.drawable.wiimote_a, ButtonType.WIIMOTE_BUTTON_A));
overlayButtons.add(initializeOverlayButton(context, R.drawable.wiimote_b, ButtonType.WIIMOTE_BUTTON_B));
overlayButtons.add(initializeOverlayButton(context, R.drawable.wiimote_one, ButtonType.WIIMOTE_BUTTON_1));
overlayButtons.add(initializeOverlayButton(context, R.drawable.wiimote_two, ButtonType.WIIMOTE_BUTTON_2));
overlayButtons.add(initializeOverlayButton(context, R.drawable.wiimote_plus, ButtonType.WIIMOTE_BUTTON_PLUS));
overlayButtons.add(initializeOverlayButton(context, R.drawable.wiimote_minus, ButtonType.WIIMOTE_BUTTON_MINUS));
overlayButtons.add(initializeOverlayButton(context, R.drawable.wiimote_home, ButtonType.WIIMOTE_BUTTON_HOME));
overlayButtons.add(initializeOverlayButton(context, R.drawable.nunchuk_c, ButtonType.NUNCHUK_BUTTON_C));
overlayButtons.add(initializeOverlayButton(context, R.drawable.nunchuk_z, ButtonType.NUNCHUK_BUTTON_Z));
overlayDpads.add(initializeOverlayDpad(context, R.drawable.gcwii_dpad,
ButtonType.WIIMOTE_UP, ButtonType.WIIMOTE_DOWN,
ButtonType.WIIMOTE_LEFT, ButtonType.WIIMOTE_RIGHT));
overlayJoysticks.add(initializeOverlayJoystick(context,
R.drawable.gcwii_joystick_range, R.drawable.gcwii_joystick,
ButtonType.NUNCHUK_STICK));
}
mPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
// Load the controls.
refreshControls();
// Set the on touch listener.
setOnTouchListener(this);
@ -358,6 +323,124 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
return true;
}
public void refreshControls()
{
// Remove all the overlay buttons from the HashSet.
overlayButtons.removeAll(overlayButtons);
overlayDpads.removeAll(overlayDpads);
overlayJoysticks.removeAll(overlayJoysticks);
// Add all the enabled overlay items back to the HashSet.
if (EmulationActivity.isGameCubeGame())
{
// GameCube
if (mPreferences.getBoolean("buttonToggleGc0", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.gcpad_a, ButtonType.BUTTON_A));
}
if (mPreferences.getBoolean("buttonToggleGc1", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.gcpad_b, ButtonType.BUTTON_B));
}
if (mPreferences.getBoolean("buttonToggleGc2", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.gcpad_x, ButtonType.BUTTON_X));
}
if (mPreferences.getBoolean("buttonToggleGc3", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.gcpad_y, ButtonType.BUTTON_Y));
}
if (mPreferences.getBoolean("buttonToggleGc4", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.gcpad_z, ButtonType.BUTTON_Z));
}
if (mPreferences.getBoolean("buttonToggleGc5", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.gcpad_start, ButtonType.BUTTON_START));
}
if (mPreferences.getBoolean("buttonToggleGc6", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.gcpad_l, ButtonType.TRIGGER_L));
}
if (mPreferences.getBoolean("buttonToggleGc7", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.gcpad_r, ButtonType.TRIGGER_R));
}
if (mPreferences.getBoolean("buttonToggleGc8", true))
{
overlayDpads.add(initializeOverlayDpad(getContext(), R.drawable.gcwii_dpad,
ButtonType.BUTTON_UP, ButtonType.BUTTON_DOWN,
ButtonType.BUTTON_LEFT, ButtonType.BUTTON_RIGHT));
}
if (mPreferences.getBoolean("buttonToggleGc9", true))
{
overlayJoysticks.add(initializeOverlayJoystick(getContext(),
R.drawable.gcwii_joystick_range, R.drawable.gcwii_joystick,
ButtonType.STICK_MAIN));
}
if (mPreferences.getBoolean("buttonToggleGc10", true))
{
overlayJoysticks.add(initializeOverlayJoystick(getContext(),
R.drawable.gcwii_joystick_range, R.drawable.gcpad_c,
ButtonType.STICK_C));
}
}
else
{
// Wiimote + Nunchuk
if (mPreferences.getBoolean("buttonToggleWii0", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.wiimote_a, ButtonType.WIIMOTE_BUTTON_A));
}
if (mPreferences.getBoolean("buttonToggleWii1", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.wiimote_b, ButtonType.WIIMOTE_BUTTON_B));
}
if (mPreferences.getBoolean("buttonToggleWii2", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.wiimote_one, ButtonType.WIIMOTE_BUTTON_1));
}
if (mPreferences.getBoolean("buttonToggleWii3", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.wiimote_two, ButtonType.WIIMOTE_BUTTON_2));
}
if (mPreferences.getBoolean("buttonToggleWii4", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.wiimote_plus, ButtonType.WIIMOTE_BUTTON_PLUS));
}
if (mPreferences.getBoolean("buttonToggleWii5", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.wiimote_minus, ButtonType.WIIMOTE_BUTTON_MINUS));
}
if (mPreferences.getBoolean("buttonToggleWii6", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.wiimote_home, ButtonType.WIIMOTE_BUTTON_HOME));
}
if (mPreferences.getBoolean("buttonToggleWii7", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.nunchuk_c, ButtonType.NUNCHUK_BUTTON_C));
}
if (mPreferences.getBoolean("buttonToggleWii8", true))
{
overlayButtons.add(initializeOverlayButton(getContext(), R.drawable.nunchuk_z, ButtonType.NUNCHUK_BUTTON_Z));
}
if (mPreferences.getBoolean("buttonToggleWii9", true))
{
overlayDpads.add(initializeOverlayDpad(getContext(), R.drawable.gcwii_dpad,
ButtonType.WIIMOTE_UP, ButtonType.WIIMOTE_DOWN,
ButtonType.WIIMOTE_LEFT, ButtonType.WIIMOTE_RIGHT));
}
if (mPreferences.getBoolean("buttonToggleWii10", true))
{
overlayJoysticks.add(initializeOverlayJoystick(getContext(),
R.drawable.gcwii_joystick_range, R.drawable.gcwii_joystick,
ButtonType.NUNCHUK_STICK));
}
}
invalidate();
}
private void saveControlPosition(int sharedPrefsId, int x, int y)
{
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());

View File

@ -159,6 +159,20 @@
<item>12</item>
</integer-array>
<string-array name="gcpadButtons">
<item>A</item>
<item>B</item>
<item>X</item>
<item>Y</item>
<item>Z</item>
<item>Start</item>
<item>L</item>
<item>R</item>
<item>D-Pad</item>
<item>Main Stick</item>
<item>C Stick</item>
</string-array>
<string-array name="wiimoteTypeEntries">
<item>Disabled</item>
<item>Emulated</item>
@ -170,4 +184,18 @@
<item>2</item>
</integer-array>
<string-array name="wiimoteButtons">
<item>A</item>
<item>B</item>
<item>1</item>
<item>2</item>
<item>+</item>
<item>-</item>
<item>Home</item>
<item>C</item>
<item>Z</item>
<item>D-Pad</item>
<item>Nunchuk Stick</item>
</string-array>
</resources>

View File

@ -356,6 +356,7 @@
<!-- Emulation Menu -->
<string name="emulation_toggle_input">Toggle Touch Controls</string>
<string name="emulation_toggle_all">Toggle All</string>
<string name="emulation_quicksave">Quick Save</string>
<string name="emulation_quickload">Quick Load</string>
<string name="emulation_refresh_wiimotes">Refresh Wiimotes</string>