Replace VerbTypes with verb classes (#6525)

This commit is contained in:
Leon Friedrich
2022-02-10 15:30:59 +13:00
committed by GitHub
parent 0cd2c2fa9d
commit 1c9ffdc78c
60 changed files with 409 additions and 405 deletions

View File

@@ -39,8 +39,8 @@ namespace Content.Shared.Containers.ItemSlots
SubscribeLocalEvent<ItemSlotsComponent, InteractHandEvent>(OnInteractHand);
SubscribeLocalEvent<ItemSlotsComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<ItemSlotsComponent, GetAlternativeVerbsEvent>(AddEjectVerbs);
SubscribeLocalEvent<ItemSlotsComponent, GetInteractionVerbsEvent>(AddInteractionVerbsVerbs);
SubscribeLocalEvent<ItemSlotsComponent, GetVerbsEvent<AlternativeVerb>>(AddEjectVerbs);
SubscribeLocalEvent<ItemSlotsComponent, GetVerbsEvent<InteractionVerb>>(AddInteractionVerbsVerbs);
SubscribeLocalEvent<ItemSlotsComponent, BreakageEventArgs>(OnBreak);
SubscribeLocalEvent<ItemSlotsComponent, DestructionEventArgs>(OnBreak);
@@ -380,7 +380,7 @@ namespace Content.Shared.Containers.ItemSlots
#endregion
#region Verbs
private void AddEjectVerbs(EntityUid uid, ItemSlotsComponent itemSlots, GetAlternativeVerbsEvent args)
private void AddEjectVerbs(EntityUid uid, ItemSlotsComponent itemSlots, GetVerbsEvent<AlternativeVerb> args)
{
if (args.Hands == null || !args.CanAccess ||!args.CanInteract ||
!_actionBlockerSystem.CanPickup(args.User))
@@ -402,7 +402,7 @@ namespace Content.Shared.Containers.ItemSlots
? Loc.GetString(slot.Name)
: EntityManager.GetComponent<MetaDataComponent>(slot.Item!.Value).EntityName ?? string.Empty;
Verb verb = new();
AlternativeVerb verb = new();
verb.Act = () => TryEjectToHands(uid, slot, args.User, excludeUserAudio: true);
if (slot.EjectVerbText == null)
@@ -420,7 +420,7 @@ namespace Content.Shared.Containers.ItemSlots
}
}
private void AddInteractionVerbsVerbs(EntityUid uid, ItemSlotsComponent itemSlots, GetInteractionVerbsEvent args)
private void AddInteractionVerbsVerbs(EntityUid uid, ItemSlotsComponent itemSlots, GetVerbsEvent<InteractionVerb> args)
{
if (args.Hands == null || !args.CanAccess || !args.CanInteract)
return;
@@ -437,7 +437,7 @@ namespace Content.Shared.Containers.ItemSlots
? Loc.GetString(slot.Name)
: EntityManager.GetComponent<MetaDataComponent>(slot.Item!.Value).EntityName ?? string.Empty;
Verb takeVerb = new();
InteractionVerb takeVerb = new();
takeVerb.Act = () => TryEjectToHands(uid, slot, args.User, excludeUserAudio: true);
takeVerb.IconTexture = "/Textures/Interface/VerbIcons/pickup.svg.192dpi.png";
@@ -463,7 +463,7 @@ namespace Content.Shared.Containers.ItemSlots
? Loc.GetString(slot.Name)
: Name(args.Using.Value) ?? string.Empty;
Verb insertVerb = new();
InteractionVerb insertVerb = new();
insertVerb.Act = () => Insert(uid, slot, args.Using.Value, args.User, excludeUserAudio: true);
if (slot.InsertVerbText != null)

View File

@@ -15,12 +15,12 @@ public class FollowerSystem : EntitySystem
{
base.Initialize();
SubscribeLocalEvent<GetAlternativeVerbsEvent>(OnGetAlternativeVerbs);
SubscribeLocalEvent<GetVerbsEvent<AlternativeVerb>>(OnGetAlternativeVerbs);
SubscribeLocalEvent<FollowerComponent, RelayMoveInputEvent>(OnFollowerMove);
SubscribeLocalEvent<FollowedComponent, EntityTerminatingEvent>(OnFollowedTerminating);
}
private void OnGetAlternativeVerbs(GetAlternativeVerbsEvent ev)
private void OnGetAlternativeVerbs(GetVerbsEvent<AlternativeVerb> ev)
{
if (!HasComp<SharedGhostComponent>(ev.User))
return;
@@ -28,7 +28,7 @@ public class FollowerSystem : EntitySystem
if (ev.User == ev.Target)
return;
var verb = new Verb
var verb = new AlternativeVerb
{
Priority = 10,
Act = (() =>

View File

@@ -743,7 +743,7 @@ namespace Content.Shared.Interaction
public bool AltInteract(EntityUid user, EntityUid target)
{
// Get list of alt-interact verbs
var verbs = _verbSystem.GetLocalVerbs(target, user, VerbType.Alternative)[VerbType.Alternative];
var verbs = _verbSystem.GetLocalVerbs(target, user, typeof(AlternativeVerb));
if (!verbs.Any())
return false;

View File

@@ -12,7 +12,7 @@ namespace Content.Shared.Item
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SharedItemComponent, GetInteractionVerbsEvent>(AddPickupVerb);
SubscribeLocalEvent<SharedItemComponent, GetVerbsEvent<InteractionVerb>>(AddPickupVerb);
SubscribeLocalEvent<SharedSpriteComponent, GotEquippedEvent>(OnEquipped);
SubscribeLocalEvent<SharedSpriteComponent, GotUnequippedEvent>(OnUnequipped);
@@ -52,7 +52,7 @@ namespace Content.Shared.Item
component.Visible = false;
}
private void AddPickupVerb(EntityUid uid, SharedItemComponent component, GetInteractionVerbsEvent args)
private void AddPickupVerb(EntityUid uid, SharedItemComponent component, GetVerbsEvent<InteractionVerb> args)
{
if (args.Hands == null ||
args.Using != null ||
@@ -61,7 +61,7 @@ namespace Content.Shared.Item
!args.Hands.CanPickupEntityToActiveHand(args.Target))
return;
Verb verb = new();
InteractionVerb verb = new();
verb.Act = () => args.Hands.TryPickupEntityToActiveHand(args.Target);
verb.IconTexture = "/Textures/Interface/VerbIcons/pickup.svg.192dpi.png";

View File

@@ -68,14 +68,14 @@ namespace Content.Shared.Pulling
SubscribeLocalEvent<SharedPullableComponent, PullStartedMessage>(PullableHandlePullStarted);
SubscribeLocalEvent<SharedPullableComponent, PullStoppedMessage>(PullableHandlePullStopped);
SubscribeLocalEvent<SharedPullableComponent, GetOtherVerbsEvent>(AddPullVerbs);
SubscribeLocalEvent<SharedPullableComponent, GetVerbsEvent<Verb>>(AddPullVerbs);
CommandBinds.Builder
.Bind(ContentKeyFunctions.MovePulledObject, new PointerInputCmdHandler(HandleMovePulledObject))
.Register<SharedPullingSystem>();
}
private void AddPullVerbs(EntityUid uid, SharedPullableComponent component, GetOtherVerbsEvent args)
private void AddPullVerbs(EntityUid uid, SharedPullableComponent component, GetVerbsEvent<Verb> args)
{
if (args.Hands == null || !args.CanAccess || !args.CanInteract)
return;

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Content.Shared.ActionBlocker;
using Content.Shared.Hands.Components;
@@ -5,6 +6,7 @@ using Content.Shared.Interaction;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Utility;
namespace Content.Shared.Verbs
{
@@ -29,7 +31,7 @@ namespace Content.Shared.Verbs
// Get the list of verbs. This effectively also checks that the requested verb is in fact a valid verb that
// the user can perform.
var verbs = GetLocalVerbs(args.Target, user.Value, args.Type)[args.Type];
var verbs = GetLocalVerbs(args.Target, user.Value, args.RequestedVerb.GetType());
// Note that GetLocalVerbs might waste time checking & preparing unrelated verbs even though we know
// precisely which one we want to run. However, MOST entities will only have 1 or 2 verbs of a given type.
@@ -44,9 +46,18 @@ namespace Content.Shared.Verbs
/// Raises a number of events in order to get all verbs of the given type(s) defined in local systems. This
/// does not request verbs from the server.
/// </summary>
public virtual Dictionary<VerbType, SortedSet<Verb>> GetLocalVerbs(EntityUid target, EntityUid user, VerbType verbTypes, bool force = false)
public SortedSet<Verb> GetLocalVerbs(EntityUid target, EntityUid user, Type type, bool force = false, bool all = false)
{
Dictionary<VerbType, SortedSet<Verb>> verbs = new();
return GetLocalVerbs(target, user, new List<Type>() { type }, force, all);
}
/// <summary>
/// Raises a number of events in order to get all verbs of the given type(s) defined in local systems. This
/// does not request verbs from the server.
/// </summary>
public SortedSet<Verb> GetLocalVerbs(EntityUid target, EntityUid user, List<Type> types, bool force = false, bool all = false)
{
SortedSet<Verb> verbs = new();
// accessibility checks
bool canAccess = false;
@@ -80,32 +91,33 @@ namespace Content.Shared.Verbs
}
}
if ((verbTypes & VerbType.Interaction) == VerbType.Interaction)
if (types.Contains(typeof(InteractionVerb)))
{
GetInteractionVerbsEvent getVerbEvent = new(user, target, @using, hands, canInteract, canAccess);
RaiseLocalEvent(target, getVerbEvent);
verbs.Add(VerbType.Interaction, getVerbEvent.Verbs);
var verbEvent = new GetVerbsEvent<InteractionVerb>(user, target, @using, hands, canInteract, canAccess);
RaiseLocalEvent(target, verbEvent);
verbs.UnionWith(verbEvent.Verbs);
}
if ((verbTypes & VerbType.Activation) == VerbType.Activation)
if (types.Contains(typeof(AlternativeVerb)))
{
GetActivationVerbsEvent getVerbEvent = new(user, target, @using, hands, canInteract, canAccess);
RaiseLocalEvent(target, getVerbEvent);
verbs.Add(VerbType.Activation, getVerbEvent.Verbs);
var verbEvent = new GetVerbsEvent<AlternativeVerb>(user, target, @using, hands, canInteract, canAccess);
RaiseLocalEvent(target, verbEvent);
verbs.UnionWith(verbEvent.Verbs);
}
if ((verbTypes & VerbType.Alternative) == VerbType.Alternative)
if (types.Contains(typeof(ActivationVerb)))
{
GetAlternativeVerbsEvent getVerbEvent = new(user, target, @using, hands, canInteract, canAccess);
RaiseLocalEvent(target, getVerbEvent);
verbs.Add(VerbType.Alternative, getVerbEvent.Verbs);
var verbEvent = new GetVerbsEvent<ActivationVerb>(user, target, @using, hands, canInteract, canAccess);
RaiseLocalEvent(target, verbEvent);
verbs.UnionWith(verbEvent.Verbs);
}
if ((verbTypes & VerbType.Other) == VerbType.Other)
// generic verbs
if (types.Contains(typeof(Verb)))
{
GetOtherVerbsEvent getVerbEvent = new(user, target, @using, hands, canInteract, canAccess);
RaiseLocalEvent(target, getVerbEvent);
verbs.Add(VerbType.Other, getVerbEvent.Verbs);
var verbEvent = new GetVerbsEvent<Verb>(user, target, @using, hands, canInteract, canAccess);
RaiseLocalEvent(target, verbEvent);
verbs.UnionWith(verbEvent.Verbs);
}
return verbs;

View File

@@ -1,30 +1,33 @@
using Content.Shared.Administration.Logs;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using System;
using Content.Shared.Database;
using System.Collections.Generic;
namespace Content.Shared.Verbs
{
[Flags]
public enum VerbType
{
Interaction = 1,
Activation = 2,
Alternative = 4,
Other = 8,
All = 1+2+4+8
}
/// <summary>
/// Verb objects describe actions that a user can take. The actions can be specified via an Action, local
/// events, or networked events. Verbs also provide text, icons, and categories for displaying in the
/// context-menu.
/// </summary>
[Serializable, NetSerializable]
public sealed class Verb : IComparable
public class Verb : IComparable
{
public static string DefaultTextStyleClass = "Verb";
/// <summary>
/// Determines the priority of this type of verb when displaying in the verb-menu. See <see
/// cref="CompareTo"/>.
/// </summary>
public virtual int TypePriority => 0;
/// <summary>
/// Style class for drawing in the context menu
/// </summary>
public string TextStyleClass = DefaultTextStyleClass;
/// <summary>
/// This is an action that will be run when the verb is "acted" out.
/// </summary>
@@ -161,7 +164,11 @@ namespace Content.Shared.Verbs
if (obj is not Verb otherVerb)
return -1;
// Sort first by priority
// Sort first by type-priority
if (TypePriority != otherVerb.TypePriority)
return otherVerb.TypePriority - TypePriority;
// Then by verb-priority
if (Priority != otherVerb.Priority)
return otherVerb.Priority - Priority;
@@ -180,5 +187,81 @@ namespace Content.Shared.Verbs
// Finally, compare icon texture paths. Note that this matters for verbs that don't have any text (e.g., the rotate-verbs)
return string.Compare(IconTexture, otherVerb.IconTexture, StringComparison.CurrentCulture);
}
/// <summary>
/// Collection of all verb types, along with string keys.
/// </summary>
/// <remarks>
/// Useful when iterating over verb types, though maybe this should be obtained and stored via reflection or
/// something (list of all classes that inherit from Verb). Currently used for networking (apparently Type
/// is not serializable?), and resolving console commands.
/// </remarks>
public static List<Type> VerbTypes = new()
{
{ typeof(Verb) },
{ typeof(InteractionVerb) },
{ typeof(AlternativeVerb) },
{ typeof(ActivationVerb) },
};
}
/// <summary>
/// Primary interaction verbs. This includes both use-in-hand and interacting with external entities.
/// </summary>
/// <remarks>
/// These verbs those that involve using the hands or the currently held item on some entity. These verbs usually
/// correspond to interactions that can be triggered by left-clicking or using 'Z', and often depend on the
/// currently held item. These verbs are collectively shown first in the context menu.
/// </remarks>
[Serializable, NetSerializable]
public sealed class InteractionVerb : Verb
{
public new static string DefaultTextStyleClass = "InteractionVerb";
public override int TypePriority => 4;
public InteractionVerb() : base()
{
TextStyleClass = DefaultTextStyleClass;
}
}
/// <summary>
/// Verbs for alternative-interactions.
/// </summary>
/// <remarks>
/// When interacting with an entity via alt + left-click/E/Z the highest priority alt-interact verb is executed.
/// These verbs are collectively shown second-to-last in the context menu.
/// </remarks>
[Serializable, NetSerializable]
public sealed class AlternativeVerb : Verb
{
public override int TypePriority => 2;
public new static string DefaultTextStyleClass = "AlternativeVerb";
public AlternativeVerb() : base()
{
TextStyleClass = DefaultTextStyleClass;
}
}
/// <summary>
/// Activation-type verbs.
/// </summary>
/// <remarks>
/// These are verbs that activate an item in the world but are independent of the currently held items. For
/// example, opening a door or a GUI. These verbs should correspond to interactions that can be triggered by
/// using 'E', though many of those can also be triggered by left-mouse or 'Z' if there is no other interaction.
/// These verbs are collectively shown second in the context menu.
/// </remarks>
[Serializable, NetSerializable]
public sealed class ActivationVerb : Verb
{
public override int TypePriority => 1;
public new static string DefaultTextStyleClass = "ActivationVerb";
public ActivationVerb() : base()
{
TextStyleClass = DefaultTextStyleClass;
}
}
}

View File

@@ -1,19 +1,18 @@
using System;
using System.Collections.Generic;
using Content.Shared.ActionBlocker;
using Content.Shared.Hands.Components;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Content.Shared.Interaction;
using Robust.Shared.Containers;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
namespace Content.Shared.Verbs
{
[Serializable, NetSerializable]
public class RequestServerVerbsEvent : EntityEventArgs
public sealed class RequestServerVerbsEvent : EntityEventArgs
{
public readonly EntityUid EntityUid;
public readonly VerbType Type;
public readonly List<string> VerbTypes = new();
/// <summary>
/// If the target item is inside of some storage (e.g., backpack), this is the entity that owns that item
@@ -23,121 +22,60 @@ namespace Content.Shared.Verbs
public readonly bool AdminRequest;
public RequestServerVerbsEvent(EntityUid entityUid, VerbType type, EntityUid? slotOwner = null, bool adminRequest = false)
public RequestServerVerbsEvent(EntityUid entityUid, List<Type> verbTypes, EntityUid? slotOwner = null, bool adminRequest = false)
{
EntityUid = entityUid;
Type = type;
SlotOwner = slotOwner;
AdminRequest = adminRequest;
foreach (var type in verbTypes)
{
DebugTools.Assert(typeof(Verb).IsAssignableFrom(type));
VerbTypes.Add(type.Name);
}
}
}
[Serializable, NetSerializable]
public class VerbsResponseEvent : EntityEventArgs
public sealed class VerbsResponseEvent : EntityEventArgs
{
public readonly Dictionary<VerbType, List<Verb>>? Verbs;
public readonly List<Verb>? Verbs;
public readonly EntityUid Entity;
public VerbsResponseEvent(EntityUid entity, Dictionary<VerbType, SortedSet<Verb>>? verbs)
public VerbsResponseEvent(EntityUid entity, SortedSet<Verb>? verbs)
{
Entity = entity;
if (verbs == null)
return;
// Apparently SortedSet is not serlializable. Cast to List<Verb>.
Verbs = new();
foreach (var entry in verbs)
{
Verbs.Add(entry.Key, new List<Verb>(entry.Value));
}
// Apparently SortedSet is not serializable, so we cast to List<Verb>.
Verbs = new(verbs);
}
}
[Serializable, NetSerializable]
public class ExecuteVerbEvent : EntityEventArgs
public sealed class ExecuteVerbEvent : EntityEventArgs
{
public readonly EntityUid Target;
public readonly Verb RequestedVerb;
/// <summary>
/// The type of verb to try execute. Avoids having to get a list of all verbs on the receiving end.
/// </summary>
public readonly VerbType Type;
public ExecuteVerbEvent(EntityUid target, Verb requestedVerb, VerbType type)
public ExecuteVerbEvent(EntityUid target, Verb requestedVerb)
{
Target = target;
RequestedVerb = requestedVerb;
Type = type;
}
}
/// <summary>
/// Request primary interaction verbs. This includes both use-in-hand and interacting with external entities.
/// </summary>
/// <remarks>
/// These verbs those that involve using the hands or the currently held item on some entity. These verbs usually
/// correspond to interactions that can be triggered by left-clicking or using 'Z', and often depend on the
/// currently held item. These verbs are collectively shown first in the context menu.
/// </remarks>
public class GetInteractionVerbsEvent : GetVerbsEvent
{
public GetInteractionVerbsEvent(EntityUid user, EntityUid target, EntityUid? @using, SharedHandsComponent? hands,
bool canInteract, bool canAccess) : base(user, target, @using, hands, canInteract, canAccess) { }
}
/// <summary>
/// Request activation verbs.
/// </summary>
/// <remarks>
/// These are verbs that activate an item in the world but are independent of the currently held items. For
/// example, opening a door or a GUI. These verbs should correspond to interactions that can be triggered by
/// using 'E', though many of those can also be triggered by left-mouse or 'Z' if there is no other interaction.
/// These verbs are collectively shown second in the context menu.
/// </remarks>
public class GetActivationVerbsEvent : GetVerbsEvent
{
public GetActivationVerbsEvent(EntityUid user, EntityUid target, EntityUid? @using, SharedHandsComponent? hands,
bool canInteract, bool canAccess) : base(user, target, @using, hands, canInteract, canAccess) { }
}
/// <summary>
/// Request alternative-interaction verbs.
/// </summary>
/// <remarks>
/// When interacting with an entity via alt + left-click/E/Z the highest priority alt-interact verb is executed.
/// These verbs are collectively shown second-to-last in the context menu.
/// </remarks>
public class GetAlternativeVerbsEvent : GetVerbsEvent
{
public GetAlternativeVerbsEvent(EntityUid user, EntityUid target, EntityUid? @using, SharedHandsComponent? hands,
bool canInteract, bool canAccess) : base(user, target, @using, hands, canInteract, canAccess) { }
}
/// <summary>
/// Request Miscellaneous verbs.
/// </summary>
/// <remarks>
/// Includes (nearly) global interactions like "examine", "pull", or "debug". These verbs are collectively shown
/// last in the context menu.
/// </remarks>
public class GetOtherVerbsEvent : GetVerbsEvent
{
public GetOtherVerbsEvent(EntityUid user, EntityUid target, EntityUid? @using, SharedHandsComponent? hands,
bool canInteract, bool canAccess) : base(user, target, @using, hands, canInteract, canAccess) { }
}
/// <summary>
/// Directed event that requests verbs from any systems/components on a target entity.
/// </summary>
public class GetVerbsEvent : EntityEventArgs
public sealed class GetVerbsEvent<TVerb> : EntityEventArgs where TVerb : Verb
{
/// <summary>
/// Event output. Set of verbs that can be executed.
/// </summary>
public readonly SortedSet<Verb> Verbs = new();
public readonly SortedSet<TVerb> Verbs = new();
/// <summary>
/// Can the user physically access the target?
@@ -180,7 +118,7 @@ namespace Content.Shared.Verbs
/// The entity currently being held by the active hand.
/// </summary>
/// <remarks>
/// This is only ever not null when <see cref="ActionBlockerSystem.CanUse(Robust.Shared.GameObjects.EntityUid)"/> is true and the user
/// This is only ever not null when <see cref="ActionBlockerSystem.CanUse(EntityUid)"/> is true and the user
/// has hands.
/// </remarks>
public readonly EntityUid? Using;