Skip to content

Commit

Permalink
fix,refactor: double click on touchpad, scroll without triggering click
Browse files Browse the repository at this point in the history
Signed-off-by: Mathias-Boulay <mathiasboulay@free.fr>
  • Loading branch information
Mathias-Boulay committed May 15, 2024
1 parent 85a9f21 commit 1d7ebc7
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import net.kdt.pojavlaunch.customcontrols.EditorExitable;
import net.kdt.pojavlaunch.customcontrols.keyboard.LwjglCharSender;
import net.kdt.pojavlaunch.customcontrols.keyboard.TouchCharInput;
import net.kdt.pojavlaunch.customcontrols.mouse.GyroControl;
import net.kdt.pojavlaunch.customcontrols.mouse.Touchpad;
import net.kdt.pojavlaunch.lifecycle.ContextExecutor;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
import android.view.*;
import android.view.GestureDetector.*;

import androidx.annotation.NonNull;

public class SingleTapConfirm extends SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent event) {
System.out.println("single tap up !");
return true;
}

@Override
public boolean onDoubleTap(@NonNull MotionEvent e) {
return super.onDoubleTap(e);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.kdt.pojavlaunch;
package net.kdt.pojavlaunch.customcontrols.mouse;

import android.app.Activity;
import android.content.Context;
Expand All @@ -10,6 +10,7 @@
import android.view.Surface;
import android.view.WindowManager;

import net.kdt.pojavlaunch.GrabListener;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;

import org.lwjgl.glfw.CallbackBridge;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import androidx.annotation.Nullable;

import net.kdt.pojavlaunch.LwjglGlfwKeycode;
import net.kdt.pojavlaunch.TapDetector;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import net.kdt.pojavlaunch.utils.MCOptionUtils;
import net.kdt.pojavlaunch.utils.MathUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,68 @@

public class InGUIEventProcessor implements TouchEventProcessor {
public static final float FINGER_SCROLL_THRESHOLD = Tools.dpToPx(6);
public static final float FINGER_STILL_THRESHOLD = Tools.dpToPx(5);

private final PointerTracker mTracker = new PointerTracker();
private final GestureDetector mSingleTapDetector;
private final TapDetector mSingleTapDetector;
private AbstractTouchpad mTouchpad;
private boolean mIsMouseDown = false;
private float mStartX, mStartY;
private final float mScaleFactor;
private final Scroller mScroller = new Scroller(FINGER_SCROLL_THRESHOLD);

public InGUIEventProcessor(float scaleFactor) {
mSingleTapDetector = new GestureDetector(null, new SingleTapConfirm());
mSingleTapDetector = new TapDetector(1, TapDetector.DETECTION_METHOD_BOTH);
mScaleFactor = scaleFactor;
}

@Override
public boolean processTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mTracker.startTracking(motionEvent);
if(!touchpadDisplayed()) {
sendTouchCoordinates(motionEvent.getX(), motionEvent.getY());
enableMouse();

// disabled gestures means no scrolling possible, send gesture early
if (LauncherPreferences.PREF_DISABLE_GESTURES)
enableMouse();
else {
mStartX = motionEvent.getX();
mStartY = motionEvent.getY();
}

}
break;

case MotionEvent.ACTION_MOVE:
int pointerCount = motionEvent.getPointerCount();
int pointerIndex = mTracker.trackEvent(motionEvent);
if(pointerCount == 1 || LauncherPreferences.PREF_DISABLE_GESTURES) {
if(touchpadDisplayed()) {
mTouchpad.applyMotionVector(mTracker.getMotionVector());
}else {
} else {
float mainPointerX = motionEvent.getX(pointerIndex);
float mainPointerY = motionEvent.getY(pointerIndex);
sendTouchCoordinates(mainPointerX, mainPointerY);
if(!mIsMouseDown) enableMouse();


if(!mIsMouseDown && !LeftClickGesture.isFingerStill(mStartX, mStartY, FINGER_STILL_THRESHOLD))
enableMouse();
}
} else mScroller.performScroll(mTracker.getMotionVector());
break;

case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
mScroller.resetScrollOvershoot();
mTracker.cancelTracking();
if(mIsMouseDown) disableMouse();
}
if(touchpadDisplayed() && mSingleTapDetector.onTouchEvent(motionEvent)) clickMouse();

if((!LauncherPreferences.PREF_DISABLE_GESTURES || touchpadDisplayed()) && mSingleTapDetector.onTouchEvent(motionEvent)) {
clickMouse();
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final void inputEvent() {

@Override
public boolean checkAndTrigger() {
boolean fingerStill = LeftClickGesture.isFingerStill(mGestureStartX, mGestureStartY);
boolean fingerStill = LeftClickGesture.isFingerStill(mGestureStartX, mGestureStartY, FINGER_STILL_THRESHOLD);
// If the finger is still, fire the gesture.
if(fingerStill) {
sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_LEFT, true);
Expand All @@ -53,12 +53,12 @@ public void onGestureCancelled(boolean isSwitching) {
* @param startY the starting Y of the gesture
* @return whether the finger's position counts as "still" or not
*/
public static boolean isFingerStill(float startX, float startY) {
public static boolean isFingerStill(float startX, float startY, float threshold) {
return MathUtils.dist(
CallbackBridge.mouseX,
CallbackBridge.mouseY,
startX,
startY
) <= FINGER_STILL_THRESHOLD;
) <= threshold;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public boolean checkAndTrigger() {
public void onGestureCancelled(boolean isSwitching) {
mGestureEnabled = true;
if(!mGestureValid || isSwitching) return;
boolean fingerStill = LeftClickGesture.isFingerStill(mGestureStartX, mGestureStartY);
boolean fingerStill = LeftClickGesture.isFingerStill(mGestureStartX, mGestureStartY, LeftClickGesture.FINGER_STILL_THRESHOLD);
if(!fingerStill) return;
CallbackBridge.sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_RIGHT, true);
CallbackBridge.sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_RIGHT, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.kdt.pojavlaunch;
package net.kdt.pojavlaunch.customcontrols.mouse;

import android.view.MotionEvent;

Expand All @@ -7,6 +7,8 @@
import static android.view.MotionEvent.ACTION_POINTER_UP;
import static android.view.MotionEvent.ACTION_UP;

import net.kdt.pojavlaunch.Tools;

/**
* Class aiming at better detecting X-tap events regardless of the POINTERS
* Only uses the least amount of events possible,
Expand Down

0 comments on commit 1d7ebc7

Please sign in to comment.