Files
2026-06-22 16:18:34 +02:00

171 lines
5.2 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Ashwild.Network;
using Ashwild.Player;
namespace Ashwild.UI
{
/// <summary>
/// Lets the player paste a friend's session code and connect to their room. Validates the
/// code, launches the join through NetworkSessionManager, and mirrors the connection
/// progress (connecting / error) from the PlayerEvents bus into an on-screen status line.
/// </summary>
public class JoinPanel : UIPanel
{
#region Serialized Fields
[Header("Code")]
[Tooltip("Where the player types/pastes the host's session code.")]
[SerializeField] private TMP_InputField codeInput;
[Header("Buttons")]
[SerializeField] private Button joinButton;
[SerializeField] private Button backButton;
[Header("Feedback")]
[Tooltip("Shows 'connecting…' / error messages. Optional.")]
[SerializeField] private TMP_Text statusText;
#endregion
#region State
/// <summary>
/// True while a connection attempt is in flight (buttons locked, status shown).
/// </summary>
private bool isConnecting;
#endregion
#region Unity Lifecycle
/// <summary>
/// Wires the buttons and submit-on-enter; runs once.
/// </summary>
protected override void Awake()
{
base.Awake();
joinButton.onClick.AddListener(AttemptJoin);
backButton.onClick.AddListener(() => UIManager.Instance.Back());
if (codeInput != null)
codeInput.onSubmit.AddListener(_ => AttemptJoin());
}
/// <summary>
/// Subscribes to the session bus and resets the panel each time it is shown.
/// </summary>
public override void Show()
{
base.Show();
PlayerEvents.SessionJoining += HandleSessionJoining;
PlayerEvents.SessionJoined += HandleSessionJoined;
PlayerEvents.SessionError += HandleSessionError;
ResetForInput();
}
/// <summary>
/// Unsubscribes — mirrors Show exactly so the panel never reacts while hidden.
/// </summary>
public override void Hide()
{
PlayerEvents.SessionJoining -= HandleSessionJoining;
PlayerEvents.SessionJoined -= HandleSessionJoined;
PlayerEvents.SessionError -= HandleSessionError;
base.Hide();
}
#endregion
#region Event Handlers
/// <summary>
/// Connection attempt reached the transport — show progress.
/// </summary>
private void HandleSessionJoining(string code) => SetStatus($"Connexion à {code}…");
/// <summary>
/// Connected to the host — the networked scene load takes over from here.
/// </summary>
private void HandleSessionJoined() => SetStatus("Connecté ! Chargement…");
/// <summary>
/// Connection failed — surface the reason and re-enable input.
/// </summary>
private void HandleSessionError(string reason)
{
SetStatus(reason);
isConnecting = false;
SetInteractable(true);
}
#endregion
#region Internal Helpers
/// <summary>
/// Validates the typed code and launches the join through NetworkSessionManager.
/// </summary>
private void AttemptJoin()
{
if (isConnecting) return;
string code = codeInput != null ? codeInput.text.Trim() : string.Empty;
if (string.IsNullOrEmpty(code))
{
SetStatus("Entre un code de session.");
return;
}
if (NetworkSessionManager.Instance == null)
{
Debug.LogError("[JoinPanel] NetworkSessionManager introuvable — la MenuScene doit contenir le NetworkManager.", this);
SetStatus("Erreur interne (NetworkManager manquant).");
return;
}
GameSession.Mode = GameSession.LaunchMode.Join;
GameSession.JoinCode = code;
isConnecting = true;
SetInteractable(false);
SetStatus("Connexion…");
NetworkSessionManager.Instance.JoinSession(code);
}
/// <summary>
/// Clears the status line and re-enables input — used each time the panel opens.
/// </summary>
private void ResetForInput()
{
isConnecting = false;
SetInteractable(true);
SetStatus(string.Empty);
}
/// <summary>
/// Enables/disables the code field and join button during a connection attempt.
/// </summary>
private void SetInteractable(bool interactable)
{
if (codeInput != null) codeInput.interactable = interactable;
if (joinButton != null) joinButton.interactable = interactable;
}
/// <summary>
/// Writes a message to the optional status line.
/// </summary>
private void SetStatus(string message)
{
if (statusText != null) statusText.text = message;
}
#endregion
}
}