mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-18 09:07:48 -04:00
Fix crashes on minecraft 1.13+
This commit is contained in:
parent
0ddc5db967
commit
3aa408af96
@ -11,6 +11,7 @@ import android.widget.ImageView;
|
|||||||
|
|
||||||
import net.kdt.pojavlaunch.BaseMainActivity;
|
import net.kdt.pojavlaunch.BaseMainActivity;
|
||||||
import net.kdt.pojavlaunch.LWJGLGLFWKeycode;
|
import net.kdt.pojavlaunch.LWJGLGLFWKeycode;
|
||||||
|
import net.kdt.pojavlaunch.MainActivity;
|
||||||
import net.kdt.pojavlaunch.R;
|
import net.kdt.pojavlaunch.R;
|
||||||
|
|
||||||
import org.lwjgl.glfw.CallbackBridge;
|
import org.lwjgl.glfw.CallbackBridge;
|
||||||
@ -48,12 +49,14 @@ public class Gamepad {
|
|||||||
|
|
||||||
private final GamepadMapping gameMap = new GamepadMapping();
|
private final GamepadMapping gameMap = new GamepadMapping();
|
||||||
private final GamepadMapping menuMap = new GamepadMapping();
|
private final GamepadMapping menuMap = new GamepadMapping();
|
||||||
private GamepadMapping currentMap = menuMap;
|
private GamepadMapping currentMap = gameMap;
|
||||||
|
|
||||||
private boolean isGrabbing = true;
|
private boolean lastGrabbingState = true;
|
||||||
|
|
||||||
|
|
||||||
private Thread mouseThread;
|
private final Thread mouseThread;
|
||||||
|
private final Runnable mouseRunnable;
|
||||||
|
private final Runnable switchStateRunnable;
|
||||||
|
|
||||||
public Gamepad(BaseMainActivity gameActivity, InputDevice inputDevice){
|
public Gamepad(BaseMainActivity gameActivity, InputDevice inputDevice){
|
||||||
leftJoystick = new GamepadJoystick(MotionEvent.AXIS_X, MotionEvent.AXIS_Y, inputDevice);
|
leftJoystick = new GamepadJoystick(MotionEvent.AXIS_X, MotionEvent.AXIS_Y, inputDevice);
|
||||||
@ -77,8 +80,8 @@ public class Gamepad {
|
|||||||
long now = System.nanoTime();
|
long now = System.nanoTime();
|
||||||
delta += (now - lastTime) / ns;
|
delta += (now - lastTime) / ns;
|
||||||
lastTime = now;
|
lastTime = now;
|
||||||
if(delta >= 1) {
|
|
||||||
|
|
||||||
|
if(delta >= 1) {
|
||||||
updateGrabbingState();
|
updateGrabbingState();
|
||||||
|
|
||||||
tick();
|
tick();
|
||||||
@ -93,11 +96,9 @@ public class Gamepad {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void tick(){
|
private void tick(){
|
||||||
if(lastHorizontalValue != 0 || lastVerticalValue != 0){
|
if(lastHorizontalValue != 0 || lastVerticalValue != 0){
|
||||||
GamepadJoystick currentJoystick = isGrabbing ? leftJoystick : rightJoystick;
|
GamepadJoystick currentJoystick = CallbackBridge.isGrabbing() ? leftJoystick : rightJoystick;
|
||||||
|
|
||||||
acceleration = (mouseMagnitude - currentJoystick.getDeadzone())/(1 - currentJoystick.getDeadzone());
|
acceleration = (mouseMagnitude - currentJoystick.getDeadzone())/(1 - currentJoystick.getDeadzone());
|
||||||
acceleration = Math.pow(acceleration, mouseMaxAcceleration);
|
acceleration = Math.pow(acceleration, mouseMaxAcceleration);
|
||||||
@ -105,12 +106,15 @@ public class Gamepad {
|
|||||||
if(acceleration > 1) acceleration = 1;
|
if(acceleration > 1) acceleration = 1;
|
||||||
|
|
||||||
|
|
||||||
gameActivity.mouse_x += Math.cos(mouseAngle) * acceleration * mouseSensitivity;
|
CallbackBridge.mouseX += Math.cos(mouseAngle) * acceleration * mouseSensitivity;
|
||||||
gameActivity.mouse_y -= Math.sin(mouseAngle) * acceleration * mouseSensitivity;
|
CallbackBridge.mouseY -= Math.sin(mouseAngle) * acceleration * mouseSensitivity;
|
||||||
|
gameActivity.mouse_x = CallbackBridge.mouseX;
|
||||||
|
gameActivity.mouse_y = CallbackBridge.mouseY;
|
||||||
|
|
||||||
CallbackBridge.sendCursorPos(gameActivity.mouse_x, gameActivity.mouse_y);
|
gameActivity.runOnUiThread(mouseRunnable);
|
||||||
if(!isGrabbing){
|
|
||||||
placePointerView((int)(gameActivity.mouse_x / gameActivity.scaleFactor), (int) (gameActivity.mouse_y / gameActivity.scaleFactor));
|
if(!CallbackBridge.isGrabbing()){
|
||||||
|
placePointerView((int)(CallbackBridge.mouseX / gameActivity.scaleFactor), (int) (CallbackBridge.mouseY / gameActivity.scaleFactor));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,20 +122,19 @@ public class Gamepad {
|
|||||||
};
|
};
|
||||||
mouseThread.setPriority(1);
|
mouseThread.setPriority(1);
|
||||||
mouseThread.start();
|
mouseThread.start();
|
||||||
}
|
|
||||||
|
|
||||||
private void updateGrabbingState() {
|
|
||||||
boolean lastGrabbingValue = isGrabbing;
|
//Initialize runnables to be used by the input system, avoiding generating one each time is better memory.
|
||||||
isGrabbing = CallbackBridge.isGrabbing();
|
mouseRunnable = () -> CallbackBridge.sendCursorPos(gameActivity.mouse_x, gameActivity.mouse_y);
|
||||||
if(lastGrabbingValue != isGrabbing){
|
switchStateRunnable = () -> {
|
||||||
if(isGrabbing){
|
if(lastGrabbingState){
|
||||||
//TODO hide the cursor
|
|
||||||
currentMap = gameMap;
|
currentMap = gameMap;
|
||||||
menuMap.resetPressedState();
|
menuMap.resetPressedState();
|
||||||
setPointerViewVisible(false);
|
pointerView.setVisibility(View.INVISIBLE);
|
||||||
mouseSensitivity = 19;
|
mouseSensitivity = 19;
|
||||||
}else{
|
return;
|
||||||
//TODO place the cursor at the center
|
}
|
||||||
|
|
||||||
currentMap = menuMap;
|
currentMap = menuMap;
|
||||||
gameMap.resetPressedState();
|
gameMap.resetPressedState();
|
||||||
sendDirectionalKeycode(currentJoystickDirection, false, gameMap); // removing what we were doing
|
sendDirectionalKeycode(currentJoystickDirection, false, gameMap); // removing what we were doing
|
||||||
@ -140,10 +143,16 @@ public class Gamepad {
|
|||||||
gameActivity.mouse_y = CallbackBridge.windowHeight/2;
|
gameActivity.mouse_y = CallbackBridge.windowHeight/2;
|
||||||
CallbackBridge.sendCursorPos(gameActivity.mouse_x, gameActivity.mouse_y);
|
CallbackBridge.sendCursorPos(gameActivity.mouse_x, gameActivity.mouse_y);
|
||||||
placePointerView(CallbackBridge.physicalWidth/2, CallbackBridge.physicalHeight/2);
|
placePointerView(CallbackBridge.physicalWidth/2, CallbackBridge.physicalHeight/2);
|
||||||
setPointerViewVisible(true);
|
pointerView.setVisibility(View.VISIBLE);
|
||||||
mouseSensitivity = 15;
|
mouseSensitivity = 15;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateGrabbingState() {
|
||||||
|
boolean lastGrabbingValue = lastGrabbingState;
|
||||||
|
lastGrabbingState = CallbackBridge.isGrabbing();
|
||||||
|
if(lastGrabbingValue != lastGrabbingState){
|
||||||
|
gameActivity.runOnUiThread(switchStateRunnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -233,7 +242,7 @@ public class Gamepad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updateMouseJoystick(MotionEvent event){
|
private void updateMouseJoystick(MotionEvent event){
|
||||||
GamepadJoystick currentJoystick = isGrabbing ? rightJoystick : leftJoystick;
|
GamepadJoystick currentJoystick = CallbackBridge.isGrabbing() ? rightJoystick : leftJoystick;
|
||||||
lastHorizontalValue = currentJoystick.getHorizontalAxis(event);
|
lastHorizontalValue = currentJoystick.getHorizontalAxis(event);
|
||||||
lastVerticalValue = currentJoystick.getVerticalAxis(event);
|
lastVerticalValue = currentJoystick.getVerticalAxis(event);
|
||||||
|
|
||||||
@ -242,7 +251,7 @@ public class Gamepad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updateDirectionalJoystick(MotionEvent event){
|
private void updateDirectionalJoystick(MotionEvent event){
|
||||||
GamepadJoystick currentJoystick = isGrabbing ? leftJoystick : rightJoystick;
|
GamepadJoystick currentJoystick = CallbackBridge.isGrabbing() ? leftJoystick : rightJoystick;
|
||||||
|
|
||||||
int lastJoystickDirection = currentJoystickDirection;
|
int lastJoystickDirection = currentJoystickDirection;
|
||||||
currentJoystickDirection = currentJoystick.getHeightDirection(event);
|
currentJoystickDirection = currentJoystick.getHeightDirection(event);
|
||||||
@ -256,7 +265,6 @@ public class Gamepad {
|
|||||||
private void updateAnalogTriggers(MotionEvent event){
|
private void updateAnalogTriggers(MotionEvent event){
|
||||||
getCurrentMap().TRIGGER_LEFT.update(event.getAxisValue(MotionEvent.AXIS_LTRIGGER) > 0.5);
|
getCurrentMap().TRIGGER_LEFT.update(event.getAxisValue(MotionEvent.AXIS_LTRIGGER) > 0.5);
|
||||||
getCurrentMap().TRIGGER_RIGHT.update(event.getAxisValue(MotionEvent.AXIS_RTRIGGER) > 0.5);
|
getCurrentMap().TRIGGER_RIGHT.update(event.getAxisValue(MotionEvent.AXIS_RTRIGGER) > 0.5);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private GamepadMapping getCurrentMap(){
|
private GamepadMapping getCurrentMap(){
|
||||||
@ -301,10 +309,6 @@ public class Gamepad {
|
|||||||
pointerView.setTranslationY(y-32);
|
pointerView.setTranslationY(y-32);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setPointerViewVisible(boolean state){
|
|
||||||
new Handler(Looper.getMainLooper()).post(() -> pointerView.setVisibility( state ? View.VISIBLE : View.INVISIBLE));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendButton(KeyEvent event){
|
private void sendButton(KeyEvent event){
|
||||||
int keycode = event.getKeyCode();
|
int keycode = event.getKeyCode();
|
||||||
@ -355,7 +359,7 @@ public class Gamepad {
|
|||||||
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
BaseMainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_SPACE, CallbackBridge.getCurrentMods(), event.getAction() == KeyEvent.ACTION_DOWN);
|
MainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_SPACE, CallbackBridge.getCurrentMods(), event.getAction() == KeyEvent.ACTION_DOWN);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -371,15 +375,17 @@ public class Gamepad {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT:
|
case LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT:
|
||||||
BaseMainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown);
|
CallbackBridge.putMouseEventWithCoords(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown?1:0, CallbackBridge.mouseX, CallbackBridge.mouseY);
|
||||||
|
//MainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown);
|
||||||
break;
|
break;
|
||||||
case LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT:
|
case LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT:
|
||||||
BaseMainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, isDown);
|
CallbackBridge.putMouseEventWithCoords(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, isDown?1:0, CallbackBridge.mouseX, CallbackBridge.mouseY);
|
||||||
|
//MainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, isDown);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
BaseMainActivity.sendKeyPress(keycode, CallbackBridge.getCurrentMods(), isDown);
|
MainActivity.sendKeyPress(keycode, CallbackBridge.getCurrentMods(), isDown);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user