Clean up onTouch function

This commit is contained in:
SerpentSpirale 2021-06-17 14:31:03 +02:00
parent 1aabb184d3
commit 3099301645

View File

@ -308,7 +308,6 @@ public class BaseMainActivity extends LoggableActivity {
return true; return true;
}); });
// System.loadLibrary("Regal");
minecraftGLView.setFocusable(true); minecraftGLView.setFocusable(true);
glTouchListener = new OnTouchListener(){ glTouchListener = new OnTouchListener(){
@ -317,25 +316,18 @@ public class BaseMainActivity extends LoggableActivity {
@Override @Override
public boolean onTouch(View p1, MotionEvent e) { public boolean onTouch(View p1, MotionEvent e) {
{ //Looking for a mouse to handle, won't have an effect if no mouse exists.
int mptrIndex = -1; for (int i = 0; i < e.getPointerCount(); i++) {
for (int i = 0; i < e.getPointerCount(); i++) { if (e.getToolType(i) == MotionEvent.TOOL_TYPE_MOUSE) {
if (e.getToolType(i) == MotionEvent.TOOL_TYPE_MOUSE) { //if there's at least one mouse...
mptrIndex = i; //index it if(CallbackBridge.isGrabbing()) return false;
} int x = (int) (e.getX(i) * scaleFactor);
} int y = (int) (e.getY(i) * scaleFactor);
if (mptrIndex != -1) {
if(CallbackBridge.isGrabbing()) {
return false;
}
//handle mouse events by just sending the coords of the new point in touch event
int x = (int) (e.getX(mptrIndex) * scaleFactor);
int y = (int) (e.getY(mptrIndex) * scaleFactor);
CallbackBridge.mouseX = x; CallbackBridge.mouseX = x;
CallbackBridge.mouseY = y; CallbackBridge.mouseY = y;
CallbackBridge.sendCursorPos(x, y); CallbackBridge.sendCursorPos(x, y);
return true; // event handled sucessfully return true; //mouse event handled successfully
}//if index IS -1, continue handling as an usual touch event }
} }
// System.out.println("Pre touch, isTouchInHotbar=" + Boolean.toString(isTouchInHotbar) + ", action=" + MotionEvent.actionToString(e.getActionMasked())); // System.out.println("Pre touch, isTouchInHotbar=" + Boolean.toString(isTouchInHotbar) + ", action=" + MotionEvent.actionToString(e.getActionMasked()));
@ -357,10 +349,11 @@ public class BaseMainActivity extends LoggableActivity {
switch (e.getActionMasked()) { switch (e.getActionMasked()) {
case MotionEvent.ACTION_DOWN: // 0 case MotionEvent.ACTION_DOWN: // 0
CallbackBridge.sendPrepareGrabInitialPos(); CallbackBridge.sendPrepareGrabInitialPos();
isTouchInHotbar = hudKeyHandled != -1; isTouchInHotbar = hudKeyHandled != -1;
if (isTouchInHotbar) { if (isTouchInHotbar) {
sendKeyPress(hudKeyHandled, 0, true); sendKeyPress(hudKeyHandled, 0, true);
sendKeyPress(hudKeyHandled, 0, false);
hotbarX = (int)e.getX(); hotbarX = (int)e.getX();
hotbarY = (int)e.getY(); hotbarY = (int)e.getY();
@ -396,9 +389,7 @@ public class BaseMainActivity extends LoggableActivity {
} }
if (CallbackBridge.isGrabbing()) { if (CallbackBridge.isGrabbing()) {
if (isTouchInHotbar && Math.abs(hotbarX - mouse_x) < fingerStillThreshold && Math.abs(hotbarY - mouse_y) < fingerStillThreshold) { if (!triggeredLeftMouseButton && Math.abs(initialX - mouse_x) < fingerStillThreshold && Math.abs(initialY - mouse_y) < fingerStillThreshold) {
sendKeyPress(hudKeyHandled, 0, false);
} else if (!triggeredLeftMouseButton && Math.abs(initialX - mouse_x) < fingerStillThreshold && Math.abs(initialY - mouse_y) < fingerStillThreshold) {
if (!LauncherPreferences.PREF_DISABLE_GESTURES) { if (!LauncherPreferences.PREF_DISABLE_GESTURES) {
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, true); sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, true);
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, false); sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, false);
@ -422,12 +413,9 @@ public class BaseMainActivity extends LoggableActivity {
scrollInitialX = CallbackBridge.mouseX; scrollInitialX = CallbackBridge.mouseX;
scrollInitialY = CallbackBridge.mouseY; scrollInitialY = CallbackBridge.mouseY;
break; break;
case MotionEvent.ACTION_POINTER_UP: // 6
break;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
if (!CallbackBridge.isGrabbing() && e.getPointerCount() == 2 && !LauncherPreferences.PREF_DISABLE_GESTURES) { if (!CallbackBridge.isGrabbing() && e.getPointerCount() == 2 && !LauncherPreferences.PREF_DISABLE_GESTURES) { //Scrolling feature
CallbackBridge.sendScroll(Tools.pxToDp(mouse_x - scrollInitialX)/30 , Tools.pxToDp(mouse_y - scrollInitialY)/30); CallbackBridge.sendScroll(Tools.pxToDp(mouse_x - scrollInitialX)/30 , Tools.pxToDp(mouse_y - scrollInitialY)/30);
scrollInitialX = mouse_x; scrollInitialX = mouse_x;
scrollInitialY = mouse_y; scrollInitialY = mouse_y;
@ -441,6 +429,7 @@ public class BaseMainActivity extends LoggableActivity {
} }
} }
//Camera movement
if(CallbackBridge.isGrabbing()){ if(CallbackBridge.isGrabbing()){
if(e.getPointerId(0) != currentPointerID){ if(e.getPointerId(0) != currentPointerID){
currentPointerID = e.getPointerId(0); currentPointerID = e.getPointerId(0);
@ -452,8 +441,6 @@ public class BaseMainActivity extends LoggableActivity {
prevY = e.getY(); prevY = e.getY();
} }
debugText.setText(CallbackBridge.DEBUG_STRING.toString()); debugText.setText(CallbackBridge.DEBUG_STRING.toString());
CallbackBridge.DEBUG_STRING.setLength(0); CallbackBridge.DEBUG_STRING.setLength(0);