ECS verbs and update context menu (#4594)

* Functioning ECS verbs

Currently only ID card console works.

* Changed verb types and allow ID card insertions

* Verb GUI sorting and verb networking

* More networking, and shared components

* Clientside verbs work now.

* Verb enums changed to bitmask flags

* Verb Categories redo

* Fix range check

* GasTank Verb

* Remove unnecessary bodypart verb

* Buckle Verb

* buckle & unbuckle verbs

* Updated range checks

* Item cabinet verbs

* Add range user override

* construction verb

* Chemistry machine verbs

* Climb Verb

* Generalise pulled entity verbs

* ViewVariables Verb

* rejuvenate, delete, sentient, control verbs

* Outfit verb

* inrangeunoccluded and tubedirection verbs

* attach-to verbs

* remove unused verbs and move VV

* Rename DebugVerbSystem

* Ghost role and pointing verbs

* Remove global verbs

* Allow verbs to raise events

* Changing categories and simplifying debug verbs

* Add rotate and flip verbs

* fix rejuvenate test

* redo context menu

* new Add Gas debug verb

* Add Set Temperature debug verb

* Uncuff verb

* Disposal unit verbs

* Add pickup verb

* lock/unlock verb

* Remove verb type, add specific verb events

* rename verb messages -> events

* Context menu displays verbs by interaction type

* Updated context menu HandleMove

previously, checked if entities moved 1 tile from click location.

Now checks if entities moved out of view.

Now you can actually right-click interact with yourself while walking!

* Misc Verb menu GUI changes

* Fix non-human/ghost verbs

* Update types and categories

* Allow non-ghost/human to open context menu

* configuration verb

* tagger verb

* Morgue Verbs

* Medical Scanner Verbs

* Fix solution refactor merge issues

* Fix context menu in-view check

* Remove prepare GUI

* Redo verb restrictions

* Fix context menu UI

* Disposal Verbs

* Spill verb

* Light verb

* Hand Held light verb

* power cell verbs

* storage verbs

and adding names to insert/eject

* Pulling verb

* Close context menu on verb execution

* Strip verb

* AmmoBox verb

* fix pull verb

* gun barrel verbs

revolver verb
energy weapon verbs
Bolt action verb

* Magazine gun barrel  verbs

* Add charger verbs

* PDA verbs

* Transfer amount verb

* Add reagent verb

* make alt-click use ECS verbs

* Delete old verb files

* Magboot verb

* finalising tweaks

* context menu visibility changes

* code cleanup

* Update AdminAddReagentUI.cs

* Remove HasFlag

* Consistent verb keys

* Remove Linq, add comment

* Fix in-inventory check

* Update GUI text alignment and padding

* Added close-menu option

* Changed some "interaction" verbs to "activation"

* Remove verb keys, use sorted sets

* fix master merge

* update some verb text

* Undo Changes

Remove some new verbs that can be added later

undid some .ftl bugfixes, can and should be done separately

* fix merge

* Undo file rename

* fix merge

* Misc Cleanup

* remove contraction

* Fix keybinding issue

* fix comment

* merge fix

* fix merge

* fix merge

* fix merge

* fix merge

* fix open-close verbs

* adjust uncuff verb

* fix merge

and undo the renaming of SharedPullableComponent to PullableComponent. I'm tired of all of those merge conflicts
This commit is contained in:
Leon Friedrich
2021-10-05 14:29:03 +11:00
committed by GitHub
parent 1095c8fc08
commit 6cb58e608b
175 changed files with 3391 additions and 4305 deletions

View File

@@ -1,535 +1,228 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Linq;
using Content.Client.ContextMenu.UI;
using Content.Client.Resources;
using Content.Shared.GameTicking;
using Content.Shared.Input;
using Content.Shared.Verbs;
using JetBrains.Annotations;
using Robust.Client.Graphics;
using Robust.Client.Player;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.Utility;
using Robust.Shared.GameObjects;
using Robust.Shared.Input;
using Robust.Shared.Input.Binding;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Map;
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
{
[UsedImplicitly]
public sealed class VerbSystem : SharedVerbSystem
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
public event EventHandler<PointerInputCmdHandler.PointerInputCmdArgs>? ToggleContextMenu;
public event EventHandler<bool>? ToggleContainerVisibility;
public ContextMenuPresenter ContextMenuPresenter = default!;
private ContextMenuPresenter _contextMenuPresenter = default!;
private VerbPopup? _currentVerbListRoot;
private VerbPopup? _currentGroupList;
private EntityUid _currentEntity;
public EntityUid CurrentTarget;
public ContextMenuPopup? CurrentVerbPopup;
public ContextMenuPopup? CurrentCategoryPopup;
public Dictionary<VerbType, SortedSet<Verb>> CurrentVerbs = new();
// TODO: Move presenter out of the system
// TODO: Separate the rest of the UI from the logic
/// <summary>
/// Whether to show all entities 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;
// 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<VerbSystemMessages.VerbsResponseMessage>(FillEntityPopup);
SubscribeNetworkEvent<PlayerContainerVisibilityMessage>(HandleContainerVisibilityMessage);
SubscribeNetworkEvent<VerbsResponseEvent>(HandleVerbResponse);
SubscribeNetworkEvent<SetSeeAllContextEvent>(SetSeeAllContext);
_contextMenuPresenter = new ContextMenuPresenter(this);
SubscribeLocalEvent<MoveEvent>(_contextMenuPresenter.HandleMoveEvent);
ContextMenuPresenter = new ContextMenuPresenter(this);
}
CommandBinds.Builder
.Bind(ContentKeyFunctions.OpenContextMenu,
new PointerInputCmdHandler(HandleOpenContextMenu))
.Register<VerbSystem>();
private void Reset(RoundRestartCleanupEvent ev)
{
ContextMenuPresenter.CloseAllMenus();
}
public override void Shutdown()
{
base.Shutdown();
_contextMenuPresenter?.Dispose();
CommandBinds.Unregister<VerbSystem>();
}
public void Reset(RoundRestartCleanupEvent ev)
{
ToggleContainerVisibility?.Invoke(this, false);
}
private bool HandleOpenContextMenu(in PointerInputCmdHandler.PointerInputCmdArgs args)
{
if (args.State == BoundKeyState.Down)
{
ToggleContextMenu?.Invoke(this, args);
}
return true;
}
private void HandleContainerVisibilityMessage(PlayerContainerVisibilityMessage ev)
{
ToggleContainerVisibility?.Invoke(this, ev.CanSeeThrough);
ContextMenuPresenter?.Dispose();
}
public override void Update(float frameTime)
{
base.Update(frameTime);
_contextMenuPresenter?.Update();
ContextMenuPresenter?.Update();
}
public void OpenContextMenu(IEntity entity, ScreenCoordinates screenCoordinates)
private void SetSeeAllContext(SetSeeAllContextEvent args)
{
if (_currentVerbListRoot != null)
CanSeeAllContext = args.CanSeeAllContext;
}
/// <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.
/// </summary>
public void TryExecuteVerb(Verb verb, EntityUid target, VerbType verbType)
{
if (!TryExecuteVerb(verb))
RaiseNetworkEvent(new TryExecuteVerbEvent(target, verb, verbType));
}
public void OpenVerbMenu(IEntity target, ScreenCoordinates screenCoordinates)
{
if (CurrentVerbPopup != null)
{
CloseVerbMenu();
}
_currentEntity = entity.Uid;
_currentVerbListRoot = new VerbPopup();
_userInterfaceManager.ModalRoot.AddChild(_currentVerbListRoot);
_currentVerbListRoot.OnPopupHide += CloseVerbMenu;
var user = _playerManager.LocalPlayer?.ControlledEntity;
if (user == null)
return;
if (!entity.Uid.IsClientSide())
CurrentTarget = target.Uid;
CurrentVerbPopup = new ContextMenuPopup();
_userInterfaceManager.ModalRoot.AddChild(CurrentVerbPopup);
CurrentVerbPopup.OnPopupHide += CloseVerbMenu;
CurrentVerbs = GetVerbs(target, user, VerbType.All);
if (!target.Uid.IsClientSide())
{
_currentVerbListRoot.List.AddChild(new Label { Text = Loc.GetString("verb-system-waiting-on-server-text") });
RaiseNetworkEvent(new VerbSystemMessages.RequestVerbsMessage(_currentEntity));
CurrentVerbPopup.AddToMenu(new Label { Text = Loc.GetString("verb-system-waiting-on-server-text") });
RaiseNetworkEvent(new RequestServerVerbsEvent(CurrentTarget, VerbType.All));
}
// Show the menu
FillVerbPopup(CurrentVerbPopup);
var box = UIBox2.FromDimensions(screenCoordinates.Position, (1, 1));
_currentVerbListRoot.Open(box);
CurrentVerbPopup.Open(box);
}
public void OnContextButtonPressed(IEntity entity)
{
OpenContextMenu(entity, _userInterfaceManager.MousePositionScaled);
OpenVerbMenu(entity, _userInterfaceManager.MousePositionScaled);
}
private void FillEntityPopup(VerbSystemMessages.VerbsResponseMessage msg)
private void HandleVerbResponse(VerbsResponseEvent msg)
{
if (_currentEntity != msg.Entity || !EntityManager.TryGetEntity(_currentEntity, out var entity))
if (CurrentTarget != msg.Entity || CurrentVerbPopup == null)
{
return;
}
DebugTools.AssertNotNull(_currentVerbListRoot);
var buttons = new Dictionary<string, List<ListedVerbData>>();
var groupIcons = new Dictionary<string, SpriteSpecifier>();
var vBox = _currentVerbListRoot!.List;
vBox.DisposeAllChildren();
// Local variable so that scope capture ensures this is the correct value.
var curEntity = _currentEntity;
foreach (var data in msg.Verbs)
// This **should** not happen.
if (msg.Verbs == null)
{
var list = buttons.GetOrNew(data.Category);
if (data.CategoryIcon != null && !groupIcons.ContainsKey(data.Category))
{
groupIcons.Add(data.Category, data.CategoryIcon);
}
list.Add(new ListedVerbData(data.Text, !data.Available, data.Key, entity.ToString()!, () =>
{
RaiseNetworkEvent(new VerbSystemMessages.UseVerbMessage(curEntity, data.Key));
CloseAllMenus();
}, data.Icon));
// update "waiting for server...".
CurrentVerbPopup.List.DisposeAllChildren();
CurrentVerbPopup.AddToMenu(new Label { Text = Loc.GetString("verb-system-null-server-response") });
FillVerbPopup(CurrentVerbPopup);
return;
}
var user = GetUserEntity();
//Get verbs, component dependent.
foreach (var (component, verb) in VerbUtility.GetVerbs(entity))
// Add the new server-side verbs.
foreach (var (verbType, verbSet) in msg.Verbs)
{
if (!VerbUtility.VerbAccessChecks(user, entity, verb))
SortedSet<Verb> sortedVerbs = new (verbSet);
if (!CurrentVerbs.TryAdd(verbType, sortedVerbs))
{
continue;
CurrentVerbs[verbType].UnionWith(sortedVerbs);
}
var verbData = verb.GetData(user, component);
if (verbData.IsInvisible)
continue;
var list = buttons.GetOrNew(verbData.Category);
if (verbData.CategoryIcon != null && !groupIcons.ContainsKey(verbData.Category))
{
groupIcons.Add(verbData.Category, verbData.CategoryIcon);
}
list.Add(new ListedVerbData(verbData.Text, verbData.IsDisabled, verb.ToString()!, entity.ToString()!,
() => verb.Activate(user, component), verbData.Icon));
}
//Get global verbs. Visible for all entities regardless of their components.
foreach (var globalVerb in VerbUtility.GetGlobalVerbs(Assembly.GetExecutingAssembly()))
// Clear currently shown verbs and show new ones
CurrentVerbPopup.List.DisposeAllChildren();
FillVerbPopup(CurrentVerbPopup);
}
private void FillVerbPopup(ContextMenuPopup popup)
{
if (CurrentTarget == EntityUid.Invalid)
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 (!VerbUtility.VerbAccessChecks(user, entity, globalVerb))
{
continue;
}
var verbData = globalVerb.GetData(user, entity);
if (verbData.IsInvisible)
continue;
var list = buttons.GetOrNew(verbData.Category);
if (verbData.CategoryIcon != null && !groupIcons.ContainsKey(verbData.Category))
{
groupIcons.Add(verbData.Category, verbData.CategoryIcon);
}
list.Add(new ListedVerbData(verbData.Text, verbData.IsDisabled, globalVerb.ToString()!,
entity.ToString()!,
() => globalVerb.Activate(user, entity), verbData.Icon));
AddVerbSet(popup, type);
}
if (buttons.Count > 0)
{
var first = true;
foreach (var (category, verbs) in buttons)
{
if (string.IsNullOrEmpty(category))
continue;
if (!first)
{
vBox.AddChild(new PanelContainer
{
MinSize = (0, 2),
PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex("#333") }
});
}
first = false;
groupIcons.TryGetValue(category, out var icon);
vBox.AddChild(CreateCategoryButton(category, verbs, icon));
}
if (buttons.ContainsKey(""))
{
buttons[""].Sort((a, b) => string.Compare(a.Text, b.Text, StringComparison.CurrentCulture));
foreach (var verb in buttons[""])
{
if (!first)
{
vBox.AddChild(new PanelContainer
{
MinSize = (0, 2),
PanelOverride = new StyleBoxFlat { BackgroundColor = Color.FromHex("#333") }
});
}
first = false;
vBox.AddChild(CreateVerbButton(verb));
}
}
}
else
// 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") });
vBox.AddChild(panel);
popup.AddChild(panel);
}
popup.InvalidateMeasure();
}
private VerbButton CreateVerbButton(ListedVerbData data)
/// <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)
{
var button = new VerbButton
{
Text = Loc.GetString(data.Text),
Disabled = data.Disabled
};
if (!CurrentVerbs.TryGetValue(type, out var verbSet) || verbSet.Count == 0)
return;
if (data.Icon != null)
{
button.Icon = data.Icon.Frame0();
}
HashSet<string> listedCategories = new();
if (!data.Disabled)
foreach (var verb in verbSet)
{
button.OnPressed += _ =>
if (verb.Category == null)
{
CloseAllMenus();
try
{
data.Action.Invoke();
}
catch (Exception e)
{
Logger.ErrorS("verb", "Exception in verb {0} on {1}:\n{2}", data.VerbName, data.OwnerName, e);
}
};
// 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;
}
return button;
}
private Control CreateCategoryButton(string text, List<ListedVerbData> verbButtons, SpriteSpecifier? icon)
{
verbButtons.Sort((a, b) => string.Compare(a.Text, b.Text, StringComparison.CurrentCulture));
return new VerbGroupButton(this, verbButtons, icon)
{
Text = Loc.GetString(text),
};
}
public void CloseVerbMenu()
{
_currentVerbListRoot?.Dispose();
_currentVerbListRoot = null;
_currentEntity = EntityUid.Invalid;
}
private void CloseAllMenus()
{
CloseVerbMenu();
// CloseContextPopups();
CloseGroupMenu();
}
public void CloseGroupMenu()
{
_currentGroupList?.Dispose();
_currentGroupList = null;
}
private IEntity GetUserEntity()
{
return _playerManager.LocalPlayer!.ControlledEntity!;
}
private sealed class VerbPopup : Popup
{
public BoxContainer List { get; }
public VerbPopup()
if (CurrentVerbPopup != null)
{
AddChild(new PanelContainer
{
Children = {(List = new BoxContainer
{
Orientation = LayoutOrientation.Vertical
})},
PanelOverride = new StyleBoxFlat {BackgroundColor = Color.FromHex("#111E")}
});
}
}
private sealed class VerbButton : BaseButton
{
private readonly Label _label;
private readonly TextureRect _icon;
public Texture? Icon
{
get => _icon.Texture;
set => _icon.Texture = value;
CurrentVerbPopup.OnPopupHide -= CloseVerbMenu;
CurrentVerbPopup.Dispose();
CurrentVerbPopup = null;
}
public string? Text
{
get => _label.Text;
set => _label.Text = value;
}
public VerbButton()
{
AddChild(new BoxContainer
{
Orientation = LayoutOrientation.Horizontal,
Children =
{
(_icon = new TextureRect
{
MinSize = (32, 32),
Stretch = TextureRect.StretchMode.KeepCentered,
TextureScale = (0.5f, 0.5f)
}),
(_label = new Label()),
// Padding
new Control {MinSize = (8, 0)}
}
});
}
protected override void Draw(DrawingHandleScreen handle)
{
base.Draw(handle);
if (DrawMode == DrawModeEnum.Hover)
{
handle.DrawRect(PixelSizeBox, Color.DarkSlateGray);
}
}
}
private sealed class VerbGroupButton : Control
{
private static readonly TimeSpan HoverDelay = TimeSpan.FromSeconds(0.2);
private readonly VerbSystem _system;
private readonly Label _label;
private readonly TextureRect _icon;
private CancellationTokenSource? _openCancel;
public List<ListedVerbData> VerbButtons { get; }
public string? Text
{
get => _label.Text;
set => _label.Text = value;
}
public Texture? Icon
{
get => _icon.Texture;
set => _icon.Texture = value;
}
public VerbGroupButton(VerbSystem system, List<ListedVerbData> verbButtons, SpriteSpecifier? icon)
{
_system = system;
VerbButtons = verbButtons;
MouseFilter = MouseFilterMode.Stop;
AddChild(new BoxContainer
{
Orientation = LayoutOrientation.Horizontal,
Children =
{
(_icon = new TextureRect
{
MinSize = (32, 32),
TextureScale = (0.5f, 0.5f),
Stretch = TextureRect.StretchMode.KeepCentered
}),
(_label = new Label { HorizontalExpand = true }),
// Padding
new Control {MinSize = (8, 0)},
new TextureRect
{
Texture = IoCManager.Resolve<IResourceCache>()
.GetTexture("/Textures/Interface/VerbIcons/group.svg.192dpi.png"),
TextureScale = (0.5f, 0.5f),
Stretch = TextureRect.StretchMode.KeepCentered,
}
}
});
if (icon != null)
{
_icon.Texture = icon.Frame0();
}
}
protected override void Draw(DrawingHandleScreen handle)
{
base.Draw(handle);
if (this == UserInterfaceManager.CurrentlyHovered)
{
handle.DrawRect(PixelSizeBox, Color.DarkSlateGray);
}
}
protected override void MouseEntered()
{
base.MouseEntered();
_openCancel = new CancellationTokenSource();
Timer.Spawn(HoverDelay, () =>
{
if (_system._currentGroupList != null)
{
_system.CloseGroupMenu();
}
var popup = _system._currentGroupList = new VerbPopup();
var first = true;
foreach (var verb in VerbButtons)
{
if (!first)
{
popup.List.AddChild(new PanelContainer
{
MinSize = (0, 2),
PanelOverride = new StyleBoxFlat {BackgroundColor = Color.FromHex("#333")}
});
}
first = false;
popup.List.AddChild(_system.CreateVerbButton(verb));
}
UserInterfaceManager.ModalRoot.AddChild(popup);
popup.Open(UIBox2.FromDimensions(GlobalPosition + (Width, 0), (1, 1)), GlobalPosition);
}, _openCancel.Token);
}
protected override void MouseExited()
{
base.MouseExited();
_openCancel?.Cancel();
_openCancel = null;
}
}
private sealed class ListedVerbData
{
public string Text { get; }
public bool Disabled { get; }
public string VerbName { get; }
public string OwnerName { get; }
public SpriteSpecifier? Icon { get; }
public Action Action { get; }
public ListedVerbData(string text, bool disabled, string verbName, string ownerName,
Action action, SpriteSpecifier? icon)
{
Text = text;
Disabled = disabled;
VerbName = verbName;
OwnerName = ownerName;
Action = action;
Icon = icon;
}
CurrentCategoryPopup?.Dispose();
CurrentCategoryPopup = null;
CurrentTarget = EntityUid.Invalid;
CurrentVerbs.Clear();
}
}
}