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:
Leon Friedrich
2021-10-28 18:21:19 +13:00
committed by GitHub
parent 224952110e
commit 49296e33a0
36 changed files with 1421 additions and 1535 deletions

View File

@@ -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
}
}