Android: Support binding half axes to buttons

This commit is contained in:
Connor McLaughlin 2021-04-04 02:05:59 +10:00
parent bb21a062d8
commit a9a571cd6a
5 changed files with 39 additions and 12 deletions

View file

@ -1191,6 +1191,24 @@ DEFINE_JNI_ARGS_METHOD(jint, AndroidHostInterface_getControllerAxisCode, jobject
return code.value_or(-1);
}
DEFINE_JNI_ARGS_METHOD(jint, AndroidHostInterface_getControllerAxisType, jobject unused, jstring controller_type,
jstring axis_name)
{
std::optional<ControllerType> type =
Settings::ParseControllerTypeName(AndroidHelpers::JStringToString(env, controller_type).c_str());
if (!type)
return -1;
const std::string axis_name_str(AndroidHelpers::JStringToString(env, axis_name));
for (const auto& [name, code, type] : Controller::GetAxisNames(type.value()))
{
if (name == axis_name_str)
return static_cast<jint>(type);
}
return -1;
}
DEFINE_JNI_ARGS_METHOD(jobjectArray, AndroidHostInterface_getControllerButtonNames, jobject unused,
jstring controller_type)
{

View file

@ -18,6 +18,9 @@ public class AndroidHostInterface {
public final static int DISPLAY_ALIGNMENT_CENTER = 1;
public final static int DISPLAY_ALIGNMENT_RIGHT_OR_BOTTOM = 2;
public final static int CONTROLLER_AXIS_TYPE_FULL = 0;
public final static int CONTROLLER_AXIS_TYPE_HALF = 1;
private long mNativePointer;
private Context mContext;
@ -77,6 +80,8 @@ public class AndroidHostInterface {
public static native int getControllerAxisCode(String controllerType, String axisName);
public static native int getControllerAxisType(String controllerType, String axisName);
public static native String[] getControllerButtonNames(String controllerType);
public static native String[] getControllerAxisNames(String controllerType);

View file

@ -78,7 +78,7 @@ public class ControllerBindingDialog extends AlertDialog {
return super.onKeyDown(keyCode, event);
}
if (mType == ControllerBindingPreference.Type.BUTTON) {
if (mType == ControllerBindingPreference.Type.BUTTON || mType == ControllerBindingPreference.Type.HALF_AXIS) {
mCurrentBinding = String.format("%s/Button%d", device.getDescriptor(), event.getKeyCode());
} else if (mType == ControllerBindingPreference.Type.VIBRATION) {
if (device.getVibrator() == null || !device.getVibrator().hasVibrator()) {
@ -105,7 +105,7 @@ public class ControllerBindingDialog extends AlertDialog {
mUpdatedAxisCode = axisCode;
final int controllerIndex = 0;
if (mType == ControllerBindingPreference.Type.AXIS)
if (mType == ControllerBindingPreference.Type.AXIS || mType == ControllerBindingPreference.Type.HALF_AXIS)
mCurrentBinding = String.format("%s/Axis%d", device.getDescriptor(), axisCode);
else
mCurrentBinding = String.format("%s/%cAxis%d", device.getDescriptor(), (positive) ? '+' : '-', axisCode);
@ -156,7 +156,9 @@ public class ControllerBindingDialog extends AlertDialog {
@Override
public boolean onGenericMotionEvent(@NonNull MotionEvent event) {
if (mType != ControllerBindingPreference.Type.AXIS && mType != ControllerBindingPreference.Type.BUTTON) {
if (mType != ControllerBindingPreference.Type.AXIS &&
mType != ControllerBindingPreference.Type.HALF_AXIS &&
mType != ControllerBindingPreference.Type.BUTTON) {
return false;
}

View file

@ -18,6 +18,7 @@ public class ControllerBindingPreference extends Preference {
public enum Type {
BUTTON,
AXIS,
HALF_AXIS,
VIBRATION
}
@ -149,10 +150,10 @@ public class ControllerBindingPreference extends Preference {
updateValue();
}
public void initAxis(int controllerIndex, String axisName) {
public void initAxis(int controllerIndex, String axisName, int axisType) {
mBindingName = axisName;
mDisplayName = axisName;
mType = Type.AXIS;
mType = (axisType == AndroidHostInterface.CONTROLLER_AXIS_TYPE_HALF) ? Type.HALF_AXIS : Type.AXIS;
mVisualType = VisualType.AXIS;
setKey(String.format("Controller%d/Axis%s", controllerIndex, axisName));
updateValue();

View file

@ -297,12 +297,12 @@ public class ControllerSettingsActivity extends AppCompatActivity {
private void createPreferences(String controllerType) {
final PreferenceScreen ps = getPreferenceScreen();
final SharedPreferences sp = getPreferenceManager().getSharedPreferences();
final String[] controllerButtons = AndroidHostInterface.getControllerButtonNames(controllerType);
final String[] axisButtons = AndroidHostInterface.getControllerAxisNames(controllerType);
final String[] buttonNames = AndroidHostInterface.getControllerButtonNames(controllerType);
final String[] axisNames = AndroidHostInterface.getControllerAxisNames(controllerType);
final int vibrationMotors = AndroidHostInterface.getControllerVibrationMotorCount(controllerType);
if (controllerButtons != null) {
for (String buttonName : controllerButtons) {
if (buttonNames != null) {
for (String buttonName : buttonNames) {
final ControllerBindingPreference cbp = new ControllerBindingPreference(getContext(), null);
cbp.initButton(controllerIndex, buttonName);
mButtonsCategory.addPreference(cbp);
@ -310,10 +310,11 @@ public class ControllerSettingsActivity extends AppCompatActivity {
}
}
if (axisButtons != null) {
for (String axisName : axisButtons) {
if (axisNames != null) {
for (String axisName : axisNames) {
final int axisType = AndroidHostInterface.getControllerAxisType(controllerType, axisName);
final ControllerBindingPreference cbp = new ControllerBindingPreference(getContext(), null);
cbp.initAxis(controllerIndex, axisName);
cbp.initAxis(controllerIndex, axisName, axisType);
mAxisCategory.addPreference(cbp);
activity.mPreferences.add(cbp);
}