Implement input proxy. Change input callback delegate to event. Prevents repeated (de)activation.
This commit is contained in:
@@ -1,16 +1,28 @@
|
||||
using Cryville.Common.Unity.Input;
|
||||
using Cryville.Common;
|
||||
using Cryville.Common.Pdt;
|
||||
using Cryville.Common.Unity.Input;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Logger = Cryville.Common.Logger;
|
||||
|
||||
namespace Cryville.Crtr {
|
||||
public class InputProxy {
|
||||
readonly PdtEvaluator _etor;
|
||||
readonly PdtRuleset _ruleset;
|
||||
readonly Dictionary<string, InputProxyEntry> _hash1 = new Dictionary<string, InputProxyEntry>();
|
||||
readonly Dictionary<InputSource, InputProxyEntry> _hash2 = new Dictionary<InputSource, InputProxyEntry>();
|
||||
readonly Dictionary<string, InputProxyEntry> _tproxies = new Dictionary<string, InputProxyEntry>();
|
||||
readonly Dictionary<InputSource, InputProxyEntry> _sproxies = new Dictionary<InputSource, InputProxyEntry>();
|
||||
readonly Dictionary<string, int> _use = new Dictionary<string, int>();
|
||||
readonly Dictionary<string, List<string>> _rev = new Dictionary<string, List<string>>();
|
||||
readonly Dictionary<InputIdentifier, InputVector> _vecs = new Dictionary<InputIdentifier, InputVector>();
|
||||
public event EventHandler<ProxyChangedEventArgs> ProxyChanged;
|
||||
public InputProxy(PdtRuleset ruleset) {
|
||||
unsafe {
|
||||
fixed (byte* ptr = _vecbuf) {
|
||||
*(int*)(ptr + 3 * sizeof(float)) = PdtInternalType.Number;
|
||||
}
|
||||
}
|
||||
_etor = ChartPlayer.etor;
|
||||
_ruleset = ruleset;
|
||||
foreach (var i in ruleset.inputs) {
|
||||
_use.Add(i.Key, 0);
|
||||
@@ -23,27 +35,30 @@ namespace Cryville.Crtr {
|
||||
}
|
||||
}
|
||||
}
|
||||
#region Settings
|
||||
public void Set(InputProxyEntry proxy) {
|
||||
var name = proxy.Target;
|
||||
if (_hash1.ContainsKey(name)) Remove(proxy);
|
||||
if (_tproxies.ContainsKey(name)) Remove(proxy);
|
||||
if (_use[proxy.Target] > 0)
|
||||
throw new InvalidOperationException("Input already assigned");
|
||||
if (proxy.Source != null) {
|
||||
_hash1.Add(proxy.Target, proxy);
|
||||
_hash2.Add(proxy.Source.Value, proxy);
|
||||
proxy.Source.Value.Handler.OnInput += OnInput;
|
||||
_tproxies.Add(proxy.Target, proxy);
|
||||
_sproxies.Add(proxy.Source.Value, proxy);
|
||||
IncrementUseRecursive(name);
|
||||
IncrementReversedUseRecursive(name);
|
||||
}
|
||||
}
|
||||
void Remove(InputProxyEntry proxy) {
|
||||
var name = proxy.Target;
|
||||
_hash2.Remove(_hash1[name].Source.Value);
|
||||
_hash1.Remove(name);
|
||||
proxy.Source.Value.Handler.OnInput -= OnInput;
|
||||
_sproxies.Remove(_tproxies[name].Source.Value);
|
||||
_tproxies.Remove(name);
|
||||
DecrementUseRecursive(name);
|
||||
DecrementReversedUseRecursive(name);
|
||||
}
|
||||
public bool IsUsed(InputSource src) {
|
||||
return _hash2.ContainsKey(src);
|
||||
return _sproxies.ContainsKey(src);
|
||||
}
|
||||
void IncrementUseRecursive(string name) {
|
||||
BroadcastProxyChanged(name);
|
||||
@@ -80,8 +95,47 @@ namespace Cryville.Crtr {
|
||||
}
|
||||
}
|
||||
void BroadcastProxyChanged(string name) {
|
||||
ProxyChanged(this, new ProxyChangedEventArgs(name, _hash1.ContainsKey(name) ? _hash1[name].Source : null, _use[name] > 0));
|
||||
ProxyChanged(this, new ProxyChangedEventArgs(name, _tproxies.ContainsKey(name) ? _tproxies[name].Source : null, _use[name] > 0));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Handling
|
||||
public void Activate() { foreach (var src in _sproxies.Keys) src.Handler.Activate(); }
|
||||
public void Deactivate() { foreach (var src in _sproxies.Keys) src.Handler.Deactivate(); }
|
||||
void OnInput(InputIdentifier id, InputVector vec) {
|
||||
InputProxyEntry proxy;
|
||||
if (_sproxies.TryGetValue(id.Source, out proxy)) {
|
||||
OnInput(id, vec, proxy.Target);
|
||||
}
|
||||
}
|
||||
static readonly int _var_value = IdentifierManager.SharedInstance.Request("value");
|
||||
static readonly PropOp.Arbitrary _arbop = new PropOp.Arbitrary();
|
||||
readonly byte[] _vecbuf = new byte[3 * sizeof(float) + sizeof(int)];
|
||||
unsafe void OnInput(InputIdentifier id, InputVector vec, string target) {
|
||||
_etor.ContextCascadeInsert();
|
||||
fixed (byte* ptr = _vecbuf) {
|
||||
*(Vector3*)ptr = vec.Vector;
|
||||
}
|
||||
_etor.ContextCascadeUpdate(_var_value, new PropSrc.Arbitrary(PdtInternalType.Vector, _vecbuf));
|
||||
OnInput(id, target);
|
||||
_etor.ContextCascadeDiscard();
|
||||
}
|
||||
unsafe void OnInput(InputIdentifier id, string target) {
|
||||
var def = _ruleset.inputs[target];
|
||||
if (def.pass != null) {
|
||||
foreach (var p in def.pass) {
|
||||
_arbop.Name = _var_value;
|
||||
_etor.ContextCascadeInsert();
|
||||
_etor.Evaluate(_arbop, p.Value);
|
||||
OnInput(id, p.Key);
|
||||
_etor.ContextCascadeDiscard();
|
||||
}
|
||||
}
|
||||
else {
|
||||
Logger.Log("main", 0, "Input/Proxy", "input recv {0}", target);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
public class ProxyChangedEventArgs : EventArgs {
|
||||
@@ -100,45 +154,4 @@ namespace Cryville.Crtr {
|
||||
public string Target { get; set; }
|
||||
public byte[] Mapping { get; private set; }
|
||||
}
|
||||
|
||||
public sealed class InputProxyHandler : InputHandler {
|
||||
readonly InputDefinition _def;
|
||||
|
||||
public InputProxyHandler(InputDefinition def, InputHandler src) : base() {
|
||||
_def = def;
|
||||
src.Callback = OnInput;
|
||||
}
|
||||
|
||||
public override void Activate() {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Deactivate() {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Dispose(bool disposing) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool IsNullable(int type) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override byte GetDimension(int type) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override string GetTypeName(int type) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override double GetCurrentTimestamp() {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void OnInput(InputIdentifier id, InputVector vec) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user