Gyro controls: Implement listener to correct gyro controls in reverse portrait/landscape modes

Co-authored-by: TarikBR <TarikBR@github.com>
This commit is contained in:
artdeell
2023-02-12 22:18:45 +03:00
parent 2c66709f30
commit ff1db31271

View File

@@ -5,28 +5,34 @@ import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.OrientationEventListener;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import org.lwjgl.glfw.CallbackBridge;
public class GyroControl implements SensorEventListener, GrabListener {
public class GyroControl implements SensorEventListener, GrabListener{
private final SensorManager mSensorManager;
private final Sensor mSensor;
private final OrientationCorrectionListener mCorrectionListener;
private boolean mShouldHandleEvents;
private boolean mFirstPass;
private long mPreviousTimestamp;
private float xFactor; // -1 or 1 depending on device orientation
private float yFactor;
private boolean mSwapXY;
public GyroControl(Context context) {
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
mCorrectionListener = new OrientationCorrectionListener(context);
}
public void enable() {
if(mSensor == null) return;
mFirstPass = true;
mSensorManager.registerListener(this, mSensor, 1000 * LauncherPreferences.PREF_GYRO_SAMPLE_RATE);
mCorrectionListener.enable();
mShouldHandleEvents = CallbackBridge.isGrabbing();
CallbackBridge.addGrabListener(this);
}
@@ -34,9 +40,9 @@ public class GyroControl implements SensorEventListener, GrabListener {
public void disable() {
if(mSensor == null) return;
mSensorManager.unregisterListener(this);
mCorrectionListener.disable();
CallbackBridge.removeGrabListener(this);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if(mShouldHandleEvents && sensorEvent.sensor == mSensor) {
@@ -44,8 +50,13 @@ public class GyroControl implements SensorEventListener, GrabListener {
if(!mFirstPass) {
factor *= (sensorEvent.timestamp - mPreviousTimestamp) * 0.000001;
}else mFirstPass = false;
CallbackBridge.mouseX += sensorEvent.values[0] * factor;
CallbackBridge.mouseY += sensorEvent.values[1] * factor;
if(mSwapXY) {
float interm = sensorEvent.values[0];
sensorEvent.values[0] = sensorEvent.values[1];
sensorEvent.values[1] = interm;
}
CallbackBridge.mouseX += sensorEvent.values[0] * factor * xFactor;
CallbackBridge.mouseY += sensorEvent.values[1] * factor * yFactor;
CallbackBridge.sendCursorPos(CallbackBridge.mouseX, CallbackBridge.mouseY);
mPreviousTimestamp = sensorEvent.timestamp;
}
@@ -61,4 +72,32 @@ public class GyroControl implements SensorEventListener, GrabListener {
mFirstPass = true;
mShouldHandleEvents = isGrabbing;
}
class OrientationCorrectionListener extends OrientationEventListener {
public OrientationCorrectionListener(Context context) {
super(context, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onOrientationChanged(int i) {
if((315 < i && i <= 360) || (i < 45) ) {
mSwapXY = true;
xFactor = -1;
yFactor = -1;
}else if(45 < i && i < 135) {
mSwapXY = false;
xFactor = -1;
yFactor = -1;
}else if(135 < i && i < 225) {
mSwapXY = true;
xFactor = 1;
yFactor = 1;
}else if(225 < i && i < 315) {
mSwapXY = false;
xFactor = 1;
yFactor = 1;
}
}
}
}