Implement Intrinsic UIs (#6926)
* Implement Intrinsic UIs, allowing the admin ghost to double as a computer. * ignore moment * remove debug statement, sort the actions. * ffs * didn't ever use this and don't need to, removed. * rm dead code * lil bit of commenting.
This commit is contained in:
@@ -1,18 +1,8 @@
|
||||
using System;
|
||||
using Content.Shared.Instruments;
|
||||
using Content.Shared.Interaction;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Reflection;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
|
||||
namespace Content.Server.UserInterface
|
||||
{
|
||||
|
||||
56
Content.Server/UserInterface/IntrinsicUIComponent.cs
Normal file
56
Content.Server/UserInterface/IntrinsicUIComponent.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Content.Shared.Actions.ActionTypes;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Reflection;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Server.UserInterface;
|
||||
|
||||
[RegisterComponent]
|
||||
public sealed class IntrinsicUIComponent : Component, ISerializationHooks
|
||||
{
|
||||
/// <summary>
|
||||
/// List of UIs and their actions that this entity has.
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("uis", required: true)]
|
||||
public List<IntrinsicUIEntry> UIs = new();
|
||||
|
||||
void ISerializationHooks.AfterDeserialization()
|
||||
{
|
||||
foreach (var ui in UIs)
|
||||
{
|
||||
ui.AfterDeserialization();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[DataDefinition]
|
||||
public struct IntrinsicUIEntry
|
||||
{
|
||||
[ViewVariables]
|
||||
public Enum? Key { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The BUI key that this intrinsic UI should open.
|
||||
/// </summary>
|
||||
[DataField("key", readOnly: true, required: true)]
|
||||
private string _keyRaw = default!;
|
||||
|
||||
/// <summary>
|
||||
/// The action used for this BUI.
|
||||
/// </summary>
|
||||
[DataField("toggleAction", required: true)]
|
||||
public InstantAction ToggleAction = new();
|
||||
|
||||
public void AfterDeserialization()
|
||||
{
|
||||
var reflectionManager = IoCManager.Resolve<IReflectionManager>();
|
||||
if (reflectionManager.TryParseEnumReference(_keyRaw, out var key))
|
||||
Key = key;
|
||||
|
||||
if (ToggleAction.Event is ToggleIntrinsicUIEvent ev)
|
||||
{
|
||||
ev.Key = Key;
|
||||
}
|
||||
}
|
||||
}
|
||||
89
Content.Server/UserInterface/IntrinsicUISystem.cs
Normal file
89
Content.Server/UserInterface/IntrinsicUISystem.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Content.Server.Actions;
|
||||
using Content.Shared.Actions;
|
||||
using Content.Shared.Toggleable;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Reflection;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Server.UserInterface;
|
||||
|
||||
public sealed class IntrinsicUISystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly ActionsSystem _actionsSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<IntrinsicUIComponent, ComponentStartup>(OnGetActions);
|
||||
SubscribeLocalEvent<IntrinsicUIComponent, ToggleIntrinsicUIEvent>(OnActionToggle);
|
||||
}
|
||||
|
||||
private void OnActionToggle(EntityUid uid, IntrinsicUIComponent component, ToggleIntrinsicUIEvent args)
|
||||
{
|
||||
args.Handled = InteractUI(uid, args.Key, component);
|
||||
}
|
||||
|
||||
private void OnGetActions(EntityUid uid, IntrinsicUIComponent component, ComponentStartup args)
|
||||
{
|
||||
if (!TryComp<ActionsComponent>(uid, out var actions))
|
||||
return;
|
||||
|
||||
foreach (var entry in component.UIs)
|
||||
{
|
||||
_actionsSystem.AddAction(uid, entry.ToggleAction, null, actions);
|
||||
}
|
||||
}
|
||||
|
||||
public bool InteractUI(EntityUid uid, Enum? key, IntrinsicUIComponent? iui = null, ActorComponent? actor = null)
|
||||
{
|
||||
if (!Resolve(uid, ref iui, ref actor))
|
||||
return false;
|
||||
|
||||
if (key is null)
|
||||
{
|
||||
Logger.ErrorS("bui", $"Entity {ToPrettyString(uid)} has an invalid intrinsic UI.");
|
||||
}
|
||||
|
||||
var ui = GetUIOrNull(uid, key, iui);
|
||||
|
||||
if (ui is null)
|
||||
{
|
||||
Logger.ErrorS("bui", $"Couldn't get UI {key} on {ToPrettyString(uid)}");
|
||||
return false;
|
||||
}
|
||||
|
||||
var attempt = new IntrinsicUIOpenAttemptEvent(uid, key);
|
||||
RaiseLocalEvent(uid, attempt, false);
|
||||
if (attempt.Cancelled) return false;
|
||||
|
||||
ui.Toggle(actor.PlayerSession);
|
||||
return true;
|
||||
}
|
||||
|
||||
private BoundUserInterface? GetUIOrNull(EntityUid uid, Enum? key, IntrinsicUIComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return null;
|
||||
|
||||
return key is null ? null : uid.GetUIOrNull(key);
|
||||
}
|
||||
}
|
||||
|
||||
[UsedImplicitly]
|
||||
public sealed class ToggleIntrinsicUIEvent : PerformActionEvent
|
||||
{
|
||||
[ViewVariables]
|
||||
public Enum? Key { get; set; }
|
||||
}
|
||||
|
||||
// Competing with ActivatableUI for horrible event names.
|
||||
public sealed class IntrinsicUIOpenAttemptEvent : CancellableEntityEventArgs
|
||||
{
|
||||
public EntityUid User { get; }
|
||||
public Enum? Key { get; }
|
||||
public IntrinsicUIOpenAttemptEvent(EntityUid who, Enum? key)
|
||||
{
|
||||
User = who;
|
||||
Key = key;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user