55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using System;
|
|
using System.Linq;
|
|
using AlicizaX;
|
|
using AlicizaX.InputGlyph;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.InputSystem;
|
|
|
|
[RequireComponent(typeof(TextMeshProUGUI))]
|
|
public class InputGlyphText : MonoBehaviour
|
|
{
|
|
[SerializeField] private InputActionReference actionReference;
|
|
private TMP_Text textField;
|
|
void OnEnable()
|
|
{
|
|
if (textField == null) textField = GetComponent<TMP_Text>();
|
|
InputDeviceWatcher.OnDeviceChanged += OnDeviceChanged;
|
|
UpdatePrompt();
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
InputDeviceWatcher.OnDeviceChanged -= OnDeviceChanged;
|
|
}
|
|
|
|
void OnBindingChanged(InputAction action, int idx)
|
|
{
|
|
if (actionReference == null || actionReference.action == null) return;
|
|
if (action == actionReference.action) UpdatePrompt();
|
|
}
|
|
|
|
void OnDeviceChanged(InputDeviceWatcher.InputDeviceCategory cat)
|
|
{
|
|
UpdatePrompt();
|
|
}
|
|
|
|
void UpdatePrompt()
|
|
{
|
|
if (actionReference == null || actionReference.action == null || textField == null) return;
|
|
var action = actionReference.action;
|
|
|
|
string path = GlyphService.GetBindingControlPath(action);
|
|
var device = InputDeviceWatcher.CurrentCategory;
|
|
string displayFallback = string.Empty;
|
|
string oldText = textField.text;
|
|
if (GlyphService.TryGetTMPTagForActionPath(path, device, out string tag, out displayFallback))
|
|
{
|
|
textField.text = Utility.Text.Format(oldText, tag);
|
|
return;
|
|
}
|
|
|
|
textField.text = Utility.Text.Format(oldText, displayFallback);;
|
|
}
|
|
}
|