Refactor context menu UI (#14334)
This commit is contained in:
@@ -5,18 +5,11 @@ using Content.Shared.Administration;
|
||||
using Content.Shared.IdentityManagement;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Client.ContextMenu.UI
|
||||
{
|
||||
public sealed partial class EntityMenuElement : ContextMenuElement
|
||||
{
|
||||
public const string StyleClassEntityMenuCountText = "contextMenuCount";
|
||||
|
||||
[Dependency] private readonly IClientAdminManager _adminManager = default!;
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
@@ -31,13 +24,7 @@ namespace Content.Client.ContextMenu.UI
|
||||
/// <summary>
|
||||
/// How many entities are accessible through this element's sub-menus.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is used for <see cref="CountLabel"/>
|
||||
/// </remarks>
|
||||
public int Count;
|
||||
|
||||
public readonly Label CountLabel;
|
||||
public readonly SpriteView EntityIcon = new() { OverrideDirection = Direction.South};
|
||||
public int Count { get; private set; }
|
||||
|
||||
public EntityMenuElement(EntityUid? entity = null)
|
||||
{
|
||||
@@ -45,19 +32,11 @@ namespace Content.Client.ContextMenu.UI
|
||||
|
||||
_adminSystem = _entityManager.System<AdminSystem>();
|
||||
|
||||
CountLabel = new Label { StyleClasses = { StyleClassEntityMenuCountText } };
|
||||
Icon.AddChild(new LayoutContainer() { Children = { EntityIcon, CountLabel } });
|
||||
|
||||
LayoutContainer.SetAnchorPreset(CountLabel, LayoutContainer.LayoutPreset.BottomRight);
|
||||
LayoutContainer.SetGrowHorizontal(CountLabel, LayoutContainer.GrowDirection.Begin);
|
||||
LayoutContainer.SetGrowVertical(CountLabel, LayoutContainer.GrowDirection.Begin);
|
||||
|
||||
Entity = entity;
|
||||
if (Entity == null)
|
||||
return;
|
||||
|
||||
Count = 1;
|
||||
CountLabel.Visible = false;
|
||||
UpdateEntity();
|
||||
}
|
||||
|
||||
@@ -68,6 +47,54 @@ namespace Content.Client.ContextMenu.UI
|
||||
Count = 0;
|
||||
}
|
||||
|
||||
private string? SearchPlayerName(EntityUid entity)
|
||||
{
|
||||
return _adminSystem.PlayerList.FirstOrDefault(player => player.EntityUid == entity)?.Username;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the entity count
|
||||
/// </summary>
|
||||
public void UpdateCount()
|
||||
{
|
||||
if (SubMenu == null)
|
||||
return;
|
||||
|
||||
Count = 0;
|
||||
foreach (var subElement in SubMenu.MenuBody.Children)
|
||||
{
|
||||
if (subElement is EntityMenuElement entityElement)
|
||||
Count += entityElement.Count;
|
||||
}
|
||||
|
||||
IconLabel.Visible = Count > 1;
|
||||
if (IconLabel.Visible)
|
||||
IconLabel.Text = Count.ToString();
|
||||
}
|
||||
|
||||
private string GetEntityDescriptionAdmin(EntityUid entity)
|
||||
{
|
||||
var representation = _entityManager.ToPrettyString(entity);
|
||||
|
||||
var name = representation.Name;
|
||||
var id = representation.Uid;
|
||||
var prototype = representation.Prototype;
|
||||
var playerName = representation.Session?.Name ?? SearchPlayerName(entity);
|
||||
var deleted = representation.Deleted;
|
||||
|
||||
return $"{name} ({id}{(prototype != null ? $", {prototype}" : "")}{(playerName != null ? $", {playerName}" : "")}){(deleted ? "D" : "")}";
|
||||
}
|
||||
|
||||
private string GetEntityDescription(EntityUid entity)
|
||||
{
|
||||
if (_adminManager.HasFlag(AdminFlags.Admin | AdminFlags.Debug))
|
||||
{
|
||||
return GetEntityDescriptionAdmin(entity);
|
||||
}
|
||||
|
||||
return Identity.Name(entity, _entityManager, _playerManager.LocalPlayer!.ControlledEntity!);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the icon and text of this element based on the given entity or this element's own entity if none
|
||||
/// is provided.
|
||||
@@ -80,28 +107,14 @@ namespace Content.Client.ContextMenu.UI
|
||||
// _entityManager.Deleted() implicitly checks all of these.
|
||||
if (_entityManager.Deleted(entity))
|
||||
{
|
||||
Icon.Sprite = null;
|
||||
Text = string.Empty;
|
||||
EntityIcon.Sprite = null;
|
||||
return;
|
||||
}
|
||||
|
||||
EntityIcon.Sprite = _entityManager.GetComponentOrNull<SpriteComponent>(entity);
|
||||
|
||||
if (_adminManager.HasFlag(AdminFlags.Admin | AdminFlags.Debug))
|
||||
{
|
||||
var representation = _entityManager.ToPrettyString(entity.Value);
|
||||
var name = representation.Name;
|
||||
var id = representation.Uid;
|
||||
var prototype = representation.Prototype;
|
||||
var playerName =
|
||||
representation.Session?.Name ??
|
||||
_adminSystem.PlayerList.FirstOrDefault(player => player.EntityUid == entity)?.Username;
|
||||
var deleted = representation.Deleted;
|
||||
|
||||
Text = $"{name} ({id}{(representation.Prototype != null ? $", {prototype}" : "")}{(playerName != null ? $", {playerName}" : "")}){(deleted ? "D" : "")}";
|
||||
}
|
||||
else
|
||||
Text = Identity.Name(entity.Value, _entityManager, _playerManager.LocalPlayer!.ControlledEntity!);
|
||||
{
|
||||
Icon.Sprite = _entityManager.GetComponentOrNull<SpriteComponent>(entity);
|
||||
Text = GetEntityDescription(entity.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user