Content update for UI prediction (#27214)
* Content update for UI refactor * Big update * Sharing * Remaining content updates * First big update * Prototype updates * AUGH * Fix UI comp ref * Cleanup - Fix predicted message, fix item slots, fix interaction range check. * Fix regressions * Make this predictive idk why it wasn't. * Fix slime merge * Merge conflict * Fix merge
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
using Content.Shared.Whitelist;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations;
|
||||
|
||||
namespace Content.Server.UserInterface
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed partial class ActivatableUIComponent : Component
|
||||
{
|
||||
[DataField(required: true, customTypeSerializer:typeof(EnumSerializer))]
|
||||
public Enum? Key { get; set; }
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField]
|
||||
public bool InHandsOnly { get; set; } = false;
|
||||
|
||||
[DataField]
|
||||
public bool SingleUser { get; set; } = false;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField]
|
||||
public bool AdminOnly { get; set; } = false;
|
||||
|
||||
[DataField]
|
||||
public LocId VerbText = "ui-verb-toggle-open";
|
||||
|
||||
/// <summary>
|
||||
/// Whether you need a hand to operate this UI. The hand does not need to be free, you just need to have one.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This should probably be true for most machines & computers, but there will still be UIs that represent a
|
||||
/// more generic interaction / configuration that might not require hands.
|
||||
/// </remarks>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField]
|
||||
public bool RequireHands = true;
|
||||
|
||||
/// <summary>
|
||||
/// Entities that are required to open this UI.
|
||||
/// </summary>
|
||||
[DataField("allowedItems")]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public EntityWhitelist? AllowedItems = null;
|
||||
|
||||
/// <summary>
|
||||
/// Whether you can activate this ui with activateinhand or not
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField]
|
||||
public bool RightClickOnly;
|
||||
|
||||
/// <summary>
|
||||
/// Whether spectators (non-admin ghosts) should be allowed to view this UI.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField]
|
||||
public bool AllowSpectator = true;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the UI should close when the item is deselected due to a hand swap or drop
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField]
|
||||
public bool CloseOnHandDeselect = true;
|
||||
|
||||
/// <summary>
|
||||
/// The client channel currently using the object, or null if there's none/not single user.
|
||||
/// NOTE: DO NOT DIRECTLY SET, USE ActivatableUISystem.SetCurrentSingleUser
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public ICommonSession? CurrentSingleUser;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using Content.Server.PowerCell;
|
||||
using Content.Shared.PowerCell;
|
||||
using Content.Shared.UserInterface;
|
||||
|
||||
namespace Content.Server.UserInterface;
|
||||
|
||||
/// <summary>
|
||||
/// Specifies that the attached entity requires <see cref="PowerCellDrawComponent"/> power.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed partial class ActivatableUIRequiresPowerCellComponent : Component
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
using Content.Server.PowerCell;
|
||||
using Content.Shared.PowerCell;
|
||||
using Content.Shared.UserInterface;
|
||||
using Robust.Shared.Containers;
|
||||
|
||||
namespace Content.Server.UserInterface;
|
||||
|
||||
public sealed partial class ActivatableUISystem
|
||||
{
|
||||
[Dependency] private readonly PowerCellSystem _cell = default!;
|
||||
|
||||
private void InitializePower()
|
||||
{
|
||||
SubscribeLocalEvent<ActivatableUIRequiresPowerCellComponent, ActivatableUIOpenAttemptEvent>(OnBatteryOpenAttempt);
|
||||
SubscribeLocalEvent<ActivatableUIRequiresPowerCellComponent, BoundUIOpenedEvent>(OnBatteryOpened);
|
||||
SubscribeLocalEvent<ActivatableUIRequiresPowerCellComponent, BoundUIClosedEvent>(OnBatteryClosed);
|
||||
|
||||
SubscribeLocalEvent<PowerCellDrawComponent, EntRemovedFromContainerMessage>(OnPowerCellRemoved);
|
||||
}
|
||||
|
||||
private void OnPowerCellRemoved(EntityUid uid, PowerCellDrawComponent component, EntRemovedFromContainerMessage args)
|
||||
{
|
||||
_cell.SetPowerCellDrawEnabled(uid, false);
|
||||
|
||||
if (HasComp<ActivatableUIRequiresPowerCellComponent>(uid) &&
|
||||
TryComp<ActivatableUIComponent>(uid, out var activatable) &&
|
||||
activatable.Key != null)
|
||||
{
|
||||
_uiSystem.TryCloseAll(uid, activatable.Key);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnBatteryOpened(EntityUid uid, ActivatableUIRequiresPowerCellComponent component, BoundUIOpenedEvent args)
|
||||
{
|
||||
var activatable = Comp<ActivatableUIComponent>(uid);
|
||||
|
||||
if (!args.UiKey.Equals(activatable.Key))
|
||||
return;
|
||||
|
||||
_cell.SetPowerCellDrawEnabled(uid, true);
|
||||
}
|
||||
|
||||
private void OnBatteryClosed(EntityUid uid, ActivatableUIRequiresPowerCellComponent component, BoundUIClosedEvent args)
|
||||
{
|
||||
var activatable = Comp<ActivatableUIComponent>(uid);
|
||||
|
||||
if (!args.UiKey.Equals(activatable.Key))
|
||||
return;
|
||||
|
||||
// Stop drawing power if this was the last person with the UI open.
|
||||
if (!_uiSystem.IsUiOpen(uid, activatable.Key))
|
||||
_cell.SetPowerCellDrawEnabled(uid, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Call if you want to check if the UI should close due to a recent battery usage.
|
||||
/// </summary>
|
||||
public void CheckUsage(EntityUid uid, ActivatableUIComponent? active = null, ActivatableUIRequiresPowerCellComponent? component = null, PowerCellDrawComponent? draw = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component, ref draw, ref active, false) || active.Key == null)
|
||||
return;
|
||||
|
||||
if (_cell.HasCharge(uid, draw.UseRate))
|
||||
return;
|
||||
|
||||
_uiSystem.TryCloseAll(uid, active.Key);
|
||||
}
|
||||
|
||||
private void OnBatteryOpenAttempt(EntityUid uid, ActivatableUIRequiresPowerCellComponent component, ActivatableUIOpenAttemptEvent args)
|
||||
{
|
||||
if (!TryComp<PowerCellDrawComponent>(uid, out var draw))
|
||||
return;
|
||||
|
||||
// Check if we have the appropriate drawrate / userate to even open it.
|
||||
if (args.Cancelled || !_cell.HasCharge(uid, MathF.Max(draw.DrawRate, draw.UseRate), user: args.User))
|
||||
{
|
||||
args.Cancel();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,235 +0,0 @@
|
||||
using Content.Server.Administration.Managers;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Ghost;
|
||||
using Content.Shared.Hands;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Events;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.UserInterface;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Server.UserInterface;
|
||||
|
||||
public sealed partial class ActivatableUISystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IAdminManager _adminManager = default!;
|
||||
[Dependency] private readonly ActionBlockerSystem _blockerSystem = default!;
|
||||
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
|
||||
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<ActivatableUIComponent, ActivateInWorldEvent>(OnActivate);
|
||||
SubscribeLocalEvent<ActivatableUIComponent, UseInHandEvent>(OnUseInHand);
|
||||
SubscribeLocalEvent<ActivatableUIComponent, InteractUsingEvent>(OnInteractUsing);
|
||||
SubscribeLocalEvent<ActivatableUIComponent, HandDeselectedEvent>(OnHandDeselected);
|
||||
SubscribeLocalEvent<ActivatableUIComponent, GotUnequippedHandEvent>((uid, aui, _) => CloseAll(uid, aui));
|
||||
// *THIS IS A BLATANT WORKAROUND!* RATIONALE: Microwaves need it
|
||||
SubscribeLocalEvent<ActivatableUIComponent, EntParentChangedMessage>(OnParentChanged);
|
||||
SubscribeLocalEvent<ActivatableUIComponent, BoundUIClosedEvent>(OnUIClose);
|
||||
SubscribeLocalEvent<BoundUserInterfaceMessageAttempt>(OnBoundInterfaceInteractAttempt);
|
||||
|
||||
SubscribeLocalEvent<ActivatableUIComponent, GetVerbsEvent<ActivationVerb>>(AddOpenUiVerb);
|
||||
|
||||
SubscribeLocalEvent<UserInterfaceComponent, OpenUiActionEvent>(OnActionPerform);
|
||||
|
||||
InitializePower();
|
||||
}
|
||||
|
||||
private void OnBoundInterfaceInteractAttempt(BoundUserInterfaceMessageAttempt ev)
|
||||
{
|
||||
if (!TryComp(ev.Target, out ActivatableUIComponent? comp))
|
||||
return;
|
||||
|
||||
if (!comp.RequireHands)
|
||||
return;
|
||||
|
||||
if (!TryComp(ev.Sender.AttachedEntity, out HandsComponent? hands) || hands.Hands.Count == 0)
|
||||
ev.Cancel();
|
||||
}
|
||||
|
||||
private void OnActionPerform(EntityUid uid, UserInterfaceComponent component, OpenUiActionEvent args)
|
||||
{
|
||||
if (args.Handled || args.Key == null)
|
||||
return;
|
||||
|
||||
if (!TryComp(args.Performer, out ActorComponent? actor))
|
||||
return;
|
||||
|
||||
args.Handled = _uiSystem.TryToggleUi(uid, args.Key, actor.PlayerSession);
|
||||
}
|
||||
|
||||
private void AddOpenUiVerb(EntityUid uid, ActivatableUIComponent component, GetVerbsEvent<ActivationVerb> args)
|
||||
{
|
||||
if (!args.CanAccess)
|
||||
return;
|
||||
|
||||
if (component.RequireHands && args.Hands == null)
|
||||
return;
|
||||
|
||||
if (component.InHandsOnly && args.Using != uid)
|
||||
return;
|
||||
|
||||
if (!args.CanInteract && (!component.AllowSpectator || !HasComp<GhostComponent>(args.User)))
|
||||
return;
|
||||
|
||||
ActivationVerb verb = new();
|
||||
verb.Act = () => InteractUI(args.User, uid, component);
|
||||
verb.Text = Loc.GetString(component.VerbText);
|
||||
// TODO VERBS add "open UI" icon?
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
private void OnActivate(EntityUid uid, ActivatableUIComponent component, ActivateInWorldEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
if (component.InHandsOnly)
|
||||
return;
|
||||
|
||||
if (component.AllowedItems != null)
|
||||
return;
|
||||
|
||||
args.Handled = InteractUI(args.User, uid, component);
|
||||
}
|
||||
|
||||
private void OnUseInHand(EntityUid uid, ActivatableUIComponent component, UseInHandEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
if (component.RightClickOnly)
|
||||
return;
|
||||
|
||||
if (component.AllowedItems != null)
|
||||
return;
|
||||
|
||||
args.Handled = InteractUI(args.User, uid, component);
|
||||
}
|
||||
|
||||
private void OnInteractUsing(EntityUid uid, ActivatableUIComponent component, InteractUsingEvent args)
|
||||
{
|
||||
if (args.Handled) return;
|
||||
if (component.AllowedItems == null) return;
|
||||
if (!component.AllowedItems.IsValid(args.Used, EntityManager)) return;
|
||||
args.Handled = InteractUI(args.User, uid, component);
|
||||
}
|
||||
|
||||
private void OnParentChanged(EntityUid uid, ActivatableUIComponent aui, ref EntParentChangedMessage args)
|
||||
{
|
||||
CloseAll(uid, aui);
|
||||
}
|
||||
|
||||
private void OnUIClose(EntityUid uid, ActivatableUIComponent component, BoundUIClosedEvent args)
|
||||
{
|
||||
if (args.Session != component.CurrentSingleUser)
|
||||
return;
|
||||
|
||||
if (!Equals(args.UiKey, component.Key))
|
||||
return;
|
||||
|
||||
SetCurrentSingleUser(uid, null, component);
|
||||
}
|
||||
|
||||
private bool InteractUI(EntityUid user, EntityUid uiEntity, ActivatableUIComponent aui)
|
||||
{
|
||||
if (!TryComp(user, out ActorComponent? actor))
|
||||
return false;
|
||||
|
||||
if (aui.Key == null)
|
||||
return false;
|
||||
|
||||
if (!_uiSystem.TryGetUi(uiEntity, aui.Key, out var ui))
|
||||
return false;
|
||||
|
||||
if (ui.SubscribedSessions.Contains(actor.PlayerSession))
|
||||
{
|
||||
_uiSystem.CloseUi(ui, actor.PlayerSession);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!_blockerSystem.CanInteract(user, uiEntity) && (!aui.AllowSpectator || !HasComp<GhostComponent>(user)))
|
||||
return false;
|
||||
|
||||
if (aui.RequireHands && !HasComp<HandsComponent>(user))
|
||||
return false;
|
||||
|
||||
if (aui.AdminOnly && !_adminManager.IsAdmin(actor.PlayerSession))
|
||||
return false;
|
||||
|
||||
if (aui.SingleUser && (aui.CurrentSingleUser != null) && (actor.PlayerSession != aui.CurrentSingleUser))
|
||||
{
|
||||
string message = Loc.GetString("machine-already-in-use", ("machine", uiEntity));
|
||||
_popupSystem.PopupEntity(message, uiEntity, user);
|
||||
|
||||
// If we get here, supposedly, the object is in use.
|
||||
// Check with BUI that it's ACTUALLY in use just in case.
|
||||
// Since this could brick the object if it goes wrong.
|
||||
if (ui.SubscribedSessions.Count != 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
// If we've gotten this far, fire a cancellable event that indicates someone is about to activate this.
|
||||
// This is so that stuff can require further conditions (like power).
|
||||
var oae = new ActivatableUIOpenAttemptEvent(user);
|
||||
var uae = new UserOpenActivatableUIAttemptEvent(user, uiEntity);
|
||||
RaiseLocalEvent(user, uae);
|
||||
RaiseLocalEvent(uiEntity, oae);
|
||||
if (oae.Cancelled || uae.Cancelled)
|
||||
return false;
|
||||
|
||||
// Give the UI an opportunity to prepare itself if it needs to do anything
|
||||
// before opening
|
||||
var bae = new BeforeActivatableUIOpenEvent(user);
|
||||
RaiseLocalEvent(uiEntity, bae);
|
||||
|
||||
SetCurrentSingleUser(uiEntity, actor.PlayerSession, aui);
|
||||
_uiSystem.OpenUi(ui, actor.PlayerSession);
|
||||
|
||||
//Let the component know a user opened it so it can do whatever it needs to do
|
||||
var aae = new AfterActivatableUIOpenEvent(user, actor.PlayerSession);
|
||||
RaiseLocalEvent(uiEntity, aae);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetCurrentSingleUser(EntityUid uid, ICommonSession? v, ActivatableUIComponent? aui = null)
|
||||
{
|
||||
if (!Resolve(uid, ref aui))
|
||||
return;
|
||||
if (!aui.SingleUser)
|
||||
return;
|
||||
|
||||
aui.CurrentSingleUser = v;
|
||||
|
||||
RaiseLocalEvent(uid, new ActivatableUIPlayerChangedEvent());
|
||||
}
|
||||
|
||||
public void CloseAll(EntityUid uid, ActivatableUIComponent? aui = null)
|
||||
{
|
||||
if (!Resolve(uid, ref aui, false))
|
||||
return;
|
||||
|
||||
if (aui.Key == null || !_uiSystem.TryGetUi(uid, aui.Key, out var ui))
|
||||
return;
|
||||
|
||||
_uiSystem.CloseAll(ui);
|
||||
}
|
||||
|
||||
private void OnHandDeselected(EntityUid uid, ActivatableUIComponent? aui, HandDeselectedEvent args)
|
||||
{
|
||||
if (!Resolve(uid, ref aui, false))
|
||||
return;
|
||||
|
||||
if (!aui.CloseOnHandDeselect)
|
||||
return;
|
||||
|
||||
CloseAll(uid, aui);
|
||||
}
|
||||
}
|
||||
@@ -9,18 +9,12 @@ public sealed partial class IntrinsicUIComponent : Component
|
||||
/// <summary>
|
||||
/// List of UIs and their actions that this entity has.
|
||||
/// </summary>
|
||||
[DataField("uis", required: true)] public List<IntrinsicUIEntry> UIs = new();
|
||||
[DataField("uis", required: true)] public Dictionary<Enum, IntrinsicUIEntry> UIs = new();
|
||||
}
|
||||
|
||||
[DataDefinition]
|
||||
public partial class IntrinsicUIEntry
|
||||
public sealed partial class IntrinsicUIEntry
|
||||
{
|
||||
/// <summary>
|
||||
/// The BUI key that this intrinsic UI should open.
|
||||
/// </summary>
|
||||
[DataField("key", required: true)]
|
||||
public Enum? Key { get; private set; }
|
||||
|
||||
[DataField("toggleAction", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>), required: true)]
|
||||
public string? ToggleAction;
|
||||
|
||||
|
||||
@@ -18,50 +18,31 @@ public sealed class IntrinsicUISystem : EntitySystem
|
||||
|
||||
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)
|
||||
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, ActorComponent? actor = null)
|
||||
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)
|
||||
{
|
||||
Log.Error($"Entity {ToPrettyString(uid)} has an invalid intrinsic UI.");
|
||||
}
|
||||
|
||||
var ui = GetUIOrNull(uid, key, iui);
|
||||
|
||||
if (ui is null)
|
||||
{
|
||||
Log.Error($"Couldn't get UI {key} on {ToPrettyString(uid)}");
|
||||
return false;
|
||||
}
|
||||
|
||||
var attempt = new IntrinsicUIOpenAttemptEvent(uid, key);
|
||||
RaiseLocalEvent(uid, attempt);
|
||||
if (attempt.Cancelled)
|
||||
return false;
|
||||
|
||||
_uiSystem.ToggleUi(ui, actor.PlayerSession);
|
||||
return true;
|
||||
}
|
||||
|
||||
private PlayerBoundUserInterface? GetUIOrNull(EntityUid uid, Enum? key, IntrinsicUIComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return null;
|
||||
|
||||
return key is null ? null : _uiSystem.GetUiOrNull(uid, key);
|
||||
return _uiSystem.TryToggleUi(uid, key, actor.PlayerSession);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user