Add HUD button that displays your SSS role and allies (#1895)

* Add button that displays your SSS role and allies

* Capitalize button name

* Add cases for 0, 1 and invalid number of allies

* Make the ally syncing system saner
This commit is contained in:
DrSmugleaf
2020-08-27 16:39:29 +02:00
committed by GitHub
parent 388e717a53
commit 548ef3dedb
17 changed files with 616 additions and 15 deletions

View File

@@ -17,10 +17,10 @@ namespace Content.Client.GameObjects.Components.Items
[ComponentReference(typeof(ISharedHandsComponent))]
public class HandsComponent : SharedHandsComponent
{
private HandsGui? _gui;
[Dependency] private readonly IGameHud _gameHud = default!;
private HandsGui? _gui;
/// <inheritdoc />
private readonly List<Hand> _hands = new List<Hand>();

View File

@@ -0,0 +1,109 @@
#nullable enable
using System.Collections.Generic;
using System.Linq;
using Content.Client.UserInterface;
using Content.Client.UserInterface.Suspicion;
using Content.Shared.GameObjects.Components.Suspicion;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.IoC;
using Robust.Shared.Players;
namespace Content.Client.GameObjects.Components.Suspicion
{
[RegisterComponent]
public class SuspicionRoleComponent : SharedSuspicionRoleComponent
{
[Dependency] private readonly IGameHud _gameHud = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
private SuspicionGui? _gui;
private string? _role;
private bool? _antagonist;
public string? Role
{
get => _role;
set
{
_role = value;
_gui?.UpdateLabel();
Dirty();
}
}
public bool? Antagonist
{
get => _antagonist;
set
{
_antagonist = value;
_gui?.UpdateLabel();
Dirty();
}
}
public HashSet<IEntity> Allies { get; } = new HashSet<IEntity>();
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (!(curState is SuspicionRoleComponentState state))
{
return;
}
_role = state.Role;
_antagonist = state.Antagonist;
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case PlayerAttachedMsg _:
if (_gui == null)
{
_gui = new SuspicionGui();
}
else
{
_gui.Parent?.RemoveChild(_gui);
}
_gameHud.SuspicionContainer.AddChild(_gui);
_gui.UpdateLabel();
break;
case PlayerDetachedMsg _:
_gui?.Parent?.RemoveChild(_gui);
break;
}
}
public override void HandleNetworkMessage(ComponentMessage message, INetChannel netChannel, ICommonSession? session = null)
{
base.HandleNetworkMessage(message, netChannel, session);
switch (message)
{
case SuspicionAlliesMessage msg:
Allies.Clear();
Allies.UnionWith(msg.Allies.Select(_entityManager.GetEntity));
break;
}
}
public override void OnRemove()
{
base.OnRemove();
_gui?.Dispose();
}
}
}

View File

@@ -47,6 +47,7 @@ namespace Content.Client.UserInterface
Action<bool> SandboxButtonToggled { get; set; }
Control HandsContainer { get; }
Control SuspicionContainer { get; }
Control InventoryQuickButtonContainer { get; }
bool CombatPanelVisible { get; set; }
@@ -79,6 +80,7 @@ namespace Content.Client.UserInterface
[Dependency] private readonly IInputManager _inputManager = default!;
public Control HandsContainer { get; private set; }
public Control SuspicionContainer { get; private set; }
public Control InventoryQuickButtonContainer { get; private set; }
public bool CombatPanelVisible
@@ -242,6 +244,17 @@ namespace Content.Client.UserInterface
LayoutContainer.SetAnchorAndMarginPreset(HandsContainer, LayoutContainer.LayoutPreset.CenterBottom);
LayoutContainer.SetGrowHorizontal(HandsContainer, LayoutContainer.GrowDirection.Both);
LayoutContainer.SetGrowVertical(HandsContainer, LayoutContainer.GrowDirection.Begin);
SuspicionContainer = new MarginContainer
{
SizeFlagsHorizontal = Control.SizeFlags.ShrinkCenter
};
RootControl.AddChild(SuspicionContainer);
LayoutContainer.SetAnchorAndMarginPreset(SuspicionContainer, LayoutContainer.LayoutPreset.BottomLeft, margin: 10);
LayoutContainer.SetGrowHorizontal(SuspicionContainer, LayoutContainer.GrowDirection.End);
LayoutContainer.SetGrowVertical(SuspicionContainer, LayoutContainer.GrowDirection.Begin);
}
private void ButtonTutorialOnOnToggled()

View File

@@ -0,0 +1,116 @@
using System;
using System.Globalization;
using Content.Client.GameObjects.Components.Suspicion;
using Content.Shared.Interfaces;
using Robust.Client.Player;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
using Robust.Shared.Timing;
using static Robust.Client.UserInterface.Controls.BaseButton;
namespace Content.Client.UserInterface.Suspicion
{
public class SuspicionGui : Control
{
#pragma warning disable 0649
[Dependency] private readonly IPlayerManager _playerManager;
#pragma warning restore 0649
private readonly VBoxContainer _container;
private readonly Button _roleButton;
private string _previousRoleName;
private bool _previousAntagonist;
public SuspicionGui()
{
IoCManager.InjectDependencies(this);
AddChild(_container = new VBoxContainer
{
SeparationOverride = 0,
Children =
{
(_roleButton = new Button
{
Name = "Suspicion Role Button"
})
}
});
_roleButton.CustomMinimumSize = (200, 60);
_roleButton.OnPressed += RoleButtonPressed;
}
private void RoleButtonPressed(ButtonEventArgs obj)
{
if (!TryGetComponent(out var role))
{
return;
}
if (!role.Antagonist ?? false)
{
return;
}
var allies = string.Join(", ", role.Allies);
var message = role.Allies.Count switch
{
0 => Loc.GetString("You have no allies"),
1 => Loc.GetString("Your ally is {0}", allies),
var n when n > 2 => Loc.GetString("Your allies are {0}", allies),
_ => throw new ArgumentException($"Invalid number of allies: {role.Allies.Count}")
};
role.Owner.PopupMessage(role.Owner, message);
}
private bool TryGetComponent(out SuspicionRoleComponent suspicion)
{
suspicion = default;
return _playerManager?.LocalPlayer?.ControlledEntity?.TryGetComponent(out suspicion) == true;
}
public void UpdateLabel()
{
if (!TryGetComponent(out var suspicion))
{
Visible = false;
return;
}
if (suspicion.Role == null || suspicion.Antagonist == null)
{
Visible = false;
return;
}
if (_previousRoleName == suspicion.Role && _previousAntagonist == suspicion.Antagonist)
{
return;
}
_previousRoleName = suspicion.Role;
_previousAntagonist = suspicion.Antagonist.Value;
var buttonText = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(_previousRoleName);
buttonText = Loc.GetString(buttonText);
_roleButton.Text = buttonText;
_roleButton.ModulateSelfOverride = _previousAntagonist ? Color.Red : Color.Green;
Visible = true;
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
UpdateLabel();
}
}
}