Add global verbs (#400)

* Add support for global verbs

These are verbs that are visible for all entities, regardless of their components. This works by adding a new class `GlobalVerb` and a new attribute `GlobalVerbAttribute`. It works in the same way as current verbs, except you can put the verbs class definition anywhere instead of inside a component. Also moved VerbUtility into it's own file since it now has functions for both verbs and global verbs.

* Add view variables verb as an example of global verbs

* Implement suggested changes

Implemented some suggested changes from code review:
- Remove unneeded attribute from `GlobalVerb`
- Added some useful attributes to `GlobalVerbAttribute`
- Moved constants used by both `Verb` and `GlobalVerb` into `VerbUtility`

* Reduce duplicate code in VerbSystem (client & server)

Greatly reduced the amount of duplicate code for handling component verbs and global verbs separately.

* Update engine submodule

Need this so client side permissions checks are available.
This commit is contained in:
moneyl
2019-10-30 11:31:35 -04:00
committed by Pieter-Jan Briers
parent e4f3ea7798
commit 6497cdf8ff
6 changed files with 228 additions and 69 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.EntitySystemMessages;
using Content.Shared.Input;
@@ -160,41 +161,25 @@ namespace Content.Client.GameObjects.EntitySystems
}
var user = GetUserEntity();
//Get verbs, component dependent.
foreach (var (component, verb) in VerbUtility.GetVerbs(entity))
{
if (verb.RequireInteractionRange)
{
var distanceSquared = (user.Transform.WorldPosition - entity.Transform.WorldPosition)
.LengthSquared;
if (distanceSquared > Verb.InteractionRangeSquared)
{
continue;
}
}
if (verb.RequireInteractionRange && !VerbUtility.InVerbUseRange(user, entity))
continue;
var disabled = verb.GetVisibility(user, component) != VerbVisibility.Visible;
var button = new Button
{
Text = verb.GetText(user, component),
Disabled = disabled
};
if (!disabled)
{
button.OnPressed += _ =>
{
_closeContextMenu();
try
{
verb.Activate(user, component);
}
catch (Exception e)
{
Logger.ErrorS("verb", "Exception in verb {0} on {1}:\n{2}", verb, entity, e);
}
};
}
buttons.Add(CreateVerbButton(verb.GetText(user, component), disabled, verb.ToString(),
entity.ToString(), () => verb.Activate(user, component)));
}
//Get global verbs. Visible for all entities regardless of their components.
foreach (var globalVerb in VerbUtility.GetGlobalVerbs(Assembly.GetExecutingAssembly()))
{
if (globalVerb.RequireInteractionRange && !VerbUtility.InVerbUseRange(user, entity))
continue;
buttons.Add(button);
var disabled = globalVerb.GetVisibility(user, entity) != VerbVisibility.Visible;
buttons.Add(CreateVerbButton(globalVerb.GetText(user, entity), disabled, globalVerb.ToString(),
entity.ToString(), () => globalVerb.Activate(user, entity)));
}
if (buttons.Count > 0)
@@ -224,6 +209,31 @@ namespace Content.Client.GameObjects.EntitySystems
}
}
private Button CreateVerbButton(string text, bool disabled, string verbName, string ownerName, Action action)
{
var button = new Button
{
Text = text,
Disabled = disabled
};
if (!disabled)
{
button.OnPressed += _ =>
{
_closeContextMenu();
try
{
action.Invoke();
}
catch (Exception e)
{
Logger.ErrorS("verb", "Exception in verb {0} on {1}:\n{2}", verbName, ownerName, e);
}
};
}
return button;
}
private void _closeContextMenu()
{
_currentPopup?.Dispose();