68 lines
2.9 KiB
C#
68 lines
2.9 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using BaseGames.Input;
|
||
|
||
namespace BaseGames.UI.Settings
|
||
{
|
||
/// <summary>
|
||
/// 按键重绑定面板(架构 04_InputModule §6)。
|
||
/// 统一管理所有 RebindActionRow;协调"同时只有一行处于重绑定状态"的排他锁;
|
||
/// 提供"重置全部"按钮;绑定完成后自动持久化。
|
||
/// 挂载路径:Canvas_Overlay → SettingsPanel → KeybindingTab → RebindPanel
|
||
/// </summary>
|
||
public class RebindPanel : MonoBehaviour
|
||
{
|
||
[SerializeField] private InputReaderSO _inputReader;
|
||
[SerializeField] private RebindActionRow[] _rows; // Inspector 配置,顺序对应 UI 布局
|
||
[SerializeField] private Button _resetAllButton;
|
||
[SerializeField] private ConflictDetector _conflictDetector;
|
||
|
||
// ── 生命周期 ──────────────────────────────────────────────────────
|
||
|
||
private void Awake()
|
||
{
|
||
_resetAllButton?.onClick.AddListener(OnResetAll);
|
||
|
||
if (_rows == null) return;
|
||
foreach (var row in _rows)
|
||
row.Initialize(_inputReader, _conflictDetector, OnRebindRequested);
|
||
}
|
||
|
||
// ── 供 RebindActionRow 反查行列表 ─────────────────────────────────
|
||
|
||
/// <summary>返回此面板管理的所有行(供 RebindActionRow 刷新冲突高亮时遍历)。</summary>
|
||
public RebindActionRow[] GetRows() => _rows;
|
||
|
||
// ── 内部逻辑 ──────────────────────────────────────────────────────
|
||
|
||
/// <summary>由 RebindActionRow 点击时回调;实现排他锁(同时只允许一行重绑定)。</summary>
|
||
private void OnRebindRequested(RebindActionRow requestingRow)
|
||
{
|
||
// 禁用其他所有行,防止并发重绑定操作
|
||
if (_rows != null)
|
||
foreach (var row in _rows)
|
||
row.SetInteractable(row == requestingRow);
|
||
|
||
requestingRow.StartRebind(onFinished: () =>
|
||
{
|
||
// 重绑定完成(或取消)→ 恢复所有行可交互 → 持久化
|
||
if (_rows != null)
|
||
foreach (var row in _rows)
|
||
row.SetInteractable(true);
|
||
|
||
_inputReader?.SaveBindingOverrides();
|
||
});
|
||
}
|
||
|
||
/// <summary>"重置全部"按钮回调:恢复默认绑定并刷新所有行显示。</summary>
|
||
private void OnResetAll()
|
||
{
|
||
_inputReader?.ResetBindings();
|
||
|
||
if (_rows != null)
|
||
foreach (var row in _rows)
|
||
row.RefreshDisplay();
|
||
}
|
||
}
|
||
}
|