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

50 lines
1.6 KiB
C#

using Cryville.Common.Logging;
using Cryville.Common.Reflection;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Cryville.Common.Unity.Input {
public class InputManager {
static readonly HashSet<Type> HandlerRegistries = new HashSet<Type> {
typeof(WindowsPointerHandler),
typeof(AndroidTouchHandler),
typeof(UnityKeyHandler<UnityKeyboardReceiver>),
typeof(UnityKeyHandler<UnityMouseButtonReceiver>),
typeof(UnityMouseHandler),
typeof(UnityTouchHandler),
};
readonly HashSet<InputHandler> _handlers = new HashSet<InputHandler>();
readonly Dictionary<Type, InputHandler> _typemap = new Dictionary<Type, InputHandler>();
public InputManager() {
foreach (var t in HandlerRegistries) {
try {
if (!typeof(InputHandler).IsAssignableFrom(t)) continue;
var h = (InputHandler)Activator.CreateInstance(t);
_typemap.Add(t, h);
_handlers.Add(h);
Logger.Log("main", 1, "Input", "Initialized {0}", TypeNameHelper.GetSimpleName(t));
}
catch (TargetInvocationException ex) {
Logger.Log("main", 1, "Input", "Cannot initialize {0}: {1}", TypeNameHelper.GetSimpleName(t), ex.InnerException.Message);
}
}
}
public InputHandler GetHandler(string name) {
return _typemap[Type.GetType(name)];
}
public void EnumerateHandlers(Action<InputHandler> cb) {
foreach (var h in _handlers) cb(h);
}
}
public struct InputEvent {
public InputIdentifier Id { get; set; }
public InputVector From { get; set; }
public InputVector To { get; set; }
public override string ToString() {
return string.Format("[{0}] {1} -> {2}", Id, From, To);
}
}
}