Merge pull request #55184 from madmiraal/fix-41421-3.x

[3.x] Only rotate Android sensor values for sensors that need them rotated
This commit is contained in:
Rémi Verschelde 2021-12-15 20:26:28 +01:00 committed by GitHub
commit b8f87d3f86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -856,45 +856,78 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
}); });
} }
@Override public float[] getRotatedValues(float values[]) {
public void onSensorChanged(SensorEvent event) { if (values == null || values.length != 3) {
return values;
}
Display display = Display display =
((WindowManager)getActivity().getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); ((WindowManager)getActivity().getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int displayRotation = display.getRotation(); int displayRotation = display.getRotation();
float[] adjustedValues = new float[3]; float[] rotatedValues = new float[3];
final int[][] axisSwap = { switch (displayRotation) {
{ 1, -1, 0, 1 }, // ROTATION_0 case Surface.ROTATION_0:
{ -1, -1, 1, 0 }, // ROTATION_90 rotatedValues[0] = values[0];
{ -1, 1, 0, 1 }, // ROTATION_180 rotatedValues[1] = values[1];
{ 1, 1, 1, 0 } rotatedValues[2] = values[2];
}; // ROTATION_270 break;
case Surface.ROTATION_90:
rotatedValues[0] = -values[1];
rotatedValues[1] = values[0];
rotatedValues[2] = values[2];
break;
case Surface.ROTATION_180:
rotatedValues[0] = -values[0];
rotatedValues[1] = -values[1];
rotatedValues[2] = values[2];
break;
case Surface.ROTATION_270:
rotatedValues[0] = values[1];
rotatedValues[1] = -values[0];
rotatedValues[2] = values[2];
break;
}
final int[] as = axisSwap[displayRotation]; return rotatedValues;
adjustedValues[0] = (float)as[0] * event.values[as[2]]; }
adjustedValues[1] = (float)as[1] * event.values[as[3]];
adjustedValues[2] = event.values[2];
final float x = adjustedValues[0]; @Override
final float y = adjustedValues[1]; public void onSensorChanged(SensorEvent event) {
final float z = adjustedValues[2]; if (mView == null) {
return;
}
final int typeOfSensor = event.sensor.getType(); final int typeOfSensor = event.sensor.getType();
if (mView != null) { switch (typeOfSensor) {
mView.queueEvent(() -> { case Sensor.TYPE_ACCELEROMETER: {
if (typeOfSensor == Sensor.TYPE_ACCELEROMETER) { float[] rotatedValues = getRotatedValues(event.values);
GodotLib.accelerometer(-x, y, -z); mView.queueEvent(() -> {
} GodotLib.accelerometer(-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]);
if (typeOfSensor == Sensor.TYPE_GRAVITY) { });
GodotLib.gravity(-x, y, -z); break;
} }
if (typeOfSensor == Sensor.TYPE_MAGNETIC_FIELD) { case Sensor.TYPE_GRAVITY: {
GodotLib.magnetometer(-x, y, -z); float[] rotatedValues = getRotatedValues(event.values);
} mView.queueEvent(() -> {
if (typeOfSensor == Sensor.TYPE_GYROSCOPE) { GodotLib.gravity(-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]);
GodotLib.gyroscope(x, -y, z); });
} break;
}); }
case Sensor.TYPE_MAGNETIC_FIELD: {
float[] rotatedValues = getRotatedValues(event.values);
mView.queueEvent(() -> {
GodotLib.magnetometer(-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]);
});
break;
}
case Sensor.TYPE_GYROSCOPE: {
float[] rotatedValues = getRotatedValues(event.values);
mView.queueEvent(() -> {
GodotLib.gyroscope(rotatedValues[0], rotatedValues[1], rotatedValues[2]);
});
break;
}
} }
} }