using UnityEngine; using UnityEngine.UI; using TMPro; using DG.Tweening; using Ashwild.Player; using Ashwild.Settings; namespace Ashwild.UI { /// /// The in-game pause menu, now a regular panel driven by . Wires its /// buttons to the game manager (resume / invite / quit) and to the shared settings panel. Every /// button is optional — leave a reference empty to omit it. /// public class PausePanel : UIPanel { #region Panel Kind /// /// The pause menu freezes the game while open. /// public override PanelKind Kind => PanelKind.Pause; #endregion #region Serialized Fields [Header("Buttons")] [SerializeField] private Button resumeButton; [SerializeField] private Button settingsButton; [SerializeField] private Button inviteButton; [SerializeField] private Button quitButton; [Header("Session")] [Tooltip("Displays the shareable room code (host SteamID-derived) of the current session.")] [SerializeField] private TMP_Text roomCodeLabel; [Tooltip("Shown in place of the code when no online session is active.")] [SerializeField] private string offlinePlaceholder = "—"; [Tooltip("Copies the current session code to the system clipboard when clicked.")] [SerializeField] private Button copyCodeButton; [Tooltip("Flashed in place of the code for a moment after a successful copy.")] [SerializeField] private string copyFeedback = "Copié !"; [Tooltip("How long the copy confirmation stays on screen before the code reappears.")] [SerializeField] private float copyFeedbackDuration = 1.2f; #endregion #region State /// /// The active game manager (the shared Instance is always a GameUIManager in gameplay). /// private GameUIManager Game => UIManager.Instance as GameUIManager; /// /// Pending tween that restores the room code label after the copy confirmation; killed /// before being restarted and on teardown so it never fires on a destroyed label. /// private Tween copyFeedbackTween; #endregion #region Unity Lifecycle /// /// Wires the buttons; runs once. /// protected override void Awake() { base.Awake(); if (resumeButton != null) resumeButton.onClick.AddListener(() => Game?.Resume()); if (inviteButton != null) inviteButton.onClick.AddListener(() => Game?.InviteFriend()); if (quitButton != null) quitButton.onClick.AddListener(() => Game?.QuitToMenu()); if (settingsButton != null) settingsButton.onClick.AddListener(() => UIManager.Instance.OpenPanel()); if (copyCodeButton != null) copyCodeButton.onClick.AddListener(CopySessionCode); } /// /// Kills the copy-feedback tween so it never restores the label after the panel is gone. /// private void OnDestroy() { copyFeedbackTween?.Kill(); } #endregion #region Panel Visibility /// /// Refreshes the room code each time the pause menu opens, so it always reflects the /// session that is live at that moment rather than a value cached at spawn. /// public override void Show() { base.Show(); RefreshRoomCode(); } #endregion #region Internal Helpers /// /// Writes the current shareable session code into the label, falling back to the offline /// placeholder when no online session is active. /// private void RefreshRoomCode() { copyFeedbackTween?.Kill(); bool hasCode = !string.IsNullOrEmpty(PlayerEvents.SessionCode); if (roomCodeLabel != null) roomCodeLabel.text = hasCode ? PlayerEvents.SessionCode : offlinePlaceholder; if (copyCodeButton != null) copyCodeButton.interactable = hasCode; } /// /// Copies the live session code to the system clipboard and briefly flashes a confirmation /// in the room-code label. Does nothing when no online session is active. /// private void CopySessionCode() { string code = PlayerEvents.SessionCode; if (string.IsNullOrEmpty(code)) return; GUIUtility.systemCopyBuffer = code; if (roomCodeLabel == null) return; copyFeedbackTween?.Kill(); roomCodeLabel.text = copyFeedback; copyFeedbackTween = DOVirtual.DelayedCall(copyFeedbackDuration, () => roomCodeLabel.text = code); } #endregion } }