This commit is contained in:
2026-06-07 11:49:55 +08:00
parent ff0f3bde54
commit 1897658a00
98 changed files with 9903 additions and 13907 deletions

View File

@@ -1,31 +1,30 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;
using BaseGames.UI;
using BaseGames.Localization;
namespace BaseGames.UI.Menus
{
/// <summary>
/// 通用模态确认对话框(是/否):用于删除 / 覆盖 / 退出等确认场景。
/// 通用模态确认对话框(是/否):用于删除 / 覆盖 / 退出 / 传送等确认场景。
///
/// 设计:
/// · 自包含、场景无关——通过本地 SetActive 显隐 + 回调 API 工作,不依赖 UIManager 面板栈,
/// 因此既能用于主菜单场景(不走 UIManager也能在游戏内复用。
/// · 标题 / 正文 / 按钮文案均走本地化键LocalizationManager.Get传 null 则保留 Inspector 原文。
/// · 默认焦点置于"取消"按钮,防止手柄连按误触确认(破坏性操作安全默认)。
/// <para>双调用模式(同类不同实例各用其一,互不冲突):</para>
/// <list type="bullet">
/// <item><b>导航器 async主菜单</b><see cref="ShowAsync"/> 经 <see cref="IUINavigator"/> 压栈,
/// 栈式回退、ESC 取消、焦点恢复统一由导航器负责。</item>
/// <item><b>回调 legacy游戏内地图传送等尚未接入导航器的场景</b><see cref="Show"/> 本地 SetActive
/// 显隐 + onConfirm/onCancel 回调,不依赖导航器。</item>
/// </list>
///
/// 用法:
/// _confirmDialog.Show("CONFIRM_DELETE_TITLE", "CONFIRM_DELETE_BODY",
/// onConfirm: () => DoDelete(),
/// onCancel: () => {});
/// 标题 / 正文 / 按钮文案走本地化键(<see cref="LocalizationManager"/>);传 null 保留 Inspector 原文。
/// 默认焦点置于"取消",防手柄/键盘连按误触破坏性确认。
/// </summary>
public class ConfirmDialogController : MonoBehaviour
public class ConfirmDialogController : UIResultPanel<bool>
{
[Header("根节点(显隐用,留空则用本 GameObject")]
[SerializeField] private GameObject _root;
[Header("文本")]
[SerializeField] private TMP_Text _titleText;
[SerializeField] private TMP_Text _bodyText;
@@ -38,78 +37,89 @@ namespace BaseGames.UI.Menus
[SerializeField] private Button _btnConfirm;
[SerializeField] private Button _btnCancel;
private Action _onConfirm;
private Action _onCancel;
// 取消 / ESC / 销毁默认结果:否。
protected override bool CancelResult => false;
// legacy 回调模式状态
private Action _legacyConfirm;
private Action _legacyCancel;
private bool _legacyMode;
private void Awake()
{
_btnConfirm?.onClick.AddListener(HandleConfirm);
_btnCancel? .onClick.AddListener(HandleCancel);
SetVisible(false);
_btnConfirm?.onClick.AddListener(() => OnButton(true));
_btnCancel? .onClick.AddListener(() => OnButton(false));
// 不在此 SetActive(false):面板初始由场景/脚手架序列化为隐藏,激活完全交给导航器
// (对象初始 inactive 时 Awake 会被推迟到首次激活才执行,若在此关闭会立刻自我隐藏)。
}
/// <summary>
/// 弹出确认框。
/// </summary>
/// <param name="titleKey">标题本地化键null 保留原文。</param>
/// <param name="bodyKey">正文本地化键null 保留原文。</param>
/// <param name="onConfirm">点击确认后回调(确认框已自动关闭)。</param>
/// <param name="onCancel">点击取消后回调(可选)。</param>
/// <param name="confirmKey">确认按钮文案本地化键(可选)。</param>
/// <param name="cancelKey">取消按钮文案本地化键(可选)。</param>
/// <summary>默认焦点:取消按钮(破坏性操作安全默认)。</summary>
protected override GameObject ResolveFirstSelected()
=> _btnCancel != null ? _btnCancel.gameObject
: _btnConfirm != null ? _btnConfirm.gameObject : null;
// ── 导航器 async 路径(主菜单)────────────────────────────────────────
/// <summary>弹出确认框并等待结果true=确认 / false=取消)。由导航器压栈管理。</summary>
public Task<bool> ShowAsync(string titleKey, string bodyKey, CancellationToken ct = default,
string confirmKey = null, string cancelKey = null)
{
_legacyMode = false;
ApplyText(titleKey, bodyKey, confirmKey, cancelKey);
var nav = GetService<IUINavigator>();
if (nav == null)
{
Debug.LogError("[ConfirmDialog] 未找到 IUINavigator 服务,无法以 async 模式弹出。", this);
return Task.FromResult(false);
}
return nav.PushForResultAsync<bool>(this, ct);
}
// ── legacy 回调路径(游戏内尚未接入导航器的调用方)──────────────────
/// <summary>弹出确认框(回调式,本地显隐,不走导航器)。</summary>
public void Show(string titleKey, string bodyKey, Action onConfirm, Action onCancel = null,
string confirmKey = null, string cancelKey = null)
{
_onConfirm = onConfirm;
_onCancel = onCancel;
if (_titleText != null && titleKey != null) _titleText.text = Loc(titleKey);
if (_bodyText != null && bodyKey != null) _bodyText.text = Loc(bodyKey);
if (_confirmLabel != null && confirmKey != null) _confirmLabel.text = Loc(confirmKey);
if (_cancelLabel != null && cancelKey != null) _cancelLabel.text = Loc(cancelKey);
SetVisible(true);
// 安全默认:焦点置于取消,避免手柄/键盘连按直接确认破坏性操作
EventSystem.current?.SetSelectedGameObject(_btnCancel != null
? _btnCancel.gameObject
: _btnConfirm?.gameObject);
_legacyMode = true;
_legacyConfirm = onConfirm;
_legacyCancel = onCancel;
ApplyText(titleKey, bodyKey, confirmKey, cancelKey);
gameObject.SetActive(true); // OnEnable 经 UIPanelBase 自动聚焦取消按钮
}
/// <summary>外部强制关闭(如父面板被关闭时)。不触发任何回调。</summary>
/// <summary>外部强制关闭(仅 legacy 模式有效)。不触发任何回调。</summary>
public void Close()
{
_onConfirm = null;
_onCancel = null;
SetVisible(false);
if (!_legacyMode) return;
_legacyConfirm = null;
_legacyCancel = null;
gameObject.SetActive(false);
}
// ── 按钮回调 ──────────────────────────────────────────────────────────
private void HandleConfirm()
// ── 按钮 ──────────────────────────────────────────────────────────────
private void OnButton(bool confirmed)
{
var cb = _onConfirm;
SetVisible(false);
_onConfirm = null;
_onCancel = null;
cb?.Invoke();
}
private void HandleCancel()
{
var cb = _onCancel;
SetVisible(false);
_onConfirm = null;
_onCancel = null;
cb?.Invoke();
if (_legacyMode)
{
var cb = confirmed ? _legacyConfirm : _legacyCancel;
_legacyConfirm = null;
_legacyCancel = null;
gameObject.SetActive(false);
cb?.Invoke();
}
else
{
Complete(confirmed); // 设置结果 + 由导航器出栈
}
}
// ── 工具 ──────────────────────────────────────────────────────────────
private void SetVisible(bool visible)
private void ApplyText(string titleKey, string bodyKey, string confirmKey, string cancelKey)
{
var go = _root != null ? _root : gameObject;
go.SetActive(visible);
if (_titleText != null && titleKey != null) _titleText.text = Loc(titleKey);
if (_bodyText != null && bodyKey != null) _bodyText.text = Loc(bodyKey);
if (_confirmLabel != null && confirmKey != null) _confirmLabel.text = Loc(confirmKey);
if (_cancelLabel != null && cancelKey != null) _cancelLabel.text = Loc(cancelKey);
}
private static string Loc(string key)

View File

@@ -0,0 +1,102 @@
using System.Collections.Generic;
using UnityEngine;
using BaseGames.Core.Assets;
using BaseGames.Core.Events;
using BaseGames.UI.MainMenu; // 复用通用菜单按钮视图 MainMenuButtonView
namespace BaseGames.UI
{
/// <summary>
/// 数据驱动暂停菜单(仿 <see cref="MainMenu.DataDrivenMainMenuController"/>)。
/// 据 <see cref="PauseMenuConfigSO"/> 在运行时生成按钮、派发动作;生命周期/焦点/淡入由 <see cref="UIPanelBase"/> 统一处理。
/// 策划改 UI_PauseMenuConfig 即可增删/重排/改标签/改动作,零代码;样式改 UI_PauseScreen / UI_MainMenu_Button 预制件。
/// </summary>
public class DataDrivenPauseMenuController : UIPanelBase
{
[Header("数据表 / 按钮列表")]
[SerializeField] private PauseMenuConfigSO _config;
[Tooltip("按钮的父节点(通常挂 VerticalLayoutGroup。")]
[SerializeField] private Transform _container;
[SerializeField] private MainMenuButtonView _buttonPrefab;
[Header("Event Channels")]
[SerializeField] private VoidEventChannelSO _onResumeRequested;
[SerializeField] private SceneLoadRequestEventChannelSO _onSceneLoadRequest;
private IUIManager _uiManager;
private readonly List<MainMenuButtonView> _buttons = new();
private MainMenuButtonView _firstButton;
// 暂停面板由 UIManager 开启,此时 ServiceLocator 已就绪
protected override void OnPanelOpen()
{
_uiManager = GetService<IUIManager>();
BuildMenu();
}
protected override void OnPanelClose() => _uiManager = null;
/// <summary>默认焦点 / 焦点恢复回到首个按钮。</summary>
protected override GameObject ResolveFirstSelected()
=> _firstButton != null ? _firstButton.Button.gameObject : null;
/// <summary>据配置重建按钮列表public 以便编辑器预览/测试)。</summary>
public void BuildMenu()
{
ClearMenu();
if (_config == null || _container == null || _buttonPrefab == null) return;
foreach (var item in _config.Items)
{
var view = Instantiate(_buttonPrefab, _container);
view.gameObject.SetActive(true);
var captured = item;
view.Bind(item.labelKey, item.icon, () => Dispatch(captured));
_buttons.Add(view);
if (_firstButton == null) _firstButton = view;
}
}
private void ClearMenu()
{
_buttons.Clear();
_firstButton = null;
if (_container == null) return;
for (int i = _container.childCount - 1; i >= 0; i--)
{
var child = _container.GetChild(i).gameObject;
if (Application.isPlaying) Destroy(child);
else DestroyImmediate(child);
}
}
// ── 动作派发 ──────────────────────────────────────────────────────────
private void Dispatch(PauseMenuConfigSO.Item item)
{
switch (item.action)
{
case PauseMenuAction.Resume:
_onResumeRequested?.Raise();
_uiManager?.CloseTopPanel();
break;
case PauseMenuAction.OpenSettings:
_uiManager?.OpenPanel(PanelId.Settings);
break;
case PauseMenuAction.ReturnToMainMenu:
_uiManager?.CloseTopPanel();
_onSceneLoadRequest?.Raise(new SceneLoadRequest
{
SceneName = string.IsNullOrEmpty(item.sceneKey) ? AddressKeys.SceneMainMenu : item.sceneKey,
TransitionType = TransitionType.Scene,
});
break;
case PauseMenuAction.Quit:
Application.Quit();
break;
case PauseMenuAction.RaiseEvent:
item.eventChannel?.Raise();
break;
}
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 8adf13ec10899df439ee33bc9dcbcdeb
guid: 70ebf028cc731414caf75f2c7f4c28b4
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -0,0 +1,48 @@
using System;
using UnityEngine;
using BaseGames.Core.Events;
namespace BaseGames.UI
{
/// <summary>暂停菜单项动作类型。常用动作内置;任意自定义走事件频道。</summary>
public enum PauseMenuAction
{
Resume, // 继续游戏(关闭暂停面板)
OpenSettings, // 打开设置面板
ReturnToMainMenu, // 返回主菜单(场景加载)
Quit, // 退出游戏
RaiseEvent, // 触发 eventChannel万能扩展
}
/// <summary>
/// 暂停菜单数据驱动表(策划编辑)。按顺序列出暂停菜单项;
/// <see cref="DataDrivenPauseMenuController"/> 据此生成按钮并派发动作。
/// 策划可增删 / 重排 / 改标签图标 / 改动作,无需改代码。样式改 UI_PauseScreen / UI_MainMenu_Button 预制件。
/// </summary>
[CreateAssetMenu(menuName = "BaseGames/UI/Pause Menu Config", fileName = "UI_PauseMenuConfig")]
public class PauseMenuConfigSO : ScriptableObject
{
[Serializable]
public struct Item
{
[Tooltip("按钮标签本地化 KeyUI 表)。")]
public string labelKey;
[Tooltip("按钮图标(可空)。")]
public Sprite icon;
[Tooltip("点击动作。")]
public PauseMenuAction action;
[Tooltip("ReturnToMainMenu 的目标场景 Addressable Key留空用默认主菜单。")]
public string sceneKey;
[Tooltip("RaiseEvent 动作触发的事件频道。")]
public VoidEventChannelSO eventChannel;
}
[SerializeField] private Item[] _items;
public Item[] Items => _items;
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 93f9600681435a74187c249850a0f71c
guid: a7dd5202b8fce1d40b073624a3b22953
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -1,68 +0,0 @@
using UnityEngine;
using UnityEngine.UI;
using BaseGames.Core.Assets;
using BaseGames.Core.Events;
namespace BaseGames.UI
{
/// <summary>
/// 暂停菜单控制器(架构 10_UIModule §5
/// 挂载在 Canvas_Menu → PauseMenuPanel GameObject 上。
/// 按钮绑定在 Awake 中完成;生命周期 / 焦点由 <see cref="UIPanelBase"/> 统一处理。
/// </summary>
public class PauseMenuController : UIPanelBase
{
// UIManager 通过 ServiceLocator 解析,开启时自动获取,无需 Inspector 直接绑定具体类型
private IUIManager _uiManager;
[Header("按钮引用")]
[SerializeField] private Button _btnResume;
[SerializeField] private Button _btnSettings;
[SerializeField] private Button _btnMainMenu;
[SerializeField] private Button _btnQuit;
[Header("Event Channels")]
[SerializeField] private VoidEventChannelSO _onResumeRequested;
[SerializeField] private SceneLoadRequestEventChannelSO _onSceneLoadRequest;
private void Awake()
{
_btnResume?.onClick.AddListener(Resume);
_btnSettings?.onClick.AddListener(OpenSettings);
_btnMainMenu?.onClick.AddListener(GoToMainMenu);
_btnQuit?.onClick.AddListener(Application.Quit);
}
// 暂停面板由 UIManager 开启,此时 ServiceLocator 已就绪
protected override void OnPanelOpen() => _uiManager = GetService<IUIManager>();
protected override void OnPanelClose() => _uiManager = null;
/// <summary>默认焦点 / 焦点恢复回到"继续"按钮(基类 FocusFirst 调用)。</summary>
protected override GameObject ResolveFirstSelected()
=> _btnResume != null ? _btnResume.gameObject : null;
// ── 按钮回调 ──────────────────────────────────────────────────────────
private void Resume()
{
_onResumeRequested?.Raise();
_uiManager?.CloseTopPanel();
}
private void OpenSettings()
{
_uiManager?.OpenPanel(PanelId.Settings);
}
private void GoToMainMenu()
{
_uiManager?.CloseTopPanel();
_onSceneLoadRequest?.Raise(new SceneLoadRequest
{
SceneName = AddressKeys.SceneMainMenu,
TransitionType = TransitionType.Scene,
ShowLoadingScreen = false,
});
}
}
}

View File

@@ -3,13 +3,10 @@ using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;
using BaseGames.Core;
using BaseGames.Core.Events;
using BaseGames.Core.Save;
using BaseGames.Localization;
using BaseGames.World.Map;
using BaseGames.UI;
using BaseGames.UI.MainMenu;
namespace BaseGames.UI.Menus
@@ -26,19 +23,21 @@ namespace BaseGames.UI.Menus
/// <summary>
/// 驱动主菜单存档槽选择面板(新游戏 / 继续 / 删除)。
///
/// <para>本面板经 <see cref="IUINavigator"/> 压栈(由主菜单控制器 Push。覆盖确认 / 模式选择
/// 这两个子对话框走导航器结果面板(<see cref="ConfirmDialogController.ShowAsync"/> /
/// <see cref="NewGameModeController.ShowAsync"/>)——线性 <c>await</c>无回调嵌套ESC 逐层回退。</para>
///
/// 前端选档流程:
/// · 新游戏开档前先选模式(普通 / 钢铁之魂),占用槽位需覆盖确认。
/// · 删除强制走通用确认对话框(无静默删除旁路)。
/// · 卡片展示游玩时长 / 区域 / 货币 / 生命 / 钢魂徽章。
///
/// 模式由 MainMenuController 在打开面板前通过 <see cref="SetMode"/> 指定。
/// ConfirmDialog 与 NewGameMode 面板经 Inspector 注入(同处 MainMenu 场景)。
/// 模式由主菜单控制器在压栈前经 <see cref="SetMode"/> 指定。
/// </summary>
public class SaveSlotController : MonoBehaviour, IFocusable
public class SaveSlotController : UIPanelBase
{
[SerializeField] private SaveSlotUI[] _slotUIs;
[Header("子面板Inspector 注入,同处 MainMenu 场景)")]
[Header("子对话框Inspector 注入,同处 MainMenu 场景,走导航器结果面板")]
[Tooltip("通用确认对话框,用于覆盖 / 删除确认。为空时:覆盖退化为直接建档,删除被忽略(绝不静默删除)。")]
[SerializeField] private ConfirmDialogController _confirmDialog;
[Tooltip("新游戏模式选择面板。为空时新游戏退化为普通模式。")]
@@ -48,7 +47,7 @@ namespace BaseGames.UI.Menus
[SerializeField] private IntEventChannelSO _onSlotConfirmed;
[Header("焦点")]
[Tooltip("面板恢复为栈顶时自动聚焦的默认按钮,通常为第一个存档槽的选择按钮。")]
[Tooltip("面板默认聚焦的按钮,通常为第一个存档槽的选择按钮。")]
[SerializeField] private Button _defaultFocusButton;
private SaveSlotPanelMode _mode = SaveSlotPanelMode.NewGame;
@@ -60,40 +59,24 @@ namespace BaseGames.UI.Menus
if (_slotUIs[i] != null) _slotUIs[i].Init(i, this);
}
/// <summary>由 MainMenuController 在 SetActive(true) 之前调用,决定本次打开语境。</summary>
/// <summary>由主菜单控制器在压栈前调用,决定本次打开语境。</summary>
public void SetMode(SaveSlotPanelMode mode) => _mode = mode;
// ── IFocusable ────────────────────────────────────────────────────────
public void OnFocusRestored() => StartCoroutine(RestoreFocusNextFrame());
/// <summary>默认 / 恢复焦点回到首个存档槽按钮。</summary>
protected override GameObject ResolveFirstSelected()
=> _defaultFocusButton != null ? _defaultFocusButton.gameObject : null;
private System.Collections.IEnumerator RestoreFocusNextFrame()
{
yield return null;
if (UnityEngine.EventSystems.EventSystem.current != null && _defaultFocusButton != null)
UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(_defaultFocusButton.gameObject);
}
private void OnEnable()
// ── 生命周期UIPanelBase 驱动)──────────────────────────────────────
protected override void OnPanelOpen()
{
_cts = new CancellationTokenSource();
var ct = _cts.Token;
var task = RefreshAsync(ct);
task.ContinueWith(t =>
{
if (t.IsFaulted && !(t.Exception?.InnerException is OperationCanceledException))
Debug.LogException(t.Exception?.InnerException ?? t.Exception, this);
}, TaskScheduler.FromCurrentSynchronizationContext());
// 面板打开时设置初始焦点(键盘 / 手柄导航入口)
StartCoroutine(RestoreFocusNextFrame());
RunGuarded(RefreshAsync(_cts.Token));
}
private void OnDisable()
protected override void OnPanelClose()
{
// 关闭子对话框,避免下次打开残留
_confirmDialog?.Close();
_modeSelect?.Close();
// 注意:不再级联 Close 子对话框(旧实现于此强关 confirm/mode是"ESC 一次关多层"的根因)。
// 子对话框生命周期完全交给导航器;若本面板随场景卸载,其 await 由结果面板兜底收口。
_cts?.Cancel();
_cts?.Dispose();
_cts = null;
@@ -119,47 +102,42 @@ namespace BaseGames.UI.Menus
}
// ── 选槽 ────────────────────────────────────────────────────────────────
/// <summary>选中指定槽位。行为取决于当前模式与槽位是否有档。由 SaveSlotUI 内部按钮调用。</summary>
public void OnSlotSelected(int slotIndex)
{
if (slotIndex < 0 || slotIndex >= _slotUIs.Length) return;
RunGuarded(HandleSlotSelectedAsync(slotIndex));
}
private async Task HandleSlotSelectedAsync(int slotIndex)
{
var svc = ServiceLocator.GetOrDefault<ISaveService>();
if (svc == null) return;
bool hasData = svc.HasSave(slotIndex);
var ct = _cts?.Token ?? CancellationToken.None;
if (_mode == SaveSlotPanelMode.Continue)
{
if (!hasData) return; // 继续模式:空槽不可选
_ = ContinueSlotAsync(slotIndex);
if (!svc.HasSave(slotIndex)) return; // 继续模式:空槽不可选
await ContinueSlotAsync(slotIndex);
return;
}
// ── 新游戏模式 ──
if (hasData)
if (svc.HasSave(slotIndex) && _confirmDialog != null)
{
// 占用槽位:先覆盖确认
if (_confirmDialog != null)
_confirmDialog.Show("CONFIRM_OVERWRITE_TITLE", "CONFIRM_OVERWRITE_BODY",
onConfirm: () => BeginNewGameFlow(slotIndex));
else
BeginNewGameFlow(slotIndex); // 无对话框时退化为直接建档
bool ok = await _confirmDialog.ShowAsync("CONFIRM_OVERWRITE_TITLE", "CONFIRM_OVERWRITE_BODY", ct);
if (!ok) return; // 取消覆盖
}
else
{
BeginNewGameFlow(slotIndex);
}
}
/// <summary>开新档流程:选模式 → 建档。</summary>
private void BeginNewGameFlow(int slotIndex)
{
DifficultyLevel level = DifficultyLevel.Normal;
if (_modeSelect != null)
_modeSelect.Show(level => _ = StartNewGameAsync(slotIndex, level));
else
_ = StartNewGameAsync(slotIndex, DifficultyLevel.Normal); // 无模式面板时退化为普通
{
DifficultyLevel? chosen = await _modeSelect.ShowAsync(ct);
if (chosen == null) return; // 取消模式选择
level = chosen.Value;
}
await StartNewGameAsync(slotIndex, level);
}
private async Task StartNewGameAsync(int slotIndex, DifficultyLevel level)
@@ -190,7 +168,6 @@ namespace BaseGames.UI.Menus
}
// ── 删除(强制确认)────────────────────────────────────────────────────
/// <summary>请求删除指定槽位。强制经通用确认对话框;无对话框时忽略,绝不静默删除。</summary>
public void OnSlotDeleteRequested(int slotIndex)
{
@@ -201,9 +178,15 @@ namespace BaseGames.UI.Menus
Debug.LogWarning("[SaveSlotController] 未配置 ConfirmDialog删除请求被忽略防止静默删除。");
return;
}
RunGuarded(DeleteFlowAsync(slotIndex));
}
_confirmDialog.Show("CONFIRM_DELETE_TITLE", "CONFIRM_DELETE_BODY",
onConfirm: () => _ = DeleteAndRefreshAsync(slotIndex));
private async Task DeleteFlowAsync(int slotIndex)
{
var ct = _cts?.Token ?? CancellationToken.None;
bool ok = await _confirmDialog.ShowAsync("CONFIRM_DELETE_TITLE", "CONFIRM_DELETE_BODY", ct);
if (!ok) return;
await DeleteAndRefreshAsync(slotIndex);
}
private async Task DeleteAndRefreshAsync(int slotIndex)
@@ -213,5 +196,16 @@ namespace BaseGames.UI.Menus
await svc.DeleteSlotAsync(slotIndex);
await RefreshAsync(_cts?.Token ?? CancellationToken.None);
}
// ── 工具 ──────────────────────────────────────────────────────────────
/// <summary>fire-and-forget Task 的统一异常护栏(吞掉取消,记录其余)。</summary>
private void RunGuarded(Task task)
{
task.ContinueWith(t =>
{
if (t.IsFaulted && !(t.Exception?.InnerException is OperationCanceledException))
Debug.LogException(t.Exception?.InnerException ?? t.Exception, this);
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}
}

View File

@@ -1,142 +0,0 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using BaseGames.Core;
using BaseGames.Localization;
namespace BaseGames.UI
{
/// <summary>
/// 设置面板控制器(架构 10_UIModule §7
/// 驱动 SettingsManager 的音量与画面设置,并从当前配置初始化控件值。
/// 生命周期 / 焦点由 <see cref="UIPanelBase"/> 统一处理。
/// </summary>
public class SettingsPanelController : UIPanelBase
{
// ISettingsService 通过 ServiceLocator 获取,无需 Inspector 直接注入具体类,
// 支持测试场景替换 Mock 实现。
private ISettingsService _settings;
[Header("音量滑条")]
[SerializeField] private Slider _masterVolume;
[SerializeField] private Slider _bgmVolume;
[SerializeField] private Slider _sfxVolume;
[SerializeField] private Slider _ambientVolume;
[Header("画面")]
[SerializeField] private Toggle _vSyncToggle;
[SerializeField] private TMP_Dropdown _fpsDropdown; // 30 / 60 / 120 / 无限
[Header("可访问性")]
[SerializeField] private Slider _uiScaleSlider; // 0.8 ~ 1.5
[SerializeField] private TMP_Text _uiScaleValueText; // 实时显示 "100%"
[SerializeField] private TMP_Dropdown _colorblindDropdown; // None / Prot / Deut / Trit
[SerializeField] private Toggle _screenShakeToggle;
[Header("语言")]
[SerializeField] private TMP_Dropdown _languageDropdown; // 中文 / English / 日本語 / 한국어
[Header("按键重绑定")]
[SerializeField] private GameObject _rebindPanelRoot; // RebindPanel GameObject
private static readonly int[] FpsOptions = { 30, 60, 120, -1 };
// 语言下拉项顺序(与脚手架填充的显示项一一对应)
private static readonly Language[] LanguageOptions =
{ Language.ChineseSimplified, Language.English, Language.Japanese, Language.Korean };
protected override void OnPanelOpen()
{
_settings = ServiceLocator.GetOrDefault<ISettingsService>();
if (_settings == null) return;
var data = _settings.Current;
// 初始化控件值(先移除监听再设置值再添加,防止面板重开时重复注册)
InitSlider(_masterVolume, data.MasterVolume, v => _settings.SetMasterVolume(v));
InitSlider(_bgmVolume, data.BGMVolume, v => _settings.SetBGMVolume(v));
InitSlider(_sfxVolume, data.SFXVolume, v => _settings.SetSFXVolume(v));
InitSlider(_ambientVolume,data.AmbientVolume, v => _settings.SetAmbientVolume(v));
if (_vSyncToggle != null)
{
_vSyncToggle.onValueChanged.RemoveAllListeners();
_vSyncToggle.isOn = data.VSync;
_vSyncToggle.onValueChanged.AddListener(v => _settings.SetVSync(v));
}
if (_fpsDropdown != null)
{
_fpsDropdown.onValueChanged.RemoveAllListeners();
int idx = System.Array.IndexOf(FpsOptions, data.TargetFPS);
_fpsDropdown.value = idx >= 0 ? idx : 1; // default 60
_fpsDropdown.onValueChanged.AddListener(i =>
_settings.SetTargetFrameRate(FpsOptions[Mathf.Clamp(i, 0, FpsOptions.Length - 1)]));
}
// ── 可访问性 ──────────────────────────────────────────────────────
if (_uiScaleSlider != null)
{
_uiScaleSlider.onValueChanged.RemoveAllListeners();
_uiScaleSlider.minValue = 0.8f;
_uiScaleSlider.maxValue = 1.5f;
_uiScaleSlider.value = Mathf.Clamp(data.UIScale, _uiScaleSlider.minValue, _uiScaleSlider.maxValue);
UpdateUIScaleLabel(_uiScaleSlider.value);
_uiScaleSlider.onValueChanged.AddListener(v =>
{
_settings.SetUIScale(v);
UpdateUIScaleLabel(v);
});
}
if (_colorblindDropdown != null)
{
_colorblindDropdown.onValueChanged.RemoveAllListeners();
_colorblindDropdown.value = (int)data.ColorblindMode;
_colorblindDropdown.onValueChanged.AddListener(i =>
_settings.SetColorblindMode((ColorblindMode)Mathf.Clamp(i, 0, 3)));
}
if (_screenShakeToggle != null)
{
_screenShakeToggle.onValueChanged.RemoveAllListeners();
_screenShakeToggle.isOn = data.ScreenShakeEnabled;
_screenShakeToggle.onValueChanged.AddListener(v => _settings.SetScreenShakeEnabled(v));
}
// ── 语言 ──────────────────────────────────────────────────────────
if (_languageDropdown != null)
{
_languageDropdown.onValueChanged.RemoveAllListeners();
var loc = ServiceLocator.GetOrDefault<ILocalizationService>();
int idx = loc != null ? System.Array.IndexOf(LanguageOptions, loc.CurrentLanguage) : 0;
_languageDropdown.value = idx >= 0 ? idx : 0;
_languageDropdown.RefreshShownValue();
_languageDropdown.onValueChanged.AddListener(i =>
ServiceLocator.GetOrDefault<ILocalizationService>()?
.SetLanguage(LanguageOptions[Mathf.Clamp(i, 0, LanguageOptions.Length - 1)]));
}
}
private void UpdateUIScaleLabel(float v)
{
if (_uiScaleValueText != null)
_uiScaleValueText.text = Mathf.RoundToInt(v * 100f) + "%";
}
// ── 辅助 ──────────────────────────────────────────────────────────────
private static void InitSlider(Slider slider, float value, UnityEngine.Events.UnityAction<float> onChange)
{
if (slider == null) return;
slider.onValueChanged.RemoveAllListeners();
slider.value = value;
slider.onValueChanged.AddListener(onChange);
}
// ── 焦点 ──────────────────────────────────────────────────────────────
/// <summary>默认焦点 / 焦点恢复回到主音量滑条(基类 FocusFirst 调用)。</summary>
protected override GameObject ResolveFirstSelected()
=> _masterVolume != null ? _masterVolume.gameObject : null;
}
}