Refactor context menu UI (#14334)
This commit is contained in:
@@ -4,10 +4,18 @@
|
||||
xmlns:ui="clr-namespace:Content.Client.ContextMenu.UI"
|
||||
MinHeight="{x:Static ui:ContextMenuElement.ElementHeight}">
|
||||
<BoxContainer Orientation="Horizontal">
|
||||
<Control
|
||||
<SpriteView
|
||||
Name="Icon"
|
||||
SetWidth="{x:Static ui:ContextMenuElement.ElementHeight}"
|
||||
SetHeight="{x:Static ui:ContextMenuElement.ElementHeight}"/>
|
||||
SetHeight="{x:Static ui:ContextMenuElement.ElementHeight}"
|
||||
OverrideDirection="South">
|
||||
<Label
|
||||
Name="IconLabel"
|
||||
HorizontalAlignment="Right"
|
||||
VerticalAlignment="Bottom"
|
||||
StyleClasses="contextMenuIconLabel"
|
||||
Visible="false"/>
|
||||
</SpriteView>
|
||||
<RichTextLabel
|
||||
Name="Label"
|
||||
MaxWidth="300"
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
using Content.Client.Resources;
|
||||
using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.ResourceManagement;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.UserInterface.XAML;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Client.ContextMenu.UI
|
||||
@@ -20,6 +16,7 @@ namespace Content.Client.ContextMenu.UI
|
||||
{
|
||||
public const string StyleClassContextMenuButton = "contextMenuButton";
|
||||
public const string StyleClassContextMenuExpansionTexture = "contextMenuExpansionTexture";
|
||||
public const string StyleClassEntityMenuIconLabel = "contextMenuIconLabel";
|
||||
|
||||
public const float ElementMargin = 2;
|
||||
public const float ElementHeight = 32;
|
||||
|
||||
@@ -2,9 +2,7 @@ using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.UserInterface.XAML;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Utility;
|
||||
using static Robust.Client.UserInterface.Controls.BoxContainer;
|
||||
|
||||
namespace Content.Client.ContextMenu.UI
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using Content.Shared.IdentityManagement;
|
||||
using Robust.Client.GameObjects;
|
||||
using System.Linq;
|
||||
using Robust.Client.UserInterface.Controllers;
|
||||
|
||||
namespace Content.Client.ContextMenu.UI
|
||||
{
|
||||
|
||||
@@ -5,7 +5,6 @@ using Content.Client.Gameplay;
|
||||
using Content.Client.Verbs;
|
||||
using Content.Client.Verbs.UI;
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.CombatMode;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Input;
|
||||
using Robust.Client.GameObjects;
|
||||
@@ -320,15 +319,7 @@ namespace Content.Client.ContextMenu.UI
|
||||
}
|
||||
|
||||
element.UpdateEntity(entity);
|
||||
|
||||
// Update the entity count & count label
|
||||
element.Count = 0;
|
||||
foreach (var subElement in element.SubMenu.MenuBody.Children)
|
||||
{
|
||||
if (subElement is EntityMenuElement entityElement)
|
||||
element.Count += entityElement.Count;
|
||||
}
|
||||
element.CountLabel.Text = element.Count.ToString();
|
||||
element.UpdateCount();
|
||||
|
||||
if (element.Count == 1)
|
||||
{
|
||||
@@ -337,7 +328,6 @@ namespace Content.Client.ContextMenu.UI
|
||||
element.Entity = entity;
|
||||
element.SubMenu.Dispose();
|
||||
element.SubMenu = null;
|
||||
element.CountLabel.Visible = false;
|
||||
Elements[entity.Value] = element;
|
||||
}
|
||||
|
||||
|
||||
@@ -931,7 +931,7 @@ namespace Content.Client.Stylesheets
|
||||
}),
|
||||
|
||||
// small number for the entity counter in the entity menu
|
||||
new StyleRule(new SelectorElement(typeof(Label), new[] {EntityMenuElement.StyleClassEntityMenuCountText}, null, null), new[]
|
||||
new StyleRule(new SelectorElement(typeof(Label), new[] {ContextMenuElement.StyleClassEntityMenuIconLabel}, null, null), new[]
|
||||
{
|
||||
new StyleProperty("font", notoSans10),
|
||||
new StyleProperty(Label.StylePropertyAlignMode, Label.AlignMode.Right),
|
||||
|
||||
Reference in New Issue
Block a user