Implement InputProxy.
This commit is contained in:
@@ -11,7 +11,8 @@ namespace Cryville.Common.Unity.Input {
|
|||||||
typeof(UnityMouseHandler),
|
typeof(UnityMouseHandler),
|
||||||
typeof(UnityTouchHandler),
|
typeof(UnityTouchHandler),
|
||||||
};
|
};
|
||||||
readonly List<InputHandler> _handlers = new List<InputHandler>();
|
// TODO set private
|
||||||
|
public readonly List<InputHandler> _handlers = new List<InputHandler>();
|
||||||
readonly Dictionary<InputHandler, double> _timeOrigins = new Dictionary<InputHandler, double>();
|
readonly Dictionary<InputHandler, double> _timeOrigins = new Dictionary<InputHandler, double>();
|
||||||
readonly object _lock = new object();
|
readonly object _lock = new object();
|
||||||
readonly Dictionary<InputIdentifier, InputVector> _vectors = new Dictionary<InputIdentifier, InputVector>();
|
readonly Dictionary<InputIdentifier, InputVector> _vectors = new Dictionary<InputIdentifier, InputVector>();
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
using Cryville.Common;
|
using Cryville.Common;
|
||||||
using Cryville.Common.Plist;
|
using Cryville.Common.Plist;
|
||||||
|
using Cryville.Common.Unity.Input;
|
||||||
using Cryville.Crtr.Event;
|
using Cryville.Crtr.Event;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
@@ -65,6 +66,8 @@ namespace Cryville.Crtr {
|
|||||||
public static Dictionary<string, MotionRegistry> motionRegistry = new Dictionary<string, MotionRegistry>();
|
public static Dictionary<string, MotionRegistry> motionRegistry = new Dictionary<string, MotionRegistry>();
|
||||||
|
|
||||||
public static PdtEvaluator etor;
|
public static PdtEvaluator etor;
|
||||||
|
|
||||||
|
InputProxy inputProxy;
|
||||||
|
|
||||||
~ChartPlayer() {
|
~ChartPlayer() {
|
||||||
Dispose();
|
Dispose();
|
||||||
@@ -381,7 +384,23 @@ namespace Cryville.Crtr {
|
|||||||
}
|
}
|
||||||
Logger.Log("main", 0, "Load/Prehandle", "Initializing states");
|
Logger.Log("main", 0, "Load/Prehandle", "Initializing states");
|
||||||
cbus.BroadcastInit();
|
cbus.BroadcastInit();
|
||||||
Game.InputManager.Activate();
|
|
||||||
|
inputProxy = new InputProxy(pruleset, judge);
|
||||||
|
foreach (var i in Game.InputManager._handlers) {
|
||||||
|
/*if (i is UnityKeyHandler<UnityKeyboardReceiver>) {
|
||||||
|
inputProxy.Set(new InputProxyEntry { Source = new InputSource { Handler = i, Type = (int)KeyCode.Z }, Target = "track0" });
|
||||||
|
inputProxy.Set(new InputProxyEntry { Source = new InputSource { Handler = i, Type = (int)KeyCode.X }, Target = "track1" });
|
||||||
|
inputProxy.Set(new InputProxyEntry { Source = new InputSource { Handler = i, Type = (int)KeyCode.Comma }, Target = "track2" });
|
||||||
|
inputProxy.Set(new InputProxyEntry { Source = new InputSource { Handler = i, Type = (int)KeyCode.Period }, Target = "track3" });
|
||||||
|
break;
|
||||||
|
}*/
|
||||||
|
if (i is UnityMouseHandler) {
|
||||||
|
inputProxy.Set(new InputProxyEntry { Source = new InputSource { Handler = i, Type = 0 }, Target = "screen_x" });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inputProxy.Activate();
|
||||||
|
|
||||||
if (logEnabled) ToggleLogs();
|
if (logEnabled) ToggleLogs();
|
||||||
Logger.Log("main", 0, "Load/Prehandle", "Cleaning up");
|
Logger.Log("main", 0, "Load/Prehandle", "Cleaning up");
|
||||||
GC.Collect();
|
GC.Collect();
|
||||||
@@ -390,7 +409,7 @@ namespace Cryville.Crtr {
|
|||||||
Logger.Log("main", 1, "Load/Prehandle", "Prehandling done ({0}ms)", timer.Elapsed.TotalMilliseconds);
|
Logger.Log("main", 1, "Load/Prehandle", "Prehandling done ({0}ms)", timer.Elapsed.TotalMilliseconds);
|
||||||
Game.AudioSequencer.Playing = true;
|
Game.AudioSequencer.Playing = true;
|
||||||
Thread.Sleep((int)(Game.AudioClient.BufferPosition - Game.AudioClient.Position));
|
Thread.Sleep((int)(Game.AudioClient.BufferPosition - Game.AudioClient.Position));
|
||||||
Game.InputManager.SyncTime(cbus.Time);
|
// TODO SyncTime(cbus.Time);
|
||||||
started = true;
|
started = true;
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
@@ -408,7 +427,7 @@ namespace Cryville.Crtr {
|
|||||||
if (bbus != null) bbus.Dispose();
|
if (bbus != null) bbus.Dispose();
|
||||||
if (tbus != null) tbus.Dispose();
|
if (tbus != null) tbus.Dispose();
|
||||||
if (nbus != null) nbus.Dispose();
|
if (nbus != null) nbus.Dispose();
|
||||||
// Game.InputManager.Deactivate();
|
inputProxy.Deactivate();
|
||||||
foreach (var t in texs) Texture.Destroy(t.Value);
|
foreach (var t in texs) Texture.Destroy(t.Value);
|
||||||
Logger.Log("main", 1, "Game", "Stopped");
|
Logger.Log("main", 1, "Game", "Stopped");
|
||||||
}
|
}
|
||||||
|
@@ -64,7 +64,7 @@ namespace Cryville.Crtr.Config {
|
|||||||
});
|
});
|
||||||
if (ruleset.format != 1) throw new FormatException("Invalid ruleset file version");
|
if (ruleset.format != 1) throw new FormatException("Invalid ruleset file version");
|
||||||
ruleset.LoadPdt(dir);
|
ruleset.LoadPdt(dir);
|
||||||
_proxy = new InputProxy(ruleset.Root);
|
_proxy = new InputProxy(ruleset.Root, null);
|
||||||
foreach (var i in ruleset.Root.inputs) {
|
foreach (var i in ruleset.Root.inputs) {
|
||||||
var e = GameObject.Instantiate(m_prefabInputConfigEntry).GetComponent<InputConfigEntry>();
|
var e = GameObject.Instantiate(m_prefabInputConfigEntry).GetComponent<InputConfigEntry>();
|
||||||
e.transform.SetParent(m_entryList.transform);
|
e.transform.SetParent(m_entryList.transform);
|
||||||
|
@@ -4,19 +4,13 @@ using Cryville.Common.Unity.Input;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Logger = Cryville.Common.Logger;
|
|
||||||
|
|
||||||
namespace Cryville.Crtr {
|
namespace Cryville.Crtr {
|
||||||
public class InputProxy {
|
public class InputProxy {
|
||||||
readonly PdtEvaluator _etor;
|
readonly PdtEvaluator _etor;
|
||||||
readonly PdtRuleset _ruleset;
|
readonly PdtRuleset _ruleset;
|
||||||
readonly Dictionary<string, InputProxyEntry> _tproxies = new Dictionary<string, InputProxyEntry>();
|
readonly Judge _judge;
|
||||||
readonly Dictionary<InputSource, InputProxyEntry> _sproxies = new Dictionary<InputSource, InputProxyEntry>();
|
public InputProxy(PdtRuleset ruleset, Judge judge) {
|
||||||
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 {
|
unsafe {
|
||||||
fixed (byte* ptr = _vecbuf) {
|
fixed (byte* ptr = _vecbuf) {
|
||||||
*(int*)(ptr + 3 * sizeof(float)) = PdtInternalType.Number;
|
*(int*)(ptr + 3 * sizeof(float)) = PdtInternalType.Number;
|
||||||
@@ -24,6 +18,7 @@ namespace Cryville.Crtr {
|
|||||||
}
|
}
|
||||||
_etor = ChartPlayer.etor;
|
_etor = ChartPlayer.etor;
|
||||||
_ruleset = ruleset;
|
_ruleset = ruleset;
|
||||||
|
_judge = judge;
|
||||||
foreach (var i in ruleset.inputs) {
|
foreach (var i in ruleset.inputs) {
|
||||||
_use.Add(i.Key, 0);
|
_use.Add(i.Key, 0);
|
||||||
_rev.Add(i.Key, new List<string>());
|
_rev.Add(i.Key, new List<string>());
|
||||||
@@ -36,6 +31,11 @@ namespace Cryville.Crtr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#region Settings
|
#region Settings
|
||||||
|
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>>();
|
||||||
|
public event EventHandler<ProxyChangedEventArgs> ProxyChanged;
|
||||||
public void Set(InputProxyEntry proxy) {
|
public void Set(InputProxyEntry proxy) {
|
||||||
var name = proxy.Target;
|
var name = proxy.Target;
|
||||||
if (_tproxies.ContainsKey(name)) Remove(proxy);
|
if (_tproxies.ContainsKey(name)) Remove(proxy);
|
||||||
@@ -103,32 +103,49 @@ namespace Cryville.Crtr {
|
|||||||
#region Handling
|
#region Handling
|
||||||
public void Activate() { foreach (var src in _sproxies.Keys) src.Handler.Activate(); }
|
public void Activate() { foreach (var src in _sproxies.Keys) src.Handler.Activate(); }
|
||||||
public void Deactivate() { foreach (var src in _sproxies.Keys) src.Handler.Deactivate(); }
|
public void Deactivate() { foreach (var src in _sproxies.Keys) src.Handler.Deactivate(); }
|
||||||
|
|
||||||
readonly object _lock = new object();
|
readonly object _lock = new object();
|
||||||
void OnInput(InputIdentifier id, InputVector vec) {
|
static readonly int _var_value = IdentifierManager.SharedInstance.Request("value");
|
||||||
|
static readonly int _var_ft = IdentifierManager.SharedInstance.Request("ft");
|
||||||
|
static readonly int _var_tt = IdentifierManager.SharedInstance.Request("tt");
|
||||||
|
static readonly PropOp.Arbitrary _arbop = new PropOp.Arbitrary();
|
||||||
|
readonly byte[] _numbuf = new byte[sizeof(float)];
|
||||||
|
readonly byte[] _numbuf2 = new byte[sizeof(float)];
|
||||||
|
readonly byte[] _vecbuf = new byte[3 * sizeof(float) + sizeof(int)];
|
||||||
|
readonly Dictionary<InputIdentifier, float> _vect = new Dictionary<InputIdentifier, float>();
|
||||||
|
readonly Dictionary<ProxiedInputIdentifier, PropSrc.Arbitrary> _vecs = new Dictionary<ProxiedInputIdentifier, PropSrc.Arbitrary>();
|
||||||
|
unsafe void LoadNum(float value) {
|
||||||
|
fixed (byte* ptr = _numbuf) *(float*)ptr = value;
|
||||||
|
}
|
||||||
|
unsafe void LoadNum2(float value) {
|
||||||
|
fixed (byte* ptr = _numbuf2) *(float*)ptr = value;
|
||||||
|
}
|
||||||
|
unsafe void OnInput(InputIdentifier id, InputVector vec) {
|
||||||
lock (_lock) {
|
lock (_lock) {
|
||||||
InputProxyEntry proxy;
|
InputProxyEntry proxy;
|
||||||
if (_sproxies.TryGetValue(id.Source, out proxy)) {
|
if (_sproxies.TryGetValue(id.Source, out proxy)) {
|
||||||
OnInput(id, vec, proxy.Target);
|
_etor.ContextCascadeInsert();
|
||||||
|
float ft, tt = (float)vec.Time;
|
||||||
|
if (!_vect.TryGetValue(id, out ft)) ft = tt;
|
||||||
|
LoadNum(ft); _etor.ContextCascadeUpdate(_var_ft, new PropSrc.Arbitrary(PdtInternalType.Number, _numbuf));
|
||||||
|
LoadNum2(tt); _etor.ContextCascadeUpdate(_var_tt, new PropSrc.Arbitrary(PdtInternalType.Number, _numbuf2));
|
||||||
|
_vect[id] = tt;
|
||||||
|
if (vec.IsNull) {
|
||||||
|
_etor.ContextCascadeUpdate(_var_value, new PropSrc.Arbitrary(PdtInternalType.Null, new byte[0]));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fixed (byte* ptr = _vecbuf) {
|
||||||
|
*(Vector3*)ptr = vec.Vector;
|
||||||
|
}
|
||||||
|
_etor.ContextCascadeUpdate(_var_value, new PropSrc.Arbitrary(PdtInternalType.Vector, _vecbuf));
|
||||||
|
}
|
||||||
|
OnInput(id, proxy.Target);
|
||||||
|
_etor.ContextCascadeDiscard();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static readonly int _var_value = IdentifierManager.SharedInstance.Request("value");
|
static readonly int _var_fv = IdentifierManager.SharedInstance.Request("fv");
|
||||||
static readonly PropOp.Arbitrary _arbop = new PropOp.Arbitrary();
|
static readonly int _var_tv = IdentifierManager.SharedInstance.Request("tv");
|
||||||
readonly byte[] _vecbuf = new byte[3 * sizeof(float) + sizeof(int)];
|
|
||||||
unsafe void OnInput(InputIdentifier id, InputVector vec, string target) {
|
|
||||||
_etor.ContextCascadeInsert();
|
|
||||||
if (vec.IsNull) {
|
|
||||||
_etor.ContextCascadeUpdate(_var_value, new PropSrc.Arbitrary(PdtInternalType.Null, new byte[0]));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
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) {
|
unsafe void OnInput(InputIdentifier id, string target) {
|
||||||
var def = _ruleset.inputs[target];
|
var def = _ruleset.inputs[target];
|
||||||
if (def.pass != null) {
|
if (def.pass != null) {
|
||||||
@@ -141,7 +158,17 @@ namespace Cryville.Crtr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Logger.Log("main", 0, "Input/Proxy", "input recv {0}", target);
|
var pid = new ProxiedInputIdentifier { Source = id, Target = target };
|
||||||
|
PropSrc.Arbitrary fv, tv = _etor.ContextCascadeLookup(_var_value);
|
||||||
|
if (!_vecs.TryGetValue(pid, out fv)) fv = new PropSrc.Arbitrary(PdtInternalType.Null, new byte[0]);
|
||||||
|
if (fv.Type != PdtInternalType.Null || tv.Type != PdtInternalType.Null) {
|
||||||
|
_etor.ContextCascadeInsert();
|
||||||
|
_etor.ContextCascadeUpdate(_var_tv, tv);
|
||||||
|
_etor.ContextCascadeUpdate(_var_fv, fv);
|
||||||
|
_judge.Feed(target);
|
||||||
|
_etor.ContextCascadeDiscard();
|
||||||
|
}
|
||||||
|
_vecs[pid] = tv;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
@@ -163,4 +190,28 @@ namespace Cryville.Crtr {
|
|||||||
public string Target { get; set; }
|
public string Target { get; set; }
|
||||||
public byte[] Mapping { get; private set; }
|
public byte[] Mapping { get; private set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public struct ProxiedInputIdentifier : IEquatable<ProxiedInputIdentifier> {
|
||||||
|
public InputIdentifier Source { get; set; }
|
||||||
|
public string Target { get; set; }
|
||||||
|
public override bool Equals(object obj) {
|
||||||
|
if (obj == null || !(obj is ProxiedInputIdentifier)) return false;
|
||||||
|
return Equals((ProxiedInputIdentifier)obj);
|
||||||
|
}
|
||||||
|
public bool Equals(ProxiedInputIdentifier other) {
|
||||||
|
return Source == other.Source && Target == other.Target;
|
||||||
|
}
|
||||||
|
public override int GetHashCode() {
|
||||||
|
return Source.GetHashCode() ^ Target.GetHashCode();
|
||||||
|
}
|
||||||
|
public override string ToString() {
|
||||||
|
return string.Format("{0}->{1}", Source, Target);
|
||||||
|
}
|
||||||
|
public static bool operator ==(ProxiedInputIdentifier lhs, ProxiedInputIdentifier rhs) {
|
||||||
|
return lhs.Equals(rhs);
|
||||||
|
}
|
||||||
|
public static bool operator !=(ProxiedInputIdentifier lhs, ProxiedInputIdentifier rhs) {
|
||||||
|
return !lhs.Equals(rhs);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -14,6 +14,8 @@ namespace Cryville.Crtr {
|
|||||||
scores.Add(name, s.Value.init);
|
scores.Add(name, s.Value.init);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void Feed(string target) {
|
||||||
|
}
|
||||||
public void Feed(InputEvent ev) {
|
public void Feed(InputEvent ev) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -13,14 +13,14 @@ namespace Cryville.Crtr {
|
|||||||
}
|
}
|
||||||
protected abstract void InternalGet(out int type, out byte[] value);
|
protected abstract void InternalGet(out int type, out byte[] value);
|
||||||
public class Arbitrary : PropSrc {
|
public class Arbitrary : PropSrc {
|
||||||
readonly new int _type;
|
public int Type { get; private set; }
|
||||||
readonly byte[] _value;
|
readonly byte[] _value;
|
||||||
public Arbitrary(int type, byte[] value) {
|
public Arbitrary(int type, byte[] value) {
|
||||||
_type = type;
|
Type = type;
|
||||||
_value = value;
|
_value = value;
|
||||||
}
|
}
|
||||||
protected override void InternalGet(out int type, out byte[] value) {
|
protected override void InternalGet(out int type, out byte[] value) {
|
||||||
type = _type;
|
type = Type;
|
||||||
value = _value;
|
value = _value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user