Update Cryville.Input.
This commit is contained in:
@@ -62,15 +62,15 @@ namespace Cryville.Crtr {
|
||||
GameDataPath = Settings.Default.GameDataPath;
|
||||
|
||||
unity::Input.simulateMouseWithTouches = false;
|
||||
//InputManager.HandlerRegistries.Add(typeof(AndroidAccelerometerHandler));
|
||||
//InputManager.HandlerRegistries.Add(typeof(AndroidAccelerometerUncalibratedHandler));
|
||||
//InputManager.HandlerRegistries.Add(typeof(AndroidGameRotationVectorHandler));
|
||||
//InputManager.HandlerRegistries.Add(typeof(AndroidGravityHandler));
|
||||
//InputManager.HandlerRegistries.Add(typeof(AndroidGyroscopeHandler));
|
||||
//InputManager.HandlerRegistries.Add(typeof(AndroidLinearAccelerationHandler));
|
||||
//InputManager.HandlerRegistries.Add(typeof(AndroidMagneticFieldHandler));
|
||||
//InputManager.HandlerRegistries.Add(typeof(AndroidMagneticFieldUncalibratedHandler));
|
||||
//InputManager.HandlerRegistries.Add(typeof(AndroidRotationVectorHandler));
|
||||
InputManager.HandlerRegistries.Add(typeof(AndroidAccelerometerHandler));
|
||||
InputManager.HandlerRegistries.Add(typeof(AndroidAccelerometerUncalibratedHandler));
|
||||
InputManager.HandlerRegistries.Add(typeof(AndroidGameRotationVectorHandler));
|
||||
InputManager.HandlerRegistries.Add(typeof(AndroidGravityHandler));
|
||||
InputManager.HandlerRegistries.Add(typeof(AndroidGyroscopeHandler));
|
||||
InputManager.HandlerRegistries.Add(typeof(AndroidLinearAccelerationHandler));
|
||||
InputManager.HandlerRegistries.Add(typeof(AndroidMagneticFieldHandler));
|
||||
InputManager.HandlerRegistries.Add(typeof(AndroidMagneticFieldUncalibratedHandler));
|
||||
InputManager.HandlerRegistries.Add(typeof(AndroidRotationVectorHandler));
|
||||
InputManager.HandlerRegistries.Add(typeof(AndroidTouchHandler));
|
||||
InputManager.HandlerRegistries.Add(typeof(UnityGuiInputHandler<UnityKeyReceiver>));
|
||||
InputManager.HandlerRegistries.Add(typeof(UnityGuiInputHandler<UnityMouseReceiver>));
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,63 +3,97 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cryville.Input.Unity {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Unity GUI input.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The GUI event receiver type.</typeparam>
|
||||
public class UnityGuiInputHandler<T> : InputHandler where T : UnityGuiEventReceiver {
|
||||
GameObject _receiver;
|
||||
T _recvComp;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="UnityGuiInputHandler{T}" /> class.
|
||||
/// </summary>
|
||||
public UnityGuiInputHandler() { }
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Activate() {
|
||||
_receiver = new GameObject("__guiRecv__");
|
||||
_recvComp = _receiver.AddComponent<T>();
|
||||
_recvComp.SetCallback(Feed);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Deactivate() {
|
||||
if (_receiver) GameObject.Destroy(_receiver);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Dispose(bool disposing) {
|
||||
if (disposing) {
|
||||
Deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsNullable { get { return true; } }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override byte Dimension { get { return 0; } }
|
||||
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue { };
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue { };
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string GetTypeName(int type) {
|
||||
return _recvComp.GetKeyName(type);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override double GetCurrentTimestamp() {
|
||||
return Time.realtimeSinceStartupAsDouble;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unity GUI event receiver.
|
||||
/// </summary>
|
||||
public abstract class UnityGuiEventReceiver : MonoBehaviour {
|
||||
/// <summary>
|
||||
/// The callback function to be called when a new input frame is received.
|
||||
/// </summary>
|
||||
protected Action<int, int, InputFrame> Callback;
|
||||
protected readonly HashSet<int> Keys = new HashSet<int>();
|
||||
/// <summary>
|
||||
/// The set of currently active keys.
|
||||
/// </summary>
|
||||
protected readonly HashSet<int> ActiveKeys = new HashSet<int>();
|
||||
/// <summary>
|
||||
/// Sets the callback function to be called when a new input frame is received.
|
||||
/// </summary>
|
||||
/// <param name="h">The callback function to be called when a new input frame is received.</param>
|
||||
public void SetCallback(Action<int, int, InputFrame> h) {
|
||||
Callback = h;
|
||||
}
|
||||
public abstract string GetKeyName(int type);
|
||||
/// <summary>
|
||||
/// Gets the friendly name of the specified key.
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <returns>The friendly name of the specified key.</returns>
|
||||
public abstract string GetKeyName(int key);
|
||||
void Awake() {
|
||||
useGUILayout = false;
|
||||
}
|
||||
void Update() {
|
||||
double time = Time.realtimeSinceStartupAsDouble;
|
||||
foreach (var k in Keys) {
|
||||
foreach (var k in ActiveKeys) {
|
||||
Callback(k, 0, new InputFrame(time, new InputVector()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class UnityKeyReceiver : UnityGuiEventReceiver {
|
||||
/// <inheritdoc />
|
||||
public override string GetKeyName(int type) {
|
||||
return Enum.GetName(typeof(KeyCode), type);
|
||||
}
|
||||
@@ -70,13 +104,13 @@ namespace Cryville.Input.Unity {
|
||||
var key = (int)e.keyCode;
|
||||
switch (e.type) {
|
||||
case EventType.KeyDown:
|
||||
if (!Keys.Contains(key)) {
|
||||
if (!ActiveKeys.Contains(key)) {
|
||||
Callback(key, 0, new InputFrame(time, new InputVector()));
|
||||
Keys.Add(key);
|
||||
ActiveKeys.Add(key);
|
||||
}
|
||||
break;
|
||||
case EventType.KeyUp:
|
||||
Keys.Remove(key);
|
||||
ActiveKeys.Remove(key);
|
||||
Callback(key, 0, new InputFrame(time));
|
||||
break;
|
||||
}
|
||||
@@ -84,6 +118,7 @@ namespace Cryville.Input.Unity {
|
||||
}
|
||||
|
||||
public class UnityMouseReceiver : UnityGuiEventReceiver {
|
||||
/// <inheritdoc />
|
||||
public override string GetKeyName(int type) {
|
||||
switch (type) {
|
||||
case 0: return "Mouse Left Button";
|
||||
@@ -98,13 +133,13 @@ namespace Cryville.Input.Unity {
|
||||
var key = e.button;
|
||||
switch (e.type) {
|
||||
case EventType.MouseDown:
|
||||
if (!Keys.Contains(key)) {
|
||||
if (!ActiveKeys.Contains(key)) {
|
||||
Callback(key, 0, new InputFrame(time, new InputVector()));
|
||||
Keys.Add(key);
|
||||
ActiveKeys.Add(key);
|
||||
}
|
||||
break;
|
||||
case EventType.MouseUp:
|
||||
Keys.Remove(key);
|
||||
ActiveKeys.Remove(key);
|
||||
Callback(key, 0, new InputFrame(time));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -3,40 +3,54 @@ using UnityEngine;
|
||||
using unity = UnityEngine;
|
||||
|
||||
namespace Cryville.Input.Unity {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Unity mouse input.
|
||||
/// </summary>
|
||||
public class UnityMouseHandler : InputHandler {
|
||||
GameObject _receiver;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="UnityMouseHandler" /> class.
|
||||
/// </summary>
|
||||
/// <exception cref="NotSupportedException">Unity mouse is not supported on the current device.</exception>
|
||||
public UnityMouseHandler() {
|
||||
if (!unity::Input.mousePresent) {
|
||||
throw new NotSupportedException("Unity mouse is not supported on this device");
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Activate() {
|
||||
_receiver = new GameObject("__mouseRecv__");
|
||||
_receiver.AddComponent<UnityMouseReceiver>().SetHandler(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Deactivate() {
|
||||
if (_receiver) GameObject.Destroy(_receiver);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Dispose(bool disposing) {
|
||||
if (disposing) {
|
||||
Deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsNullable { get { return false; } }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override byte Dimension { get { return 2; } }
|
||||
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue {
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue {
|
||||
PhysicalDimension = new PhysicalDimension { Length = 1 },
|
||||
RelativeUnit = RelativeUnit.Pixel,
|
||||
};
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string GetTypeName(int type) {
|
||||
switch (type) {
|
||||
case 0: return "Mouse Position";
|
||||
@@ -44,12 +58,20 @@ namespace Cryville.Input.Unity {
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override double GetCurrentTimestamp() {
|
||||
return Time.realtimeSinceStartupAsDouble;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unity mouse receiver.
|
||||
/// </summary>
|
||||
public class UnityMouseReceiver : MonoBehaviour {
|
||||
UnityMouseHandler _handler;
|
||||
/// <summary>
|
||||
/// Sets the <see cref="UnityMouseHandler" />.
|
||||
/// </summary>
|
||||
/// <param name="h">The <see cref="UnityMouseHandler" />.</param>
|
||||
public void SetHandler(UnityMouseHandler h) {
|
||||
_handler = h;
|
||||
}
|
||||
|
||||
@@ -3,40 +3,54 @@ using UnityEngine;
|
||||
using unity = UnityEngine;
|
||||
|
||||
namespace Cryville.Input.Unity {
|
||||
/// <summary>
|
||||
/// An <see cref="InputHandler" /> that handles Unity touch input.
|
||||
/// </summary>
|
||||
public class UnityTouchHandler : InputHandler {
|
||||
GameObject _receiver;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instance of the <see cref="UnityTouchHandler" /> class.
|
||||
/// </summary>
|
||||
/// <exception cref="NotSupportedException">Unity touch is not supported on the current device.</exception>
|
||||
public UnityTouchHandler() {
|
||||
if (!unity::Input.touchSupported) {
|
||||
throw new NotSupportedException("Unity touch is not supported on this device");
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Activate() {
|
||||
_receiver = new GameObject("__touchRecv__");
|
||||
_receiver.AddComponent<UnityPointerReceiver>().SetHandler(this);
|
||||
_receiver.AddComponent<UnityTouchReceiver>().SetHandler(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Deactivate() {
|
||||
if (_receiver) GameObject.Destroy(_receiver);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Dispose(bool disposing) {
|
||||
if (disposing) {
|
||||
Deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsNullable { get { return true; } }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override byte Dimension { get { return 2; } }
|
||||
|
||||
readonly static ReferenceCue _refCue = new ReferenceCue {
|
||||
static readonly ReferenceCue _refCue = new ReferenceCue {
|
||||
PhysicalDimension = new PhysicalDimension { Length = 1 },
|
||||
RelativeUnit = RelativeUnit.Pixel,
|
||||
};
|
||||
/// <inheritdoc />
|
||||
public override ReferenceCue ReferenceCue => _refCue;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string GetTypeName(int type) {
|
||||
switch (type) {
|
||||
case 0: return "Touch";
|
||||
@@ -44,12 +58,20 @@ namespace Cryville.Input.Unity {
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override double GetCurrentTimestamp() {
|
||||
return Time.realtimeSinceStartupAsDouble;
|
||||
}
|
||||
|
||||
public class UnityPointerReceiver : MonoBehaviour {
|
||||
/// <summary>
|
||||
/// Unity touch receiver.
|
||||
/// </summary>
|
||||
public class UnityTouchReceiver : MonoBehaviour {
|
||||
UnityTouchHandler _handler;
|
||||
/// <summary>
|
||||
/// Sets the <see cref="UnityTouchHandler" />.
|
||||
/// </summary>
|
||||
/// <param name="h">The <see cref="UnityTouchHandler" />.</param>
|
||||
public void SetHandler(UnityTouchHandler h) {
|
||||
_handler = h;
|
||||
}
|
||||
|
||||
Binary file not shown.
513
Assets/Plugins/Cryville.Input.eng-latn.xml
Normal file
513
Assets/Plugins/Cryville.Input.eng-latn.xml
Normal file
File diff suppressed because it is too large
Load Diff
7
Assets/Plugins/Cryville.Input.eng-latn.xml.meta
Normal file
7
Assets/Plugins/Cryville.Input.eng-latn.xml.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 26d2baa850dc4bb4db5489359d92b61b
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
File diff suppressed because it is too large
Load Diff
@@ -244,7 +244,7 @@ PlayerSettings:
|
||||
clonedFromGUID: 00000000000000000000000000000000
|
||||
templatePackageId:
|
||||
templateDefaultScene:
|
||||
useCustomMainManifest: 0
|
||||
useCustomMainManifest: 1
|
||||
useCustomLauncherManifest: 0
|
||||
useCustomMainGradleTemplate: 0
|
||||
useCustomLauncherGradleManifest: 0
|
||||
|
||||
Reference in New Issue
Block a user