QoL(gesture): decouple gyroscope from long press gesture

This commit is contained in:
Mathias-Boulay 2024-11-29 21:17:29 +01:00
parent 68fa25cafc
commit c6fe3c3de5
3 changed files with 31 additions and 8 deletions

View File

@ -33,8 +33,12 @@ public class InGameEventProcessor implements TouchEventProcessor {
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
mTracker.trackEvent(motionEvent); mTracker.trackEvent(motionEvent);
float[] motionVector = mTracker.getMotionVector(); float[] motionVector = mTracker.getMotionVector();
CallbackBridge.mouseX += (float) (motionVector[0] * mSensitivity); float deltaX = (float) (motionVector[0] * mSensitivity);
CallbackBridge.mouseY += (float) (motionVector[1] * mSensitivity); float deltaY = (float) (motionVector[1] * mSensitivity);
mLeftClickGesture.setMotion(deltaX, deltaY);
mRightClickGesture.setMotion(deltaX, deltaY);
CallbackBridge.mouseX += deltaX;
CallbackBridge.mouseY += deltaY;
CallbackBridge.sendCursorPos(CallbackBridge.mouseX, CallbackBridge.mouseY); CallbackBridge.sendCursorPos(CallbackBridge.mouseX, CallbackBridge.mouseY);
if(LauncherPreferences.PREF_DISABLE_GESTURES) break; if(LauncherPreferences.PREF_DISABLE_GESTURES) break;
checkGestures(); checkGestures();

View File

@ -13,7 +13,7 @@ import org.lwjgl.glfw.CallbackBridge;
public class LeftClickGesture extends ValidatorGesture { public class LeftClickGesture extends ValidatorGesture {
public static final int FINGER_STILL_THRESHOLD = (int) Tools.dpToPx(9); public static final int FINGER_STILL_THRESHOLD = (int) Tools.dpToPx(9);
private float mGestureStartX, mGestureStartY; private float mGestureStartX, mGestureStartY, mGestureEndX, mGestureEndY;
private boolean mMouseActivated; private boolean mMouseActivated;
public LeftClickGesture(Handler handler) { public LeftClickGesture(Handler handler) {
@ -22,14 +22,14 @@ public class LeftClickGesture extends ValidatorGesture {
public final void inputEvent() { public final void inputEvent() {
if(submit()) { if(submit()) {
mGestureStartX = CallbackBridge.mouseX; mGestureStartX = mGestureEndX = CallbackBridge.mouseX;
mGestureStartY = CallbackBridge.mouseY; mGestureStartY = mGestureEndY = CallbackBridge.mouseY;
} }
} }
@Override @Override
public boolean checkAndTrigger() { public boolean checkAndTrigger() {
boolean fingerStill = LeftClickGesture.isFingerStill(mGestureStartX, mGestureStartY, FINGER_STILL_THRESHOLD); boolean fingerStill = LeftClickGesture.isFingerStill(mGestureStartX, mGestureStartY, mGestureEndX, mGestureEndY, FINGER_STILL_THRESHOLD);
// If the finger is still, fire the gesture. // If the finger is still, fire the gesture.
if(fingerStill) { if(fingerStill) {
sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_LEFT, true); sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_LEFT, true);
@ -47,6 +47,11 @@ public class LeftClickGesture extends ValidatorGesture {
} }
} }
public void setMotion(float deltaX, float deltaY) {
mGestureEndX += deltaX;
mGestureEndY += deltaY;
}
/** /**
* Check if the finger is still when compared to mouseX/mouseY in CallbackBridge. * Check if the finger is still when compared to mouseX/mouseY in CallbackBridge.
* @param startX the starting X of the gesture * @param startX the starting X of the gesture
@ -61,4 +66,13 @@ public class LeftClickGesture extends ValidatorGesture {
startY startY
) <= threshold; ) <= threshold;
} }
public static boolean isFingerStill(float startX, float startY, float endX, float endY, float threshold) {
return MathUtils.dist(
endX,
endY,
startX,
startY
) <= threshold;
}
} }

View File

@ -9,7 +9,7 @@ import org.lwjgl.glfw.CallbackBridge;
public class RightClickGesture extends ValidatorGesture{ public class RightClickGesture extends ValidatorGesture{
private boolean mGestureEnabled = true; private boolean mGestureEnabled = true;
private boolean mGestureValid = true; private boolean mGestureValid = true;
private float mGestureStartX, mGestureStartY; private float mGestureStartX, mGestureStartY, mGestureEndX, mGestureEndY;
public RightClickGesture(Handler mHandler) { public RightClickGesture(Handler mHandler) {
super(mHandler, 150); super(mHandler, 150);
} }
@ -24,6 +24,11 @@ public class RightClickGesture extends ValidatorGesture{
} }
} }
public void setMotion(float deltaX, float deltaY) {
mGestureEndX += deltaX;
mGestureEndY += deltaY;
}
@Override @Override
public boolean checkAndTrigger() { public boolean checkAndTrigger() {
// If the validate() method was called, it means that the user held on for too long. The cancellation should be ignored. // If the validate() method was called, it means that the user held on for too long. The cancellation should be ignored.
@ -38,7 +43,7 @@ public class RightClickGesture extends ValidatorGesture{
public void onGestureCancelled(boolean isSwitching) { public void onGestureCancelled(boolean isSwitching) {
mGestureEnabled = true; mGestureEnabled = true;
if(!mGestureValid || isSwitching) return; if(!mGestureValid || isSwitching) return;
boolean fingerStill = LeftClickGesture.isFingerStill(mGestureStartX, mGestureStartY, LeftClickGesture.FINGER_STILL_THRESHOLD); boolean fingerStill = LeftClickGesture.isFingerStill(mGestureStartX, mGestureStartY, mGestureEndX, mGestureEndY, LeftClickGesture.FINGER_STILL_THRESHOLD);
if(!fingerStill) return; if(!fingerStill) return;
CallbackBridge.sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_RIGHT, true); CallbackBridge.sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_RIGHT, true);
CallbackBridge.sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_RIGHT, false); CallbackBridge.sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_RIGHT, false);