Update Cryville.Input.

This commit is contained in:
2023-05-07 13:42:53 +08:00
parent fa9303c0a1
commit bd028c1b72
17 changed files with 1336 additions and 579 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}