Android: Add support for GunCon

This commit is contained in:
Connor McLaughlin 2021-04-03 14:53:34 +10:00
parent 17707525dc
commit bb21a062d8
16 changed files with 169 additions and 27 deletions

View file

@ -749,26 +749,6 @@ void AndroidHostInterface::SetDisplayAlignment(HostDisplay::Alignment alignment)
m_display->SetDisplayAlignment(alignment); m_display->SetDisplayAlignment(alignment);
} }
void AndroidHostInterface::SetControllerType(u32 index, std::string_view type_name)
{
ControllerType type =
Settings::ParseControllerTypeName(std::string(type_name).c_str()).value_or(ControllerType::None);
if (!IsEmulationThreadRunning())
{
g_settings.controller_types[index] = type;
return;
}
RunOnEmulationThread(
[index, type]() {
Log_InfoPrintf("Changing controller slot %d to %s", index, Settings::GetControllerTypeName(type));
g_settings.controller_types[index] = type;
System::UpdateControllers();
},
false);
}
void AndroidHostInterface::SetControllerButtonState(u32 index, s32 button_code, bool pressed) void AndroidHostInterface::SetControllerButtonState(u32 index, s32 button_code, bool pressed)
{ {
if (!IsEmulationThreadRunning()) if (!IsEmulationThreadRunning())
@ -1163,10 +1143,14 @@ DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_surfaceChanged, jobject obj, j
block); block);
} }
DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_setControllerType, jobject obj, jint index, jstring controller_type) DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_setMousePosition, jobject obj, jint positionX, jint positionY)
{ {
AndroidHelpers::GetNativeClass(env, obj)->SetControllerType(index, HostDisplay* display = AndroidHelpers::GetNativeClass(env, obj)->GetDisplay();
AndroidHelpers::JStringToString(env, controller_type)); if (!display)
return;
// Technically a race, but shouldn't cause any issues.
display->SetMousePosition(positionX, positionY);
} }
DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_setControllerButtonState, jobject obj, jint index, jint button_code, DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_setControllerButtonState, jobject obj, jint index, jint button_code,

View file

@ -53,7 +53,6 @@ public:
void SurfaceChanged(ANativeWindow* surface, int format, int width, int height); void SurfaceChanged(ANativeWindow* surface, int format, int width, int height);
void SetDisplayAlignment(HostDisplay::Alignment alignment); void SetDisplayAlignment(HostDisplay::Alignment alignment);
void SetControllerType(u32 index, std::string_view type_name);
void SetControllerButtonState(u32 index, s32 button_code, bool pressed); void SetControllerButtonState(u32 index, s32 button_code, bool pressed);
void SetControllerAxisState(u32 index, s32 button_code, float value); void SetControllerAxisState(u32 index, s32 button_code, float value);
void HandleControllerButtonEvent(u32 controller_index, u32 button_index, bool pressed); void HandleControllerButtonEvent(u32 controller_index, u32 button_index, bool pressed);

View file

@ -67,13 +67,12 @@ public class AndroidHostInterface {
public native void surfaceChanged(Surface surface, int format, int width, int height); public native void surfaceChanged(Surface surface, int format, int width, int height);
// TODO: Find a better place for this.
public native void setControllerType(int index, String typeName);
public native void setControllerButtonState(int index, int buttonCode, boolean pressed); public native void setControllerButtonState(int index, int buttonCode, boolean pressed);
public native void setControllerAxisState(int index, int axisCode, float value); public native void setControllerAxisState(int index, int axisCode, float value);
public native void setMousePosition(int positionX, int positionY);
public static native int getControllerButtonCode(String controllerType, String buttonName); public static native int getControllerButtonCode(String controllerType, String buttonName);
public static native int getControllerAxisCode(String controllerType, String axisName); public static native int getControllerAxisCode(String controllerType, String axisName);

View file

@ -36,6 +36,7 @@ public class TouchscreenControllerView extends FrameLayout {
private ArrayList<TouchscreenControllerButtonView> mButtonViews = new ArrayList<>(); private ArrayList<TouchscreenControllerButtonView> mButtonViews = new ArrayList<>();
private ArrayList<TouchscreenControllerAxisView> mAxisViews = new ArrayList<>(); private ArrayList<TouchscreenControllerAxisView> mAxisViews = new ArrayList<>();
private TouchscreenControllerDPadView mDPadView = null; private TouchscreenControllerDPadView mDPadView = null;
private int mPointerButtonCode = -1;
private boolean mHapticFeedback; private boolean mHapticFeedback;
private String mLayoutOrientation; private String mLayoutOrientation;
private boolean mEditingLayout = false; private boolean mEditingLayout = false;
@ -223,6 +224,7 @@ public class TouchscreenControllerView extends FrameLayout {
removeAllViews(); removeAllViews();
LayoutInflater inflater = LayoutInflater.from(getContext()); LayoutInflater inflater = LayoutInflater.from(getContext());
String pointerButtonName = null;
switch (viewType) { switch (viewType) {
case "digital": case "digital":
mMainView = inflater.inflate(R.layout.layout_touchscreen_controller_digital, this, true); mMainView = inflater.inflate(R.layout.layout_touchscreen_controller_digital, this, true);
@ -236,6 +238,11 @@ public class TouchscreenControllerView extends FrameLayout {
mMainView = inflater.inflate(R.layout.layout_touchscreen_controller_analog_sticks, this, true); mMainView = inflater.inflate(R.layout.layout_touchscreen_controller_analog_sticks, this, true);
break; break;
case "lightgun":
mMainView = inflater.inflate(R.layout.layout_touchscreen_controller_lightgun, this, true);
pointerButtonName = "Trigger";
break;
case "none": case "none":
default: default:
mMainView = null; mMainView = null;
@ -276,6 +283,11 @@ public class TouchscreenControllerView extends FrameLayout {
linkHotkeyButton(mMainView, R.id.controller_button_analog, "AnalogToggle", linkHotkeyButton(mMainView, R.id.controller_button_analog, "AnalogToggle",
TouchscreenControllerButtonView.Hotkey.ANALOG_TOGGLE, false); TouchscreenControllerButtonView.Hotkey.ANALOG_TOGGLE, false);
linkButton(mMainView, R.id.controller_button_a, "AButton", "A", true, true);
linkButton(mMainView, R.id.controller_button_b, "BButton", "B", true, true);
if (pointerButtonName != null)
linkPointer(pointerButtonName);
reloadButtonSettings(); reloadButtonSettings();
updateOpacity(); updateOpacity();
requestLayout(); requestLayout();
@ -371,6 +383,12 @@ public class TouchscreenControllerView extends FrameLayout {
mButtonViews.add(buttonView); mButtonViews.add(buttonView);
} }
private boolean linkPointer(String buttonName) {
mPointerButtonCode = AndroidHostInterface.getInstance().getControllerButtonCode(mControllerType, buttonName);
Log.i("TouchscreenController", String.format("Pointer -> %s,%d", buttonName, mPointerButtonCode));
return (mPointerButtonCode >= 0);
}
private int dpToPixels(float dp) { private int dpToPixels(float dp) {
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics())); return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()));
} }
@ -570,6 +588,18 @@ public class TouchscreenControllerView extends FrameLayout {
mDPadView.setUnpressed(); mDPadView.setUnpressed();
} }
if (mPointerButtonCode >= 0 && pointerCount > 0) {
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
AndroidHostInterface.getInstance().setControllerButtonState(mControllerIndex,
mPointerButtonCode, true);
}
final int pointerId = event.getPointerId(0);
AndroidHostInterface.getInstance().setMousePosition(
(int)event.getX(pointerId),
(int)event.getY(pointerId));
}
return true; return true;
} }
@ -592,6 +622,11 @@ public class TouchscreenControllerView extends FrameLayout {
if (mDPadView != null) if (mDPadView != null)
mDPadView.setUnpressed(); mDPadView.setUnpressed();
if (mPointerButtonCode >= 0) {
AndroidHostInterface.getInstance().setControllerButtonState(
mControllerIndex, mPointerButtonCode, false);
}
return true; return true;
} }

View file

@ -0,0 +1,17 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportWidth="100"
android:viewportHeight="100">
<path
android:pathData="M99.279,49.946A49.271,49.236 0,0 1,50.009 99.182,49.271 49.236,0 0,1 0.738,49.946 49.271,49.236 0,0 1,50.009 0.711,49.271 49.236,0 0,1 99.279,49.946Z"
android:strokeWidth="1.5"
android:fillColor="#000000"
android:strokeColor="#c8c8c8"
android:fillType="evenOdd"
android:fillAlpha="0"/>
<path
android:pathData="m61.587,68.222 l-4.369,-11.227h-14.376l-4.318,11.227h-4.623l14.173,-36.424h4.115l14.122,36.424zM55.846,52.931 L51.782,41.958q-0.152,-0.406 -0.508,-1.473 -0.356,-1.067 -0.711,-2.184 -0.305,-1.168 -0.508,-1.778 -0.356,1.575 -0.813,3.099 -0.457,1.473 -0.762,2.337l-4.115,10.973z"
android:fillColor="#c8c8c8"
android:strokeColor="#00000000"/>
</vector>

View file

@ -0,0 +1,16 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportWidth="100"
android:viewportHeight="100">
<path
android:pathData="M99.279,49.946A49.271,49.236 0,0 1,50.009 99.182,49.271 49.236,0 0,1 0.738,49.946 49.271,49.236 0,0 1,50.009 0.711,49.271 49.236,0 0,1 99.279,49.946Z"
android:strokeWidth="1.5"
android:fillColor="#969696"
android:strokeColor="#c8c8c8"
android:fillType="evenOdd"/>
<path
android:pathData="m61.587,68.222 l-4.369,-11.227h-14.376l-4.318,11.227h-4.623l14.173,-36.424h4.115l14.122,36.424zM55.846,52.931 L51.782,41.958q-0.152,-0.406 -0.508,-1.473 -0.356,-1.067 -0.711,-2.184 -0.305,-1.168 -0.508,-1.778 -0.356,1.575 -0.813,3.099 -0.457,1.473 -0.762,2.337l-4.115,10.973z"
android:fillColor="#c8c8c8"
android:strokeColor="#00000000"/>
</vector>

View file

@ -0,0 +1,17 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportWidth="100"
android:viewportHeight="100">
<path
android:pathData="M99.279,49.946A49.271,49.236 0,0 1,50.009 99.182,49.271 49.236,0 0,1 0.738,49.946 49.271,49.236 0,0 1,50.009 0.711,49.271 49.236,0 0,1 99.279,49.946Z"
android:strokeWidth="1.5"
android:fillColor="#000000"
android:strokeColor="#c8c8c8"
android:fillType="evenOdd"
android:fillAlpha="0"/>
<path
android:pathData="m37.431,31.811h10.363q6.807,0 10.262,2.032 3.505,1.981 3.505,6.96 0,3.2 -1.778,5.334 -1.778,2.083 -5.131,2.692v0.254q2.286,0.356 4.115,1.321 1.88,0.965 2.946,2.743 1.067,1.778 1.067,4.623 0,4.928 -3.404,7.62 -3.353,2.692 -9.195,2.692h-12.751zM42.003,47.254h6.706q4.674,0 6.401,-1.473 1.727,-1.524 1.727,-4.47 0,-2.997 -2.134,-4.267 -2.083,-1.321 -6.706,-1.321h-5.994zM42.003,51.064v13.157h7.315q4.826,0 6.706,-1.88 1.88,-1.88 1.88,-4.928 0,-2.845 -1.981,-4.572 -1.93,-1.778 -6.96,-1.778z"
android:fillColor="#c8c8c8"
android:strokeColor="#00000000"/>
</vector>

View file

@ -0,0 +1,16 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportWidth="100"
android:viewportHeight="100">
<path
android:pathData="M99.279,49.946A49.271,49.236 0,0 1,50.009 99.182,49.271 49.236,0 0,1 0.738,49.946 49.271,49.236 0,0 1,50.009 0.711,49.271 49.236,0 0,1 99.279,49.946Z"
android:strokeWidth="1.5"
android:fillColor="#969696"
android:strokeColor="#c8c8c8"
android:fillType="evenOdd"/>
<path
android:pathData="m37.431,31.811h10.363q6.807,0 10.262,2.032 3.505,1.981 3.505,6.96 0,3.2 -1.778,5.334 -1.778,2.083 -5.131,2.692v0.254q2.286,0.356 4.115,1.321 1.88,0.965 2.946,2.743 1.067,1.778 1.067,4.623 0,4.928 -3.404,7.62 -3.353,2.692 -9.195,2.692h-12.751zM42.003,47.254h6.706q4.674,0 6.401,-1.473 1.727,-1.524 1.727,-4.47 0,-2.997 -2.134,-4.267 -2.083,-1.321 -6.706,-1.321h-5.994zM42.003,51.064v13.157h7.315q4.826,0 6.706,-1.88 1.88,-1.88 1.88,-4.928 0,-2.845 -1.981,-4.572 -1.93,-1.778 -6.96,-1.778z"
android:fillColor="#c8c8c8"
android:strokeColor="#00000000"/>
</vector>

View file

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.github.stenzek.duckstation.TouchscreenControllerButtonView
android:id="@+id/controller_button_a"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:pressedDrawable="@drawable/ic_controller_a_button_pressed"
app:unpressedDrawable="@drawable/ic_controller_a_button" />
<com.github.stenzek.duckstation.TouchscreenControllerButtonView
android:id="@+id/controller_button_b"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="80dp"
android:layout_marginBottom="80dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:pressedDrawable="@drawable/ic_controller_b_button_pressed"
app:unpressedDrawable="@drawable/ic_controller_b_button" />
<com.github.stenzek.duckstation.TouchscreenControllerButtonView
android:id="@+id/controller_button_fast_forward"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginBottom="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:pressedDrawable="@drawable/ic_controller_fast_forward_pressed"
app:unpressedDrawable="@drawable/ic_controller_fast_forward" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -74,6 +74,7 @@
<item>Control Analógico (DualShock)</item> <item>Control Analógico (DualShock)</item>
<item>Analog Joystick</item> <item>Analog Joystick</item>
<item>NeGcon</item> <item>NeGcon</item>
<item>GunCon</item>
</string-array> </string-array>
<string-array name="settings_memory_card_mode_entries"> <string-array name="settings_memory_card_mode_entries">
<item>Sin tarjeta de memoria</item> <item>Sin tarjeta de memoria</item>
@ -114,6 +115,7 @@
<item>Digital Pad</item> <item>Digital Pad</item>
<item>Pad Analógico Unico</item> <item>Pad Analógico Unico</item>
<item>Pad Analógico Dual</item> <item>Pad Analógico Dual</item>
<item>Lightgun</item>
</string-array> </string-array>
<string-array name="settings_audio_backend_entries"> <string-array name="settings_audio_backend_entries">
<item>Nulo (Sin Salida)</item> <item>Nulo (Sin Salida)</item>

View file

@ -74,6 +74,7 @@
<item>Controller Analogico (DualShock)</item> <item>Controller Analogico (DualShock)</item>
<item>Analog Joystick</item> <item>Analog Joystick</item>
<item>NeGcon</item> <item>NeGcon</item>
<item>GunCon</item>
</string-array> </string-array>
<string-array name="settings_memory_card_mode_entries"> <string-array name="settings_memory_card_mode_entries">
<item>No Memory Card</item> <item>No Memory Card</item>
@ -114,6 +115,7 @@
<item>Pad Digitale</item> <item>Pad Digitale</item>
<item>Pad Analogico Singolo</item> <item>Pad Analogico Singolo</item>
<item>Pad Analogico Doppio</item> <item>Pad Analogico Doppio</item>
<item>Lightgun</item>
</string-array> </string-array>
<string-array name="settings_audio_backend_entries"> <string-array name="settings_audio_backend_entries">
<item>Null (No Output)</item> <item>Null (No Output)</item>

View file

@ -74,6 +74,7 @@
<item>Analoge Controller (DualShock)</item> <item>Analoge Controller (DualShock)</item>
<item>Analog Joystick</item> <item>Analog Joystick</item>
<item>NeGcon</item> <item>NeGcon</item>
<item>GunCon</item>
</string-array> </string-array>
<string-array name="settings_memory_card_mode_entries"> <string-array name="settings_memory_card_mode_entries">
<item>Geen Geheugenkaart</item> <item>Geen Geheugenkaart</item>
@ -114,6 +115,7 @@
<item>Digitale Pad</item> <item>Digitale Pad</item>
<item>Enekele Analoge Pad</item> <item>Enekele Analoge Pad</item>
<item>Dubbele Analoge Pad</item> <item>Dubbele Analoge Pad</item>
<item>Lightgun</item>
</string-array> </string-array>
<string-array name="settings_audio_backend_entries"> <string-array name="settings_audio_backend_entries">
<item>Null (Geen Output)</item> <item>Null (Geen Output)</item>

View file

@ -74,6 +74,7 @@
<item>Controle Analógico (DualShock)</item> <item>Controle Analógico (DualShock)</item>
<item>Analog Joystick</item> <item>Analog Joystick</item>
<item>NeGcon</item> <item>NeGcon</item>
<item>GunCon</item>
</string-array> </string-array>
<string-array name="settings_memory_card_mode_entries"> <string-array name="settings_memory_card_mode_entries">
<item>Sem Cartão de Memória</item> <item>Sem Cartão de Memória</item>
@ -120,6 +121,7 @@
<item>Digital D-Pad</item> <item>Digital D-Pad</item>
<item>1 Analógico</item> <item>1 Analógico</item>
<item>2 Analógicos</item> <item>2 Analógicos</item>
<item>Lightgun</item>
</string-array> </string-array>
<string-array name="settings_audio_backend_entries"> <string-array name="settings_audio_backend_entries">
<item>Mudo</item> <item>Mudo</item>

View file

@ -74,6 +74,7 @@
<item>Аналоговый (DualShock)</item> <item>Аналоговый (DualShock)</item>
<item>Аналоговый джойстик</item> <item>Аналоговый джойстик</item>
<item>NeGcon</item> <item>NeGcon</item>
<item>GunCon</item>
</string-array> </string-array>
<string-array name="settings_memory_card_mode_entries"> <string-array name="settings_memory_card_mode_entries">
<item>Без карты памяти</item> <item>Без карты памяти</item>
@ -120,6 +121,7 @@
<item>Цифровой</item> <item>Цифровой</item>
<item>Аналоговый, один джойстик</item> <item>Аналоговый, один джойстик</item>
<item>Аналоговый, два джойстика</item> <item>Аналоговый, два джойстика</item>
<item>Lightgun</item>
</string-array> </string-array>
<string-array name="settings_audio_backend_entries"> <string-array name="settings_audio_backend_entries">
<item>Нет (без звука)</item> <item>Нет (без звука)</item>

View file

@ -143,6 +143,7 @@
<item>Analog Controller (DualShock)</item> <item>Analog Controller (DualShock)</item>
<item>Analog Joystick</item> <item>Analog Joystick</item>
<item>NeGcon</item> <item>NeGcon</item>
<item>GunCon</item>
</string-array> </string-array>
<string-array name="settings_controller_type_values"> <string-array name="settings_controller_type_values">
<item>None</item> <item>None</item>
@ -150,6 +151,7 @@
<item>AnalogController</item> <item>AnalogController</item>
<item>AnalogJoystick</item> <item>AnalogJoystick</item>
<item>NeGcon</item> <item>NeGcon</item>
<item>NamcoGunCon</item>
</string-array> </string-array>
<string-array name="settings_memory_card_mode_entries"> <string-array name="settings_memory_card_mode_entries">
<item>No Memory Card</item> <item>No Memory Card</item>
@ -214,12 +216,14 @@
<item>Digital Pad</item> <item>Digital Pad</item>
<item>Single Analog Pad</item> <item>Single Analog Pad</item>
<item>Dual Analog Pad</item> <item>Dual Analog Pad</item>
<item>Lightgun</item>
</string-array> </string-array>
<string-array name="settings_touchscreen_controller_view_values"> <string-array name="settings_touchscreen_controller_view_values">
<item>none</item> <item>none</item>
<item>digital</item> <item>digital</item>
<item>analog_stick</item> <item>analog_stick</item>
<item>analog_sticks</item> <item>analog_sticks</item>
<item>lightgun</item>
</string-array> </string-array>
<string-array name="settings_audio_backend_entries"> <string-array name="settings_audio_backend_entries">
<item>Null (No Output)</item> <item>Null (No Output)</item>

View file

@ -278,11 +278,13 @@ void NamcoGunCon::LoadSettings(const char* section)
} }
} }
#ifndef __ANDROID__
if (!m_crosshair_image.IsValid()) if (!m_crosshair_image.IsValid())
{ {
m_crosshair_image.SetPixels(Resources::CROSSHAIR_IMAGE_WIDTH, Resources::CROSSHAIR_IMAGE_HEIGHT, m_crosshair_image.SetPixels(Resources::CROSSHAIR_IMAGE_WIDTH, Resources::CROSSHAIR_IMAGE_HEIGHT,
Resources::CROSSHAIR_IMAGE_DATA.data()); Resources::CROSSHAIR_IMAGE_DATA.data());
} }
#endif
m_crosshair_image_scale = g_host_interface->GetFloatSettingValue(section, "CrosshairScale", 1.0f); m_crosshair_image_scale = g_host_interface->GetFloatSettingValue(section, "CrosshairScale", 1.0f);