[Input pipe] Use EditText to handle input events, with support input unicodes

This commit is contained in:
khanhduytran0
2020-11-27 06:22:20 +07:00
parent cc47a72bc2
commit ae50740ff4
10 changed files with 251 additions and 712 deletions

View File

@@ -172,16 +172,18 @@ public class ActionPopupWindow extends PinnedPopupWindow implements OnClickListe
errorAt = null;
*/
int errorAt = 0;
try {
properties.insertDynamicPos(editDynamicX.getText().toString());
errorAt = 1;
properties.insertDynamicPos(editDynamicY.getText().toString());
} catch (Throwable th) {
(errorAt == 0 ? editDynamicX : editDynamicY)
.setError(th.getMessage());
return;
if (properties.isDynamicBtn) {
int errorAt = 0;
try {
properties.insertDynamicPos(editDynamicX.getText().toString());
errorAt = 1;
properties.insertDynamicPos(editDynamicY.getText().toString());
} catch (Throwable th) {
(errorAt == 0 ? editDynamicX : editDynamicY)
.setError(th.getMessage());
return;
}
}
if (spinnerKeycode.getSelectedItemPosition() < specialArr.length) {

View File

@@ -168,14 +168,14 @@ public class AndroidLWJGLKeycode {
return androidKeyNameArray;
}
public static void execKey(BaseMainActivity mainActivity, KeyEvent keyEvent, int i, boolean isDown) {
public static void execKey(KeyEvent keyEvent, int i, boolean isDown) {
for (Map.Entry<Integer, Integer> perKey : androidToLwjglMap.entrySet()) {
if (i == 1 && (keyEvent.getSource() == InputDevice.SOURCE_MOUSE)) {
// Right mouse detection
mainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, true);
mainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, false);
BaseMainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown);
// BaseMainActivity.sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, false);
} else if (perKey.getKey() == i) {
mainActivity.sendKeyPress(perKey.getValue(), keyEvent.getModifiers(), isDown);
BaseMainActivity.sendKeyPress(perKey.getValue(), keyEvent.getModifiers(), isDown);
}
}
@@ -196,14 +196,14 @@ public class AndroidLWJGLKeycode {
try {
if ((int) keyEvent.getDisplayLabel() != KeyEvent.KEYCODE_UNKNOWN && !CallbackBridge.isGrabbing()) {
mainActivity.sendKeyPress(androidToLwjglMap.get(keyEvent.getKeyCode()), (char) keyEvent.getUnicodeChar(), keyEvent.getScanCode(), mods, isDown);
BaseMainActivity.sendKeyPress(androidToLwjglMap.get(keyEvent.getKeyCode()), (char) keyEvent.getUnicodeChar(), keyEvent.getScanCode(), mods, isDown);
}
} catch (Throwable th) {
th.printStackTrace();
}
if (isBackspaceAfterChar && (int) keyEvent.getDisplayLabel() != KeyEvent.KEYCODE_UNKNOWN && !CallbackBridge.isGrabbing() && i != KeyEvent.KEYCODE_DEL) {
mainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_BACKSPACE, 0, isDown);
BaseMainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_BACKSPACE, 0, isDown);
}
}

View File

@@ -73,6 +73,8 @@ public class BaseMainActivity extends LoggableActivity {
private DrawerLayout drawerLayout;
private NavigationView navDrawer;
private CapturedEditText mKeyHandlerView;
private LinearLayout contentLog;
private TextView textLog;
@@ -196,6 +198,11 @@ public class BaseMainActivity extends LoggableActivity {
// toggleGui(null);
this.drawerLayout.closeDrawers();
mKeyHandlerView = findViewById(R.id.main_key_handler);
mKeyHandlerView.setFocusable(true);
mKeyHandlerView.setFocusableInTouchMode(true);
mKeyHandlerView.requestFocus();
AndroidLWJGLKeycode.isBackspaceAfterChar = true; // mVersionInfo.minimumLauncherVersion >= 18;
placeMouseAt(CallbackBridge.windowWidth / 2, CallbackBridge.windowHeight / 2);
@@ -712,50 +719,6 @@ public class BaseMainActivity extends LoggableActivity {
e.printStackTrace();
Tools.showError(this, e, true);
}
// Mirror video of OpenGL view.
/*
new Thread(new Runnable(){
@Override
public void run()
{
try {
while (true) {
if (bit == null) continue;
runOnUiThread(new Runnable(){
@Override
public void run()
{
fillCanvasGL();
mirrorView.setImageBitmap(bit);
}
});
// ~33fps render
Thread.sleep(30);
}
} catch (Throwable th) {
th.printStackTrace();
}
}
}).start();
*/
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
AndroidLWJGLKeycode.execKey(this, event, keyCode, false);
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
AndroidLWJGLKeycode.execKey(this, event, keyCode, true);
return super.onKeyDown(keyCode, event);
}
//private Dialog menuDial;
@@ -1042,8 +1005,10 @@ public class BaseMainActivity extends LoggableActivity {
public void onBackPressed() {
// Prevent back
// Catch back as Esc keycode at another place
}
// sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_ESCAPE);
}
public void hideKeyboard() {
try {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
@@ -1086,8 +1051,8 @@ public class BaseMainActivity extends LoggableActivity {
sendKeyPress(keyCode, 0, false);
}
private boolean isLeftMouseDown, isRightMouseDown;
public void sendMouseButton(int button, boolean status) {
private static boolean isLeftMouseDown, isRightMouseDown;
public static void sendMouseButton(int button, boolean status) {
// TODO implement this method!!!
// CallbackBridge.setMouseButtonInGrabMode((byte) button, status ? (byte) 1 : (byte) 0);
// or

View File

@@ -0,0 +1,29 @@
package net.kdt.pojavlaunch;
import android.content.*;
import android.util.*;
import android.widget.*;
import android.view.*;
public class CapturedEditText extends EditText
{
public CapturedEditText(Context ctx) {
this(ctx, null);
}
public CapturedEditText(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
AndroidLWJGLKeycode.execKey(event, keyCode, true);
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
AndroidLWJGLKeycode.execKey(event, keyCode, false);
return true;
}
}

View File

@@ -1,113 +0,0 @@
package net.kdt.pojavlaunch;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import net.kdt.pojavlaunch.customcontrols.*;
import net.kdt.pojavlaunch.prefs.*;
import org.lwjgl.glfw.*;
import java.io.*;
import com.google.gson.*;
public class CustomCtrlMainActivity extends BaseMainActivity {
private ControlLayout mControlLayout;
private View.OnClickListener mClickListener;
private View.OnTouchListener mTouchListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initLayout(R.layout.main_with_customctrl);
mClickListener = new View.OnClickListener(){
@Override
public void onClick(View view) {
if (view instanceof ControlButton) {
ControlButton button = (ControlButton) view;
switch (button.getProperties().keycode) {
case ControlData.SPECIALBTN_KEYBOARD:
showKeyboard();
break;
case ControlData.SPECIALBTN_TOGGLECTRL:
mControlLayout.toggleControlVisible();
break;
case ControlData.SPECIALBTN_VIRTUALMOUSE:
toggleMouse(button);
break;
}
}
}
};
mTouchListener = new View.OnTouchListener(){
@Override
public boolean onTouch(View view, MotionEvent e) {
boolean isDown;
switch (e.getActionMasked()) {
case MotionEvent.ACTION_DOWN: // 0
case MotionEvent.ACTION_POINTER_DOWN: // 5
isDown = true;
break;
case MotionEvent.ACTION_UP: // 1
case MotionEvent.ACTION_CANCEL: // 3
case MotionEvent.ACTION_POINTER_UP: // 6
isDown = false;
break;
default:
return false;
}
if (view instanceof ControlButton) {
ControlButton button = (ControlButton) view;
switch (button.getProperties().keycode) {
case ControlData.SPECIALBTN_MOUSEPRI:
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, isDown);
break;
case ControlData.SPECIALBTN_MOUSEMID:
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_MIDDLE, isDown);
break;
case ControlData.SPECIALBTN_MOUSESEC:
if (CallbackBridge.isGrabbing()) {
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown);
} else {
CallbackBridge.putMouseEventWithCoords(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown ? 1 : 0, CallbackBridge.mouseX, CallbackBridge.mouseY);
setRightOverride(isDown);
}
break;
}
}
return false;
}
};
ControlData[] specialButtons = ControlData.getSpecialButtons();
specialButtons[0].specialButtonListener
= specialButtons[1].specialButtonListener
= specialButtons[4].specialButtonListener
= mClickListener;
specialButtons[2].specialButtonListener
= specialButtons[3].specialButtonListener
= specialButtons[5].specialButtonListener
= mTouchListener;
mControlLayout = findViewById(R.id.main_control_layout);
mControlLayout.setModifiable(false);
try {
mControlLayout.loadLayout(LauncherPreferences.PREF_DEFAULTCTRL_PATH);
} catch (Throwable th) {
Tools.showError(this, th);
}
// toggleGui(null);
mControlLayout.toggleControlVisible();
}
}

View File

@@ -7,131 +7,107 @@ import android.widget.*;
import net.kdt.pojavlaunch.customcontrols.*;
import net.kdt.pojavlaunch.prefs.*;
import org.lwjgl.glfw.*;
import java.io.*;
import com.google.gson.*;
public class MainActivity extends BaseMainActivity implements OnClickListener, OnTouchListener {
private Button upButton,
downButton, leftButton,
rightButton, jumpButton,
primaryButton, secondaryButton,
debugButton, shiftButton,
keyboardButton, inventoryButton,
talkButton, thirdPersonButton,
zoomButton, listPlayersButton,
toggleControlButton;
private Button[] controlButtons;
public class MainActivity extends BaseMainActivity {
private ControlLayout mControlLayout;
private View.OnClickListener mClickListener;
private View.OnTouchListener mTouchListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initLayout(R.layout.main);
this.upButton = findButton(R.id.control_up);
this.downButton = findButton(R.id.control_down);
this.leftButton = findButton(R.id.control_left);
this.rightButton = findButton(R.id.control_right);
this.jumpButton = findButton(R.id.control_jump);
this.primaryButton = findButton(R.id.control_primary);
this.secondaryButton = findButton(R.id.control_secondary);
this.debugButton = findButton(R.id.control_debug);
this.shiftButton = findButton(R.id.control_shift);
this.keyboardButton = findButton(R.id.control_keyboard);
this.inventoryButton = findButton(R.id.control_inventory);
this.talkButton = findButton(R.id.control_talk);
this.thirdPersonButton = findButton(R.id.control_thirdperson);
this.zoomButton = findButton(R.id.control_zoom);
this.listPlayersButton = findButton(R.id.control_listplayers);
this.toggleControlButton = findButton(R.id.control_togglecontrol);
this.controlButtons = new Button[]{
upButton, downButton, leftButton, rightButton,
jumpButton, primaryButton, secondaryButton,
debugButton, shiftButton, keyboardButton,
inventoryButton, talkButton, thirdPersonButton,
listPlayersButton
initLayout(R.layout.main_with_customctrl);
mClickListener = new View.OnClickListener(){
@Override
public void onClick(View view) {
if (view instanceof ControlButton) {
ControlButton button = (ControlButton) view;
switch (button.getProperties().keycode) {
case ControlData.SPECIALBTN_KEYBOARD:
showKeyboard();
break;
case ControlData.SPECIALBTN_TOGGLECTRL:
mControlLayout.toggleControlVisible();
break;
case ControlData.SPECIALBTN_VIRTUALMOUSE:
toggleMouse(button);
break;
}
}
}
};
this.toggleControlButton.setOnClickListener(this);
this.zoomButton.setVisibility(mVersionInfo.optifineLib == null ? View.GONE : View.VISIBLE);
mTouchListener = new View.OnTouchListener(){
@Override
public boolean onTouch(View view, MotionEvent e) {
boolean isDown;
switch (e.getActionMasked()) {
case MotionEvent.ACTION_DOWN: // 0
case MotionEvent.ACTION_POINTER_DOWN: // 5
isDown = true;
break;
case MotionEvent.ACTION_UP: // 1
case MotionEvent.ACTION_CANCEL: // 3
case MotionEvent.ACTION_POINTER_UP: // 6
isDown = false;
break;
default:
return false;
}
if (view instanceof ControlButton) {
ControlButton button = (ControlButton) view;
switch (button.getProperties().keycode) {
case ControlData.SPECIALBTN_MOUSEPRI:
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, isDown);
break;
case ControlData.SPECIALBTN_MOUSEMID:
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_MIDDLE, isDown);
break;
case ControlData.SPECIALBTN_MOUSESEC:
if (CallbackBridge.isGrabbing()) {
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown);
} else {
CallbackBridge.putMouseEventWithCoords(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown ? 1 : 0, CallbackBridge.mouseX, CallbackBridge.mouseY);
setRightOverride(isDown);
}
break;
}
}
return false;
}
};
ControlData[] specialButtons = ControlData.getSpecialButtons();
specialButtons[0].specialButtonListener
= specialButtons[1].specialButtonListener
= specialButtons[4].specialButtonListener
= mClickListener;
specialButtons[2].specialButtonListener
= specialButtons[3].specialButtonListener
= specialButtons[5].specialButtonListener
= mTouchListener;
mControlLayout = findViewById(R.id.main_control_layout);
mControlLayout.setModifiable(false);
try {
mControlLayout.loadLayout(LauncherPreferences.PREF_DEFAULTCTRL_PATH);
} catch (Throwable th) {
Tools.showError(this, th);
}
// toggleGui(null);
onClick(toggleControlButton);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.control_togglecontrol: {
/*
switch(overlayView.getVisibility()){
case View.VISIBLE: overlayView.setVisibility(View.GONE);
break;
case View.GONE: overlayView.setVisibility(View.VISIBLE);
}
*/
for (Button button : controlButtons) {
button.setVisibility(button.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}
zoomButton.setVisibility((zoomButton.getVisibility() == View.GONE && mVersionInfo.optifineLib != null) ? View.VISIBLE : View.GONE);
}
}
}
public boolean onTouch(View v, MotionEvent e) {
boolean isDown;
switch (e.getActionMasked()) {
case MotionEvent.ACTION_DOWN: // 0
case MotionEvent.ACTION_POINTER_DOWN: // 5
isDown = true;
break;
case MotionEvent.ACTION_UP: // 1
case MotionEvent.ACTION_CANCEL: // 3
case MotionEvent.ACTION_POINTER_UP: // 6
isDown = false;
break;
default:
return false;
}
switch (v.getId()) {
case R.id.control_up: sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_W, 0, isDown); break;
case R.id.control_left: sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_A, 0, isDown); break;
case R.id.control_down: sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_S, 0, isDown); break;
case R.id.control_right: sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_D, 0, isDown); break;
case R.id.control_jump: sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_SPACE, 0, isDown); break;
case R.id.control_primary: sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT, isDown); break;
case R.id.control_secondary:
if (CallbackBridge.isGrabbing()) {
sendMouseButton(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown);
} else {
/*
if (!isDown) {
CallbackBridge.putMouseEventWithCoords(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, CallbackBridge.mouseX, CallbackBridge.mouseY);
}
*/
CallbackBridge.putMouseEventWithCoords(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown ? 1 : 0, CallbackBridge.mouseX, CallbackBridge.mouseY);
setRightOverride(isDown);
} break;
case R.id.control_debug: sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_F3, 0, isDown); break;
case R.id.control_shift: sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_LEFT_SHIFT, 0, isDown); break;
case R.id.control_inventory: sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_E, 0, isDown); break;
case R.id.control_talk: sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_T, 0, isDown); break;
case R.id.control_keyboard: showKeyboard(); break;
case R.id.control_thirdperson: sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_F5, 0, isDown); break;
case R.id.control_zoom: sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_C, 0, isDown); break;
case R.id.control_listplayers: sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_TAB, 0, isDown); break;
}
return false;
}
private Button findButton(int id) {
Button button = (Button) findViewById(id);
button.setOnTouchListener(this);
button.setFocusable(false);
button.setFocusableInTouchMode(false);
return button;
mControlLayout.toggleControlVisible();
}
}

View File

@@ -229,7 +229,7 @@ public class MinecraftDownloaderTask extends AsyncTask<String, String, Throwable
jvmArgs.add("-Xms128M");
jvmArgs.add("-Xmx1G");
*/
Intent mainIntent = new Intent(mActivity, CustomCtrlMainActivity.class /* MainActivity.class */);
Intent mainIntent = new Intent(mActivity, MainActivity.class /* MainActivity.class */);
// mainIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);