using System; using System.Collections.Generic; using System.Reflection; using JetBrains.Annotations; using Robust.Shared.Interfaces.GameObjects; namespace Content.Shared.GameObjects { /// /// A verb is an action in the right click menu of an entity. /// /// /// To add a verb to an entity, define it as a nested class inside the owning component, /// and mark it with /// [UsedImplicitly] public abstract class Verb { /// /// If true, this verb requires the user to be inside within /// meters from the entity on which this verb resides. /// public virtual bool RequireInteractionRange => true; public const float InteractionRange = 2; public const float InteractionRangeSquared = InteractionRange * InteractionRange; /// /// Gets the text string that will be shown to in the right click menu. /// /// The entity of the user opening this menu. /// The component instance for which this verb is being loaded. /// The text string that is shown in the right click menu for this verb. public abstract string GetText(IEntity user, IComponent component); /// /// Gets the visibility level of this verb in the right click menu. /// /// The entity of the user opening this menu. /// The component instance for which this verb is being loaded. /// The visibility level of the verb in the client's right click menu. public abstract VerbVisibility GetVisibility(IEntity user, IComponent component); /// /// Invoked when this verb is activated from the right click menu. /// /// The entity of the user opening this menu. /// The component instance for which this verb is being loaded. public abstract void Activate(IEntity user, IComponent component); } /// /// /// Sub class of that works on a specific type of component, /// to reduce casting boiler plate for implementations. /// /// The type of component that this verb will run on. public abstract class Verb : Verb where T : IComponent { /// /// Gets the text string that will be shown to in the right click menu. /// /// The entity of the user opening this menu. /// The component instance for which this verb is being loaded. /// The text string that is shown in the right click menu for this verb. protected abstract string GetText(IEntity user, T component); /// /// Gets the visibility level of this verb in the right click menu. /// /// The entity of the user opening this menu. /// The component instance for which this verb is being loaded. /// The visibility level of the verb in the client's right click menu. protected abstract VerbVisibility GetVisibility(IEntity user, T component); /// /// Invoked when this verb is activated from the right click menu. /// /// The entity of the user opening this menu. /// The component instance for which this verb is being loaded. protected abstract void Activate(IEntity user, T component); /// public sealed override string GetText(IEntity user, IComponent component) { return GetText(user, (T) component); } /// public sealed override VerbVisibility GetVisibility(IEntity user, IComponent component) { return GetVisibility(user, (T) component); } /// public sealed override void Activate(IEntity user, IComponent component) { Activate(user, (T) component); } } /// /// This attribute should be used on implementations nested inside component classes, /// so that they're automatically detected. /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] public sealed class VerbAttribute : Attribute { } public static class VerbUtility { // TODO: This is a quick hack. Verb objects should absolutely be cached properly. // This works for now though. public static IEnumerable<(IComponent, Verb)> GetVerbs(IEntity entity) { foreach (var component in entity.GetAllComponents()) { var type = component.GetType(); foreach (var nestedType in type.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)) { if (!typeof(Verb).IsAssignableFrom(nestedType) || nestedType.IsAbstract) { continue; } var verb = (Verb) Activator.CreateInstance(nestedType); yield return (component, verb); } } } } /// /// Possible states of visibility for the verb in the right click menu. /// public enum VerbVisibility { /// /// The verb will be listed in the right click menu. /// Visible, /// /// The verb will be listed, but it will be grayed out and unable to be clicked on. /// Disabled, /// /// The verb will not be listed in the right click menu. /// Invisible } }