mirror of
https://github.com/2009scape/2009Scape-mobile.git
synced 2025-12-18 04:20:11 -07:00
- Move most input handling to the MCGLView
- Remove context holding from the Gamepad object - Simplified main_with_custom_ctrl layout hierarchy
This commit is contained in:
parent
3cb50a46ca
commit
15ece0c416
4 changed files with 252 additions and 233 deletions
|
|
@ -2,6 +2,7 @@ package net.kdt.pojavlaunch;
|
|||
|
||||
import static net.kdt.pojavlaunch.BaseMainActivity.sendKeyPress;
|
||||
import static net.kdt.pojavlaunch.BaseMainActivity.sendMouseButton;
|
||||
import static net.kdt.pojavlaunch.BaseMainActivity.touchCharInput;
|
||||
import static net.kdt.pojavlaunch.utils.MCOptionUtils.getMcScale;
|
||||
|
||||
import static org.lwjgl.glfw.CallbackBridge.windowHeight;
|
||||
|
|
@ -10,6 +11,7 @@ import static org.lwjgl.glfw.CallbackBridge.windowWidth;
|
|||
import android.app.Activity;
|
||||
import android.content.*;
|
||||
import android.graphics.SurfaceTexture;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
|
|
@ -17,8 +19,12 @@ import android.util.*;
|
|||
import android.view.*;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.google.android.material.math.MathUtils;
|
||||
|
||||
import net.kdt.pojavlaunch.customcontrols.TouchCharInput;
|
||||
import net.kdt.pojavlaunch.customcontrols.gamepad.Gamepad;
|
||||
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
|
||||
import net.kdt.pojavlaunch.utils.JREUtils;
|
||||
import net.kdt.pojavlaunch.utils.MCOptionUtils;
|
||||
|
|
@ -26,9 +32,11 @@ import net.kdt.pojavlaunch.utils.MCOptionUtils;
|
|||
import org.lwjgl.glfw.CallbackBridge;
|
||||
|
||||
/**
|
||||
* Class dealing with showing minecraft surface and taking inputs and dispatching them to minecraft
|
||||
* Class dealing with showing minecraft surface and taking inputs to dispatch them to minecraft
|
||||
*/
|
||||
public class MinecraftGLView extends TextureView {
|
||||
/* Gamepad object for gamepad inputs, instantiated on need */
|
||||
private Gamepad gamepad = null;
|
||||
|
||||
/* Resolution scaler option, allow downsizing a window */
|
||||
private final float scaleFactor = LauncherPreferences.DEFAULT_PREF.getInt("resolutionRatio",100)/100f;
|
||||
|
|
@ -112,8 +120,8 @@ public class MinecraftGLView extends TextureView {
|
|||
MCOptionUtils.addMCOptionListener(GUIScaleListener);
|
||||
}
|
||||
|
||||
|
||||
public void init(){
|
||||
/** Initialize the view and all its settings */
|
||||
public void start(){
|
||||
setSurfaceTextureListener(new SurfaceTextureListener() {
|
||||
private boolean isCalled = false;
|
||||
@Override
|
||||
|
|
@ -168,19 +176,19 @@ public class MinecraftGLView extends TextureView {
|
|||
|
||||
|
||||
/**
|
||||
* The touch event for both grabbed an non-grabbed mouse state
|
||||
* The touch event for both grabbed an non-grabbed mouse state on the touch screen
|
||||
* Does not cover the virtual mouse touchpad
|
||||
*/
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent e) {
|
||||
//Looking for a mouse to handle, won't have an effect if no mouse exists.
|
||||
// Looking for a mouse to handle, won't have an effect if no mouse exists.
|
||||
for (int i = 0; i < e.getPointerCount(); i++) {
|
||||
if (e.getToolType(i) == MotionEvent.TOOL_TYPE_MOUSE) {
|
||||
if (e.getToolType(i) != MotionEvent.TOOL_TYPE_MOUSE) continue;
|
||||
|
||||
if(CallbackBridge.isGrabbing()) return false;
|
||||
CallbackBridge.sendCursorPos( e.getX(i) * scaleFactor, e.getY(i) * scaleFactor);
|
||||
return true; //mouse event handled successfully
|
||||
}
|
||||
// Mouse found
|
||||
if(CallbackBridge.isGrabbing()) return false;
|
||||
CallbackBridge.sendCursorPos( e.getX(i) * scaleFactor, e.getY(i) * scaleFactor);
|
||||
return true; //mouse event handled successfully
|
||||
}
|
||||
|
||||
// System.out.println("Pre touch, isTouchInHotbar=" + Boolean.toString(isTouchInHotbar) + ", action=" + MotionEvent.actionToString(e.getActionMasked()));
|
||||
|
|
@ -346,6 +354,203 @@ public class MinecraftGLView extends TextureView {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The event for mouse/joystick movements
|
||||
* We don't do the gamepad right now.
|
||||
*/
|
||||
@Override
|
||||
public boolean dispatchGenericMotionEvent(MotionEvent event) {
|
||||
int mouseCursorIndex = -1;
|
||||
|
||||
if(Gamepad.isGamepadEvent(event)){
|
||||
if(gamepad == null){
|
||||
gamepad = new Gamepad(this, event.getDevice());
|
||||
}
|
||||
|
||||
gamepad.update(event);
|
||||
return true;
|
||||
}
|
||||
|
||||
for(int i = 0; i < event.getPointerCount(); i++) {
|
||||
if(event.getToolType(i) != MotionEvent.TOOL_TYPE_MOUSE) continue;
|
||||
// Mouse found
|
||||
mouseCursorIndex = i;
|
||||
break;
|
||||
}
|
||||
if(mouseCursorIndex == -1) return false; // we cant consoom that, theres no mice!
|
||||
if(CallbackBridge.isGrabbing()) {
|
||||
if(BaseMainActivity.isAndroid8OrHigher()){
|
||||
requestFocus();
|
||||
requestPointerCapture();
|
||||
}
|
||||
}
|
||||
switch(event.getActionMasked()) {
|
||||
case MotionEvent.ACTION_HOVER_MOVE:
|
||||
mouse_x = (event.getX(mouseCursorIndex) * scaleFactor);
|
||||
mouse_y = (event.getY(mouseCursorIndex) * scaleFactor);
|
||||
CallbackBridge.sendCursorPos(mouse_x, mouse_y);
|
||||
//debugText.setText(CallbackBridge.DEBUG_STRING.toString());
|
||||
CallbackBridge.DEBUG_STRING.setLength(0);
|
||||
return true;
|
||||
case MotionEvent.ACTION_SCROLL:
|
||||
CallbackBridge.sendScroll((double) event.getAxisValue(MotionEvent.AXIS_VSCROLL), (double) event.getAxisValue(MotionEvent.AXIS_HSCROLL));
|
||||
return true;
|
||||
case MotionEvent.ACTION_BUTTON_PRESS:
|
||||
return sendMouseButtonUnconverted(event.getActionButton(),true);
|
||||
case MotionEvent.ACTION_BUTTON_RELEASE:
|
||||
return sendMouseButtonUnconverted(event.getActionButton(),false);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//TODO MOVE THIS SOMEWHERE ELSE
|
||||
private boolean debugErrored = false;
|
||||
/** The input event for mouse with a captured pointer */
|
||||
@RequiresApi(26)
|
||||
@Override
|
||||
public boolean dispatchCapturedPointerEvent(MotionEvent e) {
|
||||
mouse_x += (e.getX()*scaleFactor);
|
||||
mouse_y += (e.getY()*scaleFactor);
|
||||
CallbackBridge.mouseX = mouse_x;
|
||||
CallbackBridge.mouseY = mouse_y;
|
||||
if(!CallbackBridge.isGrabbing()){
|
||||
releasePointerCapture();
|
||||
clearFocus();
|
||||
}
|
||||
/*
|
||||
if (debugText.getVisibility() == View.VISIBLE && !debugErrored) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
try {
|
||||
builder.append("PointerCapture debug\n");
|
||||
builder.append("MotionEvent=").append(e.getActionMasked()).append("\n");
|
||||
builder.append("PressingBtn=").append(MotionEvent.class.getDeclaredMethod("buttonStateToString").invoke(null, e.getButtonState())).append("\n\n");
|
||||
|
||||
builder.append("PointerX=").append(e.getX()).append("\n");
|
||||
builder.append("PointerY=").append(e.getY()).append("\n");
|
||||
builder.append("RawX=").append(e.getRawX()).append("\n");
|
||||
builder.append("RawY=").append(e.getRawY()).append("\n\n");
|
||||
|
||||
builder.append("XPos=").append(mouse_x).append("\n");
|
||||
builder.append("YPos=").append(mouse_y).append("\n\n");
|
||||
builder.append("MovingX=").append(getMoving(e.getX(), true)).append("\n");
|
||||
builder.append("MovingY=").append(getMoving(e.getY(), false)).append("\n");
|
||||
} catch (Throwable th) {
|
||||
debugErrored = true;
|
||||
builder.append("Error getting debug. The debug will be stopped!\n").append(Log.getStackTraceString(th));
|
||||
} finally {
|
||||
debugText.setText(builder.toString());
|
||||
builder.setLength(0);
|
||||
}
|
||||
}*/
|
||||
|
||||
//debugText.setText(CallbackBridge.DEBUG_STRING.toString());
|
||||
CallbackBridge.DEBUG_STRING.setLength(0);
|
||||
switch (e.getActionMasked()) {
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
CallbackBridge.sendCursorPos(mouse_x, mouse_y);
|
||||
return true;
|
||||
case MotionEvent.ACTION_BUTTON_PRESS:
|
||||
return sendMouseButtonUnconverted(e.getActionButton(), true);
|
||||
case MotionEvent.ACTION_BUTTON_RELEASE:
|
||||
return sendMouseButtonUnconverted(e.getActionButton(), false);
|
||||
case MotionEvent.ACTION_SCROLL:
|
||||
CallbackBridge.sendScroll(e.getAxisValue(MotionEvent.AXIS_HSCROLL), e.getAxisValue(MotionEvent.AXIS_VSCROLL));
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** The event for keyboard/ gamepad button inputs */
|
||||
@Override
|
||||
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
|
||||
//Toast.makeText(this, event.toString(),Toast.LENGTH_SHORT).show();
|
||||
//Toast.makeText(this, event.getDevice().toString(), Toast.LENGTH_SHORT).show();
|
||||
|
||||
//Filtering useless events by order of probability
|
||||
if((event.getFlags() & KeyEvent.FLAG_FALLBACK) == KeyEvent.FLAG_FALLBACK) return true;
|
||||
int eventKeycode = event.getKeyCode();
|
||||
if(eventKeycode == KeyEvent.KEYCODE_UNKNOWN) return true;
|
||||
if(eventKeycode == KeyEvent.KEYCODE_VOLUME_DOWN) return false;
|
||||
if(eventKeycode == KeyEvent.KEYCODE_VOLUME_UP) return false;
|
||||
if(event.getRepeatCount() != 0) return true;
|
||||
if(event.getAction() == KeyEvent.ACTION_MULTIPLE) return true;
|
||||
|
||||
//Toast.makeText(this, "FIRST VERIF PASSED", Toast.LENGTH_SHORT).show();
|
||||
|
||||
//Sometimes, key events comes from SOME keys of the software keyboard
|
||||
//Even weirder, is is unknown why a key or another is selected to trigger a keyEvent
|
||||
if((event.getFlags() & KeyEvent.FLAG_SOFT_KEYBOARD) == KeyEvent.FLAG_SOFT_KEYBOARD){
|
||||
if(eventKeycode == KeyEvent.KEYCODE_ENTER) return true; //We already listen to it.
|
||||
touchCharInput.dispatchKeyEvent(event);
|
||||
return true;
|
||||
}
|
||||
//Toast.makeText(this, "SECOND VERIF PASSED", Toast.LENGTH_SHORT).show();
|
||||
|
||||
|
||||
//Sometimes, key events may come from the mouse
|
||||
if(event.getDevice() != null
|
||||
&& ( (event.getSource() & InputDevice.SOURCE_MOUSE_RELATIVE) == InputDevice.SOURCE_MOUSE_RELATIVE
|
||||
|| (event.getSource() & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE) ){
|
||||
//Toast.makeText(this, "THE EVENT COMES FROM A MOUSE", Toast.LENGTH_SHORT).show();
|
||||
|
||||
|
||||
if(eventKeycode == KeyEvent.KEYCODE_BACK){
|
||||
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, event.getAction() == KeyEvent.ACTION_DOWN);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
System.out.println(event);
|
||||
|
||||
if(Gamepad.isGamepadEvent(event)){
|
||||
|
||||
if(gamepad == null){
|
||||
gamepad = new Gamepad(this, event.getDevice());
|
||||
}
|
||||
|
||||
gamepad.update(event);
|
||||
return true;
|
||||
}
|
||||
|
||||
int index = EfficientAndroidLWJGLKeycode.getIndexByKey(eventKeycode);
|
||||
if(index >= 0) {
|
||||
//Toast.makeText(this,"THIS IS A KEYBOARD EVENT !", Toast.LENGTH_SHORT).show();
|
||||
EfficientAndroidLWJGLKeycode.execKey(event, index);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Get the mouse direction as a string */
|
||||
private String getMoving(float pos, boolean xOrY) {
|
||||
if (pos == 0) return "STOPPED";
|
||||
if (pos > 0) return xOrY ? "RIGHT" : "DOWN";
|
||||
return xOrY ? "LEFT" : "UP";
|
||||
}
|
||||
|
||||
/** Convert the mouse button, then send it
|
||||
* @return Whether the event was processed
|
||||
*/
|
||||
public static boolean sendMouseButtonUnconverted(int button, boolean status) {
|
||||
int glfwButton = -256;
|
||||
switch (button) {
|
||||
case MotionEvent.BUTTON_PRIMARY:
|
||||
glfwButton = LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT;
|
||||
break;
|
||||
case MotionEvent.BUTTON_TERTIARY:
|
||||
glfwButton = LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_MIDDLE;
|
||||
break;
|
||||
case MotionEvent.BUTTON_SECONDARY:
|
||||
glfwButton = LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT;
|
||||
break;
|
||||
}
|
||||
if(glfwButton == -256) return false;
|
||||
sendMouseButton(glfwButton, status);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return the hotbar key, given the position. -1 if no key are pressed */
|
||||
public int handleGuiBar(int x, int y) {
|
||||
if (!CallbackBridge.isGrabbing()) return -1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue