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