using System; using System.Collections.Generic; using System.Reflection; namespace Cryville.Common.Unity.Input { public class InputManager { static readonly HashSet HandlerRegistries = new HashSet { typeof(WindowsPointerHandler), typeof(UnityKeyHandler), typeof(UnityKeyHandler), typeof(UnityMouseHandler), typeof(UnityTouchHandler), }; readonly HashSet _handlers = new HashSet(); readonly Dictionary _typemap = new Dictionary(); public InputManager() { foreach (var t in HandlerRegistries) { try { if (!typeof(InputHandler).IsAssignableFrom(t)) continue; var h = (InputHandler)ReflectionHelper.InvokeEmptyConstructor(t); _typemap.Add(t, h); _handlers.Add(h); Logger.Log("main", 1, "Input", "Initialized {0}", ReflectionHelper.GetSimpleName(t)); } catch (TargetInvocationException ex) { Logger.Log("main", 1, "Input", "Cannot initialize {0}: {1}", ReflectionHelper.GetSimpleName(t), ex.InnerException.Message); } } } public InputHandler GetHandler(string name) { return _typemap[Type.GetType(name)]; } public void EnumerateHandlers(Action 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); } } }