using Content.Shared.Eui;
using Robust.Shared.Network;
using Robust.Shared.Player;
namespace Content.Server.EUI
{
///
/// Base class to implement server-side for an EUI.
///
///
/// An EUI is a system for making a relatively-easy connection between client and server
/// for the purposes of UIs.
///
///
/// An equivalently named class much exist server side for an EUI to work.
/// It will be instantiated, opened and closed automatically.
///
public abstract class BaseEui
{
private bool _isStateDirty = false;
///
/// The player that this EUI is open for.
///
public ICommonSession Player { get; private set; } = default!;
public bool IsShutDown { get; private set; }
public EuiManager Manager { get; private set; } = default!;
public uint Id { get; private set; }
///
/// Called when the UI has been opened. Do initializing logic here.
///
public virtual void Opened()
{
}
///
/// Called when the UI has been closed.
///
public virtual void Closed()
{
}
///
/// Called when a message comes in from the client.
///
public virtual void HandleMessage(EuiMessageBase msg)
{
if (msg is CloseEuiMessage)
Close();
}
///
/// Mark the current UI state as dirty and queue for an update.
///
///
public void StateDirty()
{
if (_isStateDirty)
{
return;
}
_isStateDirty = true;
Manager.QueueStateUpdate(this);
}
///
/// Called some time after has been called
/// to get a new UI state that can be sent to the client.
///
public virtual EuiStateBase GetNewState()
{
throw new NotSupportedException();
}
///
/// Send a message to the client-side EUI.
///
public void SendMessage(EuiMessageBase message)
{
var netMgr = IoCManager.Resolve();
var msg = new MsgEuiMessage();
msg.Id = Id;
msg.Message = message;
netMgr.ServerSendMessage(msg, Player.Channel);
}
///
/// Close the EUI, breaking the connection between client and server.
///
public void Close()
{
Manager.CloseEui(this);
}
internal void Shutdown()
{
Closed();
IsShutDown = true;
}
internal void DoStateUpdate()
{
_isStateDirty = false;
var state = GetNewState();
var netMgr = IoCManager.Resolve();
var msg = new MsgEuiState();
msg.Id = Id;
msg.State = state;
netMgr.ServerSendMessage(msg, Player.Channel);
}
internal void Initialize(EuiManager manager, ICommonSession player, uint id)
{
Manager = manager;
Player = player;
Id = id;
Opened();
}
}
}