]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Refactor context menu UI (#14334)
author08A <git@08a.re>
Mon, 13 Mar 2023 18:51:36 +0000 (19:51 +0100)
committerGitHub <noreply@github.com>
Mon, 13 Mar 2023 18:51:36 +0000 (14:51 -0400)
Content.Client/ContextMenu/UI/ContextMenuElement.xaml
Content.Client/ContextMenu/UI/ContextMenuElement.xaml.cs
Content.Client/ContextMenu/UI/ContextMenuPopup.xaml.cs
Content.Client/ContextMenu/UI/EntityMenuElement.cs
Content.Client/ContextMenu/UI/EntityMenuPresenterGrouping.cs
Content.Client/ContextMenu/UI/EntityMenuUIController.cs
Content.Client/Stylesheets/StyleNano.cs

index 05f02ddbd2998f6c6f9b6889d6f5b43fbe98edae..d0e39865b84e431bd010e311942fdb467c8b4a78 100644 (file)
@@ -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"
index d39bd7f5ad567e8188c1247323716d834c696e73..364986dd9d232754f01944e6d4e3a10990662ccf 100644 (file)
@@ -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;
index 90836ade4cb5ad062efd0f9e2363f4e029272fbf..a50e2c8ee448ffff960467cc3d0a7a0190485922 100644 (file)
@@ -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
 {
index 701cba861c7fd612e8cdc42bfe5a6f0edf77684a..74934724ff4b4dd4a48bbb47a4140d9f6d5b0be8 100644 (file)
@@ -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))
+            else
             {
-                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" : "")}";
+                Icon.Sprite = _entityManager.GetComponentOrNull<SpriteComponent>(entity);
+                Text = GetEntityDescription(entity.Value);
             }
-            else
-                Text = Identity.Name(entity.Value, _entityManager, _playerManager.LocalPlayer!.ControlledEntity!);
         }
     }
 }
index 2eb7869c5cabebbb451f9e61e37ddad3d81f7972..057bdd60266ddcd780e336c8ffdbdd514d3279ab 100644 (file)
@@ -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
 {
index 24ff87fc3ec442004d8ebeaacaf9f39742213e7f..0752f630ec55200ab0e771dc458c141250d366b6 100644 (file)
@@ -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;
             }
 
index 58f68ff45821effcd9a6c2d9bb079882884bb4b9..2c1bbb7e928d042b0acc7824f8ce3339abda0c63 100644 (file)
@@ -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),