EntityMenuElement.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Linq;
  2. using Content.Client.Administration.Managers;
  3. using Content.Client.Administration.Systems;
  4. using Content.Client.UserInterface;
  5. using Content.Shared.Administration;
  6. using Content.Shared.IdentityManagement;
  7. using Robust.Client.GameObjects;
  8. using Robust.Client.Player;
  9. namespace Content.Client.ContextMenu.UI
  10. {
  11. public sealed partial class EntityMenuElement : ContextMenuElement, IEntityControl
  12. {
  13. [Dependency] private readonly IClientAdminManager _adminManager = default!;
  14. [Dependency] private readonly IEntityManager _entityManager = default!;
  15. [Dependency] private readonly IPlayerManager _playerManager = default!;
  16. private AdminSystem _adminSystem;
  17. /// <summary>
  18. /// The entity that can be accessed by interacting with this element.
  19. /// </summary>
  20. public EntityUid? Entity;
  21. /// <summary>
  22. /// How many entities are accessible through this element's sub-menus.
  23. /// </summary>
  24. public int Count { get; private set; }
  25. public EntityMenuElement(EntityUid? entity = null)
  26. {
  27. IoCManager.InjectDependencies(this);
  28. _adminSystem = _entityManager.System<AdminSystem>();
  29. Entity = entity;
  30. if (Entity == null)
  31. return;
  32. Count = 1;
  33. UpdateEntity();
  34. }
  35. protected override void Dispose(bool disposing)
  36. {
  37. base.Dispose(disposing);
  38. Entity = null;
  39. Count = 0;
  40. }
  41. private string? SearchPlayerName(EntityUid entity)
  42. {
  43. var netEntity = _entityManager.GetNetEntity(entity);
  44. return _adminSystem.PlayerList.FirstOrDefault(player => player.NetEntity == netEntity)?.Username;
  45. }
  46. /// <summary>
  47. /// Update the entity count
  48. /// </summary>
  49. public void UpdateCount()
  50. {
  51. if (SubMenu == null)
  52. return;
  53. Count = 0;
  54. foreach (var subElement in SubMenu.MenuBody.Children)
  55. {
  56. if (subElement is EntityMenuElement entityElement)
  57. Count += entityElement.Count;
  58. }
  59. IconLabel.Visible = Count > 1;
  60. if (IconLabel.Visible)
  61. IconLabel.Text = Count.ToString();
  62. }
  63. private string GetEntityDescriptionAdmin(EntityUid entity)
  64. {
  65. var representation = _entityManager.ToPrettyString(entity);
  66. var name = representation.Name;
  67. var prototype = representation.Prototype;
  68. var playerName = representation.Session?.Name ?? SearchPlayerName(entity);
  69. var deleted = representation.Deleted;
  70. return $"{name} ({_entityManager.GetNetEntity(entity).ToString()}{(prototype != null ? $", {prototype}" : "")}{(playerName != null ? $", {playerName}" : "")}){(deleted ? "D" : "")}";
  71. }
  72. private string GetEntityDescription(EntityUid entity)
  73. {
  74. if (_adminManager.HasFlag(AdminFlags.Admin | AdminFlags.Debug))
  75. {
  76. return GetEntityDescriptionAdmin(entity);
  77. }
  78. return Identity.Name(entity, _entityManager, _playerManager.LocalEntity!);
  79. }
  80. /// <summary>
  81. /// Update the icon and text of this element based on the given entity or this element's own entity if none
  82. /// is provided.
  83. /// </summary>
  84. public void UpdateEntity(EntityUid? entity = null)
  85. {
  86. entity ??= Entity;
  87. // check whether entity is null, invalid, or has been deleted.
  88. // _entityManager.Deleted() implicitly checks all of these.
  89. if (_entityManager.Deleted(entity))
  90. {
  91. Icon.SetEntity(null);
  92. Text = string.Empty;
  93. }
  94. else
  95. {
  96. Icon.SetEntity(entity);
  97. Text = GetEntityDescription(entity.Value);
  98. }
  99. }
  100. EntityUid? IEntityControl.UiEntity => Entity;
  101. }
  102. }