- 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.Tools.currentDisplayMetrics;
import static org.lwjgl.glfw.CallbackBridge.sendKeyPress;
import static org.lwjgl.glfw.CallbackBridge.windowHeight;
import static org.lwjgl.glfw.CallbackBridge.windowWidth;
@ -54,7 +55,7 @@ public class BaseMainActivity extends LoggableActivity {
private ScrollView contentScroll;
private ToggleButton toggleLog;
private TextView debugText;
private NavigationView.OnNavigationItemSelectedListener gameActionListener;
public NavigationView.OnNavigationItemSelectedListener ingameControlsEditorListener;
@ -124,7 +125,7 @@ public class BaseMainActivity extends LoggableActivity {
break;
case R.id.nav_viewlog: openLogOutput();
break;
case R.id.nav_debug: toggleDebug();
case R.id.nav_debug: minecraftGLView.togglepointerDebugging();
break;
case R.id.nav_customkey: dialogSendCustomKey();
break;
@ -153,7 +154,6 @@ public class BaseMainActivity extends LoggableActivity {
appendToLog("");
});
this.debugText = findViewById(R.id.content_text_debug);
this.minecraftGLView = findViewById(R.id.main_game_render_view);
this.drawerLayout.closeDrawers();
@ -267,10 +267,6 @@ public class BaseMainActivity extends LoggableActivity {
return s.toString();
}
private void toggleDebug() {
debugText.setVisibility(debugText.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}
private void dialogSendCustomKey() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.control_customkey);
@ -280,14 +276,14 @@ public class BaseMainActivity extends LoggableActivity {
boolean isInEditor;
private void openCustomControls() {
if(ingameControlsEditorListener != null) {
if(ingameControlsEditorListener == null) return;
((MainActivity)this).mControlLayout.setModifiable(true);
navDrawer.getMenu().clear();
navDrawer.inflateMenu(R.menu.menu_customctrl);
navDrawer.setNavigationItemSelectedListener(ingameControlsEditorListener);
isInEditor = true;
}
}
public void leaveCustomControls() {
if(this instanceof MainActivity) {
@ -365,27 +361,6 @@ public class BaseMainActivity extends LoggableActivity {
}
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;
import static org.lwjgl.glfw.CallbackBridge.sendKeyPress;
import android.view.KeyEvent;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
@ -197,7 +199,7 @@ public class EfficientAndroidLWJGLKeycode {
try {
System.out.println(keyEvent.getKeyCode() + " " +keyEvent.getDisplayLabel());
char key = (char)(keyEvent.getUnicodeChar() != 0 ? keyEvent.getUnicodeChar() : '\u0000');
BaseMainActivity.sendKeyPress(
sendKeyPress(
getValueByIndex(valueIndex),
key,
0,
@ -211,7 +213,7 @@ public class EfficientAndroidLWJGLKeycode {
public static void execKeyIndex(int index){
//Send a quick key press.
BaseMainActivity.sendKeyPress(getValueByIndex(index));
sendKeyPress(getValueByIndex(index));
}
public static int getValueByIndex(int index) {

View file

@ -1,10 +1,10 @@
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.sendKeyPress;
import static org.lwjgl.glfw.CallbackBridge.sendMouseButton;
import static org.lwjgl.glfw.CallbackBridge.windowHeight;
import static org.lwjgl.glfw.CallbackBridge.windowWidth;
@ -17,6 +17,7 @@ import android.os.Looper;
import android.os.Message;
import android.util.*;
import android.view.*;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
@ -37,6 +38,8 @@ import org.lwjgl.glfw.CallbackBridge;
public class MinecraftGLView extends TextureView {
/* Gamepad object for gamepad inputs, instantiated on need */
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 */
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 y = CallbackBridge.mouseY;
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;
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, true);
}
@ -122,6 +125,13 @@ public class MinecraftGLView extends TextureView {
/** Initialize the view and all its settings */
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() {
private boolean isCalled = false;
@Override
@ -418,8 +428,8 @@ public class MinecraftGLView extends TextureView {
releasePointerCapture();
clearFocus();
}
/*
if (debugText.getVisibility() == View.VISIBLE && !debugErrored) {
if (pointerDebugText.getVisibility() == View.VISIBLE && !debugErrored) {
StringBuilder builder = new StringBuilder();
try {
builder.append("PointerCapture debug\n");
@ -439,12 +449,12 @@ public class MinecraftGLView extends TextureView {
debugErrored = true;
builder.append("Error getting debug. The debug will be stopped!\n").append(Log.getStackTraceString(th));
} finally {
debugText.setText(builder.toString());
pointerDebugText.setText(builder.toString());
builder.setLength(0);
}
}*/
}
//debugText.setText(CallbackBridge.DEBUG_STRING.toString());
pointerDebugText.setText(CallbackBridge.DEBUG_STRING.toString());
CallbackBridge.DEBUG_STRING.setLength(0);
switch (e.getActionMasked()) {
case MotionEvent.ACTION_MOVE:
@ -523,6 +533,21 @@ public class MinecraftGLView extends TextureView {
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 */
private String getMoving(float pos, boolean xOrY) {
if (pos == 0) return "STOPPED";
@ -571,12 +596,11 @@ public class MinecraftGLView extends TextureView {
return (int)((GUIScale * input)/scaleFactor);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
return super.dispatchKeyEvent(event);
/** Toggle the pointerDebugText visibility state */
public void togglepointerDebugging() {
pointerDebugText.setVisibility(pointerDebugText.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}
/** A small interface called when the listener is ready for the first time */
public interface SurfaceReadyListener {
void isReady();

View file

@ -15,6 +15,7 @@ import net.objecthunter.exp4j.function.Function;
import org.lwjgl.glfw.*;
import static net.kdt.pojavlaunch.LWJGLGLFWKeycode.GLFW_KEY_UNKNOWN;
import static org.lwjgl.glfw.CallbackBridge.sendKeyPress;
public class ControlData {
@ -174,7 +175,7 @@ public class ControlData {
public void execute(boolean isDown) {
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 org.lwjgl.glfw.CallbackBridge.sendKeyPress;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
@ -125,7 +127,7 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText
* Send the enter key.
*/
private void sendEnter(){
BaseMainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_ENTER);
sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_ENTER);
clear();
}

View file

@ -20,13 +20,14 @@ import net.kdt.pojavlaunch.prefs.LauncherPreferences;
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.prefs.LauncherPreferences.PREF_BUTTONSIZE;
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_RIGHT_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")
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);
for(int keycode : mProperties.keycodes){
if(keycode >= GLFW_KEY_UNKNOWN){
MainActivity.sendKeyPress(keycode, CallbackBridge.getCurrentMods(), isDown);
sendKeyPress(keycode, CallbackBridge.getCurrentMods(), isDown);
CallbackBridge.setModifiers(keycode, isDown);
}else{
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.isJoystickEvent;
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 {
@ -354,7 +356,7 @@ public class Gamepad {
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;
}
}
@ -370,15 +372,15 @@ public class Gamepad {
break;
case LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT:
MainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown);
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown);
break;
case LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT:
MainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, isDown);
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, isDown);
break;
default:
MainActivity.sendKeyPress(keycode, CallbackBridge.getCurrentMods(), isDown);
sendKeyPress(keycode, CallbackBridge.getCurrentMods(), isDown);
break;
}
CallbackBridge.setModifiers(keycode, isDown);

View file

@ -84,6 +84,27 @@ public class CallbackBridge {
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) {
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");

View file

@ -98,13 +98,6 @@
</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>
<com.google.android.material.navigation.NavigationView