Files
crtr/Assets/Cryville/Crtr/Config/InputConfigPanel.cs
2023-03-26 23:25:20 +08:00

110 lines
2.8 KiB
C#

using Cryville.Common;
using Cryville.Common.Unity;
using Cryville.Common.Unity.Input;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Cryville.Crtr.Config {
public class InputConfigPanel : MonoBehaviour {
[SerializeField]
ConfigPanelMaster m_configScene;
[SerializeField]
GameObject m_inputDialog;
[SerializeField]
Transform m_deviceList;
[SerializeField]
GameObject m_prefabListItem;
[SerializeField]
Transform m_entryList;
[SerializeField]
GameObject m_prefabInputConfigEntry;
SimpleInputConsumer _consumer;
public InputProxy proxy;
readonly Dictionary<Identifier, InputConfigPanelEntry> _entries = new Dictionary<Identifier, InputConfigPanelEntry>();
Identifier _sel;
public void OpenDialog(Identifier entry) {
_sel = entry;
m_inputDialog.SetActive(true);
CallHelper.Purge(m_deviceList);
_consumer.EnumerateEvents(ev => { });
_recvsrcs.Clear();
AddSourceItem(null);
}
public void CloseDialog() {
m_inputDialog.SetActive(false);
}
public void CloseDialog(InputSource? src) {
proxy.Set(new InputProxyEntry {
Target = _sel,
Source = src,
});
CloseDialog();
}
public void OnConfigEnable() {
CallHelper.Purge(m_entryList);
_entries.Clear();
_consumer = new SimpleInputConsumer(Game.InputManager);
_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);
e.SetKey(this, i.Key);
OnProxyChanged(this, proxy[i.Key]);
}
proxy.ProxyChanged += OnProxyChanged;
}
public void OnConfigDisable() {
_consumer.Deactivate();
}
void OnProxyChanged(object sender, ProxyChangedEventArgs e) {
_entries[e.Name].OnProxyChanged(e);
}
readonly List<InputSource?> _recvsrcs = new List<InputSource?>();
void Update() {
if (m_inputDialog.activeSelf) {
_consumer.EnumerateEvents(ev => {
AddSourceItem(ev.Id.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.GetDimension(src.Value.Type) < m_configScene.ruleset.Root.inputs[_sel].dim) {
text.text += " <size=9>(Not Applicable)</size>";
}
else flag = true;
btn.interactable = flag;
}
btn.onClick.AddListener(() => {
CloseDialog(src);
});
}
}
}