Files
2026-06-24 20:01:40 +02:00

143 lines
4.9 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using DG.Tweening;
using Ashwild.Player;
using Ashwild.Settings;
namespace Ashwild.UI
{
/// <summary>
/// The in-game pause menu, now a regular panel driven by <see cref="GameUIManager"/>. 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.
/// </summary>
public class PausePanel : UIPanel
{
#region Panel Kind
/// <summary>
/// The pause menu freezes the game while open.
/// </summary>
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
/// <summary>
/// The active game manager (the shared Instance is always a GameUIManager in gameplay).
/// </summary>
private GameUIManager Game => UIManager.Instance as GameUIManager;
/// <summary>
/// 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.
/// </summary>
private Tween copyFeedbackTween;
#endregion
#region Unity Lifecycle
/// <summary>
/// Wires the buttons; runs once.
/// </summary>
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<SettingsPanel>());
if (copyCodeButton != null) copyCodeButton.onClick.AddListener(CopySessionCode);
}
/// <summary>
/// Kills the copy-feedback tween so it never restores the label after the panel is gone.
/// </summary>
private void OnDestroy()
{
copyFeedbackTween?.Kill();
}
#endregion
#region Panel Visibility
/// <summary>
/// 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.
/// </summary>
public override void Show()
{
base.Show();
RefreshRoomCode();
}
#endregion
#region Internal Helpers
/// <summary>
/// Writes the current shareable session code into the label, falling back to the offline
/// placeholder when no online session is active.
/// </summary>
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;
}
/// <summary>
/// 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.
/// </summary>
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
}
}