Added orientation sensor support

This commit is contained in:
Sergii Pylypenko
2015-07-14 21:28:45 +03:00
parent 4635e72dd5
commit b7733367a3
4 changed files with 59 additions and 4 deletions

View File

@@ -35,6 +35,7 @@ import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.util.Log;
import android.widget.TextView;
import android.os.Build;
class AccelerometerReader implements SensorEventListener
@@ -43,6 +44,7 @@ class AccelerometerReader implements SensorEventListener
private SensorManager _manager = null;
public boolean openedBySDL = false;
public static final GyroscopeListener gyro = new GyroscopeListener();
public static final OrientationListener orientation = new OrientationListener();
public AccelerometerReader(Activity context)
{
@@ -53,9 +55,10 @@ class AccelerometerReader implements SensorEventListener
{
if( _manager != null )
{
Log.i("SDL", "libSDL: stopping accelerometer/gyroscope");
Log.i("SDL", "libSDL: stopping accelerometer/gyroscope/orientation");
_manager.unregisterListener(this);
_manager.unregisterListener(gyro);
_manager.unregisterListener(orientation);
}
}
@@ -73,6 +76,14 @@ class AccelerometerReader implements SensorEventListener
Log.i("SDL", "libSDL: starting gyroscope");
_manager.registerListener(gyro, _manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
}
if( (Globals.AppUsesOrientationSensor) && _manager != null &&
_manager.getDefaultSensor(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ? Sensor.TYPE_GAME_ROTATION_VECTOR : Sensor.TYPE_ROTATION_VECTOR) != null )
{
Log.i("SDL", "libSDL: starting orientation sensor");
_manager.registerListener(orientation, _manager.getDefaultSensor(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ? Sensor.TYPE_GAME_ROTATION_VECTOR : Sensor.TYPE_ROTATION_VECTOR),
SensorManager.SENSOR_DELAY_GAME);
}
}
public void onSensorChanged(SensorEvent event)
@@ -129,7 +140,10 @@ class AccelerometerReader implements SensorEventListener
SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
if ( manager == null && manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) == null )
return;
manager.registerListener(l, manager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
manager.registerListener(gyro, manager.getDefaultSensor(
Globals.AppUsesOrientationSensor ? Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ?
Sensor.TYPE_GAME_ROTATION_VECTOR : Sensor.TYPE_ROTATION_VECTOR : Sensor.TYPE_GYROSCOPE),
SensorManager.SENSOR_DELAY_GAME);
}
public void unregisterListener(Activity context,SensorEventListener l)
{
@@ -140,6 +154,21 @@ class AccelerometerReader implements SensorEventListener
}
}
static class OrientationListener implements SensorEventListener
{
public OrientationListener()
{
}
public void onSensorChanged(SensorEvent event)
{
nativeOrientation(event.values[0], event.values[1], event.values[2]);
}
public void onAccuracyChanged(Sensor s, int a)
{
}
}
private static native void nativeAccelerometer(float accX, float accY, float accZ);
private static native void nativeGyroscope(float X, float Y, float Z);
private static native void nativeOrientation(float X, float Y, float Z);
}

View File

@@ -61,6 +61,7 @@ class Globals
public static boolean AppUsesThirdJoystick = false;
public static boolean AppUsesAccelerometer = false;
public static boolean AppUsesGyroscope = false;
public static boolean AppUsesOrientationSensor = false;
public static boolean AppUsesMultitouch = false;
public static boolean NonBlockingSwapBuffers = false;
public static boolean ResetSdlConfigForThisVersion = false;

View File

@@ -1063,7 +1063,6 @@ JAVA_EXPORT_NAME(AccelerometerReader_nativeAccelerometer) ( JNIEnv* env, jobjec
SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 7, fminf(32767.0f, fmaxf(-32767.0f, accPosZ*1000.0f)));
}
JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(AccelerometerReader_nativeGyroscope) ( JNIEnv* env, jobject thiz, jfloat X, jfloat Y, jfloat Z )
{
@@ -1099,6 +1098,22 @@ JAVA_EXPORT_NAME(AccelerometerReader_nativeGyroscope) ( JNIEnv* env, jobject th
}
}
JNIEXPORT void JNICALL
JAVA_EXPORT_NAME(AccelerometerReader_nativeOrientation) ( JNIEnv* env, jobject thiz, jfloat X, jfloat Y, jfloat Z )
{
#if SDL_VERSION_ATLEAST(1,3,0)
#else
if( !SDL_CurrentVideoSurface )
return;
#endif
//__android_log_print(ANDROID_LOG_INFO, "libSDL", "Orientation %f %f %f", X, Y, Z);
SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 8, NORMALIZE_FLOAT_32767(X));
SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 9, NORMALIZE_FLOAT_32767(Y));
SDL_ANDROID_MainThreadPushJoystickAxis(JOY_ACCELGYRO, 10, NORMALIZE_FLOAT_32767(Z));
}
static int getClickTimeout(int v)
{
switch(v)
@@ -1489,7 +1504,7 @@ int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
}
if( joystick->index == JOY_ACCELGYRO )
{
joystick->naxes = 8; // Normalized accelerometer = axes 0-1, gyroscope = axes 2-4, raw accelerometer = axes 5-7
joystick->naxes = 11; // Normalized accelerometer = axes 0-1, gyroscope = axes 2-4, raw accelerometer = axes 5-7, orientation = axes 8-10
if( !moveMouseWithGyroscope )
SDL_ANDROID_CallJavaStartAccelerometerGyroscope(1);
}