Predict IntrinsicUI (#31310)
This commit is contained in:
26
Content.Shared/UserInterface/IntrinsicUIComponent.cs
Normal file
26
Content.Shared/UserInterface/IntrinsicUIComponent.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.UserInterface;
|
||||
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed partial class IntrinsicUIComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// List of UIs and their actions that this entity has.
|
||||
/// </summary>
|
||||
[DataField("uis", required: true)] public Dictionary<Enum, IntrinsicUIEntry> UIs = new();
|
||||
}
|
||||
|
||||
[DataDefinition]
|
||||
public sealed partial class IntrinsicUIEntry
|
||||
{
|
||||
[DataField("toggleAction", required: true)]
|
||||
public EntProtoId? ToggleAction;
|
||||
|
||||
/// <summary>
|
||||
/// The action used for this BUI.
|
||||
/// </summary>
|
||||
[DataField("toggleActionEntity")]
|
||||
public EntityUid? ToggleActionEntity = new();
|
||||
}
|
||||
56
Content.Shared/UserInterface/IntrinsicUISystem.cs
Normal file
56
Content.Shared/UserInterface/IntrinsicUISystem.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Content.Shared.Actions;
|
||||
|
||||
namespace Content.Shared.UserInterface;
|
||||
|
||||
public sealed class IntrinsicUISystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
|
||||
[Dependency] private readonly SharedUserInterfaceSystem _uiSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<IntrinsicUIComponent, MapInitEvent>(InitActions);
|
||||
SubscribeLocalEvent<IntrinsicUIComponent, ToggleIntrinsicUIEvent>(OnActionToggle);
|
||||
}
|
||||
|
||||
private void OnActionToggle(EntityUid uid, IntrinsicUIComponent component, ToggleIntrinsicUIEvent args)
|
||||
{
|
||||
if (args.Key == null)
|
||||
return;
|
||||
|
||||
args.Handled = InteractUI(uid, args.Key, component);
|
||||
}
|
||||
|
||||
private void InitActions(EntityUid uid, IntrinsicUIComponent component, MapInitEvent args)
|
||||
{
|
||||
foreach (var entry in component.UIs.Values)
|
||||
{
|
||||
_actionsSystem.AddAction(uid, ref entry.ToggleActionEntity, entry.ToggleAction);
|
||||
}
|
||||
}
|
||||
|
||||
public bool InteractUI(EntityUid uid, Enum key, IntrinsicUIComponent? iui = null)
|
||||
{
|
||||
if (!Resolve(uid, ref iui))
|
||||
return false;
|
||||
|
||||
var attempt = new IntrinsicUIOpenAttemptEvent(uid, key);
|
||||
RaiseLocalEvent(uid, attempt);
|
||||
if (attempt.Cancelled)
|
||||
return false;
|
||||
|
||||
return _uiSystem.TryToggleUi(uid, key, uid);
|
||||
}
|
||||
}
|
||||
|
||||
// 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