Files
tbd-station-14/Content.Client/UserInterface/Systems/Ghost/GhostUIController.cs
deltanedas ff762e56a7 add ghostnado button to warp menu (#27556)
* add ghostnado button to warp menu

* translator ops

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
2024-05-01 06:59:35 -07:00

164 lines
4.1 KiB
C#

using Content.Client.Gameplay;
using Content.Client.Ghost;
using Content.Client.UserInterface.Systems.Gameplay;
using Content.Client.UserInterface.Systems.Ghost.Widgets;
using Content.Shared.Ghost;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controllers;
namespace Content.Client.UserInterface.Systems.Ghost;
// TODO hud refactor BEFORE MERGE fix ghost gui being too far up
public sealed class GhostUIController : UIController, IOnSystemChanged<GhostSystem>
{
[Dependency] private readonly IEntityNetworkManager _net = default!;
[UISystemDependency] private readonly GhostSystem? _system = default;
private GhostGui? Gui => UIManager.GetActiveUIWidgetOrNull<GhostGui>();
public override void Initialize()
{
base.Initialize();
var gameplayStateLoad = UIManager.GetUIController<GameplayStateLoadController>();
gameplayStateLoad.OnScreenLoad += OnScreenLoad;
gameplayStateLoad.OnScreenUnload += OnScreenUnload;
}
private void OnScreenLoad()
{
LoadGui();
}
private void OnScreenUnload()
{
UnloadGui();
}
public void OnSystemLoaded(GhostSystem system)
{
system.PlayerRemoved += OnPlayerRemoved;
system.PlayerUpdated += OnPlayerUpdated;
system.PlayerAttached += OnPlayerAttached;
system.PlayerDetached += OnPlayerDetached;
system.GhostWarpsResponse += OnWarpsResponse;
system.GhostRoleCountUpdated += OnRoleCountUpdated;
}
public void OnSystemUnloaded(GhostSystem system)
{
system.PlayerRemoved -= OnPlayerRemoved;
system.PlayerUpdated -= OnPlayerUpdated;
system.PlayerAttached -= OnPlayerAttached;
system.PlayerDetached -= OnPlayerDetached;
system.GhostWarpsResponse -= OnWarpsResponse;
system.GhostRoleCountUpdated -= OnRoleCountUpdated;
}
public void UpdateGui()
{
if (Gui == null)
{
return;
}
Gui.Visible = _system?.IsGhost ?? false;
Gui.Update(_system?.AvailableGhostRoleCount, _system?.Player?.CanReturnToBody);
}
private void OnPlayerRemoved(GhostComponent component)
{
Gui?.Hide();
}
private void OnPlayerUpdated(GhostComponent component)
{
UpdateGui();
}
private void OnPlayerAttached(GhostComponent component)
{
if (Gui == null)
return;
Gui.Visible = true;
UpdateGui();
}
private void OnPlayerDetached()
{
Gui?.Hide();
}
private void OnWarpsResponse(GhostWarpsResponseEvent msg)
{
if (Gui?.TargetWindow is not { } window)
return;
window.UpdateWarps(msg.Warps);
window.Populate();
}
private void OnRoleCountUpdated(GhostUpdateGhostRoleCountEvent msg)
{
UpdateGui();
}
private void OnWarpClicked(NetEntity player)
{
var msg = new GhostWarpToTargetRequestEvent(player);
_net.SendSystemNetworkMessage(msg);
}
private void OnGhostnadoClicked()
{
var msg = new GhostnadoRequestEvent();
_net.SendSystemNetworkMessage(msg);
}
public void LoadGui()
{
if (Gui == null)
return;
Gui.RequestWarpsPressed += RequestWarps;
Gui.ReturnToBodyPressed += ReturnToBody;
Gui.GhostRolesPressed += GhostRolesPressed;
Gui.TargetWindow.WarpClicked += OnWarpClicked;
Gui.TargetWindow.OnGhostnadoClicked += OnGhostnadoClicked;
UpdateGui();
}
public void UnloadGui()
{
if (Gui == null)
return;
Gui.RequestWarpsPressed -= RequestWarps;
Gui.ReturnToBodyPressed -= ReturnToBody;
Gui.GhostRolesPressed -= GhostRolesPressed;
Gui.TargetWindow.WarpClicked -= OnWarpClicked;
Gui.Hide();
}
private void ReturnToBody()
{
_system?.ReturnToBody();
}
private void RequestWarps()
{
_system?.RequestWarps();
Gui?.TargetWindow.Populate();
Gui?.TargetWindow.OpenCentered();
}
private void GhostRolesPressed()
{
_system?.OpenGhostRoles();
}
}