Analog stick for scrolling in mouse emulation mode

This commit is contained in:
joaomacp
2024-01-07 18:32:57 +00:00
committed by Cameron Gutman
parent 3f9f8f7b3b
commit 3aab9eb13b
10 changed files with 107 additions and 12 deletions

View File

@@ -1857,21 +1857,30 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD
return true;
}
private short scaleRawStickAxis(float stickValue) {
return (short)Math.pow(stickValue, 3);
}
private void sendEmulatedMouseEvent(short x, short y) {
private Vector2d convertRawStickAxisToPixelMovement(short stickX, short stickY) {
Vector2d vector = new Vector2d();
vector.initialize(x, y);
vector.initialize(stickX, stickY);
vector.scalarMultiply(1 / 32766.0f);
vector.scalarMultiply(4);
if (vector.getMagnitude() > 0) {
// Move faster as the stick is pressed further from center
vector.scalarMultiply(Math.pow(vector.getMagnitude(), 2));
if (vector.getMagnitude() >= 1) {
conn.sendMouseMove((short)vector.getX(), (short)-vector.getY());
}
}
return vector;
}
private void sendEmulatedMouseMove(short x, short y) {
Vector2d vector = convertRawStickAxisToPixelMovement(x, y);
if (vector.getMagnitude() >= 1) {
conn.sendMouseMove((short)vector.getX(), (short)-vector.getY());
}
}
private void sendEmulatedMouseScroll(short x, short y) {
Vector2d vector = convertRawStickAxisToPixelMovement(x, y);
if (vector.getMagnitude() >= 1) {
conn.sendMouseHighResScroll((short)vector.getY());
conn.sendMouseHighResHScroll((short)vector.getX());
}
}
@@ -2906,9 +2915,19 @@ public class ControllerHandler implements InputManager.InputDeviceListener, UsbD
return;
}
// Send mouse movement events from analog sticks
sendEmulatedMouseEvent(leftStickX, leftStickY);
sendEmulatedMouseEvent(rightStickX, rightStickY);
// Send mouse events from analog sticks
if (prefConfig.analogStickForScrolling == PreferenceConfiguration.AnalogStickForScrolling.RIGHT) {
sendEmulatedMouseMove(leftStickX, leftStickY);
sendEmulatedMouseScroll(rightStickX, rightStickY);
}
else if (prefConfig.analogStickForScrolling == PreferenceConfiguration.AnalogStickForScrolling.LEFT) {
sendEmulatedMouseMove(rightStickX, rightStickY);
sendEmulatedMouseScroll(leftStickX, leftStickY);
}
else {
sendEmulatedMouseMove(leftStickX, leftStickY);
sendEmulatedMouseMove(rightStickX, rightStickY);
}
// Requeue the callback
mainThreadHandler.postDelayed(this, mouseEmulationReportPeriod);

View File

@@ -17,6 +17,12 @@ public class PreferenceConfiguration {
FORCE_H264,
};
public enum AnalogStickForScrolling {
NONE,
RIGHT,
LEFT
}
private static final String LEGACY_RES_FPS_PREF_STRING = "list_resolution_fps";
private static final String LEGACY_ENABLE_51_SURROUND_PREF_STRING = "checkbox_51_surround";
@@ -44,6 +50,7 @@ public class PreferenceConfiguration {
private static final String ENABLE_PERF_OVERLAY_STRING = "checkbox_enable_perf_overlay";
private static final String BIND_ALL_USB_STRING = "checkbox_usb_bind_all";
private static final String MOUSE_EMULATION_STRING = "checkbox_mouse_emulation";
private static final String ANALOG_SCROLLING_PREF_STRING = "analog_scrolling";
private static final String MOUSE_NAV_BUTTONS_STRING = "checkbox_mouse_nav_buttons";
static final String UNLOCK_FPS_STRING = "checkbox_unlock_fps";
private static final String VIBRATE_OSC_PREF_STRING = "checkbox_vibrate_osc";
@@ -80,6 +87,7 @@ public class PreferenceConfiguration {
private static final boolean DEFAULT_ENABLE_PERF_OVERLAY = false;
private static final boolean DEFAULT_BIND_ALL_USB = false;
private static final boolean DEFAULT_MOUSE_EMULATION = true;
private static final String DEFAULT_ANALOG_STICK_FOR_SCROLLING = "right";
private static final boolean DEFAULT_MOUSE_NAV_BUTTONS = false;
private static final boolean DEFAULT_UNLOCK_FPS = false;
private static final boolean DEFAULT_VIBRATE_OSC = true;
@@ -126,6 +134,7 @@ public class PreferenceConfiguration {
public boolean enableLatencyToast;
public boolean bindAllUsb;
public boolean mouseEmulation;
public AnalogStickForScrolling analogStickForScrolling;
public boolean mouseNavButtons;
public boolean unlockFps;
public boolean vibrateOsc;
@@ -384,6 +393,21 @@ public class PreferenceConfiguration {
}
}
private static AnalogStickForScrolling getAnalogStickForScrollingValue(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String str = prefs.getString(ANALOG_SCROLLING_PREF_STRING, DEFAULT_ANALOG_STICK_FOR_SCROLLING);
if (str.equals("right")) {
return AnalogStickForScrolling.RIGHT;
}
else if (str.equals("left")) {
return AnalogStickForScrolling.LEFT;
}
else {
return AnalogStickForScrolling.NONE;
}
}
public static void resetStreamingSettings(Context context) {
// We consider resolution, FPS, bitrate, HDR, and video format as "streaming settings" here
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
@@ -532,6 +556,8 @@ public class PreferenceConfiguration {
config.videoFormat = getVideoFormatValue(context);
config.framePacing = getFramePacingValue(context);
config.analogStickForScrolling = getAnalogStickForScrollingValue(context);
config.deadzonePercentage = prefs.getInt(DEADZONE_PREF_STRING, DEFAULT_DEADZONE);
config.oscOpacity = prefs.getInt(OSC_OPACITY_PREF_STRING, DEFAULT_OPACITY);