using System;
using Sirenix.OdinInspector;
using UnityEngine;
namespace SLSUtilities.UI
{
///
/// 覆盖式 UI 页面基类(机械台、物流中心、地图、结算等)。
/// 继承自 UIElementBase,额外提供 Open/Close 生命周期和 UIPageManager 注册。
/// 页面打开时自动阻塞游戏输入,关闭时恢复。
///
[RequireComponent(typeof(CanvasGroup))]
public class UIPageBase : UIElementBase
{
[Title("Page Settings")]
[SerializeField] protected bool closeOnEsc = true;
/// 当前页面是否处于打开状态。
public bool IsOpen { get; private set; }
/// 是否允许按 ESC 关闭此页面。
public bool CloseOnEsc => closeOnEsc;
/// 页面打开后触发。
public event Action PageOpened;
/// 页面关闭后触发。
public event Action PageClosed;
protected virtual void Start()
{
Hide();
}
///
/// 打开页面:显示 UI,注册到 UIPageManager 栈,触发生命周期回调。
/// 若已打开则忽略。
///
public virtual void Open()
{
if (IsOpen) return;
IsOpen = true;
Show();
UIPageManager.Instance.RegisterPage(this);
OnPageOpened();
PageOpened?.Invoke();
}
///
/// 关闭页面:从 UIPageManager 栈中移除,隐藏 UI,触发生命周期回调。
/// 若未打开则忽略。
///
public virtual void Close()
{
if (!IsOpen) return;
IsOpen = false;
UIPageManager.Instance.UnregisterPage(this);
Hide();
OnPageClosed();
PageClosed?.Invoke();
}
/// 页面打开后的可覆盖回调。
protected virtual void OnPageOpened() { }
/// 页面关闭后的可覆盖回调。
protected virtual void OnPageClosed() { }
}
}