Merge pull request #8088 from BastiaanOlij/android_gravity_2.1

Android gravity vector Godot 2.1
This commit is contained in:
Rémi Verschelde 2017-03-24 22:49:23 +01:00 committed by GitHub
commit 492f6e06c0
7 changed files with 72 additions and 28 deletions

View file

@ -295,6 +295,7 @@ struct engine {
ASensorManager *sensorManager; ASensorManager *sensorManager;
const ASensor *accelerometerSensor; const ASensor *accelerometerSensor;
const ASensor *gravitySensor;
const ASensor *magnetometerSensor; const ASensor *magnetometerSensor;
const ASensor *gyroscopeSensor; const ASensor *gyroscopeSensor;
ASensorEventQueue *sensorEventQueue; ASensorEventQueue *sensorEventQueue;
@ -694,6 +695,14 @@ static void engine_handle_cmd(struct android_app *app, int32_t cmd) {
ASensorEventQueue_setEventRate(engine->sensorEventQueue, ASensorEventQueue_setEventRate(engine->sensorEventQueue,
engine->accelerometerSensor, (1000L / 60) * 1000); engine->accelerometerSensor, (1000L / 60) * 1000);
} }
// and start monitoring our gravity vector
if (engine->gravitySensor != NULL) {
ASensorEventQueue_enableSensor(engine->sensorEventQueue,
engine->gravitySensor);
// We'd like to get 60 events per second (in us).
ASensorEventQueue_setEventRate(engine->sensorEventQueue,
engine->gravitySensor, (1000L / 60) * 1000);
}
// Also start monitoring the magnetometer. // Also start monitoring the magnetometer.
if (engine->magnetometerSensor != NULL) { if (engine->magnetometerSensor != NULL) {
ASensorEventQueue_enableSensor(engine->sensorEventQueue, ASensorEventQueue_enableSensor(engine->sensorEventQueue,
@ -719,6 +728,10 @@ static void engine_handle_cmd(struct android_app *app, int32_t cmd) {
ASensorEventQueue_disableSensor(engine->sensorEventQueue, ASensorEventQueue_disableSensor(engine->sensorEventQueue,
engine->accelerometerSensor); engine->accelerometerSensor);
} }
if (engine->gravitySensor != NULL) {
ASensorEventQueue_disableSensor(engine->sensorEventQueue,
engine->gravitySensor);
}
if (engine->magnetometerSensor != NULL) { if (engine->magnetometerSensor != NULL) {
ASensorEventQueue_disableSensor(engine->sensorEventQueue, ASensorEventQueue_disableSensor(engine->sensorEventQueue,
engine->magnetometerSensor); engine->magnetometerSensor);
@ -754,6 +767,8 @@ void android_main(struct android_app *state) {
engine.sensorManager = ASensorManager_getInstance(); engine.sensorManager = ASensorManager_getInstance();
engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager, engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
ASENSOR_TYPE_ACCELEROMETER); ASENSOR_TYPE_ACCELEROMETER);
engine.gravitySensor = ASensorManager_getDefaultSensor(engine.sensorManager,
ASENSOR_TYPE_GRAVITY);
engine.magnetometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager, engine.magnetometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
ASENSOR_TYPE_MAGNETIC_FIELD); ASENSOR_TYPE_MAGNETIC_FIELD);
engine.gyroscopeSensor = ASensorManager_getDefaultSensor(engine.sensorManager, engine.gyroscopeSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
@ -795,7 +810,7 @@ void android_main(struct android_app *state) {
// If a sensor has data, process it now. // If a sensor has data, process it now.
// LOGI("events\n"); // LOGI("events\n");
if (ident == LOOPER_ID_USER) { if (ident == LOOPER_ID_USER) {
if (engine.accelerometerSensor != NULL || engine.magnetometerSensor != NULL || engine.gyroscopeSensor != NULL) { if (engine.accelerometerSensor != NULL || engine.gravitySensor != NULL || engine.magnetometerSensor != NULL || engine.gyroscopeSensor != NULL) {
ASensorEvent event; ASensorEvent event;
while (ASensorEventQueue_getEvents(engine.sensorEventQueue, while (ASensorEventQueue_getEvents(engine.sensorEventQueue,
&event, 1) > 0) { &event, 1) > 0) {
@ -805,6 +820,10 @@ void android_main(struct android_app *state) {
engine.os->process_accelerometer(Vector3(event.acceleration.x, event.acceleration.y, engine.os->process_accelerometer(Vector3(event.acceleration.x, event.acceleration.y,
event.acceleration.z)); event.acceleration.z));
} }
if (event.gravity != NULL) {
engine.os->process_gravitymeter(Vector3(event.gravity.x, event.gravity.y,
event.gravity.z));
}
if (event.magnetic != NULL) { if (event.magnetic != NULL) {
engine.os->process_magnetometer(Vector3(event.magnetic.x, event.magnetic.y, engine.os->process_magnetometer(Vector3(event.magnetic.x, event.magnetic.y,
event.magnetic.z)); event.magnetic.z));

View file

@ -216,6 +216,7 @@ public class Godot extends Activity implements SensorEventListener, IDownloaderC
private SensorManager mSensorManager; private SensorManager mSensorManager;
private Sensor mAccelerometer; private Sensor mAccelerometer;
private Sensor mGravity;
private Sensor mMagnetometer; private Sensor mMagnetometer;
private Sensor mGyroscope; private Sensor mGyroscope;
@ -405,6 +406,8 @@ public class Godot extends Activity implements SensorEventListener, IDownloaderC
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
mSensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_GAME);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME); mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME);
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
@ -625,6 +628,7 @@ public class Godot extends Activity implements SensorEventListener, IDownloaderC
mView.onResume(); mView.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME); mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_GAME); mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_GAME);
GodotLib.focusin(); GodotLib.focusin();
@ -690,6 +694,9 @@ public class Godot extends Activity implements SensorEventListener, IDownloaderC
if (typeOfSensor == event.sensor.TYPE_ACCELEROMETER) { if (typeOfSensor == event.sensor.TYPE_ACCELEROMETER) {
GodotLib.accelerometer(x,y,z); GodotLib.accelerometer(x,y,z);
} }
if (typeOfSensor == event.sensor.TYPE_GRAVITY) {
GodotLib.gravity(x,y,z);
}
if (typeOfSensor == event.sensor.TYPE_MAGNETIC_FIELD) { if (typeOfSensor == event.sensor.TYPE_MAGNETIC_FIELD) {
GodotLib.magnetometer(x,y,z); GodotLib.magnetometer(x,y,z);
} }

View file

@ -51,6 +51,7 @@ public class GodotLib {
public static native void step(); public static native void step();
public static native void touch(int what,int pointer,int howmany, int[] arr); public static native void touch(int what,int pointer,int howmany, int[] arr);
public static native void accelerometer(float x, float y, float z); public static native void accelerometer(float x, float y, float z);
public static native void gravity(float x, float y, float z);
public static native void magnetometer(float x, float y, float z); public static native void magnetometer(float x, float y, float z);
public static native void gyroscope(float x, float y, float z); public static native void gyroscope(float x, float y, float z);
public static native void key(int p_scancode, int p_unicode_char, boolean p_pressed); public static native void key(int p_scancode, int p_unicode_char, boolean p_pressed);

View file

@ -620,6 +620,7 @@ static bool resized_reload = false;
static bool quit_request = false; static bool quit_request = false;
static Size2 new_size; static Size2 new_size;
static Vector3 accelerometer; static Vector3 accelerometer;
static Vector3 gravity;
static Vector3 magnetometer; static Vector3 magnetometer;
static Vector3 gyroscope; static Vector3 gyroscope;
static HashMap<String, JNISingleton *> jni_singletons; static HashMap<String, JNISingleton *> jni_singletons;
@ -1051,6 +1052,8 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, job
os_android->process_accelerometer(accelerometer); os_android->process_accelerometer(accelerometer);
os_android->process_gravitymeter(gravity);
os_android->process_magnetometer(magnetometer); os_android->process_magnetometer(magnetometer);
os_android->process_gyroscope(gyroscope); os_android->process_gyroscope(gyroscope);
@ -1449,6 +1452,13 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv
input_mutex->unlock(); input_mutex->unlock();
} }
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gravity(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat z) {
input_mutex->lock();
gravity = Vector3(x, y, z);
input_mutex->unlock();
}
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat z) { JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat z) {
input_mutex->lock(); input_mutex->lock();

View file

@ -48,6 +48,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, j
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jobject obj, jint p_device, jboolean p_connected, jstring p_name); JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jobject obj, jint p_device, jboolean p_connected, jstring p_name);
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_audio(JNIEnv *env, jobject obj); JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_audio(JNIEnv *env, jobject obj);
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat z); JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat z);
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gravity(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat z);
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat z); JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat z);
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gyroscope(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat z); JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gyroscope(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat z);
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jobject obj); JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jobject obj);

View file

@ -580,6 +580,11 @@ void OS_Android::process_accelerometer(const Vector3 &p_accelerometer) {
input->set_accelerometer(p_accelerometer); input->set_accelerometer(p_accelerometer);
} }
void OS_Android::process_gravitymeter(const Vector3 &p_gravitymeter) {
input->set_gravity(p_gravitymeter);
}
void OS_Android::process_magnetometer(const Vector3 &p_magnetometer) { void OS_Android::process_magnetometer(const Vector3 &p_magnetometer) {
input->set_magnetometer(p_magnetometer); input->set_magnetometer(p_magnetometer);

View file

@ -238,6 +238,7 @@ public:
virtual String get_system_dir(SystemDir p_dir) const; virtual String get_system_dir(SystemDir p_dir) const;
void process_accelerometer(const Vector3 &p_accelerometer); void process_accelerometer(const Vector3 &p_accelerometer);
void process_gravitymeter(const Vector3 &p_gravitymeter);
void process_magnetometer(const Vector3 &p_magnetometer); void process_magnetometer(const Vector3 &p_magnetometer);
void process_gyroscope(const Vector3 &p_gyroscope); void process_gyroscope(const Vector3 &p_gyroscope);
void process_touch(int p_what, int p_pointer, const Vector<TouchPos> &p_points); void process_touch(int p_what, int p_pointer, const Vector<TouchPos> &p_points);