mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-19 20:19:13 +12:00
Implement support to sensors (#618)
* Implement sensors * Fix memory leak in accel
This commit is contained in:
parent
cdc61ea95a
commit
8cf0fbef1b
7 changed files with 169 additions and 23 deletions
|
@ -24,7 +24,9 @@ public class AlberDriver {
|
|||
public static native void KeyUp(int code);
|
||||
public static native void SetCirclepadAxis(int x, int y);
|
||||
public static native void TouchScreenUp();
|
||||
public static native void TouchScreenDown(int x, int y);
|
||||
public static native void TouchScreenDown(int x, int y);;
|
||||
public static native void SetGyro(float roll, float pitch, float yaw);
|
||||
public static native void SetAccel(float x, float y, float z);
|
||||
public static native void Pause();
|
||||
public static native void Resume();
|
||||
public static native void LoadLuaScript(String script);
|
||||
|
|
|
@ -3,11 +3,21 @@ package com.panda3ds.pandroid.app;
|
|||
import android.app.ActivityManager;
|
||||
import android.app.PictureInPictureParams;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Configuration;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
import android.opengl.Matrix;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.renderscript.Matrix3f;
|
||||
import android.renderscript.Matrix4f;
|
||||
import android.util.Log;
|
||||
import android.util.Rational;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.Surface;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.WindowManager;
|
||||
|
@ -25,6 +35,7 @@ import com.panda3ds.pandroid.app.game.EmulatorCallback;
|
|||
import com.panda3ds.pandroid.data.config.GlobalConfig;
|
||||
import com.panda3ds.pandroid.input.InputHandler;
|
||||
import com.panda3ds.pandroid.input.InputMap;
|
||||
import com.panda3ds.pandroid.math.Vector3;
|
||||
import com.panda3ds.pandroid.utils.Constants;
|
||||
import com.panda3ds.pandroid.view.PandaGlSurfaceView;
|
||||
import com.panda3ds.pandroid.view.PandaLayoutController;
|
||||
|
@ -32,7 +43,7 @@ import com.panda3ds.pandroid.view.ds.DsLayoutManager;
|
|||
import com.panda3ds.pandroid.view.renderer.ConsoleRenderer;
|
||||
import com.panda3ds.pandroid.view.utils.PerformanceView;
|
||||
|
||||
public class GameActivity extends BaseActivity implements EmulatorCallback {
|
||||
public class GameActivity extends BaseActivity implements EmulatorCallback, SensorEventListener {
|
||||
private final DrawerFragment drawerFragment = new DrawerFragment();
|
||||
private final AlberInputListener inputListener = new AlberInputListener(this);
|
||||
private ConsoleRenderer renderer;
|
||||
|
@ -74,6 +85,19 @@ public class GameActivity extends BaseActivity implements EmulatorCallback {
|
|||
((FrameLayout) findViewById(R.id.panda_gl_frame)).addView(view, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||
}
|
||||
swapScreens(GlobalConfig.get(GlobalConfig.KEY_CURRENT_DS_LAYOUT));
|
||||
registerSensors();
|
||||
}
|
||||
|
||||
private void registerSensors() {
|
||||
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
|
||||
Sensor accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
if (accel != null) {
|
||||
sensorManager.registerListener(this, accel, 1);
|
||||
}
|
||||
Sensor gryro = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
|
||||
if (gryro != null) {
|
||||
sensorManager.registerListener(this, gryro, 1);
|
||||
}
|
||||
}
|
||||
|
||||
private void changeOverlayVisibility(boolean visible) {
|
||||
|
@ -94,6 +118,7 @@ public class GameActivity extends BaseActivity implements EmulatorCallback {
|
|||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
|
||||
getTheme().applyStyle(R.style.GameActivityNavigationBar, true);
|
||||
}
|
||||
registerSensors();
|
||||
}
|
||||
|
||||
private void enablePIP() {
|
||||
|
@ -113,6 +138,7 @@ public class GameActivity extends BaseActivity implements EmulatorCallback {
|
|||
protected void onPause() {
|
||||
super.onPause();
|
||||
|
||||
((SensorManager)getSystemService(SENSOR_SERVICE)).unregisterListener(this);
|
||||
InputHandler.reset();
|
||||
if (GlobalConfig.get(GlobalConfig.KEY_PICTURE_IN_PICTURE)) {
|
||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
|
||||
|
@ -174,10 +200,45 @@ public class GameActivity extends BaseActivity implements EmulatorCallback {
|
|||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
((SensorManager)getSystemService(SENSOR_SERVICE)).unregisterListener(this);
|
||||
if (AlberDriver.HasRomLoaded()) {
|
||||
AlberDriver.Finalize();
|
||||
}
|
||||
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
private float getDeviceRotationAngle() {
|
||||
int rotation = getWindow().getDecorView().getDisplay().getRotation();
|
||||
switch (rotation) {
|
||||
case Surface.ROTATION_90: return 90.0f;
|
||||
case Surface.ROTATION_180: return 180.0f;
|
||||
case Surface.ROTATION_270: return -90.0f;
|
||||
default: return 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
if (AlberDriver.HasRomLoaded()) {
|
||||
Sensor sensor = event.sensor;
|
||||
switch (sensor.getType()) {
|
||||
case Sensor.TYPE_ACCELEROMETER: {
|
||||
float[] values = event.values;
|
||||
Vector3 vec3 = new Vector3(values[0], values[1], values[2]);
|
||||
vec3.rotateByEuler(new Vector3(0, 0, (float) (getDeviceRotationAngle() * (Math.PI / 180.0f))));
|
||||
AlberDriver.SetAccel(vec3.x, vec3.y, vec3.z);
|
||||
} break;
|
||||
case Sensor.TYPE_GYROSCOPE: {
|
||||
float[] values = event.values;
|
||||
Vector3 vec3 = new Vector3(values[0], values[1], values[2]);
|
||||
vec3.rotateByEuler(new Vector3(0, 0, (float) (getDeviceRotationAngle() * (Math.PI / 180.0f))));
|
||||
AlberDriver.SetGyro(vec3.x, vec3.y, vec3.z);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
|
||||
}
|
||||
|
|
|
@ -95,7 +95,7 @@ public class AppDataDocumentProvider extends DocumentsProvider {
|
|||
private void includeFile(MatrixCursor cursor, File file) {
|
||||
int flags = 0;
|
||||
if (file.isDirectory()) {
|
||||
flags = Document.FLAG_DIR_SUPPORTS_CREATE;
|
||||
flags = Document.FLAG_DIR_SUPPORTS_CREATE | Document.FLAG_SUPPORTS_DELETE;
|
||||
} else {
|
||||
flags = Document.FLAG_SUPPORTS_WRITE | Document.FLAG_SUPPORTS_REMOVE | Document.FLAG_SUPPORTS_DELETE;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.panda3ds.pandroid.math;
|
||||
|
||||
public class Quaternion {
|
||||
public float x, y, z, w;
|
||||
public Quaternion(float x, float y, float z, float w) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
|
||||
public Quaternion fromEuler(Vector3 euler) {
|
||||
float x = euler.x;
|
||||
float y = euler.y;
|
||||
float z = euler.z;
|
||||
|
||||
double c1 = Math.cos(x / 2.0);
|
||||
double c2 = Math.cos(y / 2.0);
|
||||
double c3 = Math.cos(z / 2.0);
|
||||
|
||||
double s1 = Math.sin(x / 2.0);
|
||||
double s2 = Math.sin(y / 2.0);
|
||||
double s3 = Math.sin(z / 2.0);
|
||||
|
||||
this.x = (float) (s1 * c2 * c3 + c1 * s2 * s3);
|
||||
this.y = (float) (c1 * s2 * c3 - s1 * c2 * s3);
|
||||
this.z = (float) (c1 * c2 * s3 + s1 * s2 * c3);
|
||||
this.w = (float) (c1 * c2 * c3 - s1 * s2 * s3);
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.panda3ds.pandroid.math;
|
||||
|
||||
public class Vector3 {
|
||||
private final Quaternion quaternion = new Quaternion(0, 0, 0, 0);
|
||||
public float x, y, z;
|
||||
|
||||
public Vector3(float x, float y, float z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public Vector3 rotateByEuler(Vector3 euler) {
|
||||
this.quaternion.fromEuler(euler);
|
||||
|
||||
float x = this.x, y = this.y, z = this.z;
|
||||
float qx = this.quaternion.x;
|
||||
float qy = this.quaternion.y;
|
||||
float qz = this.quaternion.z;
|
||||
float qw = this.quaternion.w;
|
||||
|
||||
float ix = qw * x + qy * z - qz * y;
|
||||
float iy = qw * y + qz * x - qx * z;
|
||||
float iz = qw * z + qx * y - qy * x;
|
||||
float iw = -qx * x - qy * qz * z;
|
||||
|
||||
this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
|
||||
this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
|
||||
this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
|
||||
return this;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue