103 lines
3.1 KiB
C#
103 lines
3.1 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace FloodSWE.Networking
|
|
{
|
|
/// <summary>
|
|
/// Displays Quest->Server connection status based on SweQuestControlClient ACKs.
|
|
/// </summary>
|
|
public sealed class SweConnectionStatusHud : MonoBehaviour
|
|
{
|
|
[Header("References")]
|
|
[SerializeField] private SweQuestControlClient controlClient;
|
|
[SerializeField] private TMP_Text statusText;
|
|
[SerializeField] private TMP_Text ackText;
|
|
|
|
[Header("Display")]
|
|
[SerializeField] private string connectedLabel = "Connected";
|
|
[SerializeField] private string waitingLabel = "Waiting for ACK";
|
|
[SerializeField] private string disconnectedLabel = "Disconnected";
|
|
[SerializeField] private string ackPrefix = "Last ACK: ";
|
|
[SerializeField] private Color connectedColor = new Color(0.4f, 1.0f, 0.4f);
|
|
[SerializeField] private Color waitingColor = new Color(1.0f, 0.9f, 0.3f);
|
|
[SerializeField] private Color disconnectedColor = new Color(1.0f, 0.5f, 0.5f);
|
|
[SerializeField] private string ageFormat = "0.00";
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (controlClient == null)
|
|
{
|
|
controlClient = FindFirstObjectByType<SweQuestControlClient>();
|
|
}
|
|
|
|
Refresh();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
Refresh();
|
|
}
|
|
|
|
private void Refresh()
|
|
{
|
|
if (controlClient == null)
|
|
{
|
|
SetStatus(disconnectedLabel, disconnectedColor);
|
|
SetAck("Control client missing");
|
|
return;
|
|
}
|
|
|
|
if (controlClient.IsConnectionAlive)
|
|
{
|
|
SetStatus(connectedLabel, connectedColor);
|
|
string ackMsg = string.IsNullOrWhiteSpace(controlClient.LastAckMessage)
|
|
? "(empty)"
|
|
: controlClient.LastAckMessage;
|
|
SetAck($"{ackPrefix}{ackMsg} ({controlClient.LastAckAgeSeconds.ToString(ageFormat)}s)");
|
|
return;
|
|
}
|
|
|
|
if (controlClient.HasReceivedAck)
|
|
{
|
|
SetStatus(disconnectedLabel, disconnectedColor);
|
|
string ackMsg = string.IsNullOrWhiteSpace(controlClient.LastAckMessage)
|
|
? "(empty)"
|
|
: controlClient.LastAckMessage;
|
|
SetAck($"{ackPrefix}{ackMsg} ({controlClient.LastAckAgeSeconds.ToString(ageFormat)}s)");
|
|
return;
|
|
}
|
|
|
|
SetStatus(waitingLabel, waitingColor);
|
|
SetAck("No ACK yet");
|
|
}
|
|
|
|
private void SetStatus(string text, Color color)
|
|
{
|
|
if (statusText == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (statusText.text != text)
|
|
{
|
|
statusText.text = text;
|
|
}
|
|
|
|
if (statusText.color != color)
|
|
{
|
|
statusText.color = color;
|
|
}
|
|
}
|
|
|
|
private void SetAck(string text)
|
|
{
|
|
if (ackText == null || ackText.text == text)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ackText.text = text;
|
|
}
|
|
}
|
|
}
|