- Fix distance evaluation bug

- Move static inputs methods to CallbackBridge
- Restored pointerDebugTextview, now auto-instanciated.
- Simplified main_with_ctrl hierarchy
This commit is contained in:
SerpentSpirale 2021-11-19 15:54:41 +01:00 committed by Boulay Mathias
parent 15ece0c416
commit 2a12db6216
9 changed files with 85 additions and 64 deletions

View file

@ -3,6 +3,7 @@ package net.kdt.pojavlaunch;
import static net.kdt.pojavlaunch.Architecture.ARCH_X86; import static net.kdt.pojavlaunch.Architecture.ARCH_X86;
import static net.kdt.pojavlaunch.Tools.currentDisplayMetrics; import static net.kdt.pojavlaunch.Tools.currentDisplayMetrics;
import static org.lwjgl.glfw.CallbackBridge.sendKeyPress;
import static org.lwjgl.glfw.CallbackBridge.windowHeight; import static org.lwjgl.glfw.CallbackBridge.windowHeight;
import static org.lwjgl.glfw.CallbackBridge.windowWidth; import static org.lwjgl.glfw.CallbackBridge.windowWidth;
@ -54,7 +55,7 @@ public class BaseMainActivity extends LoggableActivity {
private ScrollView contentScroll; private ScrollView contentScroll;
private ToggleButton toggleLog; private ToggleButton toggleLog;
private TextView debugText;
private NavigationView.OnNavigationItemSelectedListener gameActionListener; private NavigationView.OnNavigationItemSelectedListener gameActionListener;
public NavigationView.OnNavigationItemSelectedListener ingameControlsEditorListener; public NavigationView.OnNavigationItemSelectedListener ingameControlsEditorListener;
@ -124,7 +125,7 @@ public class BaseMainActivity extends LoggableActivity {
break; break;
case R.id.nav_viewlog: openLogOutput(); case R.id.nav_viewlog: openLogOutput();
break; break;
case R.id.nav_debug: toggleDebug(); case R.id.nav_debug: minecraftGLView.togglepointerDebugging();
break; break;
case R.id.nav_customkey: dialogSendCustomKey(); case R.id.nav_customkey: dialogSendCustomKey();
break; break;
@ -153,7 +154,6 @@ public class BaseMainActivity extends LoggableActivity {
appendToLog(""); appendToLog("");
}); });
this.debugText = findViewById(R.id.content_text_debug);
this.minecraftGLView = findViewById(R.id.main_game_render_view); this.minecraftGLView = findViewById(R.id.main_game_render_view);
this.drawerLayout.closeDrawers(); this.drawerLayout.closeDrawers();
@ -267,10 +267,6 @@ public class BaseMainActivity extends LoggableActivity {
return s.toString(); return s.toString();
} }
private void toggleDebug() {
debugText.setVisibility(debugText.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}
private void dialogSendCustomKey() { private void dialogSendCustomKey() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this); AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.control_customkey); dialog.setTitle(R.string.control_customkey);
@ -280,13 +276,13 @@ public class BaseMainActivity extends LoggableActivity {
boolean isInEditor; boolean isInEditor;
private void openCustomControls() { private void openCustomControls() {
if(ingameControlsEditorListener != null) { if(ingameControlsEditorListener == null) return;
((MainActivity)this).mControlLayout.setModifiable(true);
navDrawer.getMenu().clear(); ((MainActivity)this).mControlLayout.setModifiable(true);
navDrawer.inflateMenu(R.menu.menu_customctrl); navDrawer.getMenu().clear();
navDrawer.setNavigationItemSelectedListener(ingameControlsEditorListener); navDrawer.inflateMenu(R.menu.menu_customctrl);
isInEditor = true; navDrawer.setNavigationItemSelectedListener(ingameControlsEditorListener);
} isInEditor = true;
} }
public void leaveCustomControls() { public void leaveCustomControls() {
@ -364,27 +360,6 @@ public class BaseMainActivity extends LoggableActivity {
if(touchCharInput != null) touchCharInput.switchKeyboardState(); if(touchCharInput != null) touchCharInput.switchKeyboardState();
} }
public static void sendKeyPress(int keyCode, int modifiers, boolean status) {
sendKeyPress(keyCode, 0, modifiers, status);
}
public static void sendKeyPress(int keyCode, int scancode, int modifiers, boolean status) {
sendKeyPress(keyCode, '\u0000', scancode, modifiers, status);
}
public static void sendKeyPress(int keyCode, char keyChar, int scancode, int modifiers, boolean status) {
CallbackBridge.sendKeycode(keyCode, keyChar, scancode, modifiers, status);
}
public static void sendKeyPress(int keyCode) {
sendKeyPress(keyCode, CallbackBridge.getCurrentMods(), true);
sendKeyPress(keyCode, CallbackBridge.getCurrentMods(), false);
}
public static void sendMouseButton(int button, boolean status) {
CallbackBridge.sendMouseKeycode(button, CallbackBridge.getCurrentMods(), status);
}

View file

@ -1,5 +1,7 @@
package net.kdt.pojavlaunch; package net.kdt.pojavlaunch;
import static org.lwjgl.glfw.CallbackBridge.sendKeyPress;
import android.view.KeyEvent; import android.view.KeyEvent;
import net.kdt.pojavlaunch.prefs.LauncherPreferences; import net.kdt.pojavlaunch.prefs.LauncherPreferences;
@ -197,7 +199,7 @@ public class EfficientAndroidLWJGLKeycode {
try { try {
System.out.println(keyEvent.getKeyCode() + " " +keyEvent.getDisplayLabel()); System.out.println(keyEvent.getKeyCode() + " " +keyEvent.getDisplayLabel());
char key = (char)(keyEvent.getUnicodeChar() != 0 ? keyEvent.getUnicodeChar() : '\u0000'); char key = (char)(keyEvent.getUnicodeChar() != 0 ? keyEvent.getUnicodeChar() : '\u0000');
BaseMainActivity.sendKeyPress( sendKeyPress(
getValueByIndex(valueIndex), getValueByIndex(valueIndex),
key, key,
0, 0,
@ -211,7 +213,7 @@ public class EfficientAndroidLWJGLKeycode {
public static void execKeyIndex(int index){ public static void execKeyIndex(int index){
//Send a quick key press. //Send a quick key press.
BaseMainActivity.sendKeyPress(getValueByIndex(index)); sendKeyPress(getValueByIndex(index));
} }
public static int getValueByIndex(int index) { public static int getValueByIndex(int index) {

View file

@ -1,10 +1,10 @@
package net.kdt.pojavlaunch; 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.BaseMainActivity.touchCharInput;
import static net.kdt.pojavlaunch.utils.MCOptionUtils.getMcScale; import static net.kdt.pojavlaunch.utils.MCOptionUtils.getMcScale;
import static org.lwjgl.glfw.CallbackBridge.sendKeyPress;
import static org.lwjgl.glfw.CallbackBridge.sendMouseButton;
import static org.lwjgl.glfw.CallbackBridge.windowHeight; import static org.lwjgl.glfw.CallbackBridge.windowHeight;
import static org.lwjgl.glfw.CallbackBridge.windowWidth; import static org.lwjgl.glfw.CallbackBridge.windowWidth;
@ -17,6 +17,7 @@ import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.util.*; import android.util.*;
import android.view.*; import android.view.*;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
@ -37,6 +38,8 @@ import org.lwjgl.glfw.CallbackBridge;
public class MinecraftGLView extends TextureView { public class MinecraftGLView extends TextureView {
/* Gamepad object for gamepad inputs, instantiated on need */ /* Gamepad object for gamepad inputs, instantiated on need */
private Gamepad gamepad = null; private Gamepad gamepad = null;
/* Pointer Debug textview, used to show info about the pointer state */
private TextView pointerDebugText;
/* Resolution scaler option, allow downsizing a window */ /* Resolution scaler option, allow downsizing a window */
private final float scaleFactor = LauncherPreferences.DEFAULT_PREF.getInt("resolutionRatio",100)/100f; private final float scaleFactor = LauncherPreferences.DEFAULT_PREF.getInt("resolutionRatio",100)/100f;
@ -88,7 +91,7 @@ public class MinecraftGLView extends TextureView {
float x = CallbackBridge.mouseX; float x = CallbackBridge.mouseX;
float y = CallbackBridge.mouseY; float y = CallbackBridge.mouseY;
if (CallbackBridge.isGrabbing() && if (CallbackBridge.isGrabbing() &&
MathUtils.dist(x, y, mouse_x, mouse_y) < FINGER_STILL_THRESHOLD) { MathUtils.dist(x, y, initialX, initialY) < FINGER_STILL_THRESHOLD) {
triggeredLeftMouseButton = true; triggeredLeftMouseButton = true;
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, true); sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, true);
} }
@ -122,6 +125,13 @@ public class MinecraftGLView extends TextureView {
/** Initialize the view and all its settings */ /** Initialize the view and all its settings */
public void start(){ public void start(){
// Add the pointer debug textview
pointerDebugText = new TextView(getContext());
pointerDebugText.setX(0);
pointerDebugText.setY(0);
pointerDebugText.setVisibility(GONE);
((ViewGroup)getParent()).addView(pointerDebugText);
setSurfaceTextureListener(new SurfaceTextureListener() { setSurfaceTextureListener(new SurfaceTextureListener() {
private boolean isCalled = false; private boolean isCalled = false;
@Override @Override
@ -418,8 +428,8 @@ public class MinecraftGLView extends TextureView {
releasePointerCapture(); releasePointerCapture();
clearFocus(); clearFocus();
} }
/*
if (debugText.getVisibility() == View.VISIBLE && !debugErrored) { if (pointerDebugText.getVisibility() == View.VISIBLE && !debugErrored) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
try { try {
builder.append("PointerCapture debug\n"); builder.append("PointerCapture debug\n");
@ -439,12 +449,12 @@ public class MinecraftGLView extends TextureView {
debugErrored = true; debugErrored = true;
builder.append("Error getting debug. The debug will be stopped!\n").append(Log.getStackTraceString(th)); builder.append("Error getting debug. The debug will be stopped!\n").append(Log.getStackTraceString(th));
} finally { } finally {
debugText.setText(builder.toString()); pointerDebugText.setText(builder.toString());
builder.setLength(0); builder.setLength(0);
} }
}*/ }
//debugText.setText(CallbackBridge.DEBUG_STRING.toString()); pointerDebugText.setText(CallbackBridge.DEBUG_STRING.toString());
CallbackBridge.DEBUG_STRING.setLength(0); CallbackBridge.DEBUG_STRING.setLength(0);
switch (e.getActionMasked()) { switch (e.getActionMasked()) {
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
@ -523,6 +533,21 @@ public class MinecraftGLView extends TextureView {
return false; return false;
} }
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
return super.dispatchKeyEventPreIme(event);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
return super.dispatchKeyEvent(event);
}
@Override
public boolean dispatchKeyShortcutEvent(KeyEvent event) {
return super.dispatchKeyShortcutEvent(event);
}
/** Get the mouse direction as a string */ /** Get the mouse direction as a string */
private String getMoving(float pos, boolean xOrY) { private String getMoving(float pos, boolean xOrY) {
if (pos == 0) return "STOPPED"; if (pos == 0) return "STOPPED";
@ -571,12 +596,11 @@ public class MinecraftGLView extends TextureView {
return (int)((GUIScale * input)/scaleFactor); return (int)((GUIScale * input)/scaleFactor);
} }
@Override /** Toggle the pointerDebugText visibility state */
public boolean dispatchKeyEvent(KeyEvent event) { public void togglepointerDebugging() {
return super.dispatchKeyEvent(event); pointerDebugText.setVisibility(pointerDebugText.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
} }
/** A small interface called when the listener is ready for the first time */ /** A small interface called when the listener is ready for the first time */
public interface SurfaceReadyListener { public interface SurfaceReadyListener {
void isReady(); void isReady();

View file

@ -15,6 +15,7 @@ import net.objecthunter.exp4j.function.Function;
import org.lwjgl.glfw.*; import org.lwjgl.glfw.*;
import static net.kdt.pojavlaunch.LWJGLGLFWKeycode.GLFW_KEY_UNKNOWN; import static net.kdt.pojavlaunch.LWJGLGLFWKeycode.GLFW_KEY_UNKNOWN;
import static org.lwjgl.glfw.CallbackBridge.sendKeyPress;
public class ControlData { public class ControlData {
@ -174,7 +175,7 @@ public class ControlData {
public void execute(boolean isDown) { public void execute(boolean isDown) {
for(int keycode : keycodes){ for(int keycode : keycodes){
BaseMainActivity.sendKeyPress(keycode, 0, isDown); sendKeyPress(keycode, 0, isDown);
} }
} }

View file

@ -3,6 +3,8 @@ package net.kdt.pojavlaunch.customcontrols;
import static android.content.Context.INPUT_METHOD_SERVICE; import static android.content.Context.INPUT_METHOD_SERVICE;
import static org.lwjgl.glfw.CallbackBridge.sendKeyPress;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration; import android.content.res.Configuration;
@ -125,7 +127,7 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText
* Send the enter key. * Send the enter key.
*/ */
private void sendEnter(){ private void sendEnter(){
BaseMainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_ENTER); sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_ENTER);
clear(); clear();
} }

View file

@ -20,13 +20,14 @@ import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import org.lwjgl.glfw.*; import org.lwjgl.glfw.*;
import static net.kdt.pojavlaunch.BaseMainActivity.sendMouseButton;
import static net.kdt.pojavlaunch.LWJGLGLFWKeycode.GLFW_KEY_UNKNOWN; import static net.kdt.pojavlaunch.LWJGLGLFWKeycode.GLFW_KEY_UNKNOWN;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_BUTTONSIZE; import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_BUTTONSIZE;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_BOTTOM_OFFSET; import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_BOTTOM_OFFSET;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_LEFT_OFFSET; import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_LEFT_OFFSET;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_RIGHT_OFFSET; import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_RIGHT_OFFSET;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_TOP_OFFSET; import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_CONTROL_TOP_OFFSET;
import static org.lwjgl.glfw.CallbackBridge.sendKeyPress;
import static org.lwjgl.glfw.CallbackBridge.sendMouseButton;
@SuppressLint("ViewConstructor") @SuppressLint("ViewConstructor")
public class ControlButton extends androidx.appcompat.widget.AppCompatButton implements OnLongClickListener public class ControlButton extends androidx.appcompat.widget.AppCompatButton implements OnLongClickListener
@ -512,7 +513,7 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
setActivated(isDown); setActivated(isDown);
for(int keycode : mProperties.keycodes){ for(int keycode : mProperties.keycodes){
if(keycode >= GLFW_KEY_UNKNOWN){ if(keycode >= GLFW_KEY_UNKNOWN){
MainActivity.sendKeyPress(keycode, CallbackBridge.getCurrentMods(), isDown); sendKeyPress(keycode, CallbackBridge.getCurrentMods(), isDown);
CallbackBridge.setModifiers(keycode, isDown); CallbackBridge.setModifiers(keycode, isDown);
}else{ }else{
sendSpecialKey(keycode, isDown); sendSpecialKey(keycode, isDown);

View file

@ -38,6 +38,8 @@ import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.DIRECTI
import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.DIRECTION_WEST; import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.DIRECTION_WEST;
import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.isJoystickEvent; import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.isJoystickEvent;
import static net.kdt.pojavlaunch.utils.MCOptionUtils.getMcScale; import static net.kdt.pojavlaunch.utils.MCOptionUtils.getMcScale;
import static org.lwjgl.glfw.CallbackBridge.sendKeyPress;
import static org.lwjgl.glfw.CallbackBridge.sendMouseButton;
public class Gamepad { public class Gamepad {
@ -354,7 +356,7 @@ public class Gamepad {
default: default:
MainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_SPACE, CallbackBridge.getCurrentMods(), event.getAction() == KeyEvent.ACTION_DOWN); sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_SPACE, CallbackBridge.getCurrentMods(), event.getAction() == KeyEvent.ACTION_DOWN);
break; break;
} }
} }
@ -370,15 +372,15 @@ public class Gamepad {
break; break;
case LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT: case LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT:
MainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown); sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown);
break; break;
case LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT: case LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT:
MainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, isDown); sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, isDown);
break; break;
default: default:
MainActivity.sendKeyPress(keycode, CallbackBridge.getCurrentMods(), isDown); sendKeyPress(keycode, CallbackBridge.getCurrentMods(), isDown);
break; break;
} }
CallbackBridge.setModifiers(keycode, isDown); CallbackBridge.setModifiers(keycode, isDown);

View file

@ -84,6 +84,27 @@ public class CallbackBridge {
nativeSendChar(keychar); nativeSendChar(keychar);
} }
public static void sendKeyPress(int keyCode, int modifiers, boolean status) {
sendKeyPress(keyCode, 0, modifiers, status);
}
public static void sendKeyPress(int keyCode, int scancode, int modifiers, boolean status) {
sendKeyPress(keyCode, '\u0000', scancode, modifiers, status);
}
public static void sendKeyPress(int keyCode, char keyChar, int scancode, int modifiers, boolean status) {
CallbackBridge.sendKeycode(keyCode, keyChar, scancode, modifiers, status);
}
public static void sendKeyPress(int keyCode) {
sendKeyPress(keyCode, CallbackBridge.getCurrentMods(), true);
sendKeyPress(keyCode, CallbackBridge.getCurrentMods(), false);
}
public static void sendMouseButton(int button, boolean status) {
CallbackBridge.sendMouseKeycode(button, CallbackBridge.getCurrentMods(), status);
}
public static void sendMouseKeycode(int button, int modifiers, boolean isDown) { public static void sendMouseKeycode(int button, int modifiers, boolean isDown) {
DEBUG_STRING.append("MouseKey=").append(button).append(", down=").append(isDown).append("\n"); DEBUG_STRING.append("MouseKey=").append(button).append(", down=").append(isDown).append("\n");
// if (isGrabbing()) DEBUG_STRING.append("MouseGrabStrace: " + android.util.Log.getStackTraceString(new Throwable()) + "\n"); // if (isGrabbing()) DEBUG_STRING.append("MouseGrabStrace: " + android.util.Log.getStackTraceString(new Throwable()) + "\n");

View file

@ -98,13 +98,6 @@
</LinearLayout> </LinearLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="PointerCapture debug"
android:id="@+id/content_text_debug"
android:visibility="gone"/>
</FrameLayout> </FrameLayout>
<com.google.android.material.navigation.NavigationView <com.google.android.material.navigation.NavigationView