Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android gravity vector Godot 2.1 #8088

Merged
merged 1 commit into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion platform/android/godot_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ struct engine {

ASensorManager *sensorManager;
const ASensor *accelerometerSensor;
const ASensor *gravitySensor;
const ASensor *magnetometerSensor;
const ASensor *gyroscopeSensor;
ASensorEventQueue *sensorEventQueue;
Expand Down Expand Up @@ -694,6 +695,14 @@ static void engine_handle_cmd(struct android_app *app, int32_t cmd) {
ASensorEventQueue_setEventRate(engine->sensorEventQueue,
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.
if (engine->magnetometerSensor != NULL) {
ASensorEventQueue_enableSensor(engine->sensorEventQueue,
Expand All @@ -719,6 +728,10 @@ static void engine_handle_cmd(struct android_app *app, int32_t cmd) {
ASensorEventQueue_disableSensor(engine->sensorEventQueue,
engine->accelerometerSensor);
}
if (engine->gravitySensor != NULL) {
ASensorEventQueue_disableSensor(engine->sensorEventQueue,
engine->gravitySensor);
}
if (engine->magnetometerSensor != NULL) {
ASensorEventQueue_disableSensor(engine->sensorEventQueue,
engine->magnetometerSensor);
Expand Down Expand Up @@ -754,6 +767,8 @@ void android_main(struct android_app *state) {
engine.sensorManager = ASensorManager_getInstance();
engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
ASENSOR_TYPE_ACCELEROMETER);
engine.gravitySensor = ASensorManager_getDefaultSensor(engine.sensorManager,
ASENSOR_TYPE_GRAVITY);
engine.magnetometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
ASENSOR_TYPE_MAGNETIC_FIELD);
engine.gyroscopeSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
Expand Down Expand Up @@ -795,7 +810,7 @@ void android_main(struct android_app *state) {
// If a sensor has data, process it now.
// LOGI("events\n");
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;
while (ASensorEventQueue_getEvents(engine.sensorEventQueue,
&event, 1) > 0) {
Expand All @@ -805,6 +820,10 @@ void android_main(struct android_app *state) {
engine.os->process_accelerometer(Vector3(event.acceleration.x, event.acceleration.y,
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) {
engine.os->process_magnetometer(Vector3(event.magnetic.x, event.magnetic.y,
event.magnetic.z));
Expand Down
7 changes: 7 additions & 0 deletions platform/android/java/src/org/godotengine/godot/Godot.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ protected void instanceSingleton(SingletonBase s) {

private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Sensor mGravity;
private Sensor mMagnetometer;
private Sensor mGyroscope;

Expand Down Expand Up @@ -405,6 +406,8 @@ private void initializeGodot() {
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
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);
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_GAME);
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
Expand Down Expand Up @@ -625,6 +628,7 @@ protected void onCreate(Bundle icicle) {

mView.onResume();
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, mGyroscope, SensorManager.SENSOR_DELAY_GAME);
GodotLib.focusin();
Expand Down Expand Up @@ -690,6 +694,9 @@ public void onSystemUiVisibilityChange(int visibility) {
if (typeOfSensor == event.sensor.TYPE_ACCELEROMETER) {
GodotLib.accelerometer(x,y,z);
}
if (typeOfSensor == event.sensor.TYPE_GRAVITY) {
GodotLib.gravity(x,y,z);
}
if (typeOfSensor == event.sensor.TYPE_MAGNETIC_FIELD) {
GodotLib.magnetometer(x,y,z);
}
Expand Down
55 changes: 28 additions & 27 deletions platform/android/java/src/org/godotengine/godot/GodotLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,38 @@
public class GodotLib {


public static GodotIO io;
public static GodotIO io;

static {
static {
System.loadLibrary("godot_android");
}
}

/**
* @param width the current view width
* @param height the current view height
*/
/**
* @param width the current view width
* @param height the current view height
*/

public static native void initialize(Godot p_instance,boolean need_reload_hook,String[] p_cmdline,Object p_asset_manager);
public static native void resize(int width, int height,boolean reload);
public static native void newcontext(boolean p_32_bits);
public static native void quit();
public static native void step();
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 magnetometer(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 joybutton(int p_device, int p_but, boolean p_pressed);
public static native void joyaxis(int p_device, int p_axis, float p_value);
public static native void joyhat(int p_device, int p_hat_x, int p_hat_y);
public static native void joyconnectionchanged(int p_device, boolean p_connected, String p_name);
public static native void focusin();
public static native void focusout();
public static native void audio();
public static native void singleton(String p_name,Object p_object);
public static native void method(String p_sname,String p_name,String p_ret,String[] p_params);
public static native String getGlobal(String p_key);
public static native void initialize(Godot p_instance,boolean need_reload_hook,String[] p_cmdline,Object p_asset_manager);
public static native void resize(int width, int height,boolean reload);
public static native void newcontext(boolean p_32_bits);
public static native void quit();
public static native void step();
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 gravity(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 key(int p_scancode, int p_unicode_char, boolean p_pressed);
public static native void joybutton(int p_device, int p_but, boolean p_pressed);
public static native void joyaxis(int p_device, int p_axis, float p_value);
public static native void joyhat(int p_device, int p_hat_x, int p_hat_y);
public static native void joyconnectionchanged(int p_device, boolean p_connected, String p_name);
public static native void focusin();
public static native void focusout();
public static native void audio();
public static native void singleton(String p_name,Object p_object);
public static native void method(String p_sname,String p_name,String p_ret,String[] p_params);
public static native String getGlobal(String p_key);
public static native void callobject(int p_ID, String p_method, Object[] p_params);
public static native void calldeferred(int p_ID, String p_method, Object[] p_params);

Expand Down
10 changes: 10 additions & 0 deletions platform/android/java_glue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ static bool resized_reload = false;
static bool quit_request = false;
static Size2 new_size;
static Vector3 accelerometer;
static Vector3 gravity;
static Vector3 magnetometer;
static Vector3 gyroscope;
static HashMap<String, JNISingleton *> jni_singletons;
Expand Down Expand Up @@ -1051,6 +1052,8 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, job

os_android->process_accelerometer(accelerometer);

os_android->process_gravitymeter(gravity);

os_android->process_magnetometer(magnetometer);

os_android->process_gyroscope(gyroscope);
Expand Down Expand Up @@ -1449,6 +1452,13 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv
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) {

input_mutex->lock();
Expand Down
1 change: 1 addition & 0 deletions platform/android/java_glue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_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_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_gyroscope(JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat z);
JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jobject obj);
Expand Down
5 changes: 5 additions & 0 deletions platform/android/os_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@ void OS_Android::process_accelerometer(const Vector3 &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) {

input->set_magnetometer(p_magnetometer);
Expand Down
1 change: 1 addition & 0 deletions platform/android/os_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class OS_Android : public OS_Unix {
virtual String get_system_dir(SystemDir p_dir) const;

void process_accelerometer(const Vector3 &p_accelerometer);
void process_gravitymeter(const Vector3 &p_gravitymeter);
void process_magnetometer(const Vector3 &p_magnetometer);
void process_gyroscope(const Vector3 &p_gyroscope);
void process_touch(int p_what, int p_pointer, const Vector<TouchPos> &p_points);
Expand Down