Update Cryville.Input.

This commit is contained in:
2023-11-20 20:23:33 +08:00
parent 30e9f091d7
commit 4f12e4816f
8 changed files with 58 additions and 79 deletions

View File

@@ -5,7 +5,13 @@ namespace Cryville.Input.Unity.Android {
/// <summary>
/// An <see cref="InputHandler" /> that handles Android input.
/// </summary>
public abstract class AndroidInputHandler : InputHandler {
/// <typeparam name="TSelf">The type that inherits this class.</typeparam>
public abstract class AndroidInputHandler<TSelf> : InputHandler where TSelf : AndroidInputHandler<TSelf> {
/// <summary>
/// The instance of this class.
/// </summary>
protected static TSelf Instance { get; private set; }
readonly IntPtr _t_T;
static readonly jvalue[] _p_void = new jvalue[0];
readonly IntPtr _i_T;
@@ -22,8 +28,11 @@ namespace Cryville.Input.Unity.Android {
/// <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();
@@ -40,7 +49,10 @@ namespace Cryville.Input.Unity.Android {
_m_T_activate = AndroidJNI.GetMethodID(_t_T, "activate", "()V");
_m_T_deactivate = AndroidJNI.GetMethodID(_t_T, "deactivate", "()V");
AndroidInputPoller.Instance.Register(AndroidJNI.CallIntMethod(_i_T, _m_T_getId, _p_void), this);
NativeMethods.AndroidInputProxy_RegisterCallback(
AndroidJNI.CallIntMethod(_i_T, _m_T_getId, _p_void),
Callback
);
}
/// <inheritdoc />
@@ -66,6 +78,6 @@ namespace Cryville.Input.Unity.Android {
}
}
internal abstract void OnFeed(int id, int action, long time, float x, float y, float z, float w);
private protected abstract AndroidInputProxy_Callback Callback { get; }
}
}

View File

@@ -1,34 +0,0 @@
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) { IsBackground = true, Priority = ThreadPriority.AboveNormal };
_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);
}
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 9b69004cd86eb0b42bfa2bcf3d1f7e87
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,3 +1,4 @@
using Cryville.Common.Interop;
using Cryville.Common.Logging;
using System;
using System.Text.RegularExpressions;
@@ -6,9 +7,9 @@ namespace Cryville.Input.Unity.Android {
/// <summary>
/// An <see cref="InputHandler" /> that handles Android sensor input.
/// </summary>
public abstract class AndroidSensorHandler : AndroidInputHandler {
public abstract class AndroidSensorHandler<TSelf> : AndroidInputHandler<TSelf> where TSelf : AndroidSensorHandler<TSelf> {
/// <summary>
/// Creates an instance of the <see cref="AndroidSensorHandler{TSelf}" /> class.
/// Creates an instance of the <see cref="AndroidSensorHandler" /> 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>
@@ -38,11 +39,14 @@ namespace Cryville.Input.Unity.Android {
return JavaStaticMethods.SystemClock_elapsedRealtimeNanos() / 1e9;
}
internal override void OnFeed(int id, int action, long time, float x, float y, float z, float w) {
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) {
try {
double timeSecs = time / 1e9;
Feed(0, id, new InputFrame(timeSecs, new InputVector(x, y, z, w)));
Batch(timeSecs);
Instance.Feed(0, id, new InputFrame(timeSecs, new InputVector(x, y, z, w)));
Instance.Batch(timeSecs);
}
catch (Exception ex) {
Shared.Logger.Log(4, "Input", "An error occurred while handling an Android sensor event: {0}", ex);
@@ -53,7 +57,7 @@ namespace Cryville.Input.Unity.Android {
/// <summary>
/// An <see cref="InputHandler" /> that handles Android accelerometer sensor input.
/// </summary>
public class AndroidAccelerometerHandler : AndroidSensorHandler {
public class AndroidAccelerometerHandler : AndroidSensorHandler<AndroidAccelerometerHandler> {
/// <summary>
/// Creates an instance of the <see cref="AndroidAccelerometerHandler" /> class.
/// </summary>
@@ -67,7 +71,7 @@ namespace Cryville.Input.Unity.Android {
/// <summary>
/// An <see cref="InputHandler" /> that handles Android accelerometer (uncalibrated) sensor input.
/// </summary>
public class AndroidAccelerometerUncalibratedHandler : AndroidSensorHandler {
public class AndroidAccelerometerUncalibratedHandler : AndroidSensorHandler<AndroidAccelerometerUncalibratedHandler> {
/// <summary>
/// Creates an instance of the <see cref="AndroidAccelerometerUncalibratedHandler" /> class.
/// </summary>
@@ -81,7 +85,7 @@ namespace Cryville.Input.Unity.Android {
/// <summary>
/// An <see cref="InputHandler" /> that handles Android game rotation vector sensor input.
/// </summary>
public class AndroidGameRotationVectorHandler : AndroidSensorHandler {
public class AndroidGameRotationVectorHandler : AndroidSensorHandler<AndroidGameRotationVectorHandler> {
/// <summary>
/// Creates an instance of the <see cref="AndroidGameRotationVectorHandler" /> class.
/// </summary>
@@ -95,7 +99,7 @@ namespace Cryville.Input.Unity.Android {
/// <summary>
/// An <see cref="InputHandler" /> that handles Android gravity sensor input.
/// </summary>
public class AndroidGravityHandler : AndroidSensorHandler {
public class AndroidGravityHandler : AndroidSensorHandler<AndroidGravityHandler> {
/// <summary>
/// Creates an instance of the <see cref="AndroidGravityHandler" /> class.
/// </summary>
@@ -109,7 +113,7 @@ namespace Cryville.Input.Unity.Android {
/// <summary>
/// An <see cref="InputHandler" /> that handles Android gyroscope sensor input.
/// </summary>
public class AndroidGyroscopeHandler : AndroidSensorHandler {
public class AndroidGyroscopeHandler : AndroidSensorHandler<AndroidGyroscopeHandler> {
/// <summary>
/// Creates an instance of the <see cref="AndroidGyroscopeHandler" /> class.
/// </summary>
@@ -121,9 +125,23 @@ namespace Cryville.Input.Unity.Android {
public override ReferenceCue ReferenceCue => _refCue;
}
/// <summary>
/// An <see cref="InputHandler" /> that handles Android gyroscope (uncalibrated) sensor input.
/// </summary>
public class AndroidGyroscopeUncalibratedHandler : AndroidSensorHandler<AndroidGyroscopeUncalibratedHandler> {
/// <summary>
/// Creates an instance of the <see cref="AndroidGyroscopeUncalibratedHandler" /> class.
/// </summary>
public AndroidGyroscopeUncalibratedHandler() : base("GyroscopeUncalibrated", 3) { }
static readonly ReferenceCue _refCue = new ReferenceCue {
PhysicalDimension = new PhysicalDimension { Time = -1 },
};
/// <inheritdoc />
public override ReferenceCue ReferenceCue => _refCue;
}
/// <summary>
/// An <see cref="InputHandler" /> that handles Android linear acceleration sensor input.
/// </summary>
public class AndroidLinearAccelerationHandler : AndroidSensorHandler {
public class AndroidLinearAccelerationHandler : AndroidSensorHandler<AndroidLinearAccelerationHandler> {
/// <summary>
/// Creates an instance of the <see cref="AndroidLinearAccelerationHandler" /> class.
/// </summary>
@@ -137,7 +155,7 @@ namespace Cryville.Input.Unity.Android {
/// <summary>
/// An <see cref="InputHandler" /> that handles Android magnetic field sensor input.
/// </summary>
public class AndroidMagneticFieldHandler : AndroidSensorHandler {
public class AndroidMagneticFieldHandler : AndroidSensorHandler<AndroidMagneticFieldHandler> {
/// <summary>
/// Creates an instance of the <see cref="AndroidMagneticFieldHandler" /> class.
/// </summary>
@@ -151,7 +169,7 @@ namespace Cryville.Input.Unity.Android {
/// <summary>
/// An <see cref="InputHandler" /> that handles Android magnetic field (uncalibrated) sensor input.
/// </summary>
public class AndroidMagneticFieldUncalibratedHandler : AndroidSensorHandler {
public class AndroidMagneticFieldUncalibratedHandler : AndroidSensorHandler<AndroidMagneticFieldUncalibratedHandler> {
/// <summary>
/// Creates an instance of the <see cref="AndroidMagneticFieldUncalibratedHandler" /> class.
/// </summary>
@@ -165,7 +183,7 @@ namespace Cryville.Input.Unity.Android {
/// <summary>
/// An <see cref="InputHandler" /> that handles Android rotation vector sensor input.
/// </summary>
public class AndroidRotationVectorHandler : AndroidSensorHandler {
public class AndroidRotationVectorHandler : AndroidSensorHandler<AndroidRotationVectorHandler> {
/// <summary>
/// Creates an instance of the <see cref="AndroidRotationVectorHandler" /> class.
/// </summary>

View File

@@ -1,3 +1,4 @@
using Cryville.Common.Interop;
using Cryville.Common.Logging;
using System;
@@ -5,7 +6,7 @@ namespace Cryville.Input.Unity.Android {
/// <summary>
/// An <see cref="InputHandler" /> that handles Android touch input.
/// </summary>
public class AndroidTouchHandler : AndroidInputHandler {
public class AndroidTouchHandler : AndroidInputHandler<AndroidTouchHandler> {
/// <summary>
/// Creates an instance of the <see cref="AndroidTouchHandler" /> class.
/// </summary>
@@ -39,16 +40,19 @@ namespace Cryville.Input.Unity.Android {
return JavaStaticMethods.SystemClock_uptimeMillis() / 1000.0;
}
internal override void OnFeed(int id, int action, long time, float x, float y, float z, float w) {
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) {
try {
double timeSecs = time / 1000.0;
if (action == -2) {
Batch(timeSecs);
Instance.Batch(timeSecs);
}
else {
Feed(0, id, new InputFrame(timeSecs, new InputVector(x, y)));
Instance.Feed(0, id, new InputFrame(timeSecs, new InputVector(x, y)));
if (action == 1 /*ACTION_UP*/ || action == 3 /*ACTION_CANCEL*/ || action == 6 /*ACTION_POINTER_UP*/)
Feed(0, id, new InputFrame(timeSecs));
Instance.Feed(0, id, new InputFrame(timeSecs));
}
}
catch (Exception ex) {

View File

@@ -1,19 +1,9 @@
using System.Runtime.InteropServices;
namespace Cryville.Input.Unity.Android {
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 delegate void AndroidInputProxy_Callback(int id, int action, long time, float x, float y, float z, float w);
internal static class NativeMethods {
[DllImport("AndroidInputProxy")]
[PreserveSig]
public static extern int AndroidInputProxy_Poll(out ProxiedInputFrame frame);
public static extern void AndroidInputProxy_RegisterCallback(int hid, AndroidInputProxy_Callback cb);
}
}