Files
crtr/Assets/Cryville/Common/Unity/Input/UnityMouseHandler.cs

65 lines
1.5 KiB
C#

using System;
using UnityEngine;
using unity = UnityEngine;
namespace Cryville.Common.Unity.Input {
public class UnityMouseHandler : InputHandler {
GameObject receiver;
public UnityMouseHandler() {
if (!unity::Input.mousePresent) {
throw new NotSupportedException("Unity mouse is not supported on this device");
}
}
public override void Activate() {
receiver = new GameObject("__mouserecv__");
receiver.AddComponent<UnityMouseReceiver>().SetHandler(this);
}
public override void Deactivate() {
if (receiver) GameObject.Destroy(receiver);
}
public override void Dispose(bool disposing) {
if (disposing) {
Deactivate();
}
}
public override bool IsNullable(int type) {
if (type != 0) throw new ArgumentOutOfRangeException(nameof(type));
return false;
}
public override byte GetDimension(int type) {
if (type != 0) throw new ArgumentOutOfRangeException(nameof(type));
return 2;
}
public override string GetTypeName(int type) {
switch (type) {
case 0: return "Mouse Position";
default: throw new ArgumentOutOfRangeException(nameof(type));
}
}
public override double GetCurrentTimestamp() {
return Time.timeAsDouble;
}
public class UnityMouseReceiver : MonoBehaviour {
UnityMouseHandler handler;
public void SetHandler(UnityMouseHandler h) {
handler = h;
}
void Update() {
double time = Time.timeAsDouble;
Vector2 pos = unity::Input.mousePosition;
pos.y = Screen.height - pos.y;
handler.OnInput(0, 0, new InputVector(time, pos));
}
}
}
}