Move part of the input module to Cryville.Input.

This commit is contained in:
2023-05-05 00:40:51 +08:00
parent b143fb49ce
commit 0b2ea3ddbc
62 changed files with 1417 additions and 1602 deletions

View File

@@ -623,7 +623,7 @@ namespace Cryville.Crtr {
judge = new Judge(this, pruleset);
etor.ContextJudge = judge;
inputProxy = new InputProxy(pruleset, judge);
inputProxy = new InputProxy(pruleset, judge, screenSize);
inputProxy.LoadFrom(_rscfg.inputs);
if (!inputProxy.IsCompleted()) {
throw new ArgumentException("Input config not completed\nPlease complete the input settings");

View File

@@ -56,7 +56,7 @@ namespace Cryville.Crtr.Config {
m_genericConfigPanel.Target = _rscfg.generic;
var proxy = new InputProxy(ruleset.Root, null);
var proxy = new InputProxy(ruleset.Root, null, new Vector2(Screen.width, Screen.height));
proxy.LoadFrom(_rscfg.inputs);
m_inputConfigPanel.proxy = proxy;

View File

@@ -1,6 +1,6 @@
using Cryville.Common;
using Cryville.Common.Unity;
using Cryville.Common.Unity.Input;
using Cryville.Input;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
@@ -77,7 +77,7 @@ namespace Cryville.Crtr.Config {
void Update() {
if (m_inputDialog.activeSelf) {
_consumer.EnumerateEvents(ev => {
AddSourceItem(ev.Id.Source);
AddSourceItem(ev.Identifier.Source);
});
}
}
@@ -95,7 +95,7 @@ namespace Cryville.Crtr.Config {
if (proxy.IsUsed(tsrc)) {
text.text += " <size=9>(Used)</size>";
}
else if (tsrc.Handler.GetDimension(src.Value.Type) < m_configScene.ruleset.Root.inputs[_sel].dim) {
else if (tsrc.Handler.Dimension < m_configScene.ruleset.Root.inputs[_sel].dim) {
text.text += " <size=9>(Not Applicable)</size>";
}
else flag = true;

View File

@@ -6,6 +6,7 @@ using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Logger = Cryville.Common.Logging.Logger;
using unity = UnityEngine;
namespace Cryville.Crtr {
public class Console : MonoBehaviour {
@@ -36,8 +37,8 @@ namespace Cryville.Crtr {
void Update() {
if (
Input.GetKeyDown(KeyCode.Return)
|| Input.GetKeyDown(KeyCode.KeypadEnter)
unity::Input.GetKeyDown(KeyCode.Return)
|| unity::Input.GetKeyDown(KeyCode.KeypadEnter)
) {
Submit();
InputBox.text = "";

View File

@@ -3,8 +3,10 @@ using Cryville.Audio.Source;
using Cryville.Common.Font;
using Cryville.Common.Logging;
using Cryville.Common.Unity;
using Cryville.Common.Unity.Input;
using Cryville.Common.Unity.UI;
using Cryville.Input;
using Cryville.Input.Unity;
using Cryville.Input.Unity.Android;
using FFmpeg.AutoGen;
using Ionic.Zip;
using Newtonsoft.Json;
@@ -12,6 +14,7 @@ using System;
using System.IO;
using UnityEngine;
using Logger = Cryville.Common.Logging.Logger;
using unity = UnityEngine;
namespace Cryville.Crtr {
public static class Game {
@@ -58,7 +61,21 @@ namespace Cryville.Crtr {
GameDataPath = Settings.Default.GameDataPath;
Input.simulateMouseWithTouches = false;
unity::Input.simulateMouseWithTouches = false;
//InputManager.HandlerRegistries.Add(typeof(AndroidAccelerometerHandler));
//InputManager.HandlerRegistries.Add(typeof(AndroidAccelerometerUncalibratedHandler));
//InputManager.HandlerRegistries.Add(typeof(AndroidGameRotationVectorHandler));
//InputManager.HandlerRegistries.Add(typeof(AndroidGravityHandler));
//InputManager.HandlerRegistries.Add(typeof(AndroidGyroscopeHandler));
//InputManager.HandlerRegistries.Add(typeof(AndroidLinearAccelerationHandler));
//InputManager.HandlerRegistries.Add(typeof(AndroidMagneticFieldHandler));
//InputManager.HandlerRegistries.Add(typeof(AndroidMagneticFieldUncalibratedHandler));
//InputManager.HandlerRegistries.Add(typeof(AndroidRotationVectorHandler));
InputManager.HandlerRegistries.Add(typeof(AndroidTouchHandler));
InputManager.HandlerRegistries.Add(typeof(UnityGuiInputHandler<UnityKeyReceiver>));
InputManager.HandlerRegistries.Add(typeof(UnityGuiInputHandler<UnityMouseReceiver>));
InputManager.HandlerRegistries.Add(typeof(UnityMouseHandler));
InputManager.HandlerRegistries.Add(typeof(UnityTouchHandler));
InputManager = new InputManager();
#if UNITY_EDITOR_WIN

View File

@@ -1,20 +1,23 @@
using Cryville.Common;
using Cryville.Common.Logging;
using Cryville.Common.Pdt;
using Cryville.Common.Reflection;
using Cryville.Common.Unity.Input;
using Cryville.Crtr.Config;
using Cryville.Input;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using RVector3 = UnityEngine.Vector3;
using System.Threading;
using UnityEngine;
using UnityEngine.Profiling;
using Logger = Cryville.Common.Logging.Logger;
using RVector4 = UnityEngine.Vector4;
namespace Cryville.Crtr {
public class InputProxy : IDisposable {
readonly PdtEvaluator _etor;
readonly PdtRuleset _ruleset;
readonly Judge _judge;
public InputProxy(PdtRuleset ruleset, Judge judge) {
readonly InputVector _screenSize;
public InputProxy(PdtRuleset ruleset, Judge judge, Vector2 screenSize) {
for (int i = 0; i <= MAX_DEPTH; i++) {
var vecsrc = new InputVectorSrc();
_vecsrcs[i] = vecsrc;
@@ -23,6 +26,7 @@ namespace Cryville.Crtr {
_etor = judge != null ? judge._etor : ChartPlayer.etor;
_ruleset = ruleset;
_judge = judge;
_screenSize = new InputVector(screenSize.x, screenSize.y);
foreach (var i in ruleset.inputs) {
_use.Add(i.Key, 0);
_rev.Add(i.Key, new List<Identifier>());
@@ -42,10 +46,15 @@ namespace Cryville.Crtr {
public event EventHandler<ProxyChangedEventArgs> ProxyChanged;
public void LoadFrom(Dictionary<string, RulesetConfig.InputEntry> config) {
foreach (var cfg in config) {
var handler = Game.InputManager.GetHandlerByTypeName(cfg.Value.handler);
if (handler == null) {
Logger.Log("main", 3, "Input", "Uninitialized or unknown handler in ruleset config: {0}", cfg.Value.handler);
continue;
}
Set(new InputProxyEntry {
Target = new Identifier(cfg.Key),
Source = new InputSource {
Handler = Game.InputManager.GetHandler(cfg.Value.handler),
Handler = handler,
Type = cfg.Value.type
}
});
@@ -55,7 +64,7 @@ namespace Cryville.Crtr {
config.Clear();
foreach (var p in _tproxies) {
config.Add((string)p.Key.Name, new RulesetConfig.InputEntry {
handler = TypeNameHelper.GetNamespaceQualifiedName(p.Value.Source.Value.Handler.GetType()),
handler = p.Value.Source.Value.Handler.GetType().AssemblyQualifiedName,
type = p.Value.Source.Value.Type
});
}
@@ -176,7 +185,7 @@ namespace Cryville.Crtr {
static readonly int _var_value = IdentifierManager.Shared.Request("value");
const int MAX_DEPTH = 15;
const int MAX_DIMENSION = 3;
const int MAX_DIMENSION = 4;
readonly InputVectorSrc[] _vecsrcs = new InputVectorSrc[MAX_DEPTH + 1];
readonly InputVectorOp[] _vecops = new InputVectorOp[MAX_DEPTH + 1];
unsafe class InputVectorSrc : PropSrc.FixedBuffer {
@@ -186,9 +195,9 @@ namespace Cryville.Crtr {
}
}
public bool IsNull { get; set; }
public void Set(RVector3 vec) {
public void Set(RVector4 vec) {
fixed (byte* _ptr = buf) {
*(RVector3*)_ptr = vec;
*(RVector4*)_ptr = vec;
}
Invalidate();
}
@@ -204,7 +213,7 @@ namespace Cryville.Crtr {
_src.IsNull = true;
}
else {
var vec = new RVector3();
var vec = new RVector4();
int dim;
if (op.Type == PdtInternalType.Number) dim = 1;
else if (op.Type == PdtInternalType.Vector) {
@@ -228,19 +237,32 @@ namespace Cryville.Crtr {
readonly Dictionary<InputIdentifier, float> _vect = new Dictionary<InputIdentifier, float>();
readonly Dictionary<ProxiedInputIdentifier, PropSrc> _vecs = new Dictionary<ProxiedInputIdentifier, PropSrc>();
double? _lockTime = null;
unsafe void OnInput(InputIdentifier id, InputVector vec) {
lock (_etor) {
unsafe void OnInput(InputIdentifier id, InputFrame frame) {
var rc = id.Source.Handler.ReferenceCue;
if (rc.RelativeUnit == RelativeUnit.Pixel) {
frame = rc.InverseTransform(frame, _screenSize);
var vec = frame.Vector;
vec.X /= _screenSize.X; vec.Y /= _screenSize.Y;
vec.X -= 0.5f; vec.Y -= 0.5f;
vec.X *= ChartPlayer.hitRect.width; vec.Y *= ChartPlayer.hitRect.height;
frame.Vector = vec;
}
else frame = rc.InverseTransform(frame);
bool locked = false;
try {
Profiler.BeginSample("InputProxy.OnInput");
Monitor.Enter(_etor, ref locked);
InputProxyEntry proxy;
if (_sproxies.TryGetValue(id.Source, out proxy)) {
_etor.ContextCascadeInsert();
float ft, tt = (float)(_lockTime != null ? _lockTime.Value : (vec.Time - _timeOrigins[id.Source.Handler]));
float ft, tt = (float)(_lockTime != null ? _lockTime.Value : (frame.Time - _timeOrigins[id.Source.Handler]));
if (!_vect.TryGetValue(id, out ft)) ft = tt;
if (vec.IsNull) {
if (frame.IsNull) {
_etor.ContextCascadeUpdate(_var_value, PropSrc.Null);
OnInput(id, proxy.Target, ft, tt, true);
}
else {
_vecsrcs[0].Set(vec.Vector);
_vecsrcs[0].Set(new RVector4(frame.Vector.X, frame.Vector.Y, frame.Vector.Z, frame.Vector.W));
_etor.ContextCascadeUpdate(_var_value, _vecsrcs[0]);
OnInput(id, proxy.Target, ft, tt, false);
}
@@ -248,6 +270,10 @@ namespace Cryville.Crtr {
_etor.ContextCascadeDiscard();
}
}
finally {
if (locked) Monitor.Exit(_etor);
Profiler.EndSample();
}
}
static readonly int _var_fv = IdentifierManager.Shared.Request("input_vec_from");
static readonly int _var_tv = IdentifierManager.Shared.Request("input_vec_to");
@@ -295,7 +321,7 @@ namespace Cryville.Crtr {
foreach (var s in _sproxies) {
var src = s.Key;
if (_activeCounts[src] == 0) {
OnInput(new InputIdentifier { Source = src, Id = 0 }, new InputVector(_lockTime != null ? _lockTime.Value : src.Handler.GetCurrentTimestamp()));
OnInput(new InputIdentifier { Source = src, Id = 0 }, new InputFrame(_lockTime != null ? _lockTime.Value : src.Handler.GetCurrentTimestamp()));
}
}
}

View File

@@ -3,6 +3,7 @@ using Cryville.Crtr.Browsing;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using unity = UnityEngine;
namespace Cryville.Crtr {
public class Menu : MonoBehaviour {
@@ -45,7 +46,7 @@ namespace Cryville.Crtr {
m_targetAnimator.SetTrigger("T_Main");
}
}
if (Input.GetKeyDown(KeyCode.Escape)) {
if (unity::Input.GetKeyDown(KeyCode.Escape)) {
if (m_targetAnimator != null) Back();
}
}