Update Cryville.Input.
This commit is contained in:
@@ -2,9 +2,10 @@ using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cryville.Input.Unity.Android {
|
||||
public abstract class AndroidInputHandler<TSelf> : InputHandler where TSelf : AndroidInputHandler<TSelf> {
|
||||
protected static TSelf Instance { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Android input.
|
||||
/// </summary>
|
||||
public abstract class AndroidInputHandler : InputHandler {
|
||||
readonly IntPtr _t_T;
|
||||
static readonly jvalue[] _p_void = new jvalue[0];
|
||||
readonly IntPtr _i_T;
|
||||
@@ -14,12 +15,15 @@ namespace Cryville.Input.Unity.Android {
|
||||
|
||||
bool _activated;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="AndroidInputHandler{TSelf}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="className">The full name of the Java class that performs the low-level jobs.</param>
|
||||
/// <exception cref="InvalidOperationException">An instance of this class have already been created.</exception>
|
||||
/// <exception cref="NotSupportedException">Android input is not supported on the current device.</exception>
|
||||
public AndroidInputHandler(string className) {
|
||||
if (Instance != null)
|
||||
throw new InvalidOperationException("AndroidInputHandler already created");
|
||||
if (Environment.OSVersion.Platform != PlatformID.Unix)
|
||||
throw new NotSupportedException("Android input is not supported on this device");
|
||||
Instance = (TSelf)this;
|
||||
|
||||
JavaStaticMethods.Init();
|
||||
|
||||
@@ -36,24 +40,24 @@ namespace Cryville.Input.Unity.Android {
|
||||
_m_T_activate = AndroidJNI.GetMethodID(_t_T, "activate", "()V");
|
||||
_m_T_deactivate = AndroidJNI.GetMethodID(_t_T, "deactivate", "()V");
|
||||
|
||||
NativeMethods.AndroidInputProxy_RegisterCallback(
|
||||
AndroidJNI.CallIntMethod(_i_T, _m_T_getId, _p_void),
|
||||
Callback
|
||||
);
|
||||
AndroidInputPoller.Instance.Register(AndroidJNI.CallIntMethod(_i_T, _m_T_getId, _p_void), this);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Activate() {
|
||||
if (_activated) return;
|
||||
_activated = true;
|
||||
AndroidJNI.CallVoidMethod(_i_T, _m_T_activate, _p_void);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Deactivate() {
|
||||
if (!_activated) return;
|
||||
_activated = false;
|
||||
AndroidJNI.CallVoidMethod(_i_T, _m_T_deactivate, _p_void);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Dispose(bool disposing) {
|
||||
if (disposing) {
|
||||
Deactivate();
|
||||
@@ -62,6 +66,6 @@ namespace Cryville.Input.Unity.Android {
|
||||
}
|
||||
}
|
||||
|
||||
private protected abstract AndroidInputProxy_Callback Callback { get; }
|
||||
internal abstract void OnFeed(int id, int action, long time, float x, float y, float z, float w);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,34 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cryville.Input.Unity.Android {
|
||||
internal class AndroidInputPoller {
|
||||
static AndroidInputPoller m_instance;
|
||||
public static AndroidInputPoller Instance {
|
||||
get {
|
||||
if (m_instance == null) m_instance = new AndroidInputPoller();
|
||||
return m_instance;
|
||||
}
|
||||
}
|
||||
|
||||
readonly Thread _thread;
|
||||
private AndroidInputPoller() {
|
||||
_thread = new Thread(ThreadLogic);
|
||||
_thread.Start();
|
||||
}
|
||||
|
||||
readonly Dictionary<int, AndroidInputHandler> _handlers = new Dictionary<int, AndroidInputHandler>();
|
||||
public void Register(int id, AndroidInputHandler handler) {
|
||||
_handlers[id] = handler;
|
||||
}
|
||||
|
||||
void ThreadLogic() {
|
||||
while (true) {
|
||||
while (NativeMethods.AndroidInputProxy_Poll(out var frame) == 1) {
|
||||
_handlers[frame.hid].OnFeed(frame.id, frame.action, frame.time, frame.x, frame.y, frame.z, frame.w);
|
||||
}
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b69004cd86eb0b42bfa2bcf3d1f7e87
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -1,21 +1,31 @@
|
||||
using Cryville.Common.Interop;
|
||||
using Cryville.Common.Logging;
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Cryville.Input.Unity.Android {
|
||||
public abstract class AndroidSensorHandler<TSelf> : AndroidInputHandler<AndroidSensorHandler<TSelf>> where TSelf : AndroidSensorHandler<TSelf> {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Android sensor input.
|
||||
/// </summary>
|
||||
public abstract class AndroidSensorHandler : AndroidInputHandler {
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="AndroidSensorHandler{TSelf}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="typeName">The name of the Java class nested in <c>world/cryville/input/unity/android/SensorProxy</c> that performs the low-level jobs.</param>
|
||||
/// <param name="dimension">The dimension.</param>
|
||||
public AndroidSensorHandler(string typeName, byte dimension) : base("world/cryville/input/unity/android/SensorProxy$" + typeName) {
|
||||
m_typeName = Regex.Replace(typeName, @"(?<=[a-z])(?=[A-Z])", " ");
|
||||
m_dimension = dimension;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsNullable => false;
|
||||
|
||||
readonly byte m_dimension;
|
||||
/// <inheritdoc />
|
||||
public override byte Dimension => m_dimension;
|
||||
|
||||
readonly string m_typeName;
|
||||
/// <inheritdoc />
|
||||
public override string GetTypeName(int type) {
|
||||
switch (type) {
|
||||
case 0: return m_typeName;
|
||||
@@ -23,17 +33,15 @@ namespace Cryville.Input.Unity.Android {
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override double GetCurrentTimestamp() {
|
||||
return JavaStaticMethods.SystemClock_elapsedRealtimeNanos() / 1e9;
|
||||
}
|
||||
|
||||
private protected sealed override AndroidInputProxy_Callback Callback { get { return OnFeed; } }
|
||||
|
||||
[MonoPInvokeCallback]
|
||||
static void OnFeed(int id, int action, long time, float x, float y, float z, float w) {
|
||||
internal override void OnFeed(int id, int action, long time, float x, float y, float z, float w) {
|
||||
try {
|
||||
double timeSecs = time / 1e9;
|
||||
Instance.Feed(0, id, new InputFrame(timeSecs, new InputVector(x, y, z, w)));
|
||||
Feed(0, id, new InputFrame(timeSecs, new InputVector(x, y, z, w)));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Logger.Log("main", 4, "Input", "An error occurred while handling an Android sensor event: {0}", ex);
|
||||
@@ -41,67 +49,130 @@ namespace Cryville.Input.Unity.Android {
|
||||
}
|
||||
}
|
||||
|
||||
public class AndroidAccelerometerHandler : AndroidSensorHandler<AndroidAccelerometerHandler> {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Android accelerometer sensor input.
|
||||
/// </summary>
|
||||
public class AndroidAccelerometerHandler : AndroidSensorHandler {
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="AndroidAccelerometerHandler" /> class.
|
||||
/// </summary>
|
||||
public AndroidAccelerometerHandler() : base("Accelerometer", 3) { }
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue {
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue {
|
||||
PhysicalDimension = new PhysicalDimension { Length = 1, Time = -2 },
|
||||
};
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
}
|
||||
public class AndroidAccelerometerUncalibratedHandler : AndroidSensorHandler<AndroidAccelerometerUncalibratedHandler> {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Android accelerometer (uncalibrated) sensor input.
|
||||
/// </summary>
|
||||
public class AndroidAccelerometerUncalibratedHandler : AndroidSensorHandler {
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="AndroidAccelerometerUncalibratedHandler" /> class.
|
||||
/// </summary>
|
||||
public AndroidAccelerometerUncalibratedHandler() : base("AccelerometerUncalibrated", 3) { }
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue {
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue {
|
||||
PhysicalDimension = new PhysicalDimension { Length = 1, Time = -2 },
|
||||
};
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
}
|
||||
public class AndroidGameRotationVectorHandler : AndroidSensorHandler<AndroidGameRotationVectorHandler> {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Android game rotation vector sensor input.
|
||||
/// </summary>
|
||||
public class AndroidGameRotationVectorHandler : AndroidSensorHandler {
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="AndroidGameRotationVectorHandler" /> class.
|
||||
/// </summary>
|
||||
public AndroidGameRotationVectorHandler() : base("GameRotationVector", 4) { }
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue {
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue {
|
||||
PhysicalDimension = new PhysicalDimension(),
|
||||
};
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
}
|
||||
public class AndroidGravityHandler : AndroidSensorHandler<AndroidGravityHandler> {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Android gravity sensor input.
|
||||
/// </summary>
|
||||
public class AndroidGravityHandler : AndroidSensorHandler {
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="AndroidGravityHandler" /> class.
|
||||
/// </summary>
|
||||
public AndroidGravityHandler() : base("Gravity", 3) { }
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue {
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue {
|
||||
PhysicalDimension = new PhysicalDimension { Length = 1, Time = -2 },
|
||||
};
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
}
|
||||
public class AndroidGyroscopeHandler : AndroidSensorHandler<AndroidGyroscopeHandler> {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Android gyroscope sensor input.
|
||||
/// </summary>
|
||||
public class AndroidGyroscopeHandler : AndroidSensorHandler {
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="AndroidGyroscopeHandler" /> class.
|
||||
/// </summary>
|
||||
public AndroidGyroscopeHandler() : base("Gyroscope", 3) { }
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue {
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue {
|
||||
PhysicalDimension = new PhysicalDimension { Time = -1 },
|
||||
};
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
}
|
||||
public class AndroidLinearAccelerationHandler : AndroidSensorHandler<AndroidLinearAccelerationHandler> {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Android linear acceleration sensor input.
|
||||
/// </summary>
|
||||
public class AndroidLinearAccelerationHandler : AndroidSensorHandler {
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="AndroidLinearAccelerationHandler" /> class.
|
||||
/// </summary>
|
||||
public AndroidLinearAccelerationHandler() : base("LinearAcceleration", 3) { }
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue {
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue {
|
||||
PhysicalDimension = new PhysicalDimension { Length = 1, Time = -2 },
|
||||
};
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
}
|
||||
public class AndroidMagneticFieldHandler : AndroidSensorHandler<AndroidMagneticFieldHandler> {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Android magnetic field sensor input.
|
||||
/// </summary>
|
||||
public class AndroidMagneticFieldHandler : AndroidSensorHandler {
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="AndroidMagneticFieldHandler" /> class.
|
||||
/// </summary>
|
||||
public AndroidMagneticFieldHandler() : base("MagneticField", 3) { }
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue {
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue {
|
||||
PhysicalDimension = new PhysicalDimension { Mass = 1, Time = -2, ElectricCurrent = -1 },
|
||||
};
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
}
|
||||
public class AndroidMagneticFieldUncalibratedHandler : AndroidSensorHandler<AndroidMagneticFieldUncalibratedHandler> {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Android magnetic field (uncalibrated) sensor input.
|
||||
/// </summary>
|
||||
public class AndroidMagneticFieldUncalibratedHandler : AndroidSensorHandler {
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="AndroidMagneticFieldUncalibratedHandler" /> class.
|
||||
/// </summary>
|
||||
public AndroidMagneticFieldUncalibratedHandler() : base("MagneticFieldUncalibrated", 3) { }
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue {
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue {
|
||||
PhysicalDimension = new PhysicalDimension { Mass = 1, Time = -2, ElectricCurrent = -1 },
|
||||
};
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
}
|
||||
public class AndroidRotationVectorHandler : AndroidSensorHandler<AndroidRotationVectorHandler> {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Android rotation vector sensor input.
|
||||
/// </summary>
|
||||
public class AndroidRotationVectorHandler : AndroidSensorHandler {
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="AndroidRotationVectorHandler" /> class.
|
||||
/// </summary>
|
||||
public AndroidRotationVectorHandler() : base("RotationVector", 4) { }
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue {
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue {
|
||||
PhysicalDimension = new PhysicalDimension(),
|
||||
};
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
}
|
||||
}
|
||||
|
@@ -1,23 +1,32 @@
|
||||
using Cryville.Common.Interop;
|
||||
using Cryville.Common.Logging;
|
||||
using System;
|
||||
|
||||
namespace Cryville.Input.Unity.Android {
|
||||
public class AndroidTouchHandler : AndroidInputHandler<AndroidTouchHandler> {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Android touch input.
|
||||
/// </summary>
|
||||
public class AndroidTouchHandler : AndroidInputHandler {
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="AndroidTouchHandler" /> class.
|
||||
/// </summary>
|
||||
public AndroidTouchHandler() : base("world/cryville/input/unity/android/TouchProxy") { }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsNullable => true;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override byte Dimension => 2;
|
||||
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue {
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue {
|
||||
PhysicalDimension = new PhysicalDimension { Length = 1 },
|
||||
RelativeUnit = RelativeUnit.Pixel,
|
||||
Flags = ReferenceFlag.FlipY,
|
||||
Pivot = new InputVector(0, 1),
|
||||
};
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string GetTypeName(int type) {
|
||||
switch (type) {
|
||||
case 0: return "Android Touch";
|
||||
@@ -25,19 +34,17 @@ namespace Cryville.Input.Unity.Android {
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override double GetCurrentTimestamp() {
|
||||
return JavaStaticMethods.SystemClock_uptimeMillis() / 1000.0;
|
||||
}
|
||||
|
||||
private protected override AndroidInputProxy_Callback Callback { get { return OnFeed; } }
|
||||
|
||||
[MonoPInvokeCallback]
|
||||
static void OnFeed(int id, int action, long time, float x, float y, float z, float w) {
|
||||
internal override void OnFeed(int id, int action, long time, float x, float y, float z, float w) {
|
||||
try {
|
||||
double timeSecs = time / 1000.0;
|
||||
Instance.Feed(0, id, new InputFrame(timeSecs, new InputVector(x, y)));
|
||||
Feed(0, id, new InputFrame(timeSecs, new InputVector(x, y)));
|
||||
if (action == 1 /*ACTION_UP*/ || action == 3 /*ACTION_CANCEL*/ || action == 6 /*ACTION_POINTER_UP*/)
|
||||
Instance.Feed(0, id, new InputFrame(timeSecs));
|
||||
Feed(0, id, new InputFrame(timeSecs));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Logger.Log("main", 4, "Input", "An error occurred while handling an Android touch event: {0}", ex);
|
||||
|
@@ -1,9 +1,19 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Cryville.Input.Unity.Android {
|
||||
internal delegate void AndroidInputProxy_Callback(int id, int action, long time, float x, float y, float z, float w);
|
||||
struct ProxiedInputFrame {
|
||||
public int hid;
|
||||
public int id;
|
||||
public int action;
|
||||
public long time;
|
||||
public float x;
|
||||
public float y;
|
||||
public float z;
|
||||
public float w;
|
||||
};
|
||||
internal static class NativeMethods {
|
||||
[DllImport("AndroidInputProxy")]
|
||||
public static extern void AndroidInputProxy_RegisterCallback(int hid, AndroidInputProxy_Callback cb);
|
||||
[PreserveSig]
|
||||
public static extern int AndroidInputProxy_Poll(out ProxiedInputFrame frame);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user