Skip to content

Commit

Permalink
Merge pull request #103 from Xtr126/sensitivity-test
Browse files Browse the repository at this point in the history
Non-linear scaling and x,y sensitivity adjustment #90
  • Loading branch information
Xtr126 authored Aug 10, 2024
2 parents b8b19ac + 31b6554 commit 5c874ae
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 50 deletions.
41 changes: 36 additions & 5 deletions app/src/main/java/xtr/keymapper/editor/EditorUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@
import xtr.keymapper.databinding.DpadArrowsBinding;
import xtr.keymapper.databinding.DpadBinding;
import xtr.keymapper.databinding.KeymapEditorBinding;
import xtr.keymapper.databinding.MouseAimConfigBinding;
import xtr.keymapper.databinding.ResizableBinding;
import xtr.keymapper.dpad.Dpad;
import xtr.keymapper.dpad.DpadKeyCodes;
import xtr.keymapper.floatingkeys.MovableFloatingActionKey;
import xtr.keymapper.floatingkeys.MovableFrameLayout;
import xtr.keymapper.keymap.KeymapConfig;
import xtr.keymapper.keymap.KeymapProfile;
import xtr.keymapper.keymap.KeymapProfileKey;
import xtr.keymapper.keymap.KeymapProfiles;
import xtr.keymapper.mouse.MouseAimConfig;
import xtr.keymapper.mouse.MouseAimSettings;
import xtr.keymapper.server.RemoteServiceHelper;
import xtr.keymapper.swipekey.SwipeKey;
import xtr.keymapper.swipekey.SwipeKeyView;
Expand Down Expand Up @@ -322,7 +323,7 @@ private void moveResizeDpad(ViewGroup dpadLayout, Dpad dpad, float x, float y) {
// resize dpad from saved profile configuration
float x1 = dpad.getWidth() - dpadLayout.getLayoutParams().width;
float y1 = dpad.getHeight() - dpadLayout.getLayoutParams().height;
resizeView(dpadLayout, x1, y1);
resizeView(dpadLayout, (int) x1, (int) y1);
}
}

Expand Down Expand Up @@ -353,6 +354,36 @@ public void onClick(View view) {
keyInFocus = key -> ((MovableFloatingActionKey)view).setText(key);
}

public void showMouseAimSettingsDialog() {
KeymapConfig keymapConfig = new KeymapConfig(context);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
MouseAimConfigBinding binding = MouseAimConfigBinding.inflate(layoutInflater, null, false);

// Load settings
binding.rightClickCheckbox.setChecked(keymapConfig.rightClickMouseAim);
binding.graveKeyCheckbox.setChecked(keymapConfig.keyGraveMouseAim);
binding.applyNonLinearScalingCheckbox.setChecked(profile.mouseAimConfig.applyNonLinearScaling);
binding.sliderXSensitivity.setValue(profile.mouseAimConfig.xSensitivity);
binding.sliderYSensitivity.setValue(profile.mouseAimConfig.ySensitivity);

View view = binding.getRoot();
builder.setView(view)
.setPositiveButton(R.string.ok, (dialog, which) -> {
// Save settings
keymapConfig.rightClickMouseAim = binding.rightClickCheckbox.isChecked();
keymapConfig.keyGraveMouseAim = binding.graveKeyCheckbox.isChecked();
keymapConfig.applySharedPrefs();

profile.mouseAimConfig.applyNonLinearScaling = binding.applyNonLinearScalingCheckbox.isChecked();
profile.mouseAimConfig.xSensitivity = binding.sliderXSensitivity.getValue();
profile.mouseAimConfig.ySensitivity = binding.sliderYSensitivity.getValue();
})
.setNegativeButton(R.string.cancel, null);
AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
dialog.show();
}

private void addCrosshair(float x, float y) {
if (crosshair == null) {
CrosshairBinding binding = CrosshairBinding.inflate(layoutInflater, mainView, true);
Expand Down Expand Up @@ -381,7 +412,7 @@ private void addCrosshair(float x, float y) {
if(overlayOpen) dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
dialog.show();
});
binding.editButton.setOnClickListener(v -> MouseAimSettings.getKeyDialog(context).show());
binding.editButton.setOnClickListener(v -> showMouseAimSettingsDialog());
}
crosshair.animate().x(x).y(y)
.setDuration(500)
Expand Down Expand Up @@ -418,7 +449,7 @@ private void addSwipeKey() {
swipeKeyList.add(swipeKeyView);
}

public static void resizeView(View view, float x, float y) {
public static void resizeView(View view, int x, int y) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width += x;
layoutParams.height += y;
Expand Down Expand Up @@ -446,7 +477,7 @@ private void getDefaultPivotXY(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
resizeView(rootView, event.getX(), event.getY());
resizeView(rootView, (int) event.getX(), (int) event.getY());
// Resize View from center point
if (defaultPivotX > 0) {
float newPivotX = rootView.getPivotX() - defaultPivotX;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private void getDefaultPivotXY() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
// Resize View in fixed ratio
float newSize = (event.getX() + event.getY()) / 2;
int newSize = ((int)event.getX() + (int)event.getY()) / 2;
EditorUI.resizeView(rootView, newSize, newSize);

// Resize View from center point
Expand Down
17 changes: 16 additions & 1 deletion app/src/main/java/xtr/keymapper/mouse/MouseAimConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class MouseAimConfig implements Parcelable {
public boolean limitedBounds = true;
private static final int initXY = 300;
public static final String TAG = "MOUSE_AIM";
public float xSensitivity = 1, ySensitivity = 1;
public boolean applyNonLinearScaling = false;

public MouseAimConfig() {
xCenter = xleftClick = yleftClick = yCenter = initXY;
Expand All @@ -27,6 +29,9 @@ protected MouseAimConfig(Parcel in) {
width = in.readFloat();
height = in.readFloat();
limitedBounds = in.readByte() != 0;
xSensitivity = in.readFloat();
ySensitivity = in.readFloat();
applyNonLinearScaling = in.readByte() != 0;
}

public static final Creator<MouseAimConfig> CREATOR = new Creator<>() {
Expand All @@ -49,14 +54,21 @@ public MouseAimConfig parse(String[] data){
height = Float.parseFloat(data[5]);
xleftClick = Float.parseFloat(data[6]);
yleftClick = Float.parseFloat(data[7]);
if (data.length == 11) {
xSensitivity = Float.parseFloat(data[8]);
ySensitivity = Float.parseFloat(data[9]);
applyNonLinearScaling = Integer.parseInt(data[10]) != 0;
}
return this;
}

public String getData() {
return TAG + " " + xCenter + " " + yCenter + " "
+ (limitedBounds ? 1 : 0) + " "
+ width + " " + height + " "
+ xleftClick + " " + yleftClick;
+ xleftClick + " " + yleftClick + " "
+ xSensitivity + " " + ySensitivity + " "
+ (limitedBounds ? 1 : 0);
}

public void setCenterXY(MovableFrameLayout crosshair){
Expand All @@ -83,5 +95,8 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeFloat(width);
dest.writeFloat(height);
dest.writeByte((byte) (limitedBounds ? 1 : 0));
dest.writeFloat(xSensitivity);
dest.writeFloat(ySensitivity);
dest.writeByte((byte) (applyNonLinearScaling ? 1 : 0));
}
}
34 changes: 27 additions & 7 deletions app/src/main/java/xtr/keymapper/mouse/MouseAimHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@ public class MouseAimHandler {
private final int pointerIdMouse = PointerId.pid1.id;
private final int pointerIdAim = PointerId.pid2.id;
private final Handler mHandler;
private final boolean limitedBounds;


public MouseAimHandler(MouseAimConfig config){
currentX = config.xCenter;
currentY = config.yCenter;
this.config = config;
this.limitedBounds = config.limitedBounds;
mHandler = new Handler(Looper.getMainLooper());
}

Expand All @@ -55,6 +52,7 @@ public void setDimensions(int width, int height){
area.top = currentY - config.height;
area.bottom = currentY + config.height;
}

}

public void resetPointer() {
Expand All @@ -70,14 +68,14 @@ public void resetPointer() {
public void handleEvent(int code, int value, OnButtonClickListener listener) {
switch (code) {
case REL_X:
currentX += value;
if (limitedBounds && (currentX > area.right || currentX < area.left))
currentX += (float) calculateScaledX(value);
if (config.limitedBounds && (currentX > area.right || currentX < area.left))
resetPointer();
service.injectEvent(currentX, currentY, MOVE, pointerIdAim);
break;
case REL_Y:
currentY += value;
if (limitedBounds && (currentY > area.bottom || currentY < area.top))
currentY += calculateScaledY(value);
if (config.limitedBounds && (currentY > area.bottom || currentY < area.top))
resetPointer();
service.injectEvent(currentX, currentY, MOVE, pointerIdAim);
break;
Expand All @@ -95,6 +93,28 @@ public void handleEvent(int code, int value, OnButtonClickListener listener) {
}
}

public double calculateScaledX(int value) {
if (config.applyNonLinearScaling) {
double dx = Math.abs(config.xCenter - currentX);
double dy = Math.abs(config.yCenter - currentY);
double distance = Math.hypot(dx, dy);

double maxWidth = area.right - area.left;
double minDistanceToApplyScaling = maxWidth / 20;
if (distance > minDistanceToApplyScaling) {
return config.xSensitivity * value * Math.sqrt(minDistanceToApplyScaling / distance);
} else {
return 1;
}
} else {
return 1;
}
}

private float calculateScaledY(int value) {
return value * config.ySensitivity;
}

public void stop() {
service.injectEvent(currentX, currentY, UP, pointerIdAim);
}
Expand Down
36 changes: 0 additions & 36 deletions app/src/main/java/xtr/keymapper/mouse/MouseAimSettings.java

This file was deleted.

58 changes: 58 additions & 0 deletions app/src/main/res/layout/mouse_aim_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/activate_with" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<com.google.android.material.checkbox.MaterialCheckBox
android:id="@+id/grave_key_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/grave_key" />
<com.google.android.material.checkbox.MaterialCheckBox
android:id="@+id/right_click_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/right_click" />

</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/x_sensitivity" />
<com.google.android.material.slider.Slider
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:valueFrom="0.1"
android:valueTo="5"
android:id="@+id/slider_x_sensitivity"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/y_sensitivity" />
<com.google.android.material.slider.Slider
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:valueFrom="0.1"
android:valueTo="5"
android:id="@+id/slider_y_sensitivity"/>

<com.google.android.material.checkbox.MaterialCheckBox
android:id="@+id/apply_non_linear_scaling_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/apply_non_linear_scaling" />
</LinearLayout>
5 changes: 5 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,9 @@

<string name="shizuku_not_authorized_title">Shizuku not authorized</string>
<string name="shizuku_not_authorized_message">Authorize XtMapper from Shizuku manager app to continue</string>
<string name="activate_with">Activate with</string>
<string name="grave_key">~ key</string>
<string name="x_sensitivity">X-Sensitivity</string>
<string name="y_sensitivity">Y-Sensitivity</string>
<string name="apply_non_linear_scaling">Apply non linear scaling</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@

<string name="shizuku_not_authorized_title">Shizuku not authorized</string>
<string name="shizuku_not_authorized_message">Authorize XtMapper from Shizuku manager app to continue</string>
<string name="activate_with">Activate with</string>
<string name="grave_key">~ key</string>
<string name="x_sensitivity">X-Sensitivity</string>
<string name="y_sensitivity">Y-Sensitivity</string>
<string name="apply_non_linear_scaling">Apply non linear scaling</string>
</resources>

0 comments on commit 5c874ae

Please sign in to comment.