using System.Collections.Generic;
using Ichni.UI;
using UnityEngine;
namespace Ichni.Menu.UI
{
///
/// 通用 SelectionBox 的页面宿主,职责等同于 MessageBox 的页面容器:
/// 显示时动态生成一个 SelectionBox,选择完成后统一淡出并销毁本次实例。
///
public class SelectionBoxUIPage : UIPageBase
{
[Header("Selection Box Page")]
public SelectionBox selectionBoxPrefab;
public RectTransform selectionBoxContainer;
private SelectionBox _currentSelectionBox;
private bool _isShowing;
/// 当前是否已有一个 SelectionBox 等待玩家选择。
public bool IsShowing => _isShowing;
///
/// 生成并显示一组运行时选项。页面显示期间拒绝重复请求,避免两个弹窗争夺输入。
///
public bool Show(string title, string content, IReadOnlyList options)
{
if (_isShowing)
{
Debug.LogWarning("[SelectionBoxUIPage] 已有 SelectionBox 显示中,忽略重复请求。");
return false;
}
if (mainCanvasGroup == null || selectionBoxPrefab == null || selectionBoxContainer == null)
{
Debug.LogWarning("[SelectionBoxUIPage] 缺少 CanvasGroup、SelectionBox Prefab 或容器,无法显示选项框。");
return false;
}
_currentSelectionBox = Instantiate(selectionBoxPrefab, selectionBoxContainer);
_currentSelectionBox.Closed += HandleSelectionBoxClosed;
if (!_currentSelectionBox.SetUp(title, content, options))
{
Destroy(_currentSelectionBox.gameObject);
_currentSelectionBox = null;
return false;
}
_isShowing = true;
// UIPageBase 默认淡入完成后才开启射线;选择框从出现瞬间就必须阻断底层输入。
mainCanvasGroup.gameObject.SetActive(true);
mainCanvasGroup.interactable = true;
mainCanvasGroup.blocksRaycasts = true;
FadeIn();
return true;
}
private void HandleSelectionBoxClosed(SelectionBox selectionBox)
{
if (selectionBox != _currentSelectionBox) return;
FadeOut(0.2f, false, () =>
{
if (_currentSelectionBox != null)
Destroy(_currentSelectionBox.gameObject);
_currentSelectionBox = null;
_isShowing = false;
});
}
}
}