@@ -47,7 +47,10 @@ public class Cocos2dxAccelerometer implements SensorEventListener {
4747 private final Context mContext ;
4848 private final SensorManager mSensorManager ;
4949 private final Sensor mAccelerometer ;
50+ private final Sensor mCompass ;
5051 private final int mNaturalOrientation ;
52+ final float [] accelerometerValues = new float [3 ];
53+ final float [] magneticFieldValues = new float [3 ];
5154
5255 // ===========================================================
5356 // Constructors
@@ -58,6 +61,7 @@ public Cocos2dxAccelerometer(final Context context) {
5861
5962 this .mSensorManager = (SensorManager ) this .mContext .getSystemService (Context .SENSOR_SERVICE );
6063 this .mAccelerometer = this .mSensorManager .getDefaultSensor (Sensor .TYPE_ACCELEROMETER );
64+ this .mCompass = this .mSensorManager .getDefaultSensor (Sensor .TYPE_MAGNETIC_FIELD );
6165
6266 final Display display = ((WindowManager ) this .mContext .getSystemService (Context .WINDOW_SERVICE )).getDefaultDisplay ();
6367 this .mNaturalOrientation = display .getOrientation ();
@@ -67,20 +71,26 @@ public Cocos2dxAccelerometer(final Context context) {
6771 // Getter & Setter
6872 // ===========================================================
6973
74+ public void enableAll () {
75+ this .mSensorManager .registerListener (this , this .mAccelerometer , SensorManager .SENSOR_DELAY_GAME );
76+ this .mSensorManager .registerListener (this , this .mCompass , SensorManager .SENSOR_DELAY_GAME );
77+ }
78+
7079 public void enable () {
80+ this .mSensorManager .registerListener (this , this .mCompass , SensorManager .SENSOR_DELAY_GAME );
7181 this .mSensorManager .registerListener (this , this .mAccelerometer , SensorManager .SENSOR_DELAY_GAME );
7282 }
7383
74- public void setInterval (float interval ) {
75- // Honeycomb version is 11
76- if (android .os .Build .VERSION .SDK_INT < 11 ) {
84+ public void setInterval (float interval ) {
85+ // Honeycomb version is 11
86+ if (android .os .Build .VERSION .SDK_INT < 11 ) {
7787 this .mSensorManager .registerListener (this , this .mAccelerometer , SensorManager .SENSOR_DELAY_GAME );
7888 } else {
7989 //convert seconds to microseconds
8090 this .mSensorManager .registerListener (this , this .mAccelerometer , (int )(interval *100000 ));
8191 }
8292 }
83-
93+
8494 public void disable () {
8595 this .mSensorManager .unregisterListener (this );
8696 }
@@ -91,38 +101,46 @@ public void disable() {
91101
92102 @ Override
93103 public void onSensorChanged (final SensorEvent sensorEvent ) {
94- if (sensorEvent .sensor .getType () != Sensor .TYPE_ACCELEROMETER ) {
95- return ;
104+ if (sensorEvent .sensor .getType () == Sensor .TYPE_ACCELEROMETER ) {
105+
106+ float x = sensorEvent .values [0 ];
107+ float y = sensorEvent .values [1 ];
108+ final float z = sensorEvent .values [2 ];
109+
110+ /*
111+ * Because the axes are not swapped when the device's screen orientation
112+ * changes. So we should swap it here. In tablets such as Motorola Xoom,
113+ * the default orientation is landscape, so should consider this.
114+ */
115+ final int orientation = this .mContext .getResources ().getConfiguration ().orientation ;
116+
117+ if ((orientation == Configuration .ORIENTATION_LANDSCAPE ) && (this .mNaturalOrientation != Surface .ROTATION_0 )) {
118+ final float tmp = x ;
119+ x = -y ;
120+ y = tmp ;
121+ } else if ((orientation == Configuration .ORIENTATION_PORTRAIT ) && (this .mNaturalOrientation != Surface .ROTATION_0 )) {
122+ final float tmp = x ;
123+ x = y ;
124+ y = -tmp ;
125+ }
126+
127+ Cocos2dxGLSurfaceView .queueAccelerometer (x ,y ,z ,sensorEvent .timestamp );
128+
129+ this .accelerometerValues [0 ] = x ;
130+ this .accelerometerValues [1 ] = y ;
131+ this .accelerometerValues [2 ] = z ;
132+
133+ /*
134+ if(BuildConfig.DEBUG) {
135+ Log.d(TAG, "x = " + sensorEvent.values[0] + " y = " + sensorEvent.values[1] + " z = " + pSensorEvent.values[2]);
136+ }
137+ */
96138 }
97-
98- float x = sensorEvent .values [0 ];
99- float y = sensorEvent .values [1 ];
100- final float z = sensorEvent .values [2 ];
101-
102- /*
103- * Because the axes are not swapped when the device's screen orientation
104- * changes. So we should swap it here. In tablets such as Motorola Xoom,
105- * the default orientation is landscape, so should consider this.
106- */
107- final int orientation = this .mContext .getResources ().getConfiguration ().orientation ;
108-
109- if ((orientation == Configuration .ORIENTATION_LANDSCAPE ) && (this .mNaturalOrientation != Surface .ROTATION_0 )) {
110- final float tmp = x ;
111- x = -y ;
112- y = tmp ;
113- } else if ((orientation == Configuration .ORIENTATION_PORTRAIT ) && (this .mNaturalOrientation != Surface .ROTATION_0 )) {
114- final float tmp = x ;
115- x = y ;
116- y = -tmp ;
117- }
118-
119- Cocos2dxGLSurfaceView .queueAccelerometer (x ,y ,z ,sensorEvent .timestamp );
120-
121- /*
122- if(BuildConfig.DEBUG) {
123- Log.d(TAG, "x = " + sensorEvent.values[0] + " y = " + sensorEvent.values[1] + " z = " + pSensorEvent.values[2]);
139+ else if (sensorEvent .sensor .getType () == Sensor .TYPE_MAGNETIC_FIELD ) {
140+ this .magneticFieldValues [0 ] = sensorEvent .values [0 ];
141+ this .magneticFieldValues [1 ] = sensorEvent .values [1 ];
142+ this .magneticFieldValues [2 ] = sensorEvent .values [2 ];
124143 }
125- */
126144 }
127145
128146 @ Override
0 commit comments