Adapted the double tap for ALL pointers

This commit is contained in:
SerpentSpirale 2021-09-09 20:34:02 +02:00 committed by SerpentSpirale
parent eec850dfcc
commit c0f9d73208
3 changed files with 81 additions and 16 deletions

View File

@ -97,7 +97,7 @@ public class BaseMainActivity extends LoggableActivity {
private ScrollView contentScroll;
private ToggleButton toggleLog;
private GestureDetector gestureDetector;
private GestureDetector doubleTapDetector;
private DoubleTapDetector doubleTapDetector;
private TextView debugText;
private NavigationView.OnNavigationItemSelectedListener gameActionListener;
@ -165,7 +165,7 @@ public class BaseMainActivity extends LoggableActivity {
gestureDetector = new GestureDetector(this, new SingleTapConfirm());
doubleTapDetector = new GestureDetector(this, new DoubleTapConfirm());
doubleTapDetector = new DoubleTapDetector();
// Menu
@ -317,6 +317,9 @@ public class BaseMainActivity extends LoggableActivity {
glTouchListener = new OnTouchListener(){
private boolean isTouchInHotbar = false;
private int lastHotbarKey = -1;
/*
* Tells if a double tap happened [MOUSE GRAB ONLY]. Doesn't tell where though.
*/
private boolean hasDoubleTapped = false;
/*
* Events can start with only a move instead of an pointerDown
@ -425,7 +428,15 @@ public class BaseMainActivity extends LoggableActivity {
scrollInitialY = CallbackBridge.mouseY;
//Checking if we are pressing the hotbar to select the item
hudKeyHandled = handleGuiBar((int)e.getX(e.getPointerCount()-1), (int) e.getY(e.getPointerCount()-1));
if(hudKeyHandled != -1) sendKeyPress(hudKeyHandled);
if(hudKeyHandled != -1){
sendKeyPress(hudKeyHandled);
if(hasDoubleTapped && hudKeyHandled == lastHotbarKey){
//Prevent double tapping Event on two different slots
sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_F);
}
}
lastHotbarKey = hudKeyHandled;
break;
case MotionEvent.ACTION_MOVE:

View File

@ -1,13 +0,0 @@
package net.kdt.pojavlaunch;
import android.view.GestureDetector;
import android.view.GestureDetector.*;
import android.view.MotionEvent;
public class DoubleTapConfirm extends SimpleOnGestureListener {
@Override
public boolean onDoubleTap(MotionEvent e) {return true;}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {return false;}
}

View File

@ -0,0 +1,67 @@
package net.kdt.pojavlaunch;
import android.view.MotionEvent;
import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_POINTER_DOWN;
/**
* Class aiming at better detecting double tap events for EVERY POINTER
* Only uses the least amount of events possible,
* since we aren't guaranteed to have all events in order
*/
public class DoubleTapDetector {
private final static int DOUBLE_TAP_MIN_DELTA_MS = 50;
private final static int DOUBLE_TAP_MAX_DELTA_MS = 300;
private final static int DOUBLE_TAP_SLOP_SQUARE_PX = (int) Math.pow(Tools.dpToPx(100), 2);
private long mLastEventTime = 0;
private float mLastX = 9999;
private float mLastY = 9999;
/**
* A function to call when you have a touch event.
* @param e The MotionEvent to inspect
* @return whether or not a double tap happened for a pointer
*/
public boolean onTouchEvent(MotionEvent e){
int eventAction = e.getActionMasked();
int pointerIndex;
//Get the pointer index we want to look at
if(eventAction == ACTION_DOWN) pointerIndex = 0;
else if(eventAction == ACTION_POINTER_DOWN) pointerIndex = e.getActionIndex();
else return false;
float eventX = e.getX(pointerIndex);
float eventY = e.getY(pointerIndex);
long eventTime = e.getEventTime();
long deltaTime = eventTime - mLastEventTime;
if(deltaTime > DOUBLE_TAP_MIN_DELTA_MS && deltaTime < DOUBLE_TAP_MAX_DELTA_MS){
int deltaX = (int) mLastX - (int) eventX;
int deltaY = (int) mLastY - (int) eventY;
if((deltaX*deltaX + deltaY*deltaY) < DOUBLE_TAP_SLOP_SQUARE_PX){
//Then I guess there is a double tap :thonk:
resetDoubleTapState();
return true;
}
}
mLastEventTime = eventTime;
mLastX = eventX;
mLastY = eventY;
return false;
}
/**
* Reset the double tap values.
*/
private void resetDoubleTapState(){
mLastEventTime = 0;
mLastX = 9999;
mLastY = 9999;
}
}