Skip to content

Commit

Permalink
Refactor(gyro): pre-compute the factor one quick setting change
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias-Boulay committed Nov 19, 2024
1 parent be69154 commit 85e98a9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public void onResolutionChanged() {

@Override
public void onGyroStateChanged() {
mGyroControl.updateOrientation();
if (PREF_ENABLE_GYRO) {
mGyroControl.enable();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ public void onSensorChanged(SensorEvent sensorEvent) {
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;
Expand All @@ -106,20 +101,20 @@ public void onSensorChanged(SensorEvent sensorEvent) {
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;
}
Expand Down Expand Up @@ -156,6 +151,9 @@ public void updateOrientation(){
yFactor = -1;
break;
}

if(LauncherPreferences.PREF_GYRO_INVERT_X) xFactor *= -1;
if(LauncherPreferences.PREF_GYRO_INVERT_Y) yFactor *= -1;
}

@Override
Expand Down Expand Up @@ -244,6 +242,9 @@ public void onOrientationChanged(int i) {
}
break;
}

if(LauncherPreferences.PREF_GYRO_INVERT_X) xFactor *= -1;
if(LauncherPreferences.PREF_GYRO_INVERT_Y) yFactor *= -1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ private void setupListeners() {

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();
});

Expand Down Expand Up @@ -217,7 +219,11 @@ public void cancel() {
/** 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();

}

0 comments on commit 85e98a9

Please sign in to comment.