154 lines
4.8 KiB
C#
154 lines
4.8 KiB
C#
using UnityEngine;
|
||
using TMPro;
|
||
using System;
|
||
using UnityEngine.UI;
|
||
|
||
public class InputManager : MonoBehaviour
|
||
{
|
||
public static InputManager Instance { get; private set; }
|
||
public static event Action<KeyCode> OnKeyPressed;
|
||
public static event Action<KeyCode> OnKeyReleased;
|
||
|
||
[Header("轨道判定显示TMP对象(请在Inspector中连接)")]
|
||
public TextMeshProUGUI[] trackJudgeTexts = new TextMeshProUGUI[5];
|
||
|
||
[Header("按键指示文本(独立于判定文本,五个分别对应按键:D,F,Space,J,K)")]
|
||
public Text[] trackKeyTexts = new Text[5];
|
||
|
||
[Header("是否显示判定文字")]
|
||
public bool showJudgeText = true;
|
||
|
||
// 按键激活/非激活颜色
|
||
[Header("按键高亮颜色设置")]
|
||
[Tooltip("按键被按下时的颜色")]
|
||
public Color keyActiveColor = Color.yellow;
|
||
[Tooltip("按键未按下时的颜色(默认黑色)")]
|
||
public Color keyInactiveColor = Color.black;
|
||
|
||
// 判定结果对应颜色
|
||
[Header("判定结果对应颜色")]
|
||
public Color perfectColor = Color.yellow;
|
||
public Color greatColor = Color.green;
|
||
public Color goodColor = Color.cyan;
|
||
public Color missColor = Color.red;
|
||
|
||
private void Awake()
|
||
{
|
||
if (Instance == null)
|
||
Instance = this;
|
||
else
|
||
Destroy(gameObject);
|
||
|
||
// 初始化按键文本为非激活颜色
|
||
if (trackKeyTexts != null)
|
||
{
|
||
foreach (var t in trackKeyTexts)
|
||
{
|
||
if (t != null) t.color = keyInactiveColor;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
// Ensure UI shows current bindings from KeyBindingManager/PlayerPrefs
|
||
RefreshKeyLabels();
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
foreach (string color in new string[] { "red", "green", "yellow", "purple", "blue" })
|
||
{
|
||
KeyCode key = KeyBindingManager.GetKeyForColor(color);
|
||
if (key == KeyCode.None) continue;
|
||
|
||
int index = GetIndexForColor(color);
|
||
|
||
if (Input.GetKeyDown(key))
|
||
{
|
||
OnKeyPressed?.Invoke(key);
|
||
// 调用 JudgeManager 统一判断最早的音符
|
||
JudgeManager.Instance.JudgeEarliestNote(key);
|
||
|
||
// 按下时设置对应的按键文本颜色为激活色(如果已设置)
|
||
if (trackKeyTexts != null && index >= 0 && index < trackKeyTexts.Length)
|
||
{
|
||
var txt = trackKeyTexts[index];
|
||
if (txt != null)
|
||
txt.color = keyActiveColor;
|
||
}
|
||
}
|
||
if (Input.GetKeyUp(key))
|
||
{
|
||
OnKeyReleased?.Invoke(key);
|
||
|
||
// 松开时恢复为非激活颜色
|
||
if (trackKeyTexts != null && index >= 0 && index < trackKeyTexts.Length)
|
||
{
|
||
var txt = trackKeyTexts[index];
|
||
if (txt != null)
|
||
txt.color = keyInactiveColor;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// Refresh displayed labels for key bindings (called after rebind)
|
||
public void RefreshKeyLabels()
|
||
{
|
||
string[] colors = new string[] { "red", "green", "yellow", "purple", "blue" };
|
||
if (trackKeyTexts == null) return;
|
||
for (int i = 0; i < colors.Length && i < trackKeyTexts.Length; i++)
|
||
{
|
||
var txt = trackKeyTexts[i];
|
||
if (txt == null) continue;
|
||
KeyCode k = KeyBindingManager.GetKeyForColor(colors[i]);
|
||
txt.text = KeyBindingManager.GetDisplayName(k);
|
||
}
|
||
}
|
||
|
||
private int GetIndexForColor(string color)
|
||
{
|
||
// 保持与 trackJudgeTexts 相同的索引映射
|
||
switch (color)
|
||
{
|
||
case "red": return 0;
|
||
case "green": return 1;
|
||
case "yellow": return 2;
|
||
case "purple": return 3;
|
||
case "blue": return 4;
|
||
default: return -1;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新指定轨道的判定文本和颜色
|
||
/// </summary>
|
||
public void ShowJudgeResult(int trackIndex, string result)
|
||
{
|
||
if (!showJudgeText) return;
|
||
if (trackJudgeTexts == null || trackIndex < 0 || trackIndex >= trackJudgeTexts.Length) return;
|
||
var textObj = trackJudgeTexts[trackIndex];
|
||
if (textObj == null) return;
|
||
textObj.text = result;
|
||
switch (result)
|
||
{
|
||
case "Perfect":
|
||
textObj.color = perfectColor;
|
||
break;
|
||
case "Great":
|
||
textObj.color = greatColor;
|
||
break;
|
||
case "Good":
|
||
textObj.color = goodColor;
|
||
break;
|
||
case "Miss":
|
||
textObj.color = missColor;
|
||
break;
|
||
default:
|
||
textObj.color = Color.white;
|
||
break;
|
||
}
|
||
}
|
||
}
|