using UnityEngine;
using UnityEngine.UIElements;
namespace ProjectM.Client
{
///
/// In-game pause overlay (UI Toolkit), spawned by in the client world. Esc
/// toggles it; Resume / Settings / Quit-to-Menu / Quit-to-Desktop. Quit-to-Menu hands off to
/// (autosave + dispose worlds + load MainMenu). Builds its own
/// UIDocument in code (shared PanelSettings from Resources) above the HUD; the scene swap on Quit-to-Menu
/// destroys it.
///
public class PauseMenuController : MonoBehaviour
{
UIDocument _doc;
VisualElement _root;
VisualElement _pausePanel;
VisualElement _settingsPanel;
bool _open;
/// True while the pause overlay is shown (BuildSendSystem suspends build-clicks while paused).
public static bool Open;
void Awake()
{
MenuUi.EnsureEventSystem();
_doc = gameObject.AddComponent();
_doc.panelSettings = MenuUi.LoadPanelSettings();
_doc.sortingOrder = 100; // above the in-game HUD
}
void Start()
{
_root = _doc.rootVisualElement;
Build();
SetOpen(false);
}
void Update()
{
var kb = UnityEngine.InputSystem.Keyboard.current;
if (kb != null && kb.escapeKey.wasPressedThisFrame)
SetOpen(!_open);
}
void Build()
{
_pausePanel = MenuUi.FullScreenRoot(false);
_pausePanel.style.backgroundColor = new Color(0.02f, 0.03f, 0.05f, 0.72f);
var card = MenuUi.Card("PAUSED");
card.Add(MenuUi.Button("Resume", () => SetOpen(false)));
card.Add(MenuUi.Button("Settings", ShowSettings));
card.Add(MenuUi.Button("Quit to Menu", WorldLauncher.TeardownToMenu));
card.Add(MenuUi.Button("Quit to Desktop", Quit));
_pausePanel.Add(card);
_root.Add(_pausePanel);
}
void SetOpen(bool open)
{
_open = open;
Open = open;
if (_pausePanel != null) _pausePanel.style.display = open ? DisplayStyle.Flex : DisplayStyle.None;
if (!open && _settingsPanel != null) { _settingsPanel.RemoveFromHierarchy(); _settingsPanel = null; }
if (open) { UnityEngine.Cursor.lockState = CursorLockMode.None; UnityEngine.Cursor.visible = true; }
}
void ShowSettings()
{
_pausePanel.style.display = DisplayStyle.None;
_settingsPanel = SettingsScreen.Build(() =>
{
if (_settingsPanel != null) { _settingsPanel.RemoveFromHierarchy(); _settingsPanel = null; }
_pausePanel.style.display = DisplayStyle.Flex;
});
_root.Add(_settingsPanel);
}
static void Quit()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
}