Files
tbd-station-14/Content.Client/Suspicion/SuspicionRoleComponent.cs
Jezithyr 571dd4e6d5 Hud refactor (#7202)
Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
Co-authored-by: Jezithyr <jmaster9999@gmail.com>
Co-authored-by: Jezithyr <Jezithyr@gmail.com>
Co-authored-by: Visne <39844191+Visne@users.noreply.github.com>
Co-authored-by: wrexbe <wrexbe@protonmail.com>
Co-authored-by: wrexbe <81056464+wrexbe@users.noreply.github.com>
2022-10-12 10:16:23 +02:00

128 lines
3.3 KiB
C#

using Content.Shared.Suspicion;
using Robust.Client.Graphics;
using Robust.Client.Player;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
using static Robust.Client.UserInterface.Controls.LayoutContainer;
namespace Content.Client.Suspicion
{
[RegisterComponent]
public sealed class SuspicionRoleComponent : SharedSuspicionRoleComponent
{
[Dependency] private readonly IOverlayManager _overlayManager = default!;
[Dependency] private readonly IResourceCache _resourceCache = default!;
[Dependency] private readonly IUserInterfaceManager _ui = default!;
private SuspicionGui? _gui;
private string? _role;
private bool? _antagonist;
private bool _overlayActive;
public string? Role
{
get => _role;
set
{
if (_role == value)
{
return;
}
_role = value;
_gui?.UpdateLabel();
Dirty();
}
}
public bool? Antagonist
{
get => _antagonist;
set
{
if (_antagonist == value)
{
return;
}
_antagonist = value;
_gui?.UpdateLabel();
if (value ?? false)
{
AddTraitorOverlay();
}
Dirty();
}
}
[ViewVariables]
public List<(string name, EntityUid uid)> Allies { get; } = new();
private void AddTraitorOverlay()
{
if (_overlayManager.HasOverlay<TraitorOverlay>())
{
return;
}
_overlayActive = true;
var overlay = new TraitorOverlay(IoCManager.Resolve<IEntityManager>(), IoCManager.Resolve<IPlayerManager>(), _resourceCache);
_overlayManager.AddOverlay(overlay);
}
private void RemoveTraitorOverlay()
{
if (!_overlayActive)
{
return;
}
_overlayManager.RemoveOverlay<TraitorOverlay>();
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (curState is not SuspicionRoleComponentState state)
{
return;
}
Role = state.Role;
Antagonist = state.Antagonist;
Allies.Clear();
Allies.AddRange(state.Allies);
}
public void RemoveUI()
{
_gui?.Parent?.RemoveChild(_gui);
RemoveTraitorOverlay();
}
public void AddUI()
{
// TODO move this out of the component
_gui = _ui.ActiveScreen?.GetOrNewWidget<SuspicionGui>();
_gui!.UpdateLabel();
SetAnchorAndMarginPreset(_gui, LayoutPreset.BottomLeft);
if (_antagonist ?? false)
{
AddTraitorOverlay();
}
}
protected override void OnRemove()
{
base.OnRemove();
_gui?.Dispose();
RemoveTraitorOverlay();
}
}
}