Android: Refactor reading "wiiController" preference

This had a lot of duplicated code and magic numbers.
This commit is contained in:
JosJuice 2022-06-27 08:54:27 +02:00
parent e18053d307
commit 9e97377690
2 changed files with 32 additions and 24 deletions

View File

@ -260,9 +260,7 @@ public final class EmulationActivity extends AppCompatActivity
public static void updateWiimoteNewIniPreferences(Context context)
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
updateWiimoteNewController(preferences.getInt("wiiController", 3), context);
updateWiimoteNewController(InputOverlay.getConfiguredControllerType(context), context);
updateWiimoteNewImuIr(IntSetting.MAIN_MOTION_CONTROLS.getIntGlobal());
}
@ -810,7 +808,10 @@ public final class EmulationActivity extends AppCompatActivity
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.emulation_toggle_controls);
if (!NativeLibrary.IsEmulatingWii() || mPreferences.getInt("wiiController", 3) == 0)
int currentController = InputOverlay.getConfiguredControllerType(this);
if (!NativeLibrary.IsEmulatingWii() || currentController == InputOverlay.OVERLAY_GAMECUBE)
{
boolean[] gcEnabledButtons = new boolean[11];
String gcSettingBase = "MAIN_BUTTON_TOGGLE_GC_";
@ -823,7 +824,7 @@ public final class EmulationActivity extends AppCompatActivity
(dialog, indexSelected, isChecked) -> BooleanSetting
.valueOf(gcSettingBase + indexSelected).setBoolean(mSettings, isChecked));
}
else if (mPreferences.getInt("wiiController", 3) == 4)
else if (currentController == InputOverlay.OVERLAY_WIIMOTE_CLASSIC)
{
boolean[] wiiClassicEnabledButtons = new boolean[14];
String classicSettingBase = "MAIN_BUTTON_TOGGLE_CLASSIC_";
@ -847,7 +848,7 @@ public final class EmulationActivity extends AppCompatActivity
{
wiiEnabledButtons[i] = BooleanSetting.valueOf(wiiSettingBase + i).getBoolean(mSettings);
}
if (mPreferences.getInt("wiiController", 3) == 3)
if (currentController == InputOverlay.OVERLAY_WIIMOTE_NUNCHUK)
{
builder.setMultiChoiceItems(R.array.nunchukButtons, wiiEnabledButtons,
(dialog, indexSelected, isChecked) -> BooleanSetting
@ -860,6 +861,7 @@ public final class EmulationActivity extends AppCompatActivity
.valueOf(wiiSettingBase + indexSelected).setBoolean(mSettings, isChecked));
}
}
builder.setNeutralButton(R.string.emulation_toggle_all,
(dialogInterface, i) -> mEmulationFragment.toggleInputOverlayVisibility(mSettings));
builder.setPositiveButton(R.string.ok, (dialogInterface, i) ->
@ -872,13 +874,10 @@ public final class EmulationActivity extends AppCompatActivity
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
int currentController =
mPreferences.getInt("wiiController", InputOverlay.OVERLAY_WIIMOTE_NUNCHUK);
int currentValue = IntSetting.MAIN_DOUBLE_TAP_BUTTON.getInt(mSettings);
int buttonList = currentController == InputOverlay.OVERLAY_WIIMOTE_CLASSIC ?
R.array.doubleTapWithClassic : R.array.doubleTap;
int buttonList = InputOverlay.getConfiguredControllerType(this) ==
InputOverlay.OVERLAY_WIIMOTE_CLASSIC ? R.array.doubleTapWithClassic : R.array.doubleTap;
int checkedItem = -1;
int itemCount = getResources().getStringArray(buttonList).length;
@ -981,7 +980,7 @@ public final class EmulationActivity extends AppCompatActivity
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.emulation_choose_controller);
builder.setSingleChoiceItems(R.array.controllersEntries,
mPreferences.getInt("wiiController", 3),
InputOverlay.getConfiguredControllerType(this),
(dialog, indexSelected) ->
{
editor.putInt("wiiController", indexSelected);

View File

@ -157,8 +157,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
int doubleTapButton = IntSetting.MAIN_DOUBLE_TAP_BUTTON.getIntGlobal();
if (mPreferences.getInt("wiiController", OVERLAY_WIIMOTE_NUNCHUK) !=
InputOverlay.OVERLAY_WIIMOTE_CLASSIC &&
if (getConfiguredControllerType() != InputOverlay.OVERLAY_WIIMOTE_CLASSIC &&
doubleTapButton == InputOverlayPointer.DOUBLE_TAP_CLASSIC_A)
{
doubleTapButton = InputOverlayPointer.DOUBLE_TAP_A;
@ -345,8 +344,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
int fingerPositionX = (int) event.getX(pointerIndex);
int fingerPositionY = (int) event.getY(pointerIndex);
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
int controller = sPrefs.getInt("wiiController", 3);
int controller = getConfiguredControllerType();
String orientation =
getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ?
"-Portrait" : "";
@ -745,7 +743,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
}
else
{
switch (mPreferences.getInt("wiiController", 3))
switch (getConfiguredControllerType())
{
case OVERLAY_GAMECUBE:
addGameCubeOverlayControls(orientation);
@ -789,14 +787,14 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
// Values for these come from R.array.controllersEntries
if (!NativeLibrary.IsEmulatingWii() || mPreferences.getInt("wiiController", 3) == 0)
if (!NativeLibrary.IsEmulatingWii() || getConfiguredControllerType() == OVERLAY_GAMECUBE)
{
if (isLandscape)
gcDefaultOverlay();
else
gcPortraitDefaultOverlay();
}
else if (mPreferences.getInt("wiiController", 3) == 4)
else if (getConfiguredControllerType() == OVERLAY_WIIMOTE_CLASSIC)
{
if (isLandscape)
wiiClassicDefaultOverlay();
@ -819,6 +817,17 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
refreshControls();
}
public static int getConfiguredControllerType(Context context)
{
return PreferenceManager.getDefaultSharedPreferences(context)
.getInt("wiiController", OVERLAY_WIIMOTE_NUNCHUK);
}
private int getConfiguredControllerType()
{
return mPreferences.getInt("wiiController", OVERLAY_WIIMOTE_NUNCHUK);
}
private void saveControlPosition(int sharedPrefsId, int x, int y, int controller,
String orientation)
{
@ -831,11 +840,11 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
private static String getKey(int sharedPrefsId, int controller, String orientation, String suffix)
{
if (controller == 2 && WIIMOTE_H_BUTTONS.contains(sharedPrefsId))
if (controller == OVERLAY_WIIMOTE_SIDEWAYS && WIIMOTE_H_BUTTONS.contains(sharedPrefsId))
{
return sharedPrefsId + "_H" + orientation + suffix;
}
else if (controller == 1 && WIIMOTE_O_BUTTONS.contains(sharedPrefsId))
else if (controller == OVERLAY_WIIMOTE && WIIMOTE_O_BUTTONS.contains(sharedPrefsId))
{
return sharedPrefsId + "_O" + orientation + suffix;
}
@ -893,7 +902,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
// SharedPreference to retrieve the X and Y coordinates for the InputOverlayDrawableButton.
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(context);
int controller = sPrefs.getInt("wiiController", 3);
int controller = getConfiguredControllerType(context);
// Decide scale based on button ID and user preference
float scale;
@ -1001,7 +1010,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
// SharedPreference to retrieve the X and Y coordinates for the InputOverlayDrawableDpad.
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(context);
int controller = sPrefs.getInt("wiiController", 3);
int controller = getConfiguredControllerType(context);
// Decide scale based on button ID and user preference
float scale;
@ -1076,7 +1085,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
// SharedPreference to retrieve the X and Y coordinates for the InputOverlayDrawableJoystick.
final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(context);
int controller = sPrefs.getInt("wiiController", 3);
int controller = getConfiguredControllerType(context);
// Decide scale based on user preference
float scale = 0.275f;