Refactor(gyro): pre-compute the factor one quick setting change

This commit is contained in:
Mathias-Boulay
2024-11-19 18:51:42 +01:00
parent be69154471
commit 85e98a98bf
3 changed files with 18 additions and 10 deletions

View File

@@ -222,6 +222,7 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe
@Override
public void onGyroStateChanged() {
mGyroControl.updateOrientation();
if (PREF_ENABLE_GYRO) {
mGyroControl.enable();
} else {

View File

@@ -87,11 +87,6 @@ public class GyroControl implements SensorEventListener, GrabListener {
SensorManager.getRotationMatrixFromVector(mCurrentRotation, sensorEvent.values);
float xLocalFactor = xFactor;
float yLocalFactor = yFactor;
if(LauncherPreferences.PREF_GYRO_INVERT_X) xLocalFactor *= -1;
if(LauncherPreferences.PREF_GYRO_INVERT_Y) yLocalFactor *= -1;
if(mFirstPass){ // Setup initial position
mFirstPass = false;
return;
@@ -106,20 +101,20 @@ public class GyroControl implements SensorEventListener, GrabListener {
float absY = Math.abs(mStoredY);
if(absX + absY > MULTI_AXIS_LOW_PASS_THRESHOLD) {
CallbackBridge.mouseX -= ((mSwapXY ? mStoredY : mStoredX) * xLocalFactor);
CallbackBridge.mouseY += ((mSwapXY ? mStoredX : mStoredY) * yLocalFactor);
CallbackBridge.mouseX -= ((mSwapXY ? mStoredY : mStoredX) * xFactor);
CallbackBridge.mouseY += ((mSwapXY ? mStoredX : mStoredY) * yFactor);
mStoredX = 0;
mStoredY = 0;
updatePosition = true;
} else {
if(Math.abs(mStoredX) > SINGLE_AXIS_LOW_PASS_THRESHOLD){
CallbackBridge.mouseX -= ((mSwapXY ? mStoredY : mStoredX) * xLocalFactor);
CallbackBridge.mouseX -= ((mSwapXY ? mStoredY : mStoredX) * xFactor);
mStoredX = 0;
updatePosition = true;
}
if(Math.abs(mStoredY) > SINGLE_AXIS_LOW_PASS_THRESHOLD) {
CallbackBridge.mouseY += ((mSwapXY ? mStoredX : mStoredY) * yLocalFactor);
CallbackBridge.mouseY += ((mSwapXY ? mStoredX : mStoredY) * yFactor);
mStoredY = 0;
updatePosition = true;
}
@@ -156,6 +151,9 @@ public class GyroControl implements SensorEventListener, GrabListener {
yFactor = -1;
break;
}
if(LauncherPreferences.PREF_GYRO_INVERT_X) xFactor *= -1;
if(LauncherPreferences.PREF_GYRO_INVERT_Y) yFactor *= -1;
}
@Override
@@ -244,6 +242,9 @@ public class GyroControl implements SensorEventListener, GrabListener {
}
break;
}
if(LauncherPreferences.PREF_GYRO_INVERT_X) xFactor *= -1;
if(LauncherPreferences.PREF_GYRO_INVERT_Y) yFactor *= -1;
}
}
}

View File

@@ -103,11 +103,13 @@ public abstract class QuickSettingSideDialog extends com.kdt.SideDialogView<Cons
mGyroXSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
PREF_GYRO_INVERT_X = isChecked;
onGyroStateChanged();
LauncherPreferences.DEFAULT_PREF.edit().putBoolean("gyroInvertX", isChecked).apply();
});
mGyroYSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
PREF_GYRO_INVERT_Y = isChecked;
onGyroStateChanged();
LauncherPreferences.DEFAULT_PREF.edit().putBoolean("gyroInvertY", isChecked).apply();
});
@@ -217,7 +219,11 @@ public abstract class QuickSettingSideDialog extends com.kdt.SideDialogView<Cons
/** Called when the resolution is changed. Use {@link LauncherPreferences#PREF_SCALE_FACTOR} */
public abstract void onResolutionChanged();
/** Called when the gyro state is changed. Use {@link LauncherPreferences#PREF_ENABLE_GYRO} */
/** Called when the gyro state is changed.
* Use {@link LauncherPreferences#PREF_ENABLE_GYRO}
* Use {@link LauncherPreferences#PREF_GYRO_INVERT_X}
* Use {@link LauncherPreferences#PREF_GYRO_INVERT_Y}
*/
public abstract void onGyroStateChanged();
}