Fancy Verb Menu & Verb API Refactor (#928)

This commit is contained in:
Pieter-Jan Briers
2020-05-23 03:09:44 +02:00
committed by GitHub
parent 4527fc9e84
commit cad59d2cb4
33 changed files with 1099 additions and 399 deletions

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
namespace Content.Shared.GameObjects.EntitySystemMessages
{
@@ -21,29 +22,33 @@ namespace Content.Shared.GameObjects.EntitySystemMessages
[Serializable, NetSerializable]
public class VerbsResponseMessage : EntitySystemMessage
{
public readonly List<VerbData> Verbs;
public readonly NetVerbData[] Verbs;
public readonly EntityUid Entity;
public VerbsResponseMessage(List<VerbData> verbs, EntityUid entity)
public VerbsResponseMessage(NetVerbData[] verbs, EntityUid entity)
{
Verbs = verbs;
Entity = entity;
}
[Serializable, NetSerializable]
public readonly struct VerbData
public readonly struct NetVerbData
{
public readonly string Text;
public readonly string Key;
public readonly string Category;
public readonly SpriteSpecifier Icon;
public readonly SpriteSpecifier CategoryIcon;
public readonly bool Available;
public VerbData(string text, string key, string category, bool available)
public NetVerbData(VerbData data, string key)
{
Text = text;
Text = data.Text;
Key = key;
Category = category;
Available = available;
Category = data.Category;
CategoryIcon = data.CategoryIcon;
Icon = data.Icon;
Available = data.Visibility == VerbVisibility.Visible;
}
}
}

View File

@@ -21,25 +21,16 @@ namespace Content.Shared.GameObjects
public virtual bool RequireInteractionRange => true;
/// <summary>
/// Gets the text string that will be shown to <paramref name="user"/> in the right click menu.
/// Gets the visible verb data for the user.
/// </summary>
/// <remarks>
/// Implementations should write into <paramref name="data"/> to return their data.
/// </remarks>
/// <param name="user">The entity of the user opening this menu.</param>
/// <param name="target">The entity this verb is being evaluated for.</param>
/// <param name="data">The data that must be filled in.</param>
/// <returns>The text string that is shown in the right click menu for this verb.</returns>
public abstract string GetText(IEntity user, IEntity target);
/// <summary>
/// Gets the category of this verb.
/// </summary>
/// <param name="user">The entity of the user opening this menu.</param>
/// <returns>The category of this verb.</returns>
public virtual string GetCategory(IEntity user, IEntity target) => "";
/// <summary>
/// Gets the visibility level of this verb in the right click menu.
/// </summary>
/// <param name="user">The entity of the user opening this menu.</param>
/// <returns>The visibility level of the verb in the client's right click menu.</returns>
public abstract VerbVisibility GetVisibility(IEntity user, IEntity target);
public abstract void GetData(IEntity user, IEntity target, VerbData data);
/// <summary>
/// Invoked when this verb is activated from the right click menu.
@@ -47,6 +38,13 @@ namespace Content.Shared.GameObjects
/// <param name="user">The entity of the user opening this menu.</param>
/// <param name="target">The entity that is being acted upon.</param>
public abstract void Activate(IEntity user, IEntity target);
public VerbData GetData(IEntity user, IEntity target)
{
var data = new VerbData();
GetData(user, target, data);
return data;
}
}
/// <summary>

View File

@@ -19,33 +19,20 @@ namespace Content.Shared.GameObjects
{
/// <summary>
/// If true, this verb requires the user to be inside within
/// <see cref="InteractionRange"/> meters from the entity on which this verb resides.
/// <see cref="VerbUtility.InteractionRange"/> meters from the entity on which this verb resides.
/// </summary>
public virtual bool RequireInteractionRange => true;
/// <summary>
/// Gets the text string that will be shown to <paramref name="user"/> in the right click menu.
/// Gets the visible verb data for the user.
/// </summary>
/// <remarks>
/// Implementations should write into <paramref name="data"/> to return their data.
/// </remarks>
/// <param name="user">The entity of the user opening this menu.</param>
/// <param name="component">The component instance for which this verb is being loaded.</param>
/// <returns>The text string that is shown in the right click menu for this verb.</returns>
public abstract string GetText(IEntity user, IComponent component);
/// <summary>
/// Gets the category of this verb.
/// </summary>
/// <param name="user">The entity of the user opening this menu.</param>
/// <param name="component">The component instance for which this verb is being loaded.</param>
/// <returns>The category of this verb.</returns>
public virtual string GetCategory(IEntity user, IComponent component) => "";
/// <summary>
/// Gets the visibility level of this verb in the right click menu.
/// </summary>
/// <param name="user">The entity of the user opening this menu.</param>
/// <param name="component">The component instance for which this verb is being loaded.</param>
/// <returns>The visibility level of the verb in the client's right click menu.</returns>
public abstract VerbVisibility GetVisibility(IEntity user, IComponent component);
/// <param name="data">The data that must be filled into.</param>
protected abstract void GetData(IEntity user, IComponent component, VerbData data);
/// <summary>
/// Invoked when this verb is activated from the right click menu.
@@ -53,6 +40,13 @@ namespace Content.Shared.GameObjects
/// <param name="user">The entity of the user opening this menu.</param>
/// <param name="component">The component instance for which this verb is being loaded.</param>
public abstract void Activate(IEntity user, IComponent component);
public VerbData GetData(IEntity user, IComponent component)
{
var data = new VerbData();
GetData(user, component, data);
return data;
}
}
/// <inheritdoc />
@@ -64,28 +58,15 @@ namespace Content.Shared.GameObjects
public abstract class Verb<T> : Verb where T : IComponent
{
/// <summary>
/// Gets the text string that will be shown to <paramref name="user"/> in the right click menu.
/// Gets the visible verb data for the user.
/// </summary>
/// <remarks>
/// Implementations should write into <paramref name="data"/> to return their data.
/// </remarks>
/// <param name="user">The entity of the user opening this menu.</param>
/// <param name="component">The component instance for which this verb is being loaded.</param>
/// <returns>The text string that is shown in the right click menu for this verb.</returns>
protected abstract string GetText(IEntity user, T component);
/// <summary>
/// Gets the category of this verb.
/// </summary>
/// <param name="user">The entity of the user opening this menu.</param>
/// <param name="component">The component instance for which this verb is being loaded.</param>
/// <returns>The category of this verb.</returns>
protected virtual string GetCategory(IEntity user, T component) => "";
/// <summary>
/// Gets the visibility level of this verb in the right click menu.
/// </summary>
/// <param name="user">The entity of the user opening this menu.</param>
/// <param name="component">The component instance for which this verb is being loaded.</param>
/// <returns>The visibility level of the verb in the client's right click menu.</returns>
protected abstract VerbVisibility GetVisibility(IEntity user, T component);
/// <param name="data">The data that must be filled into.</param>
protected abstract void GetData(IEntity user, T component, VerbData data);
/// <summary>
/// Invoked when this verb is activated from the right click menu.
@@ -94,22 +75,9 @@ namespace Content.Shared.GameObjects
/// <param name="component">The component instance for which this verb is being loaded.</param>
protected abstract void Activate(IEntity user, T component);
/// <inheritdoc />
public sealed override string GetText(IEntity user, IComponent component)
protected sealed override void GetData(IEntity user, IComponent component, VerbData data)
{
return GetText(user, (T) component);
}
/// <inheritdoc />
public sealed override string GetCategory(IEntity user, IComponent component)
{
return GetCategory(user, (T) component);
}
/// <inheritdoc />
public sealed override VerbVisibility GetVisibility(IEntity user, IComponent component)
{
return GetVisibility(user, (T) component);
GetData(user, (T) component, data);
}
/// <inheritdoc />
@@ -128,25 +96,4 @@ namespace Content.Shared.GameObjects
public sealed class VerbAttribute : Attribute
{
}
/// <summary>
/// Possible states of visibility for the verb in the right click menu.
/// </summary>
public enum VerbVisibility
{
/// <summary>
/// The verb will be listed in the right click menu.
/// </summary>
Visible,
/// <summary>
/// The verb will be listed, but it will be grayed out and unable to be clicked on.
/// </summary>
Disabled,
/// <summary>
/// The verb will not be listed in the right click menu.
/// </summary>
Invisible
}
}

View File

@@ -0,0 +1,13 @@
namespace Content.Shared.GameObjects
{
/// <summary>
/// Standard verb categories.
/// </summary>
public static class VerbCategories
{
public static readonly VerbCategoryData Debug =
("Debug", "/Textures/UserInterface/VerbIcons/debug.svg.96dpi.png");
public static readonly VerbCategoryData Rotate = ("Rotate", null);
}
}

View File

@@ -0,0 +1,24 @@
using Robust.Shared.Utility;
namespace Content.Shared.GameObjects
{
/// <summary>
/// Contains combined name and icon information for a verb category.
/// </summary>
public readonly struct VerbCategoryData
{
public VerbCategoryData(string name, SpriteSpecifier icon)
{
Name = name;
Icon = icon;
}
public string Name { get; }
public SpriteSpecifier Icon { get; }
public static implicit operator VerbCategoryData((string name, string icon) tuple)
{
return new VerbCategoryData(tuple.name, tuple.icon == null ? null : new SpriteSpecifier.Texture(new ResourcePath(tuple.icon)));
}
}
}

View File

@@ -0,0 +1,65 @@
using JetBrains.Annotations;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Utility;
namespace Content.Shared.GameObjects
{
/// <summary>
/// Stores visual data for a verb.
/// </summary>
/// <remarks>
/// An instance of this class gets instantiated by the verb system and should be filled in by implementations of
/// <see cref="Verb.GetData(IEntity, IComponent, VerbData)"/>.
/// </remarks>
public sealed class VerbData
{
/// <summary>
/// The text that the user sees on the verb button.
/// </summary>
public string Text { get; set; }
/// <summary>
/// Sprite of the icon that the user sees on the verb button.
/// </summary>
public SpriteSpecifier Icon { get; set; }
/// <summary>
/// Name of the category this button is under.
/// </summary>
public string Category { get; set; } = "";
/// <summary>
/// Sprite of the icon that the user sees on the verb button.
/// </summary>
public SpriteSpecifier CategoryIcon { get; set; }
/// <summary>
/// Whether this verb is visible, disabled (greyed out) or hidden.
/// </summary>
public VerbVisibility Visibility { get; set; } = VerbVisibility.Visible;
public bool IsInvisible => Visibility == VerbVisibility.Invisible;
public bool IsDisabled => Visibility == VerbVisibility.Disabled;
/// <summary>
/// Convenience property to set verb category and icon at once.
/// </summary>
[ValueProvider("Content.Shared.GameObjects.VerbCategories")]
public VerbCategoryData CategoryData
{
set
{
Category = value.Name;
CategoryIcon = value.Icon;
}
}
/// <summary>
/// Convenience property to set <see cref="Icon"/> to a raw texture path.
/// </summary>
public string IconTexture
{
set => Icon = new SpriteSpecifier.Texture(new ResourcePath(value));
}
}
}

View File

@@ -60,17 +60,5 @@ namespace Content.Shared.GameObjects
}
return true;
}
public static bool IsVerbInvisible(Verb verb, IEntity user, IComponent target, out VerbVisibility visibility)
{
visibility = verb.GetVisibility(user, target);
return visibility == VerbVisibility.Invisible;
}
public static bool IsVerbInvisible(GlobalVerb verb, IEntity user, IEntity target, out VerbVisibility visibility)
{
visibility = verb.GetVisibility(user, target);
return visibility == VerbVisibility.Invisible;
}
}
}

View File

@@ -0,0 +1,23 @@
namespace Content.Shared.GameObjects
{
/// <summary>
/// Possible states of visibility for the verb in the right click menu.
/// </summary>
public enum VerbVisibility
{
/// <summary>
/// The verb will be listed in the right click menu.
/// </summary>
Visible,
/// <summary>
/// The verb will be listed, but it will be grayed out and unable to be clicked on.
/// </summary>
Disabled,
/// <summary>
/// The verb will not be listed in the right click menu.
/// </summary>
Invisible
}
}