Move part of the input module to Cryville.Input.

This commit is contained in:
2023-05-05 00:40:51 +08:00
parent b143fb49ce
commit 0b2ea3ddbc
62 changed files with 1417 additions and 1602 deletions

View File

@@ -0,0 +1,3 @@
{
"name": "Cryville.Input.Unity.Builtin"
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: ae5eee924eae80345b704d2b7de05cc0
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Cryville.Input.Unity {
public class UnityGuiInputHandler<T> : InputHandler where T : UnityGuiEventReceiver {
GameObject _receiver;
T _recvComp;
public UnityGuiInputHandler() { }
protected override void Activate() {
_receiver = new GameObject("__guiRecv__");
_recvComp = _receiver.AddComponent<T>();
_recvComp.SetCallback(Feed);
}
protected override void Deactivate() {
if (_receiver) GameObject.Destroy(_receiver);
}
public override void Dispose(bool disposing) {
if (disposing) {
Deactivate();
}
}
public override bool IsNullable { get { return true; } }
public override byte Dimension { get { return 0; } }
readonly static ReferenceCue _refCue = new ReferenceCue { };
public override ReferenceCue ReferenceCue => _refCue;
public override string GetTypeName(int type) {
return _recvComp.GetKeyName(type);
}
public override double GetCurrentTimestamp() {
return Time.realtimeSinceStartupAsDouble;
}
}
public abstract class UnityGuiEventReceiver : MonoBehaviour {
protected Action<int, int, InputFrame> Callback;
protected readonly HashSet<int> Keys = new HashSet<int>();
public void SetCallback(Action<int, int, InputFrame> h) {
Callback = h;
}
public abstract string GetKeyName(int type);
void Awake() {
useGUILayout = false;
}
void Update() {
double time = Time.realtimeSinceStartupAsDouble;
foreach (var k in Keys) {
Callback(k, 0, new InputFrame(time, new InputVector()));
}
}
}
public class UnityKeyReceiver : UnityGuiEventReceiver {
public override string GetKeyName(int type) {
return Enum.GetName(typeof(KeyCode), type);
}
void OnGUI() {
var e = Event.current;
if (e.keyCode == KeyCode.None) return;
double time = Time.realtimeSinceStartupAsDouble;
var key = (int)e.keyCode;
switch (e.type) {
case EventType.KeyDown:
if (!Keys.Contains(key)) {
Callback(key, 0, new InputFrame(time, new InputVector()));
Keys.Add(key);
}
break;
case EventType.KeyUp:
Keys.Remove(key);
Callback(key, 0, new InputFrame(time));
break;
}
}
}
public class UnityMouseReceiver : UnityGuiEventReceiver {
public override string GetKeyName(int type) {
switch (type) {
case 0: return "Mouse Left Button";
case 1: return "Mouse Right Button";
case 2: return "Mouse Middle Button";
default: return string.Format("Mouse Button {0}", type);
}
}
void OnGUI() {
var e = Event.current;
double time = Time.realtimeSinceStartupAsDouble;
var key = e.button;
switch (e.type) {
case EventType.MouseDown:
if (!Keys.Contains(key)) {
Callback(key, 0, new InputFrame(time, new InputVector()));
Keys.Add(key);
}
break;
case EventType.MouseUp:
Keys.Remove(key);
Callback(key, 0, new InputFrame(time));
break;
}
}
}
}

View File

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

View File

@@ -0,0 +1,63 @@
using System;
using UnityEngine;
using unity = UnityEngine;
namespace Cryville.Input.Unity {
public class UnityMouseHandler : InputHandler {
GameObject _receiver;
public UnityMouseHandler() {
if (!unity::Input.mousePresent) {
throw new NotSupportedException("Unity mouse is not supported on this device");
}
}
protected override void Activate() {
_receiver = new GameObject("__mouseRecv__");
_receiver.AddComponent<UnityMouseReceiver>().SetHandler(this);
}
protected override void Deactivate() {
if (_receiver) GameObject.Destroy(_receiver);
}
public override void Dispose(bool disposing) {
if (disposing) {
Deactivate();
}
}
public override bool IsNullable { get { return false; } }
public override byte Dimension { get { return 2; } }
readonly static ReferenceCue _refCue = new ReferenceCue {
PhysicalDimension = new PhysicalDimension { Length = 1 },
RelativeUnit = RelativeUnit.Pixel,
};
public override ReferenceCue ReferenceCue => _refCue;
public override string GetTypeName(int type) {
switch (type) {
case 0: return "Mouse Position";
default: throw new ArgumentOutOfRangeException("type");
}
}
public override double GetCurrentTimestamp() {
return Time.realtimeSinceStartupAsDouble;
}
public class UnityMouseReceiver : MonoBehaviour {
UnityMouseHandler _handler;
public void SetHandler(UnityMouseHandler h) {
_handler = h;
}
void Update() {
double time = Time.realtimeSinceStartupAsDouble;
Vector3 pos = unity::Input.mousePosition;
_handler.Feed(0, 0, new InputFrame(time, new InputVector(pos.x, pos.y)));
}
}
}
}

View File

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

View File

@@ -0,0 +1,70 @@
using System;
using UnityEngine;
using unity = UnityEngine;
namespace Cryville.Input.Unity {
public class UnityTouchHandler : InputHandler {
GameObject _receiver;
public UnityTouchHandler() {
if (!unity::Input.touchSupported) {
throw new NotSupportedException("Unity touch is not supported on this device");
}
}
protected override void Activate() {
_receiver = new GameObject("__touchRecv__");
_receiver.AddComponent<UnityPointerReceiver>().SetHandler(this);
}
protected override void Deactivate() {
if (_receiver) GameObject.Destroy(_receiver);
}
public override void Dispose(bool disposing) {
if (disposing) {
Deactivate();
}
}
public override bool IsNullable { get { return true; } }
public override byte Dimension { get { return 2; } }
readonly static ReferenceCue _refCue = new ReferenceCue {
PhysicalDimension = new PhysicalDimension { Length = 1 },
RelativeUnit = RelativeUnit.Pixel,
};
public override ReferenceCue ReferenceCue => _refCue;
public override string GetTypeName(int type) {
switch (type) {
case 0: return "Touch";
default: throw new ArgumentOutOfRangeException("type");
}
}
public override double GetCurrentTimestamp() {
return Time.realtimeSinceStartupAsDouble;
}
public class UnityPointerReceiver : MonoBehaviour {
UnityTouchHandler _handler;
public void SetHandler(UnityTouchHandler h) {
_handler = h;
}
void Update() {
double time = Time.realtimeSinceStartupAsDouble;
for (int i = 0; i < unity::Input.touchCount; i++) {
var t = unity::Input.GetTouch(i);
Vector2 pos = t.position;
var vec = new InputFrame(time, new InputVector(pos.x, pos.y));
_handler.Feed(0, t.fingerId, vec);
if (t.phase == TouchPhase.Ended || t.phase == TouchPhase.Canceled) {
_handler.Feed(0, t.fingerId, new InputFrame(time));
}
}
}
}
}
}

View File

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