Rearrange platform specific plugins.

This commit is contained in:
2023-09-25 10:52:16 +08:00
parent 84acb2c12d
commit 825818679c
27 changed files with 30 additions and 6 deletions

View File

@@ -0,0 +1,6 @@
package world.cryville.input.unity.android;
final class NativeMethods {
private NativeMethods() { }
public static native void feed(int hid, int id, int action, long time, float x, float y, float z, float t);
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: 1796f226463344b48bb9789967b38eda
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,28 @@
package world.cryville.input.unity.android;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
import world.cryville.input.unity.android.NativeMethods;
public abstract class Proxy {
protected static UnityPlayerActivity activity;
static int _count;
int _id;
public Proxy() {
if (activity == null) activity = (UnityPlayerActivity)UnityPlayer.currentActivity;
_id = _count++;
}
public int getId() { return _id; }
public abstract void activate();
public abstract void deactivate();
protected void feed(int id, int action, long time) { NativeMethods.feed(_id, id, action, time, 0, 0, 0, 0); }
protected void feed(int id, int action, long time, float x) { NativeMethods.feed(_id, id, action, time, x, 0, 0, 0); }
protected void feed(int id, int action, long time, float x, float y) { NativeMethods.feed(_id, id, action, time, x, y, 0, 0); }
protected void feed(int id, int action, long time, float x, float y, float z) { NativeMethods.feed(_id, id, action, time, x, y, z, 0); }
protected void feed(int id, int action, long time, float x, float y, float z, float w) { NativeMethods.feed(_id, id, action, time, x, y, z, w); }
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: b60db87d680aebd4c8d9071ff17d3a56
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,104 @@
package world.cryville.input.unity.android;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import java.lang.IllegalStateException;
import java.lang.IndexOutOfBoundsException;
import java.util.HashMap;
import java.util.List;
import world.cryville.input.unity.android.Proxy;
public abstract class SensorProxy extends Proxy implements SensorEventListener {
static SensorManager manager;
int dimension;
HashMap<Sensor, Integer> sensors;
List<Sensor> candidateSensors;
public SensorProxy(int type, int dim) throws IllegalStateException, IndexOutOfBoundsException {
if (manager == null) manager = (SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);
dimension = dim;
if (dimension < 0 || dimension > 4) throw new IndexOutOfBoundsException("Invalid dimension");
sensors = new HashMap<Sensor, Integer>();
candidateSensors = manager.getSensorList(type);
if (candidateSensors.size() == 0) throw new IllegalStateException("Sensor not found");
}
@Override
public void activate() {
int workingSensorCount = 0;
for (Sensor sensor : candidateSensors) {
if (manager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST)) {
sensors.put(sensor, workingSensorCount++);
}
}
}
@Override
public void deactivate() {
manager.unregisterListener(this);
sensors.clear();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { }
@Override
public void onSensorChanged(SensorEvent event) {
Integer id = sensors.get(event.sensor);
if (id == null) return;
float[] v = event.values;
switch (dimension) {
case 0: feed(0, id, event.timestamp); break;
case 1: feed(0, id, event.timestamp, v[0]); break;
case 2: feed(0, id, event.timestamp, v[0], v[1]); break;
case 3: feed(0, id, event.timestamp, v[0], v[1], v[2]); break;
case 4: feed(0, id, event.timestamp, v[0], v[1], v[2], v[3]); break;
}
}
public static class Accelerometer extends SensorProxy {
public Accelerometer() { super(Sensor.TYPE_ACCELEROMETER, 3); }
}
public static class AccelerometerUncalibrated extends SensorProxy {
public AccelerometerUncalibrated() { super(Sensor.TYPE_ACCELEROMETER_UNCALIBRATED, 3); }
}
public static class GameRotationVector extends SensorProxy {
public GameRotationVector() { super(Sensor.TYPE_GAME_ROTATION_VECTOR, 4); }
}
public static class Gravity extends SensorProxy {
public Gravity() { super(Sensor.TYPE_GRAVITY, 3); }
}
public static class Gyroscope extends SensorProxy {
public Gyroscope() { super(Sensor.TYPE_GYROSCOPE, 3); }
}
public static class GyroscopeUncalibrated extends SensorProxy {
public GyroscopeUncalibrated() { super(Sensor.TYPE_GYROSCOPE_UNCALIBRATED, 3); }
}
public static class LinearAcceleration extends SensorProxy {
public LinearAcceleration() { super(Sensor.TYPE_LINEAR_ACCELERATION, 3); }
}
public static class MagneticField extends SensorProxy {
public MagneticField() { super(Sensor.TYPE_MAGNETIC_FIELD, 3); }
}
public static class MagneticFieldUncalibrated extends SensorProxy {
public MagneticFieldUncalibrated() { super(Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED, 3); }
}
public static class RotationVector extends SensorProxy {
public RotationVector() { super(Sensor.TYPE_ROTATION_VECTOR, 4); }
}
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: c1acbb4fd13ca1249880dbc02ff3c9d4
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,50 @@
package world.cryville.input.unity.android;
import android.view.MotionEvent;
import android.view.View;
import com.unity3d.player.UnityPlayer;
import java.lang.reflect.Field;
import world.cryville.input.unity.android.Proxy;
public final class TouchProxy extends Proxy implements View.OnTouchListener {
public TouchProxy() throws NoSuchFieldException, IllegalAccessException, SecurityException {
Field playerField = activity.getClass().getDeclaredField("mUnityPlayer");
playerField.setAccessible(true);
UnityPlayer player = (UnityPlayer)playerField.get(activity);
player.setOnTouchListener(this);
}
boolean activated;
@Override
public void activate() {
activated = true;
}
@Override
public void deactivate() {
activated = false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (!activated) return false;
int pointerCount = event.getPointerCount();
int action = event.getActionMasked();
int actionIndex = event.getActionIndex();
long time = event.getEventTime();
for (int i = 0; i < pointerCount; i++) {
int id = event.getPointerId(i);
float x = event.getX(i);
float y = event.getY(i);
if (action == 5 || action == 6) {
feed(id, i == actionIndex ? action : -1, time, x, y);
}
else {
feed(id, action, time, x, y);
}
}
feed(0, -2, time);
return false;
}
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: 4bd9aa92ec47fbf4fa63a219cfe7e4ce
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant: