Refactor Context Menus and make them use XAML & stylesheets (#4768)
* XAML verb menu * fix ghost FOV * spacing * rename missed "ContextMenu"->"EntityMenu" instances * move visibility checks to verb system * update comment * Remove CanSeeContainerCheck * use ScrollContainer measure option * MaxWidth / texxt line wrapping * verb category default Now when you click on a verb category, it should default to running the first member of that category. This makes it much more convenient to eject/insert when there is only a single option * only apply style to first verb category entry * Use new visibility flags * FoV -> Fov * Revert "only apply style to first verb category entry" This reverts commit 9a6a17dba600e3ae0421caed59fcab145c260c99. * make all entity menu visibility checks clientside * Fix empty unbuckle category * fix merge
This commit is contained in:
69
Content.Client/Verbs/UI/VerbMenuElement.cs
Normal file
69
Content.Client/Verbs/UI/VerbMenuElement.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using Content.Client.ContextMenu.UI;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.Utility;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Client.Verbs.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// Slight extension of <see cref="ContextMenuElement"/> that uses a SpriteSpecifier for it's icon and provides
|
||||
/// constructors that take verbs or verb categories.
|
||||
/// </summary>
|
||||
public partial class VerbMenuElement : ContextMenuElement
|
||||
{
|
||||
public const string StyleClassVerbInteractionText = "InteractionVerb";
|
||||
public const string StyleClassVerbActivationText = "ActivationVerb";
|
||||
public const string StyleClassVerbAlternativeText = "AlternativeVerb";
|
||||
public const string StyleClassVerbOtherText = "OtherVerb";
|
||||
|
||||
public const float VerbTooltipDelay = 0.5f;
|
||||
|
||||
// Setters to provide access to children generated by XAML.
|
||||
public bool IconVisible { set => Icon.Visible = value; }
|
||||
public bool TextVisible { set => Label.Visible = value; }
|
||||
|
||||
// Top quality variable naming
|
||||
public Verb? Verb;
|
||||
|
||||
public VerbType Type;
|
||||
|
||||
public VerbMenuElement(string? text, SpriteSpecifier? icon, VerbType verbType) : base(text)
|
||||
{
|
||||
Icon.AddChild(new TextureRect()
|
||||
{
|
||||
Texture = icon?.Frame0(),
|
||||
Stretch = TextureRect.StretchMode.KeepAspectCentered
|
||||
});
|
||||
|
||||
Type = verbType;
|
||||
|
||||
// Set text font style based on verb type
|
||||
switch (verbType)
|
||||
{
|
||||
case VerbType.Interaction:
|
||||
Label.SetOnlyStyleClass(StyleClassVerbInteractionText);
|
||||
break;
|
||||
case VerbType.Activation:
|
||||
Label.SetOnlyStyleClass(StyleClassVerbActivationText);
|
||||
break;
|
||||
case VerbType.Alternative:
|
||||
Label.SetOnlyStyleClass(StyleClassVerbAlternativeText);
|
||||
break;
|
||||
default:
|
||||
Label.SetOnlyStyleClass(StyleClassVerbOtherText);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public VerbMenuElement(Verb verb, VerbType verbType) : this(verb.Text, verb.Icon, verbType)
|
||||
{
|
||||
ToolTip = verb.Message;
|
||||
TooltipDelay = VerbTooltipDelay;
|
||||
Disabled = verb.Disabled;
|
||||
Verb = verb;
|
||||
}
|
||||
|
||||
public VerbMenuElement(VerbCategory category, VerbType verbType) : this(category.Text, category.Icon, verbType) { }
|
||||
}
|
||||
}
|
||||
200
Content.Client/Verbs/UI/VerbMenuPresenter.cs
Normal file
200
Content.Client/Verbs/UI/VerbMenuPresenter.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Client.ContextMenu.UI;
|
||||
using Content.Shared.Input;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Input;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Maths;
|
||||
using static Robust.Client.UserInterface.Controls.BoxContainer;
|
||||
|
||||
namespace Content.Client.Verbs.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// This class handles the displaying of the verb menu.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// In addition to the normal <see cref="ContextMenuPresenter"/> functionality, this also provides functions
|
||||
/// open a verb menu for a given entity, add verbs to it, and add server-verbs when the server response is
|
||||
/// received.
|
||||
/// </remarks>
|
||||
public sealed class VerbMenuPresenter : ContextMenuPresenter
|
||||
{
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
|
||||
|
||||
private readonly VerbSystem _verbSystem;
|
||||
|
||||
public EntityUid CurrentTarget;
|
||||
public Dictionary<VerbType, SortedSet<Verb>> CurrentVerbs = new();
|
||||
|
||||
public VerbMenuPresenter(VerbSystem verbSystem) : base()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
_verbSystem = verbSystem;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Open a verb menu and fill it work verbs applicable to the given target entity.
|
||||
/// </summary>
|
||||
public void OpenVerbMenu(IEntity target)
|
||||
{
|
||||
var user = _playerManager.LocalPlayer?.ControlledEntity;
|
||||
if (user == null)
|
||||
return;
|
||||
|
||||
Close();
|
||||
|
||||
CurrentTarget = target.Uid;
|
||||
CurrentVerbs = _verbSystem.GetVerbs(target, user, VerbType.All);
|
||||
|
||||
if (!target.Uid.IsClientSide())
|
||||
{
|
||||
AddElement(RootMenu, new ContextMenuElement(Loc.GetString("verb-system-waiting-on-server-text")));
|
||||
}
|
||||
|
||||
// Show the menu
|
||||
FillVerbPopup();
|
||||
RootMenu.SetPositionLast();
|
||||
var box = UIBox2.FromDimensions(_userInterfaceManager.MousePositionScaled.Position, (1, 1));
|
||||
RootMenu.Open(box);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fill the verb pop-up using the verbs stored in <see cref="CurrentVerbs"/>
|
||||
/// </summary>
|
||||
private void FillVerbPopup()
|
||||
{
|
||||
if (RootMenu == null)
|
||||
return;
|
||||
|
||||
// Add verbs to pop-up, grouped by type. Order determined by how types are defined VerbTypes
|
||||
var types = CurrentVerbs.Keys.ToList();
|
||||
types.Sort();
|
||||
foreach (var type in types)
|
||||
{
|
||||
if (!CurrentVerbs.TryGetValue(type, out var verbs))
|
||||
continue;
|
||||
|
||||
HashSet<string> listedCategories = new();
|
||||
foreach (var verb in verbs)
|
||||
{
|
||||
if (verb.Category == null)
|
||||
{
|
||||
var element = new VerbMenuElement(verb, type);
|
||||
AddElement(RootMenu, element);
|
||||
}
|
||||
|
||||
else if (listedCategories.Add(verb.Category.Text))
|
||||
AddVerbCategory(verb.Category, verbs, type);
|
||||
}
|
||||
}
|
||||
|
||||
RootMenu.InvalidateMeasure();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a verb category button to the pop-up
|
||||
/// </summary>
|
||||
public void AddVerbCategory(VerbCategory category, SortedSet<Verb> verbs, VerbType type)
|
||||
{
|
||||
// Get a list of the verbs in this category
|
||||
List<Verb> verbsInCategory = new();
|
||||
var drawIcons = false;
|
||||
foreach (var verb in verbs)
|
||||
{
|
||||
if (verb.Category?.Text == category.Text)
|
||||
{
|
||||
verbsInCategory.Add(verb);
|
||||
drawIcons = drawIcons || verb.Icon != null;
|
||||
}
|
||||
}
|
||||
|
||||
if (verbsInCategory.Count == 0)
|
||||
return;
|
||||
|
||||
var element = new VerbMenuElement(category, type);
|
||||
AddElement(RootMenu, element);
|
||||
|
||||
// Create the pop-up that appears when hovering over this element
|
||||
element.SubMenu = new ContextMenuPopup(this, element);
|
||||
foreach (var verb in verbsInCategory)
|
||||
{
|
||||
var subElement = new VerbMenuElement(verb, type)
|
||||
{
|
||||
IconVisible = drawIcons,
|
||||
TextVisible = !category.IconsOnly
|
||||
};
|
||||
AddElement(element.SubMenu, subElement);
|
||||
}
|
||||
|
||||
if (category.IconsOnly)
|
||||
element.SubMenu.MenuBody.Orientation = LayoutOrientation.Horizontal;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add verbs from the server to <see cref="CurrentVerbs"/> and update the verb menu.
|
||||
/// </summary>
|
||||
public void AddServerVerbs(Dictionary<VerbType, List<Verb>>? verbs)
|
||||
{
|
||||
RootMenu.MenuBody.DisposeAllChildren();
|
||||
|
||||
// Verbs may be null if the server does not think we can see the target entity. This **should** not happen.
|
||||
if (verbs == null)
|
||||
{
|
||||
// remove "waiting for server..." and inform user that something went wrong.
|
||||
AddElement(RootMenu, new ContextMenuElement(Loc.GetString("verb-system-null-server-response")));
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the new server-side verbs.
|
||||
foreach (var (verbType, verbSet) in verbs)
|
||||
{
|
||||
if (!CurrentVerbs.TryAdd(verbType, new SortedSet<Verb>(verbSet)))
|
||||
{
|
||||
CurrentVerbs[verbType].UnionWith(verbSet);
|
||||
}
|
||||
}
|
||||
|
||||
FillVerbPopup();
|
||||
}
|
||||
|
||||
public override void OnKeyBindDown(ContextMenuElement element, GUIBoundKeyEventArgs args)
|
||||
{
|
||||
if (args.Function != EngineKeyFunctions.Use && args.Function != ContentKeyFunctions.ActivateItemInWorld)
|
||||
return;
|
||||
|
||||
if (element is not VerbMenuElement verbElement)
|
||||
return;
|
||||
|
||||
var verb = verbElement.Verb;
|
||||
|
||||
if (verb == null)
|
||||
{
|
||||
// The user probably clicked on a verb category.
|
||||
// We will act as if they clicked on the first verb in that category.
|
||||
|
||||
if (verbElement.SubMenu == null || verbElement.SubMenu.ChildCount == 0)
|
||||
return;
|
||||
|
||||
if (verbElement.SubMenu.MenuBody.Children.First() is not VerbMenuElement verbCategoryElement)
|
||||
return;
|
||||
|
||||
verb = verbCategoryElement.Verb;
|
||||
|
||||
if (verb == null)
|
||||
return;
|
||||
}
|
||||
|
||||
_verbSystem.ExecuteVerb(CurrentTarget, verb, verbElement.Type);
|
||||
if (verb.CloseMenu)
|
||||
_verbSystem.CloseAllMenus();
|
||||
|
||||
args.Handle();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,246 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Content.Client.ContextMenu.UI;
|
||||
using Content.Client.Resources;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.ResourceManagement;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.Utility;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Utility;
|
||||
using static Robust.Client.UserInterface.Controls.BoxContainer;
|
||||
using Timer = Robust.Shared.Timing.Timer;
|
||||
|
||||
namespace Content.Client.Verbs
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// This pop-up appears when hovering over a verb category in the context menu.
|
||||
/// </summary>
|
||||
public sealed class VerbCategoryPopup : ContextMenuPopup
|
||||
{
|
||||
public VerbCategoryPopup(VerbSystem system, IEnumerable<Verb> verbs, VerbType type, EntityUid target, bool drawOnlyIcons)
|
||||
: base()
|
||||
{
|
||||
// Do any verbs have icons? If not, don't bother leaving space for icons in the pop-up.
|
||||
var drawVerbIcons = false;
|
||||
foreach (var verb in verbs)
|
||||
{
|
||||
if (verb.Icon != null)
|
||||
{
|
||||
drawVerbIcons = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If no verbs have icons. we cannot draw only icons
|
||||
if (drawVerbIcons == false)
|
||||
drawOnlyIcons = false;
|
||||
|
||||
// If we are drawing only icons, show them side by side
|
||||
if (drawOnlyIcons)
|
||||
List.Orientation = LayoutOrientation.Horizontal;
|
||||
|
||||
foreach (var verb in verbs)
|
||||
{
|
||||
AddToMenu(new VerbButton(system, verb, type, target, drawVerbIcons));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class VerbButton : BaseButton
|
||||
{
|
||||
public VerbButton(VerbSystem system, Verb verb, VerbType type, EntityUid target, bool drawIcons = true, bool categoryPrefix = false) : base()
|
||||
{
|
||||
Disabled = verb.Disabled;
|
||||
ToolTip = verb.Tooltip;
|
||||
TooltipDelay = 0.5f;
|
||||
|
||||
var buttonContents = new BoxContainer { Orientation = LayoutOrientation.Horizontal };
|
||||
|
||||
// maybe draw verb icons
|
||||
if (drawIcons)
|
||||
{
|
||||
TextureRect icon = new()
|
||||
{
|
||||
MinSize = (ContextMenuPopup.ButtonHeight, ContextMenuPopup.ButtonHeight),
|
||||
Stretch = TextureRect.StretchMode.KeepCentered,
|
||||
TextureScale = (0.5f, 0.5f)
|
||||
};
|
||||
|
||||
// Even though we are drawing icons, the icon for this specific verb may be null.
|
||||
if (verb.Icon != null)
|
||||
{
|
||||
icon.Texture = verb.Icon.Frame0();
|
||||
} else if (categoryPrefix && verb.Category?.Icon != null)
|
||||
{
|
||||
// we will use the category icon instead
|
||||
icon.Texture = verb.Category.Icon.Frame0();
|
||||
}
|
||||
|
||||
buttonContents.AddChild(icon);
|
||||
}
|
||||
|
||||
// maybe add a label
|
||||
if (verb.Text != string.Empty || categoryPrefix)
|
||||
{
|
||||
// First add a small bit of padding
|
||||
buttonContents.AddChild(new Control { MinSize = (4, ContextMenuPopup.ButtonHeight) });
|
||||
|
||||
var label = new RichTextLabel();
|
||||
var text = categoryPrefix ? verb.Category!.Text + " " + verb.Text : verb.Text;
|
||||
label.SetMessage(FormattedMessage.FromMarkupPermissive(text.Trim()));
|
||||
label.VerticalAlignment = VAlignment.Center;
|
||||
buttonContents.AddChild(label);
|
||||
|
||||
// Then also add some padding after the text.
|
||||
buttonContents.AddChild(new Control { MinSize = (4, ContextMenuPopup.ButtonHeight) });
|
||||
}
|
||||
|
||||
AddChild(buttonContents);
|
||||
|
||||
if (Disabled)
|
||||
return;
|
||||
|
||||
// give the button functionality!
|
||||
OnPressed += _ =>
|
||||
{
|
||||
if (verb.CloseMenu)
|
||||
system.ContextMenuPresenter.CloseAllMenus();
|
||||
system.TryExecuteVerb(verb, target, type);
|
||||
};
|
||||
}
|
||||
|
||||
protected override void Draw(DrawingHandleScreen handle)
|
||||
{
|
||||
base.Draw(handle);
|
||||
|
||||
if (Disabled)
|
||||
{
|
||||
// use transparent-black rectangle to create a darker background.
|
||||
handle.DrawRect(PixelSizeBox, new Color(0,0,0,155));
|
||||
}
|
||||
else if (DrawMode == DrawModeEnum.Hover)
|
||||
{
|
||||
// Draw a lighter shade of gray when hovered over
|
||||
handle.DrawRect(PixelSizeBox, Color.DarkSlateGray);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class VerbCategoryButton : Control
|
||||
{
|
||||
private readonly VerbSystem _system;
|
||||
|
||||
private CancellationTokenSource? _openCancel;
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not to hide member verb text and just show icons.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If no members have icons, this option is ignored and text is shown anyways. Defaults to using <see cref="VerbCategory.IconsOnly"/>.
|
||||
/// </remarks>
|
||||
private readonly bool _drawOnlyIcons;
|
||||
|
||||
/// <summary>
|
||||
/// The pop-up that appears when hovering over this verb group.
|
||||
/// </summary>
|
||||
private readonly VerbCategoryPopup _popup;
|
||||
|
||||
public VerbCategoryButton(VerbSystem system, VerbCategory category, IEnumerable<Verb> verbs, VerbType type, EntityUid target, bool? drawOnlyIcons = null) : base()
|
||||
{
|
||||
_system = system;
|
||||
_drawOnlyIcons = drawOnlyIcons ?? category.IconsOnly;
|
||||
|
||||
MouseFilter = MouseFilterMode.Stop;
|
||||
|
||||
// Contents of the button stored in this box container
|
||||
var box = new BoxContainer() { Orientation = LayoutOrientation.Horizontal };
|
||||
|
||||
// First we add the icon for the verb group
|
||||
var icon = new TextureRect
|
||||
{
|
||||
MinSize = (ContextMenuPopup.ButtonHeight, ContextMenuPopup.ButtonHeight),
|
||||
TextureScale = (0.5f, 0.5f),
|
||||
Stretch = TextureRect.StretchMode.KeepCentered,
|
||||
};
|
||||
if (category.Icon != null)
|
||||
{
|
||||
icon.Texture = category.Icon.Frame0();
|
||||
}
|
||||
box.AddChild(icon);
|
||||
|
||||
// Some padding before the text
|
||||
box.AddChild(new Control { MinSize = (4, ContextMenuPopup.ButtonHeight) });
|
||||
|
||||
// Then we add the label
|
||||
var label = new RichTextLabel();
|
||||
label.SetMessage(FormattedMessage.FromMarkupPermissive(category.Text));
|
||||
label.HorizontalExpand = true;
|
||||
label.VerticalAlignment = VAlignment.Center;
|
||||
box.AddChild(label);
|
||||
|
||||
// Then also add some padding after the text.
|
||||
box.AddChild(new Control { MinSize = (4, ContextMenuPopup.ButtonHeight) });
|
||||
|
||||
// Then add the little ">" icon that tells you it's a group of verbs
|
||||
box.AddChild(new TextureRect
|
||||
{
|
||||
Texture = IoCManager.Resolve<IResourceCache>()
|
||||
.GetTexture("/Textures/Interface/VerbIcons/group.svg.192dpi.png"),
|
||||
TextureScale = (0.5f, 0.5f),
|
||||
Stretch = TextureRect.StretchMode.KeepCentered,
|
||||
});
|
||||
|
||||
// The pop-up that appears when hovering over the button
|
||||
_popup = new VerbCategoryPopup(_system, verbs, type, target, _drawOnlyIcons);
|
||||
UserInterfaceManager.ModalRoot.AddChild(_popup);
|
||||
|
||||
AddChild(box);
|
||||
}
|
||||
|
||||
protected override void Draw(DrawingHandleScreen handle)
|
||||
{
|
||||
base.Draw(handle);
|
||||
|
||||
if (this == UserInterfaceManager.CurrentlyHovered ||
|
||||
_system.CurrentCategoryPopup == _popup)
|
||||
{
|
||||
handle.DrawRect(PixelSizeBox, Color.DarkSlateGray);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Open a verb category pop-up after a short delay.
|
||||
/// </summary>
|
||||
protected override void MouseEntered()
|
||||
{
|
||||
base.MouseEntered();
|
||||
|
||||
_openCancel = new CancellationTokenSource();
|
||||
|
||||
Timer.Spawn(ContextMenuPresenter.HoverDelay, () =>
|
||||
{
|
||||
_system.CurrentCategoryPopup?.Close();
|
||||
_system.CurrentCategoryPopup = _popup;
|
||||
var upperRight = GlobalPosition + (Width + ContextMenuPopup.MarginSize, -ContextMenuPopup.MarginSize);
|
||||
_popup.Open(UIBox2.FromDimensions(upperRight, (1, 1)), GlobalPosition);
|
||||
}, _openCancel.Token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancel the delayed pop-up
|
||||
/// </summary>
|
||||
protected override void MouseExited()
|
||||
{
|
||||
base.MouseExited();
|
||||
|
||||
_openCancel?.Cancel();
|
||||
_openCancel = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Client.ContextMenu.UI;
|
||||
using Content.Client.Popups;
|
||||
using Content.Client.Verbs.UI;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.GameTicking;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Content.Shared.Tag;
|
||||
using Content.Shared.Verbs;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
|
||||
@@ -18,211 +25,201 @@ namespace Content.Client.Verbs
|
||||
[UsedImplicitly]
|
||||
public sealed class VerbSystem : SharedVerbSystem
|
||||
{
|
||||
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
[Dependency] private readonly IEntityLookup _entityLookup = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
|
||||
public ContextMenuPresenter ContextMenuPresenter = default!;
|
||||
/// <summary>
|
||||
/// When a user right clicks somewhere, how large is the box we use to get entities for the context menu?
|
||||
/// </summary>
|
||||
public const float EntityMenuLookupSize = 1f;
|
||||
|
||||
public EntityUid CurrentTarget;
|
||||
public ContextMenuPopup? CurrentVerbPopup;
|
||||
public ContextMenuPopup? CurrentCategoryPopup;
|
||||
public Dictionary<VerbType, SortedSet<Verb>> CurrentVerbs = new();
|
||||
public EntityMenuPresenter EntityMenu = default!;
|
||||
public VerbMenuPresenter VerbMenu = default!;
|
||||
|
||||
[Dependency] private readonly IEyeManager _eyeManager = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Whether to show all entities on the context menu.
|
||||
/// These flags determine what entities the user can see on the context menu.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Verb execution will only be affected if the server also agrees that this player can see the target
|
||||
/// entity.
|
||||
/// </remarks>
|
||||
public bool CanSeeAllContext = false;
|
||||
public MenuVisibility Visibility;
|
||||
|
||||
// TODO VERBS Move presenter out of the system
|
||||
// TODO VERBS Separate the rest of the UI from the logic
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeNetworkEvent<RoundRestartCleanupEvent>(Reset);
|
||||
SubscribeNetworkEvent<VerbsResponseEvent>(HandleVerbResponse);
|
||||
SubscribeNetworkEvent<SetSeeAllContextEvent>(SetSeeAllContext);
|
||||
|
||||
ContextMenuPresenter = new ContextMenuPresenter(this);
|
||||
EntityMenu = new(this);
|
||||
VerbMenu = new(this);
|
||||
}
|
||||
|
||||
private void Reset(RoundRestartCleanupEvent ev)
|
||||
public void Reset(RoundRestartCleanupEvent ev)
|
||||
{
|
||||
ContextMenuPresenter.CloseAllMenus();
|
||||
CloseAllMenus();
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
ContextMenuPresenter?.Dispose();
|
||||
EntityMenu?.Dispose();
|
||||
VerbMenu?.Dispose();
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
ContextMenuPresenter?.Update();
|
||||
EntityMenu?.Update();
|
||||
}
|
||||
|
||||
private void SetSeeAllContext(SetSeeAllContextEvent args)
|
||||
public void CloseAllMenus()
|
||||
{
|
||||
CanSeeAllContext = args.CanSeeAllContext;
|
||||
EntityMenu.Close();
|
||||
VerbMenu.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute actions associated with the given verb. If there are no defined actions, this will instead ask
|
||||
/// the server to run the given verb.
|
||||
/// Get all of the entities in an area for displaying on the context menu.
|
||||
/// </summary>
|
||||
public void TryExecuteVerb(Verb verb, EntityUid target, VerbType verbType)
|
||||
public bool TryGetEntityMenuEntities(MapCoordinates targetPos, [NotNullWhen(true)] out List<IEntity>? result)
|
||||
{
|
||||
if (!TryExecuteVerb(verb))
|
||||
RaiseNetworkEvent(new TryExecuteVerbEvent(target, verb, verbType));
|
||||
}
|
||||
result = null;
|
||||
var player = _playerManager.LocalPlayer?.ControlledEntity;
|
||||
|
||||
public void OpenVerbMenu(IEntity target, ScreenCoordinates screenCoordinates)
|
||||
{
|
||||
if (CurrentVerbPopup != null)
|
||||
if (player == null)
|
||||
return false;
|
||||
|
||||
var visibility = _eyeManager.CurrentEye.DrawFov
|
||||
? Visibility
|
||||
: Visibility | MenuVisibility.NoFov;
|
||||
|
||||
// Check if we have LOS to the clicked-location.
|
||||
if ((visibility & MenuVisibility.NoFov) == 0 &&
|
||||
!player.InRangeUnOccluded(targetPos, range: ExamineSystemShared.ExamineRange))
|
||||
return false;
|
||||
|
||||
// Get entities
|
||||
var entities = _entityLookup.GetEntitiesIntersecting(
|
||||
targetPos.MapId,
|
||||
Box2.CenteredAround(targetPos.Position, (EntityMenuLookupSize, EntityMenuLookupSize)))
|
||||
.ToList();
|
||||
|
||||
if (entities.Count == 0)
|
||||
return false;
|
||||
|
||||
if (visibility == MenuVisibility.All)
|
||||
{
|
||||
CloseVerbMenu();
|
||||
result = entities;
|
||||
return true;
|
||||
}
|
||||
|
||||
var user = _playerManager.LocalPlayer?.ControlledEntity;
|
||||
if (user == null)
|
||||
return;
|
||||
// remove any entities in containers
|
||||
if ((visibility & MenuVisibility.InContainer) == 0)
|
||||
{
|
||||
foreach (var entity in entities.ToList())
|
||||
{
|
||||
if (!player.IsInSameOrTransparentContainer(entity))
|
||||
entities.Remove(entity);
|
||||
}
|
||||
}
|
||||
|
||||
CurrentTarget = target.Uid;
|
||||
// remove any invisible entities
|
||||
if ((visibility & MenuVisibility.Invisible) == 0)
|
||||
{
|
||||
foreach (var entity in entities.ToList())
|
||||
{
|
||||
if (!EntityManager.TryGetComponent(entity.Uid, out ISpriteComponent? spriteComponent) ||
|
||||
!spriteComponent.Visible)
|
||||
{
|
||||
entities.Remove(entity);
|
||||
continue;
|
||||
}
|
||||
|
||||
CurrentVerbPopup = new ContextMenuPopup();
|
||||
_userInterfaceManager.ModalRoot.AddChild(CurrentVerbPopup);
|
||||
CurrentVerbPopup.OnPopupHide += CloseVerbMenu;
|
||||
if (entity.HasTag("HideContextMenu"))
|
||||
entities.Remove(entity);
|
||||
}
|
||||
}
|
||||
|
||||
CurrentVerbs = GetVerbs(target, user, VerbType.All);
|
||||
// Remove any entities that do not have LOS
|
||||
if ((visibility & MenuVisibility.NoFov) == 0)
|
||||
{
|
||||
var playerPos = player.Transform.MapPosition;
|
||||
foreach (var entity in entities.ToList())
|
||||
{
|
||||
if (!ExamineSystemShared.InRangeUnOccluded(
|
||||
playerPos,
|
||||
entity.Transform.MapPosition,
|
||||
ExamineSystemShared.ExamineRange,
|
||||
null))
|
||||
{
|
||||
entities.Remove(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (entities.Count == 0)
|
||||
return false;
|
||||
|
||||
result = entities;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ask the server to send back a list of server-side verbs, and for now return an incomplete list of verbs
|
||||
/// (only those defined locally).
|
||||
/// </summary>
|
||||
public Dictionary<VerbType, SortedSet<Verb>> GetVerbs(IEntity target, IEntity user, VerbType verbTypes)
|
||||
{
|
||||
if (!target.Uid.IsClientSide())
|
||||
{
|
||||
CurrentVerbPopup.AddToMenu(new Label { Text = Loc.GetString("verb-system-waiting-on-server-text") });
|
||||
RaiseNetworkEvent(new RequestServerVerbsEvent(CurrentTarget, VerbType.All));
|
||||
RaiseNetworkEvent(new RequestServerVerbsEvent(target.Uid, verbTypes));
|
||||
}
|
||||
|
||||
// Show the menu
|
||||
FillVerbPopup(CurrentVerbPopup);
|
||||
var box = UIBox2.FromDimensions(screenCoordinates.Position, (1, 1));
|
||||
CurrentVerbPopup.Open(box);
|
||||
|
||||
return GetLocalVerbs(target, user, verbTypes);
|
||||
}
|
||||
|
||||
public void OnContextButtonPressed(IEntity entity)
|
||||
/// <summary>
|
||||
/// Execute actions associated with the given verb.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Unless this is a client-exclusive verb, this will also tell the server to run the same verb. However, if the verb
|
||||
/// is disabled and has a tooltip, this function will only generate a pop-up-message instead of executing anything.
|
||||
/// </remarks>
|
||||
public void ExecuteVerb(EntityUid target, Verb verb, VerbType verbType)
|
||||
{
|
||||
OpenVerbMenu(entity, _userInterfaceManager.MousePositionScaled);
|
||||
if (verb.Disabled)
|
||||
{
|
||||
if (verb.Message != null)
|
||||
_popupSystem.PopupCursor(verb.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
ExecuteVerb(verb);
|
||||
|
||||
if (!verb.ClientExclusive)
|
||||
{
|
||||
RaiseNetworkEvent(new ExecuteVerbEvent(target, verb, verbType));
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleVerbResponse(VerbsResponseEvent msg)
|
||||
{
|
||||
if (CurrentTarget != msg.Entity || CurrentVerbPopup == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// This **should** not happen.
|
||||
if (msg.Verbs == null)
|
||||
{
|
||||
// update "waiting for server...".
|
||||
CurrentVerbPopup.List.DisposeAllChildren();
|
||||
CurrentVerbPopup.AddToMenu(new Label { Text = Loc.GetString("verb-system-null-server-response") });
|
||||
FillVerbPopup(CurrentVerbPopup);
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the new server-side verbs.
|
||||
foreach (var (verbType, verbSet) in msg.Verbs)
|
||||
{
|
||||
SortedSet<Verb> sortedVerbs = new (verbSet);
|
||||
if (!CurrentVerbs.TryAdd(verbType, sortedVerbs))
|
||||
{
|
||||
CurrentVerbs[verbType].UnionWith(sortedVerbs);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear currently shown verbs and show new ones
|
||||
CurrentVerbPopup.List.DisposeAllChildren();
|
||||
FillVerbPopup(CurrentVerbPopup);
|
||||
}
|
||||
|
||||
private void FillVerbPopup(ContextMenuPopup popup)
|
||||
{
|
||||
if (CurrentTarget == EntityUid.Invalid)
|
||||
if (!VerbMenu.RootMenu.Visible || VerbMenu.CurrentTarget != msg.Entity)
|
||||
return;
|
||||
|
||||
// Add verbs to pop-up, grouped by type. Order determined by how types are defined VerbTypes
|
||||
var types = CurrentVerbs.Keys.ToList();
|
||||
types.Sort();
|
||||
foreach (var type in types)
|
||||
{
|
||||
AddVerbSet(popup, type);
|
||||
}
|
||||
|
||||
// Were the verb lists empty?
|
||||
if (popup.List.ChildCount == 0)
|
||||
{
|
||||
var panel = new PanelContainer();
|
||||
panel.AddChild(new Label { Text = Loc.GetString("verb-system-no-verbs-text") });
|
||||
popup.AddChild(panel);
|
||||
}
|
||||
|
||||
popup.InvalidateMeasure();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a list of verbs to a BoxContainer. Iterates over the given verbs list and creates GUI buttons.
|
||||
/// </summary>
|
||||
private void AddVerbSet(ContextMenuPopup popup, VerbType type)
|
||||
{
|
||||
if (!CurrentVerbs.TryGetValue(type, out var verbSet) || verbSet.Count == 0)
|
||||
return;
|
||||
|
||||
HashSet<string> listedCategories = new();
|
||||
|
||||
foreach (var verb in verbSet)
|
||||
{
|
||||
if (verb.Category == null)
|
||||
{
|
||||
// Lone verb without a category. just create a button for it
|
||||
popup.AddToMenu(new VerbButton(this, verb, type, CurrentTarget));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (listedCategories.Contains(verb.Category.Text))
|
||||
{
|
||||
// This verb was already included in a verb-category button added by a previous verb
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the verbs in the category
|
||||
var verbsInCategory = verbSet.Where(v => v.Category?.Text == verb.Category.Text);
|
||||
|
||||
popup.AddToMenu(
|
||||
new VerbCategoryButton(this, verb.Category, verbsInCategory, type, CurrentTarget));
|
||||
listedCategories.Add(verb.Category.Text);
|
||||
continue;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void CloseVerbMenu()
|
||||
{
|
||||
if (CurrentVerbPopup != null)
|
||||
{
|
||||
CurrentVerbPopup.OnPopupHide -= CloseVerbMenu;
|
||||
CurrentVerbPopup.Dispose();
|
||||
CurrentVerbPopup = null;
|
||||
}
|
||||
|
||||
CurrentCategoryPopup?.Dispose();
|
||||
CurrentCategoryPopup = null;
|
||||
CurrentTarget = EntityUid.Invalid;
|
||||
CurrentVerbs.Clear();
|
||||
VerbMenu.AddServerVerbs(msg.Verbs);
|
||||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum MenuVisibility
|
||||
{
|
||||
// What entities can a user see on the entity menu?
|
||||
Default = 0, // They can only see entities in FoV.
|
||||
NoFov = 1 << 0, // They ignore FoV restrictions
|
||||
InContainer = 1 << 1, // They can see through containers.
|
||||
Invisible = 1 << 2, // They can see entities without sprites and the "HideContextMenu" tag is ignored.
|
||||
All = NoFov | InContainer | Invisible
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user