Optimize code structure for input module.
This commit is contained in:
@@ -4,7 +4,19 @@ using UnityEngine;
|
||||
namespace Cryville.Common.Unity.Input {
|
||||
public delegate void InputEventDelegate(InputIdentifier id, InputVector vec);
|
||||
public abstract class InputHandler : IDisposable {
|
||||
public event InputEventDelegate OnInput;
|
||||
InputEventDelegate m_onInput;
|
||||
public event InputEventDelegate OnInput {
|
||||
add {
|
||||
if (m_onInput == null) Activate();
|
||||
m_onInput -= value;
|
||||
m_onInput += value;
|
||||
}
|
||||
remove {
|
||||
if (m_onInput == null) return;
|
||||
m_onInput -= value;
|
||||
if (m_onInput == null) Deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
~InputHandler() {
|
||||
Dispose(false);
|
||||
@@ -14,26 +26,15 @@ namespace Cryville.Common.Unity.Input {
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public bool Activated { get; private set; }
|
||||
public void Activate() {
|
||||
if (Activated) return;
|
||||
Activated = true;
|
||||
ActivateImpl();
|
||||
}
|
||||
protected abstract void ActivateImpl();
|
||||
public void Deactivate() {
|
||||
if (!Activated) return;
|
||||
Activated = false;
|
||||
DeactivateImpl();
|
||||
}
|
||||
protected abstract void DeactivateImpl();
|
||||
protected abstract void Activate();
|
||||
protected abstract void Deactivate();
|
||||
public abstract void Dispose(bool disposing);
|
||||
public abstract bool IsNullable(int type);
|
||||
public abstract byte GetDimension(int type);
|
||||
public abstract string GetTypeName(int type);
|
||||
public abstract double GetCurrentTimestamp();
|
||||
protected void Feed(int type, int id, InputVector vec) {
|
||||
var del = OnInput;
|
||||
var del = m_onInput;
|
||||
if (del != null) del(new InputIdentifier { Source = new InputSource { Handler = this, Type = type }, Id = id }, vec);
|
||||
}
|
||||
}
|
||||
|
@@ -4,28 +4,22 @@ using System.Reflection;
|
||||
|
||||
namespace Cryville.Common.Unity.Input {
|
||||
public class InputManager {
|
||||
static readonly List<Type> HandlerRegistries = new List<Type> {
|
||||
static readonly HashSet<Type> HandlerRegistries = new HashSet<Type> {
|
||||
typeof(WindowsPointerHandler),
|
||||
typeof(UnityKeyHandler<UnityKeyboardReceiver>),
|
||||
typeof(UnityKeyHandler<UnityMouseButtonReceiver>),
|
||||
typeof(UnityMouseHandler),
|
||||
typeof(UnityTouchHandler),
|
||||
};
|
||||
readonly List<InputHandler> _handlers = new List<InputHandler>();
|
||||
readonly HashSet<InputHandler> _handlers = new HashSet<InputHandler>();
|
||||
readonly Dictionary<Type, InputHandler> _typemap = new Dictionary<Type, InputHandler>();
|
||||
readonly Dictionary<InputHandler, double> _timeOrigins = new Dictionary<InputHandler, double>();
|
||||
readonly object _lock = new object();
|
||||
readonly Dictionary<InputIdentifier, InputVector> _vectors = new Dictionary<InputIdentifier, InputVector>();
|
||||
readonly List<InputEvent> _events = new List<InputEvent>();
|
||||
public InputManager() {
|
||||
foreach (var t in HandlerRegistries) {
|
||||
try {
|
||||
if (!typeof(InputHandler).IsAssignableFrom(t)) continue;
|
||||
var h = (InputHandler)ReflectionHelper.InvokeEmptyConstructor(t);
|
||||
_typemap.Add(t, h);
|
||||
h.OnInput += OnInput;
|
||||
_handlers.Add(h);
|
||||
_timeOrigins.Add(h, 0);
|
||||
Logger.Log("main", 1, "Input", "Initialized {0}", ReflectionHelper.GetSimpleName(t));
|
||||
}
|
||||
catch (TargetInvocationException ex) {
|
||||
@@ -36,49 +30,8 @@ namespace Cryville.Common.Unity.Input {
|
||||
public InputHandler GetHandler(string name) {
|
||||
return _typemap[Type.GetType(name)];
|
||||
}
|
||||
public void Activate() {
|
||||
lock (_lock) {
|
||||
_events.Clear();
|
||||
}
|
||||
foreach (var h in _handlers) h.Activate();
|
||||
}
|
||||
public void SyncTime(double time) {
|
||||
foreach (var h in _handlers) {
|
||||
_timeOrigins[h] = time - h.GetCurrentTimestamp();
|
||||
}
|
||||
}
|
||||
public void Deactivate() {
|
||||
foreach (var h in _handlers) h.Deactivate();
|
||||
}
|
||||
void OnInput(InputIdentifier id, InputVector vec) {
|
||||
lock (_lock) {
|
||||
double timeOrigin = _timeOrigins[id.Source.Handler];
|
||||
vec.Time += timeOrigin;
|
||||
InputVector vec0;
|
||||
if (_vectors.TryGetValue(id, out vec0)) {
|
||||
_events.Add(new InputEvent {
|
||||
Id = id,
|
||||
From = vec0,
|
||||
To = vec,
|
||||
});
|
||||
if (vec.IsNull) _vectors.Remove(id);
|
||||
else _vectors[id] = vec;
|
||||
}
|
||||
else {
|
||||
_events.Add(new InputEvent {
|
||||
Id = id,
|
||||
From = new InputVector(vec.Time),
|
||||
To = vec,
|
||||
});
|
||||
_vectors.Add(id, vec);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void EnumerateEvents(Action<InputEvent> cb) {
|
||||
lock (_lock) {
|
||||
foreach (var ev in _events) cb(ev);
|
||||
_events.Clear();
|
||||
}
|
||||
public void EnumerateHandlers(Action<InputHandler> cb) {
|
||||
foreach (var h in _handlers) cb(h);
|
||||
}
|
||||
}
|
||||
|
||||
|
49
Assets/Cryville/Common/Unity/Input/SimpleInputConsumer.cs
Normal file
49
Assets/Cryville/Common/Unity/Input/SimpleInputConsumer.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Cryville.Common.Unity.Input {
|
||||
public class SimpleInputConsumer {
|
||||
readonly InputManager _manager;
|
||||
readonly object _lock = new object();
|
||||
readonly Dictionary<InputIdentifier, InputVector> _vectors = new Dictionary<InputIdentifier, InputVector>();
|
||||
readonly List<InputEvent> _events = new List<InputEvent>();
|
||||
public SimpleInputConsumer(InputManager manager) { _manager = manager; }
|
||||
public void Activate() {
|
||||
lock (_lock) {
|
||||
_events.Clear();
|
||||
}
|
||||
_manager.EnumerateHandlers(h => h.OnInput += OnInput);
|
||||
}
|
||||
public void Deactivate() {
|
||||
_manager.EnumerateHandlers(h => h.OnInput -= OnInput);
|
||||
}
|
||||
protected void OnInput(InputIdentifier id, InputVector vec) {
|
||||
lock (_lock) {
|
||||
InputVector vec0;
|
||||
if (_vectors.TryGetValue(id, out vec0)) {
|
||||
_events.Add(new InputEvent {
|
||||
Id = id,
|
||||
From = vec0,
|
||||
To = vec,
|
||||
});
|
||||
if (vec.IsNull) _vectors.Remove(id);
|
||||
else _vectors[id] = vec;
|
||||
}
|
||||
else {
|
||||
_events.Add(new InputEvent {
|
||||
Id = id,
|
||||
From = new InputVector(vec.Time),
|
||||
To = vec,
|
||||
});
|
||||
_vectors.Add(id, vec);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void EnumerateEvents(Action<InputEvent> cb) {
|
||||
lock (_lock) {
|
||||
foreach (var ev in _events) cb(ev);
|
||||
_events.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8fd2d5f1c7ba0c74c9ce8775075750db
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -9,19 +9,19 @@ namespace Cryville.Common.Unity.Input {
|
||||
|
||||
public UnityKeyHandler() { }
|
||||
|
||||
protected override void ActivateImpl() {
|
||||
protected override void Activate() {
|
||||
receiver = new GameObject("__keyrecv__");
|
||||
recvcomp = receiver.AddComponent<T>();
|
||||
recvcomp.SetCallback(Feed);
|
||||
}
|
||||
|
||||
protected override void DeactivateImpl() {
|
||||
protected override void Deactivate() {
|
||||
if (receiver) GameObject.Destroy(receiver);
|
||||
}
|
||||
|
||||
public override void Dispose(bool disposing) {
|
||||
if (disposing) {
|
||||
DeactivateImpl();
|
||||
Deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ namespace Cryville.Common.Unity.Input {
|
||||
|
||||
public abstract class UnityKeyReceiver<T> : MonoBehaviour where T : UnityKeyReceiver<T> {
|
||||
protected Action<int, int, InputVector> Callback;
|
||||
protected readonly List<int> Keys = new List<int>();
|
||||
protected readonly HashSet<int> Keys = new HashSet<int>();
|
||||
public void SetCallback(Action<int, int, InputVector> h) {
|
||||
Callback = h;
|
||||
}
|
||||
|
@@ -12,18 +12,18 @@ namespace Cryville.Common.Unity.Input {
|
||||
}
|
||||
}
|
||||
|
||||
protected override void ActivateImpl() {
|
||||
protected override void Activate() {
|
||||
receiver = new GameObject("__mouserecv__");
|
||||
receiver.AddComponent<UnityMouseReceiver>().SetHandler(this);
|
||||
}
|
||||
|
||||
protected override void DeactivateImpl() {
|
||||
protected override void Deactivate() {
|
||||
if (receiver) GameObject.Destroy(receiver);
|
||||
}
|
||||
|
||||
public override void Dispose(bool disposing) {
|
||||
if (disposing) {
|
||||
DeactivateImpl();
|
||||
Deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -12,18 +12,18 @@ namespace Cryville.Common.Unity.Input {
|
||||
}
|
||||
}
|
||||
|
||||
protected override void ActivateImpl() {
|
||||
protected override void Activate() {
|
||||
receiver = new GameObject("__touchrecv__");
|
||||
receiver.AddComponent<UnityPointerReceiver>().SetHandler(this);
|
||||
}
|
||||
|
||||
protected override void DeactivateImpl() {
|
||||
protected override void Deactivate() {
|
||||
if (receiver) GameObject.Destroy(receiver);
|
||||
}
|
||||
|
||||
public override void Dispose(bool disposing) {
|
||||
if (disposing) {
|
||||
DeactivateImpl();
|
||||
Deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -61,7 +61,7 @@ namespace Cryville.Common.Unity.Input {
|
||||
public const int TABLET_DISABLE_PENBARRELFEEDBACK = 0x00000010;
|
||||
public const int TABLET_DISABLE_FLICKS = 0x00010000;
|
||||
|
||||
protected override void ActivateImpl() {
|
||||
protected override void Activate() {
|
||||
newWndProc = WndProc;
|
||||
newWndProcPtr = Marshal.GetFunctionPointerForDelegate(newWndProc);
|
||||
oldWndProcPtr = SetWindowLongPtr(hMainWindow, -4, newWndProcPtr);
|
||||
@@ -77,7 +77,7 @@ namespace Cryville.Common.Unity.Input {
|
||||
);
|
||||
}
|
||||
|
||||
protected override void DeactivateImpl() {
|
||||
protected override void Deactivate() {
|
||||
if (pressAndHoldAtomID != 0) {
|
||||
NativeMethods.RemoveProp(hMainWindow, PRESS_AND_HOLD_ATOM);
|
||||
NativeMethods.GlobalDeleteAtom(pressAndHoldAtomID);
|
||||
@@ -142,7 +142,7 @@ namespace Cryville.Common.Unity.Input {
|
||||
}
|
||||
|
||||
public override void Dispose(bool disposing) {
|
||||
DeactivateImpl();
|
||||
Deactivate();
|
||||
if (usePointerMessage)
|
||||
NativeMethods.EnableMouseInPointer(false);
|
||||
Instance = null;
|
||||
|
@@ -57,7 +57,6 @@ namespace Cryville.Crtr.Config {
|
||||
var proxy = new InputProxy(ruleset.Root, null);
|
||||
proxy.LoadFrom(_rscfg.inputs);
|
||||
m_inputConfigPanel.proxy = proxy;
|
||||
Game.InputManager.Activate();
|
||||
}
|
||||
|
||||
public void SwitchCategory(GameObject cat) {
|
||||
@@ -68,7 +67,6 @@ namespace Cryville.Crtr.Config {
|
||||
}
|
||||
|
||||
public void SaveAndReturnToMenu() {
|
||||
Game.InputManager.Deactivate();
|
||||
m_inputConfigPanel.proxy.SaveTo(_rscfg.inputs);
|
||||
m_inputConfigPanel.proxy.Dispose();
|
||||
FileInfo cfgfile = new FileInfo(
|
||||
|
@@ -24,6 +24,7 @@ namespace Cryville.Crtr.Config {
|
||||
[SerializeField]
|
||||
GameObject m_prefabInputConfigEntry;
|
||||
|
||||
readonly SimpleInputConsumer _consumer = new SimpleInputConsumer(Game.InputManager);
|
||||
public InputProxy proxy;
|
||||
readonly Dictionary<string, InputConfigPanelEntry> _entries = new Dictionary<string, InputConfigPanelEntry>();
|
||||
|
||||
@@ -32,7 +33,7 @@ namespace Cryville.Crtr.Config {
|
||||
_sel = entry;
|
||||
m_inputDialog.SetActive(true);
|
||||
CallHelper.Purge(m_deviceList);
|
||||
Game.InputManager.EnumerateEvents(ev => { });
|
||||
_consumer.EnumerateEvents(ev => { });
|
||||
_recvsrcs.Clear();
|
||||
AddSourceItem(null);
|
||||
}
|
||||
@@ -46,10 +47,11 @@ namespace Cryville.Crtr.Config {
|
||||
Target = _sel,
|
||||
Source = src,
|
||||
});
|
||||
m_inputDialog.SetActive(false);
|
||||
CloseDialog();
|
||||
}
|
||||
|
||||
void Start() {
|
||||
_consumer.Activate();
|
||||
foreach (var i in m_configScene.ruleset.Root.inputs) {
|
||||
var e = GameObject.Instantiate(m_prefabInputConfigEntry, m_entryList.transform).GetComponent<InputConfigPanelEntry>();
|
||||
_entries.Add(i.Key, e);
|
||||
@@ -59,6 +61,10 @@ namespace Cryville.Crtr.Config {
|
||||
proxy.ProxyChanged += OnProxyChanged;
|
||||
}
|
||||
|
||||
void OnDestroy() {
|
||||
_consumer.Deactivate();
|
||||
}
|
||||
|
||||
void OnProxyChanged(object sender, ProxyChangedEventArgs e) {
|
||||
_entries[e.Name].SetEnabled(!e.Used);
|
||||
_entries[e.Name].SetValue(e.Proxy == null ? "None" : e.Proxy.Value.Handler.GetTypeName(e.Proxy.Value.Type));
|
||||
@@ -67,7 +73,7 @@ namespace Cryville.Crtr.Config {
|
||||
readonly List<InputSource?> _recvsrcs = new List<InputSource?>();
|
||||
void Update() {
|
||||
if (m_inputDialog.activeSelf) {
|
||||
Game.InputManager.EnumerateEvents(ev => {
|
||||
_consumer.EnumerateEvents(ev => {
|
||||
AddSourceItem(ev.Id.Source);
|
||||
});
|
||||
}
|
||||
|
@@ -65,10 +65,6 @@ namespace Cryville.Crtr {
|
||||
if (_use[target] > 0)
|
||||
throw new InvalidOperationException("Input already assigned");
|
||||
if (proxy.Source != null) {
|
||||
if (_judge != null) {
|
||||
proxy.Source.Value.Handler.OnInput -= OnInput; // Prevent duplicated hooks, no exception will be thrown
|
||||
proxy.Source.Value.Handler.OnInput += OnInput;
|
||||
}
|
||||
_tproxies.Add(target, proxy);
|
||||
_sproxies.Add(proxy.Source.Value, proxy);
|
||||
IncrementUseRecursive(target);
|
||||
@@ -77,7 +73,6 @@ namespace Cryville.Crtr {
|
||||
}
|
||||
void Remove(InputProxyEntry proxy) {
|
||||
var target = proxy.Target;
|
||||
if (_judge != null) _tproxies[target].Source.Value.Handler.OnInput -= OnInput;
|
||||
_sproxies.Remove(_tproxies[target].Source.Value);
|
||||
_tproxies.Remove(target);
|
||||
DecrementUseRecursive(target);
|
||||
@@ -142,12 +137,22 @@ namespace Cryville.Crtr {
|
||||
public void Activate() {
|
||||
_activeCounts.Clear();
|
||||
_vect.Clear(); _vecs.Clear();
|
||||
foreach (var src in _sproxies.Keys) {
|
||||
_activeCounts.Add(src, 0);
|
||||
src.Handler.Activate();
|
||||
foreach (var src in _sproxies) {
|
||||
_activeCounts.Add(src.Key, 0);
|
||||
var isrc = src.Value.Source;
|
||||
if (isrc != null) {
|
||||
isrc.Value.Handler.OnInput += OnInput;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Deactivate() {
|
||||
foreach (var src in _sproxies) {
|
||||
var isrc = src.Value.Source;
|
||||
if (isrc != null) {
|
||||
isrc.Value.Handler.OnInput -= OnInput;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Deactivate() { foreach (var src in _sproxies.Keys) src.Handler.Deactivate(); }
|
||||
|
||||
~InputProxy() {
|
||||
Dispose(false);
|
||||
|
Reference in New Issue
Block a user