64 lines
1.9 KiB
C#
64 lines
1.9 KiB
C#
using System;
|
|
using System.Linq;
|
|
using AlicizaX;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.InputSystem;
|
|
|
|
[RequireComponent(typeof(TextMeshProUGUI))]
|
|
public sealed class InputGlyphText : MonoBehaviour
|
|
{
|
|
[SerializeField] private InputActionReference actionReference;
|
|
private TMP_Text textField;
|
|
private string _oldText;
|
|
private InputDeviceWatcher.InputDeviceCategory _cachedCategory;
|
|
private string _cachedFormattedText;
|
|
|
|
void OnEnable()
|
|
{
|
|
if (textField == null) textField = GetComponent<TMP_Text>();
|
|
InputDeviceWatcher.OnDeviceChanged += OnDeviceChanged;
|
|
_oldText = textField.text;
|
|
_cachedCategory = InputDeviceWatcher.InputDeviceCategory.Keyboard;
|
|
UpdatePrompt();
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
InputDeviceWatcher.OnDeviceChanged -= OnDeviceChanged;
|
|
}
|
|
|
|
void OnDeviceChanged(InputDeviceWatcher.InputDeviceCategory cat)
|
|
{
|
|
if (_cachedCategory != cat)
|
|
{
|
|
_cachedCategory = cat;
|
|
UpdatePrompt();
|
|
}
|
|
}
|
|
|
|
void UpdatePrompt()
|
|
{
|
|
if (actionReference == null || actionReference.action == null || textField == null) return;
|
|
|
|
// Use cached category instead of re-querying CurrentCategory
|
|
if (GlyphService.TryGetTMPTagForActionPath(actionReference, "", _cachedCategory, out string tag, out string displayFallback))
|
|
{
|
|
string formattedText = Utility.Text.Format(_oldText, tag);
|
|
if (_cachedFormattedText != formattedText)
|
|
{
|
|
_cachedFormattedText = formattedText;
|
|
textField.text = formattedText;
|
|
}
|
|
return;
|
|
}
|
|
|
|
string fallbackText = Utility.Text.Format(_oldText, displayFallback);
|
|
if (_cachedFormattedText != fallbackText)
|
|
{
|
|
_cachedFormattedText = fallbackText;
|
|
textField.text = fallbackText;
|
|
}
|
|
}
|
|
}
|