Mouse speed slider

This commit is contained in:
downthecrop 2022-01-02 09:25:16 -08:00
parent 4979167651
commit 0e299636c5
6 changed files with 86 additions and 285 deletions

View file

@ -39,14 +39,6 @@ public class BaseMainActivity extends BaseActivity {
decorView.setSystemUiVisibility(uiOptions);
}
@Override
protected void onPause() {
if (CallbackBridge.isGrabbing()){
sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_ESCAPE);
}
super.onPause();
}
public static void fullyExit() {
android.os.Process.killProcess(android.os.Process.myPid());
}

View file

@ -21,6 +21,7 @@ import net.kdt.pojavlaunch.prefs.*;
import net.kdt.pojavlaunch.utils.*;
import org.lwjgl.glfw.*;
import static net.kdt.pojavlaunch.Tools.currentDisplayMetrics;
import static net.kdt.pojavlaunch.Tools.getFileName;
import static net.kdt.pojavlaunch.utils.MathUtils.map;
@ -34,7 +35,6 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTou
private int totalMovement;
String specialChars = "/*!@#$%^&*()\"{}_[+:;=-_]'|\\?/<>,.";
private LoggerView loggerView;
private boolean mouseState = false;
private LinearLayout touchPad;
@ -42,10 +42,8 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTou
private GestureDetector gestureDetector;
private long lastPress = 0;
ScaleGestureDetector scaleGestureDetector;
GestureDetector longTapGestureDetector;
private long touchStart = 0;
boolean longPressTriggered = false;
boolean longPressShouldClick = false;
private boolean rcState = false;
@ -54,10 +52,6 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTou
private float scaleFactor;
public float[] scaleFactors = initScaleFactors();
private final int fingerStillThreshold = 8;
private int initialX;
private int initialY;
public class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
@ -66,7 +60,7 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTou
if (scaleFactor > 1) { //Send F4 To Zoom Out
AWTInputBridge.sendKey((char)115,115);
} else { //116 F5 To Zoom In
AWTInputBridge.sendKey((char) 116, 116);
AWTInputBridge.sendKey((char)116, 116);
}
return true;
}
@ -209,8 +203,9 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTou
case MotionEvent.ACTION_POINTER_UP: // 6
break;
case MotionEvent.ACTION_MOVE: // 2
mouseX = Math.max(0, Math.min(CallbackBridge.physicalWidth, mouseX + x - prevX));
mouseY = Math.max(0, Math.min(CallbackBridge.physicalHeight, mouseY + y - prevY));
System.out.println("DEBUG: MOUSESPEED"+LauncherPreferences.PREF_MOUSESPEED);
mouseX = Math.max(0, Math.min(currentDisplayMetrics.widthPixels, mouseX + (x - prevX) * LauncherPreferences.PREF_MOUSESPEED));
mouseY = Math.max(0, Math.min(currentDisplayMetrics.heightPixels, mouseY + (y - prevY) * LauncherPreferences.PREF_MOUSESPEED));
placeMouseAt(mouseX, mouseY);
sendScaledMousePosition(mouseX,mouseY);
break;

View file

@ -1,5 +1,6 @@
package net.kdt.pojavlaunch;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
@ -15,12 +16,17 @@ import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;
import com.google.android.material.switchmaterial.SwitchMaterial;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@ -49,33 +55,32 @@ public class SettingsMenu extends Activity {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
File config = new File(getFilesDir(), "config.json");
try {
Log.d("TAG", "Starting copy: " + uri.getPath());
InputStream inputStream = getContentResolver().openInputStream(uri);
FileOutputStream fileOutputStream = new FileOutputStream(config);
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0) {
fileOutputStream.write(buf,0,len);
}
fileOutputStream.close();
inputStream.close();
Toast.makeText(this, "Config loaded. Please restart the app.",
Toast.LENGTH_SHORT).show();
} catch (IOException e1) {
Log.d("error", "Error with file " + e1);
if (requestCode == FILE_SELECT_CODE) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
File config = new File(getFilesDir(), "config.json");
try {
Log.d("TAG", "Starting copy: " + uri.getPath());
InputStream inputStream = getContentResolver().openInputStream(uri);
FileOutputStream fileOutputStream = new FileOutputStream(config);
byte buf[] = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
fileOutputStream.write(buf, 0, len);
}
fileOutputStream.close();
inputStream.close();
Toast.makeText(this, "Config loaded. Please restart the app.",
Toast.LENGTH_SHORT).show();
} catch (IOException e1) {
Log.d("error", "Error with file " + e1);
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -88,26 +93,18 @@ public class SettingsMenu extends Activity {
final EditText username = findViewById(R.id.username);
final EditText password = findViewById(R.id.password);
final Switch righthanded = findViewById(R.id.righthanded);
final SwitchMaterial righthanded = findViewById(R.id.righthanded);
final SeekBar mouseSpeed = findViewById(R.id.mouseslider);
final Button loadConfig = findViewById(R.id.loadconfig);
loadConfig.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("Pressed","Button");
showFileChooser();
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
// RELEASED
return true; // if you want to handle the touch event
}
return false;
loadConfig.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
showFileChooser();
return true; // if you want to handle the touch event
}
return false;
});
//For storing string value in sharedPreference
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
@ -115,8 +112,8 @@ public class SettingsMenu extends Activity {
// Restore from saved values
username.setText(preferences.getString("username",""));
password.setText(preferences.getString("password",""));
righthanded.setChecked(Boolean.parseBoolean(preferences.getString("righthanded","")));
mouseSpeed.setProgress((int)LauncherPreferences.PREF_MOUSESPEED);
righthanded.setChecked(Boolean.parseBoolean(preferences.getString("righthanded","false")));
username.setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
@ -137,10 +134,25 @@ public class SettingsMenu extends Activity {
return false;
});
mouseSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
editor.putInt("mousespeed",mouseSpeed.getProgress());
editor.commit();
LauncherPreferences.PREF_MOUSESPEED = mouseSpeed.getProgress();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
righthanded.setOnCheckedChangeListener((buttonView, isChecked) -> {
// do something, the isChecked will be
// true if the switch is in the On position
System.out.println(isChecked);
editor.putString("righthanded",""+isChecked);
editor.commit();
Toast.makeText(this, "Relaunch required to update GUI.",

View file

@ -1,194 +0,0 @@
package net.kdt.pojavlaunch;
import static net.kdt.pojavlaunch.Tools.currentDisplayMetrics;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.DEFAULT_PREF;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import org.lwjgl.glfw.CallbackBridge;
/**
* Class dealing with the virtual mouse
*/
public class Touchpad extends FrameLayout {
private static final int FINGER_SCROLL_THRESHOLD = 10;
/* Whether the Touchpad should be displayed */
private boolean displayState;
/* Mouse pointer icon used by the touchpad */
private final ImageView mousePointer = new ImageView(getContext());
/* Detect a classic android Tap */
private final GestureDetector singleTapDetector = new GestureDetector(getContext(), new SingleTapConfirm());
/* Mc mouse position, scaled by the scaleFactor */
float mouse_x, mouse_y;
/* Resolution scaler option, allow downsizing a window */
private final float scaleFactor = DEFAULT_PREF.getInt("resolutionRatio",100)/100f;
/* Current pointer ID to move the mouse */
private int currentPointerID = -1000;
/* Previous MotionEvent position, not scale */
private float prevX, prevY;
/* Last first pointer positions non-scaled, used to scroll distance */
private float scrollLastInitialX, scrollLastInitialY;
public Touchpad(@NonNull Context context) {
this(context, null);
}
public Touchpad(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
// Setup mouse pointer
mousePointer.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.mouse_pointer, getContext().getTheme()));
mousePointer.post(() -> {
ViewGroup.LayoutParams params = mousePointer.getLayoutParams();
params.width = (int) (36 / 100f * LauncherPreferences.PREF_MOUSESCALE);
params.height = (int) (54 / 100f * LauncherPreferences.PREF_MOUSESCALE);
});
addView(mousePointer);
setFocusable(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setDefaultFocusHighlightEnabled(false);
}
// When the game is grabbing, we should not display the mouse
disable();
displayState = false;
Thread virtualMouseGrabThread = new Thread(() -> {
while (true) {
if (!CallbackBridge.isGrabbing() && displayState && getVisibility() != VISIBLE) {
post(this::enable);
}else{
if ((CallbackBridge.isGrabbing() && getVisibility() != View.GONE) || !displayState && getVisibility() == VISIBLE) {
post(this::disable);
}
}
}
}, "VirtualMouseGrabThread");
virtualMouseGrabThread.setPriority(Thread.MIN_PRIORITY);
virtualMouseGrabThread.start();
}
/** Enable the touchpad */
public void enable(){
setVisibility(VISIBLE);
placeMouseAt(currentDisplayMetrics.widthPixels / 2, currentDisplayMetrics.heightPixels / 2);
}
/** Disable the touchpad and hides the mouse */
public void disable(){
setVisibility(GONE);
}
/** @return The new state, enabled or disabled */
public boolean switchState(){
displayState = !displayState;
return displayState;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// MotionEvent reports input details from the touch screen
// and other input controls. In this case, you are only
// interested in events where the touch position changed.
// int index = event.getActionIndex();
if(CallbackBridge.isGrabbing()) {
disable();
return false;
}
int action = event.getActionMasked();
float x = event.getX();
float y = event.getY();
float mouseX = mousePointer.getX();
float mouseY = mousePointer.getY();
if (singleTapDetector.onTouchEvent(event)) {
mouse_x = (mouseX * scaleFactor);
mouse_y = (mouseY * scaleFactor);
CallbackBridge.sendCursorPos(mouse_x, mouse_y);
CallbackBridge.sendMouseKeycode(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT);
return true;
}
switch (action) {
case MotionEvent.ACTION_POINTER_DOWN: // 5
scrollLastInitialX = event.getX();
scrollLastInitialY = event.getY();
break;
case MotionEvent.ACTION_DOWN:
prevX = x;
prevY = y;
currentPointerID = event.getPointerId(0);
break;
case MotionEvent.ACTION_MOVE: // 2
//Scrolling feature
if (!LauncherPreferences.PREF_DISABLE_GESTURES && !CallbackBridge.isGrabbing() && event.getPointerCount() >= 2) {
int hScroll = ((int) (event.getX() - scrollLastInitialX)) / FINGER_SCROLL_THRESHOLD;
int vScroll = ((int) (event.getY() - scrollLastInitialY)) / FINGER_SCROLL_THRESHOLD;
if(vScroll != 0 || hScroll != 0){
CallbackBridge.sendScroll(hScroll, vScroll);
scrollLastInitialX = event.getX();
scrollLastInitialY = event.getY();
}
break;
}
// Mouse movement
if(currentPointerID == event.getPointerId(0)) {
mouseX = Math.max(0, Math.min(currentDisplayMetrics.widthPixels, mouseX + (x - prevX) * LauncherPreferences.PREF_MOUSESPEED));
mouseY = Math.max(0, Math.min(currentDisplayMetrics.heightPixels, mouseY + (y - prevY) * LauncherPreferences.PREF_MOUSESPEED));
placeMouseAt(mouseX, mouseY);
CallbackBridge.sendCursorPos(mouse_x, mouse_y);
}else currentPointerID = event.getPointerId(0);
prevX = x;
prevY = y;
break;
case MotionEvent.ACTION_UP:
prevX = x;
prevY = y;
currentPointerID = -1000;
break;
}
//debugText.setText(CallbackBridge.DEBUG_STRING.toString());
CallbackBridge.DEBUG_STRING.setLength(0);
return true;
}
public void placeMouseAt(float x, float y) {
mousePointer.setX(x);
mousePointer.setY(y);
mouse_x = (x * scaleFactor);
mouse_y = (y * scaleFactor);
CallbackBridge.sendCursorPos(mouse_x, mouse_y);
}
}

View file

@ -44,7 +44,7 @@ public class LauncherPreferences
PREF_BUTTONSIZE = DEFAULT_PREF.getInt("buttonscale", 100);
PREF_MOUSESCALE = DEFAULT_PREF.getInt("mousescale", 100);
PREF_MOUSESPEED = ((float)DEFAULT_PREF.getInt("mousespeed",100))/100f;
PREF_MOUSESPEED = ((float)DEFAULT_PREF.getInt("mousespeed",1));
PREF_HIDE_SIDEBAR = DEFAULT_PREF.getBoolean("hideSidebar", false);
PREF_IGNORE_NOTCH = DEFAULT_PREF.getBoolean("ignoreNotch", false);
PREF_VERTYPE_RELEASE = DEFAULT_PREF.getBoolean("vertype_release", true);
@ -65,33 +65,6 @@ public class LauncherPreferences
PREF_SUSTAINED_PERFORMANCE = DEFAULT_PREF.getBoolean("sustainedPerformance", false);
PREF_GLES_SHRINK_HACK = DEFAULT_PREF.getString("gl4es_shrink_hack", "0");
/*
if (PREF_CUSTOM_JAVA_ARGS.isEmpty()) {
String DEFAULT_JAVA_ARGS = "";
"-Xms" + (androidHeap > 800 ? 800 : androidHeap) + "m " +
// (32bit) More than 800mb may make JVM not allocateable and crash
"-Xmx" + (doubleAndroidHeap > 800 ? 800 : doubleAndroidHeap) + "m" +
"-XX:+UseG1GC " +
"-XX:+ParallelRefProcEnabled " +
"-XX:MaxGCPauseMillis=200 " +
"-XX:+UnlockExperimentalVMOptions " +
"-XX:+AlwaysPreTouch " +
"-XX:G1NewSizePercent=30 " +
"-XX:G1MaxNewSizePercent=40 " +
"-XX:G1HeapRegionSize=8M " +
"-XX:G1ReservePercent=20 " +
"-XX:G1HeapWastePercent=5 " +
"-XX:G1MixedGCCountTarget=4 " +
"-XX:InitiatingHeapOccupancyPercent=15 " +
"-XX:G1MixedGCLiveThresholdPercent=90 " +
"-XX:G1RSetUpdatingPauseTimePercent=5 " +
"-XX:SurvivorRatio=32 " +
"-XX:+PerfDisableSharedMem " +
"-XX:MaxTenuringThreshold=1";
PREF_CUSTOM_JAVA_ARGS = DEFAULT_JAVA_ARGS;
DEFAULT_PREF.edit().putString("javaArgs", DEFAULT_JAVA_ARGS).commit();
}
*/
if (PREF_RENDERER.equals("2") || PREF_RENDERER.equals("3")) {
PREF_RENDERER = "opengles" + PREF_RENDERER;

View file

@ -45,16 +45,39 @@
app:layout_constraintTop_toBottomOf="@+id/username"
tools:layout_editor_absoluteX="-1dp" />
<Switch
<TextView
android:id="@+id/mousespeed_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="Mouse Speed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password" />
<SeekBar
android:id="@+id/mouseslider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:max="10"
android:min="1"
android:layout_marginEnd="16dp"
android:layout_marginTop="35dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mousespeed_text" />
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/righthanded"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="35dp"
android:layout_marginTop="16dp"
android:text="Right Handed Mode"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password" />
app:layout_constraintTop_toBottomOf="@+id/mouseslider" />
<Button
android:id="@+id/loadconfig"