Implement the new input config.
This commit is contained in:
@@ -4,155 +4,51 @@ using Cryville.Input;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Cryville.Crtr.Config.UI {
|
||||
public class InputConfigPanel : MonoBehaviour {
|
||||
[SerializeField]
|
||||
ConfigPanelMaster m_configScene;
|
||||
|
||||
[SerializeField]
|
||||
GameObject m_inputDialog;
|
||||
|
||||
[SerializeField]
|
||||
Transform m_deviceList;
|
||||
|
||||
[SerializeField]
|
||||
GameObject m_prefabListItem;
|
||||
|
||||
internal class InputConfigPanel : MonoBehaviour {
|
||||
[SerializeField]
|
||||
Transform m_entryList;
|
||||
|
||||
[SerializeField]
|
||||
GameObject m_prefabInputConfigEntry;
|
||||
|
||||
SimpleInputConsumer _consumer;
|
||||
public InputProxy proxy;
|
||||
PdtRuleset _ruleset;
|
||||
InputProxy _proxy;
|
||||
readonly Dictionary<Identifier, InputConfigPanelEntry> _entries = new Dictionary<Identifier, InputConfigPanelEntry>();
|
||||
|
||||
int _applicableEntries;
|
||||
Identifier _sel;
|
||||
int _targetDim;
|
||||
PhysicalDimension? _targetPDim;
|
||||
bool _targetNotNull;
|
||||
public void OpenDialog(Identifier entry) {
|
||||
_sel = entry;
|
||||
var def = m_configScene.ruleset.Root.inputs[_sel];
|
||||
_targetDim = def.dim;
|
||||
if (def.pdim != null) _targetPDim = ParsePhysicalDimension(def.pdim);
|
||||
else _targetPDim = null;
|
||||
_targetNotNull = def.notnull;
|
||||
m_inputDialog.SetActive(true);
|
||||
foreach (Transform i in m_deviceList) Destroy(i.gameObject);
|
||||
_consumer.EnumerateEvents(ev => { });
|
||||
_recvsrcs.Clear();
|
||||
_applicableEntries = 1;
|
||||
AddSourceItem(null);
|
||||
}
|
||||
public void Load(PdtRuleset ruleset, RulesetConfig rulesetConfig) {
|
||||
_ruleset = ruleset;
|
||||
_proxy = new InputProxy(ruleset, null, new Vector2(Screen.width, Screen.height));
|
||||
_proxy.LoadFrom(rulesetConfig.inputs);
|
||||
|
||||
static PhysicalDimension ParsePhysicalDimension(string str) {
|
||||
var comps = str.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
var result = new PhysicalDimension();
|
||||
foreach (var comp in comps) {
|
||||
int dim = 1;
|
||||
if (comp.Length > 1) dim = int.Parse(comp.Substring(1));
|
||||
switch (comp[0]) {
|
||||
case 'T': result.Time += dim; break;
|
||||
case 'L': result.Length += dim; break;
|
||||
case 'M': result.Mass += dim; break;
|
||||
case 'I': result.ElectricCurrent += dim; break;
|
||||
case '\x0398':
|
||||
case 'H': result.ThermodynamicTemperature += dim; break;
|
||||
case 'N': result.AmountOfSubstance += dim; break;
|
||||
case 'J': result.LuminousIntensity += dim; break;
|
||||
default: throw new ArgumentException(string.Format("Invalid dimension symbol {0}", comp[0]));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void CloseDialog() {
|
||||
m_inputDialog.SetActive(false);
|
||||
}
|
||||
|
||||
public void CloseDialog(InputSource? src) {
|
||||
proxy.Set(new InputProxyEntry {
|
||||
Target = _sel,
|
||||
Source = src,
|
||||
});
|
||||
CloseDialog();
|
||||
}
|
||||
|
||||
public void OnConfigEnable() {
|
||||
foreach (Transform i in m_entryList) Destroy(i.gameObject);
|
||||
_entries.Clear();
|
||||
_consumer = new SimpleInputConsumer(Game.InputManager);
|
||||
_consumer.Activate();
|
||||
foreach (var i in m_configScene.ruleset.Root.inputs) {
|
||||
var e = Instantiate(m_prefabInputConfigEntry, m_entryList.transform).GetComponent<InputConfigPanelEntry>();
|
||||
foreach (var i in ruleset.inputs) {
|
||||
var e = Instantiate(m_prefabInputConfigEntry, m_entryList.transform, false).GetComponent<InputConfigPanelEntry>();
|
||||
_entries.Add(i.Key, e);
|
||||
e.SetKey(this, i.Key);
|
||||
OnProxyChanged(this, proxy[i.Key]);
|
||||
OnProxyChanged(this, _proxy[i.Key]);
|
||||
}
|
||||
proxy.ProxyChanged += OnProxyChanged;
|
||||
_proxy.ProxyChanged += OnProxyChanged;
|
||||
}
|
||||
|
||||
public void OnConfigDisable() {
|
||||
_consumer.Deactivate();
|
||||
Identifier _selectedEntry;
|
||||
public void OpenDialog(Identifier entry) {
|
||||
_selectedEntry = entry;
|
||||
InputDialog.Show(OnDialogClosed, "Please input and select a device", _ruleset.inputs[entry], _proxy);
|
||||
}
|
||||
public void OnDialogClosed(InputSource? src) {
|
||||
_proxy.Set(new InputProxyEntry {
|
||||
Target = _selectedEntry,
|
||||
Source = src,
|
||||
});
|
||||
}
|
||||
|
||||
void OnProxyChanged(object sender, ProxyChangedEventArgs e) {
|
||||
_entries[e.Name].OnProxyChanged(e);
|
||||
}
|
||||
|
||||
void Start() {
|
||||
_d_HandleInputEvent = HandleInputEvent;
|
||||
}
|
||||
|
||||
readonly List<InputSource?> _recvsrcs = new List<InputSource?>();
|
||||
void Update() {
|
||||
if (m_inputDialog.activeSelf) {
|
||||
_consumer.EnumerateEvents(_d_HandleInputEvent);
|
||||
}
|
||||
}
|
||||
|
||||
Action<InputEvent> _d_HandleInputEvent;
|
||||
void HandleInputEvent(InputEvent ev) {
|
||||
AddSourceItem(ev.Identifier.Source);
|
||||
}
|
||||
|
||||
void AddSourceItem(InputSource? src) {
|
||||
if (_recvsrcs.Contains(src)) return;
|
||||
_recvsrcs.Add(src);
|
||||
var obj = Instantiate(m_prefabListItem, m_deviceList);
|
||||
var text = obj.transform.Find("Text").GetComponent<Text>();
|
||||
text.text = src == null ? "(None)" : src.Value.Handler.GetTypeName(src.Value.Type);
|
||||
var btn = obj.GetComponent<Button>();
|
||||
if (src != null) {
|
||||
var tsrc = src.Value;
|
||||
bool flag = false;
|
||||
if (proxy.IsUsed(tsrc)) {
|
||||
text.text += " <size=9>(Used)</size>";
|
||||
}
|
||||
else if (tsrc.Handler.Dimension < _targetDim) {
|
||||
text.text += " <size=9>(Not Applicable)</size>";
|
||||
}
|
||||
else if (_targetPDim != null && tsrc.Handler.ReferenceCue.PhysicalDimension != _targetPDim) {
|
||||
text.text += " <size=9>(Not Applicable)</size>";
|
||||
}
|
||||
else if (!_targetNotNull && !tsrc.Handler.IsNullable) {
|
||||
text.text += " <size=9>(Not Applicable)</size>";
|
||||
}
|
||||
else flag = true;
|
||||
btn.interactable = flag;
|
||||
obj.transform.SetSiblingIndex(flag ? _applicableEntries++ : m_deviceList.childCount - 1);
|
||||
}
|
||||
else {
|
||||
obj.transform.SetSiblingIndex(0);
|
||||
}
|
||||
btn.onClick.AddListener(() => {
|
||||
CloseDialog(src);
|
||||
});
|
||||
public void SaveTo(Dictionary<string, RulesetConfig.InputEntry> inputs) {
|
||||
_proxy.SaveTo(inputs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,19 +1,15 @@
|
||||
using Cryville.Common;
|
||||
using Cryville.Crtr.Ruleset;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Cryville.Crtr.Config.UI {
|
||||
public class InputConfigPanelEntry : MonoBehaviour {
|
||||
[SerializeField]
|
||||
Text m_key;
|
||||
|
||||
[SerializeField]
|
||||
Text m_value;
|
||||
|
||||
[SerializeField]
|
||||
Button m_button;
|
||||
internal class InputConfigPanelEntry : MonoBehaviour {
|
||||
[SerializeField] TextMeshProUGUI m_key;
|
||||
[SerializeField] TextMeshProUGUI m_value;
|
||||
[SerializeField] Button m_button;
|
||||
|
||||
public void SetKey(InputConfigPanel master, Identifier name) {
|
||||
m_key.text = (string)name.Name;
|
||||
@@ -39,7 +35,7 @@ namespace Cryville.Crtr.Config.UI {
|
||||
m_value.text = e.Proxy.Value.Handler.GetTypeName(e.Proxy.Value.Type);
|
||||
}
|
||||
}
|
||||
m_value.color = e.Required ? Color.yellow : Color.black;
|
||||
m_value.color = e.Required ? Color.yellow : Color.white;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
12
Assets/Cryville/Crtr/Config/UI/InputDeviceStatus.cs
Normal file
12
Assets/Cryville/Crtr/Config/UI/InputDeviceStatus.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace Cryville.Crtr.Config.UI {
|
||||
[Flags]
|
||||
public enum InputDeviceStatus {
|
||||
Default = 0x00,
|
||||
Used = 0x01,
|
||||
InsufficientDimension = 0x02,
|
||||
IncompatiblePhysicalDimension = 0x04,
|
||||
IncompatibleNullable = 0x08,
|
||||
}
|
||||
}
|
11
Assets/Cryville/Crtr/Config/UI/InputDeviceStatus.cs.meta
Normal file
11
Assets/Cryville/Crtr/Config/UI/InputDeviceStatus.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aa74ad1769b9e1442b4659fbad2feb59
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
135
Assets/Cryville/Crtr/Config/UI/InputDialog.cs
Normal file
135
Assets/Cryville/Crtr/Config/UI/InputDialog.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
using Cryville.Crtr.Ruleset;
|
||||
using Cryville.Input;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cryville.Crtr.Config.UI {
|
||||
internal class InputDialog : MonoBehaviour {
|
||||
static InputDialog _instance;
|
||||
[SerializeField] CanvasGroup m_group;
|
||||
[SerializeField] AnimationCurve m_fadeCurve;
|
||||
[SerializeField] TextMeshProUGUI m_msgText;
|
||||
[SerializeField] Transform m_deviceList;
|
||||
[SerializeField] GameObject m_deviceItemPrefab;
|
||||
float _fadeDuration;
|
||||
void Awake() {
|
||||
_instance = this;
|
||||
_fadeDuration = m_fadeCurve[m_fadeCurve.length - 1].time;
|
||||
_consumer = new SimpleInputConsumer(Game.InputManager);
|
||||
_d_HandleInputEvent = HandleInputEvent;
|
||||
}
|
||||
float _timer;
|
||||
bool _active;
|
||||
Action<InputSource?> _callback;
|
||||
|
||||
InputProxy _proxy;
|
||||
int _applicableEntries;
|
||||
int _targetDim;
|
||||
PhysicalDimension? _targetPDim;
|
||||
bool _targetNotNull;
|
||||
readonly Dictionary<InputSource, InputDialogEntry> _recvsrcs = new Dictionary<InputSource, InputDialogEntry>();
|
||||
void ShowInternal(Action<InputSource?> callback, string message, InputDefinition def, InputProxy proxy) {
|
||||
_active = true;
|
||||
_callback = callback;
|
||||
m_group.gameObject.SetActive(true);
|
||||
m_msgText.text = message;
|
||||
_proxy = proxy;
|
||||
|
||||
_targetDim = def.dim;
|
||||
if (def.pdim != null) _targetPDim = ParsePhysicalDimension(def.pdim);
|
||||
else _targetPDim = null;
|
||||
_targetNotNull = def.notnull;
|
||||
|
||||
foreach (Transform i in m_deviceList) Destroy(i.gameObject);
|
||||
_consumer.EnumerateEvents(ev => { });
|
||||
_recvsrcs.Clear();
|
||||
_applicableEntries = 1;
|
||||
AddSourceItem(null).transform.SetSiblingIndex(0);
|
||||
}
|
||||
public static void Show(Action<InputSource?> callback, string message, InputDefinition def, InputProxy proxy) {
|
||||
_instance.ShowInternal(callback, message, def, proxy);
|
||||
}
|
||||
static PhysicalDimension ParsePhysicalDimension(string str) {
|
||||
var comps = str.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
var result = new PhysicalDimension();
|
||||
foreach (var comp in comps) {
|
||||
int dim = 1;
|
||||
if (comp.Length > 1) dim = int.Parse(comp.Substring(1));
|
||||
switch (comp[0]) {
|
||||
case 'T': result.Time += dim; break;
|
||||
case 'L': result.Length += dim; break;
|
||||
case 'M': result.Mass += dim; break;
|
||||
case 'I': result.ElectricCurrent += dim; break;
|
||||
case '\x0398':
|
||||
case 'H': result.ThermodynamicTemperature += dim; break;
|
||||
case 'N': result.AmountOfSubstance += dim; break;
|
||||
case 'J': result.LuminousIntensity += dim; break;
|
||||
default: throw new ArgumentException(string.Format("Invalid dimension symbol {0}", comp[0]));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void Update() {
|
||||
if (_active) {
|
||||
_consumer.EnumerateEvents(_d_HandleInputEvent);
|
||||
if (_timer >= _fadeDuration) return;
|
||||
_timer += Time.deltaTime;
|
||||
if (_timer > _fadeDuration) _timer = _fadeDuration;
|
||||
m_group.alpha = m_fadeCurve.Evaluate(_timer);
|
||||
}
|
||||
else {
|
||||
if (_timer <= 0) return;
|
||||
_timer -= Time.deltaTime;
|
||||
if (_timer < 0) {
|
||||
_timer = 0;
|
||||
m_group.gameObject.SetActive(false);
|
||||
}
|
||||
m_group.alpha = m_fadeCurve.Evaluate(_timer);
|
||||
}
|
||||
}
|
||||
|
||||
SimpleInputConsumer _consumer;
|
||||
void OnEnable() { _consumer.Activate(); }
|
||||
void OnDisable() { _consumer.Deactivate(); }
|
||||
|
||||
Action<InputEvent> _d_HandleInputEvent;
|
||||
void HandleInputEvent(InputEvent ev) {
|
||||
InputSource src = ev.Identifier.Source;
|
||||
InputDialogEntry entry;
|
||||
if (!_recvsrcs.TryGetValue(src, out entry)) {
|
||||
_recvsrcs.Add(src, entry = AddSourceItem(src));
|
||||
if (_proxy.IsUsed(src)) {
|
||||
entry.Status |= InputDeviceStatus.Used;
|
||||
}
|
||||
if (src.Handler.Dimension < _targetDim) {
|
||||
entry.Status |= InputDeviceStatus.InsufficientDimension;
|
||||
}
|
||||
if (_targetPDim != null && src.Handler.ReferenceCue.PhysicalDimension != _targetPDim) {
|
||||
entry.Status |= InputDeviceStatus.IncompatiblePhysicalDimension;
|
||||
}
|
||||
if (!_targetNotNull && !src.Handler.IsNullable) {
|
||||
entry.Status |= InputDeviceStatus.IncompatibleNullable;
|
||||
}
|
||||
entry.transform.SetSiblingIndex(entry.Status == InputDeviceStatus.Default ? _applicableEntries++ : m_deviceList.childCount - 1);
|
||||
}
|
||||
entry.OnInputEvent(ev);
|
||||
}
|
||||
InputDialogEntry AddSourceItem(InputSource? src) {
|
||||
var entry = Instantiate(m_deviceItemPrefab, m_deviceList, false).GetComponent<InputDialogEntry>();
|
||||
entry.Init(src);
|
||||
entry.Clicked += () => OnItemSelected(src);
|
||||
return entry;
|
||||
}
|
||||
void OnItemSelected(InputSource? src) {
|
||||
if (!_active) return;
|
||||
_active = false;
|
||||
_callback(src);
|
||||
}
|
||||
public void Close() {
|
||||
_active = false;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Crtr/Config/UI/InputDialog.cs.meta
Normal file
11
Assets/Cryville/Crtr/Config/UI/InputDialog.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b25b6477a93b5f64495739801fbdb19d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
101
Assets/Cryville/Crtr/Config/UI/InputDialogEntry.cs
Normal file
101
Assets/Cryville/Crtr/Config/UI/InputDialogEntry.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using Cryville.Input;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Cryville.Crtr.Config.UI {
|
||||
internal class InputDialogEntry : MonoBehaviour, IPointerClickHandler {
|
||||
[SerializeField] Button m_button;
|
||||
[SerializeField] TextMeshProUGUI m_nameText;
|
||||
[SerializeField] Graphic m_iNull;
|
||||
[SerializeField] Graphic m_iX;
|
||||
[SerializeField] Graphic m_iY;
|
||||
[SerializeField] Graphic m_iZ;
|
||||
[SerializeField] Graphic m_iW;
|
||||
[SerializeField] Color m_invalidColor;
|
||||
[SerializeField] Color m_inactiveColor;
|
||||
[SerializeField] Color m_activeColor;
|
||||
[SerializeField] Color m_restColor;
|
||||
[SerializeField] Color m_motionColor;
|
||||
private InputDeviceStatus status;
|
||||
|
||||
public event Action Clicked;
|
||||
public InputDeviceStatus Status {
|
||||
get { return status; }
|
||||
set {
|
||||
status = value;
|
||||
m_button.interactable = value == InputDeviceStatus.Default;
|
||||
}
|
||||
}
|
||||
|
||||
byte _dim;
|
||||
|
||||
void Awake() {
|
||||
_activeInputs = new Dictionary<int, InputVector>();
|
||||
}
|
||||
|
||||
public void Init(InputSource? src) {
|
||||
m_nameText.text = src == null ? "(None)" : src.Value.Handler.GetTypeName(src.Value.Type);
|
||||
if (src == null) {
|
||||
m_iNull.color = m_invalidColor;
|
||||
m_iX.color = m_invalidColor;
|
||||
m_iY.color = m_invalidColor;
|
||||
m_iZ.color = m_invalidColor;
|
||||
m_iW.color = m_invalidColor;
|
||||
}
|
||||
else {
|
||||
var tsrc = src.Value;
|
||||
_dim = tsrc.Handler.Dimension;
|
||||
m_iNull.color = m_inactiveColor;
|
||||
m_iX.color = _dim >= 1 ? m_restColor : m_invalidColor;
|
||||
m_iY.color = _dim >= 2 ? m_restColor : m_invalidColor;
|
||||
m_iZ.color = _dim >= 3 ? m_restColor : m_invalidColor;
|
||||
m_iW.color = _dim >= 4 ? m_restColor : m_invalidColor;
|
||||
}
|
||||
}
|
||||
public void OnPointerClick(PointerEventData eventData) {
|
||||
if (status != InputDeviceStatus.Default) return;
|
||||
Clicked?.Invoke();
|
||||
}
|
||||
|
||||
float _maxScalar;
|
||||
Dictionary<int, InputVector> _activeInputs;
|
||||
public void OnInputEvent(InputEvent ev) {
|
||||
var id = ev.Identifier.Id;
|
||||
InputVector lastVec;
|
||||
if (!_activeInputs.TryGetValue(id, out lastVec)) {
|
||||
if (ev.To.IsNull) return;
|
||||
_activeInputs.Add(id, lastVec = ev.To.Vector);
|
||||
}
|
||||
if (ev.To.IsNull) {
|
||||
_activeInputs.Remove(id);
|
||||
}
|
||||
else {
|
||||
var vec = ev.To.Vector;
|
||||
var delta = vec - lastVec;
|
||||
if (_dim >= 1) {
|
||||
UpdateVectorIndicator(m_iX, delta.X);
|
||||
if (_dim >= 2) {
|
||||
UpdateVectorIndicator(m_iY, delta.Y);
|
||||
if (_dim >= 3) {
|
||||
UpdateVectorIndicator(m_iZ, delta.Z);
|
||||
if (_dim >= 4) {
|
||||
UpdateVectorIndicator(m_iW, delta.W);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_activeInputs[id] = vec;
|
||||
}
|
||||
m_iNull.color = _activeInputs.Count > 0 ? m_activeColor : m_inactiveColor;
|
||||
}
|
||||
void UpdateVectorIndicator(Graphic i, float v) {
|
||||
float a = MathF.Abs(v);
|
||||
if (a > _maxScalar) _maxScalar = a;
|
||||
i.color = Color.Lerp(m_restColor, m_motionColor, MathF.Sqrt(a / _maxScalar));
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Cryville/Crtr/Config/UI/InputDialogEntry.cs.meta
Normal file
11
Assets/Cryville/Crtr/Config/UI/InputDialogEntry.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d709b6ddbb7cdcf4d90ef334f8212e5f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user