Files
tbd-station-14/Content.Server/UserInterface/ActivatableUISystem.cs
20kdc f6d44be34f Activatable UI component (#5184)
* Transfer most Instrument UI logic to a new component, ActivatableUIComponent

* Move more ActivatableUIComponent stuff to ECS

* ActivatableUI component ignore on client

* ActivatableUI: Get rid of component interfaces where possible

* Add in adminOnly attribute for activatable UIs

This is so that porting #4926 to this will be easier

* Transition Solar Control Computer to ActivatableUI

* Move communications console to ActivatableUI

* Move cargo console to ActivatableUI

* Move ID card console to ActivatableUI

* ActivatableUI: Make things more amiable to entity tests adding components weirdly

* ActivatableUI: Use handling or lack thereof of events properly

* ActivatableUI: component dependency issue resolution stuffs

* ActivatableUISystem: Fix #5258

* More fixes because master did stuffo

* Check for HandDeselectedEvent again because otherwise active-hand check doesn't work

* Move just a bit more code into the system, introduce a workaround for #5258

* Purge the player status detection stuff

* Oh and some obsolete stuff too
2021-11-23 19:19:08 +01:00

155 lines
5.8 KiB
C#

using System.Linq;
using Content.Shared;
using Content.Shared.CCVar;
using Content.Shared.ActionBlocker;
using Content.Shared.Hands;
using Content.Shared.Popups;
using Content.Shared.Standing;
using Content.Shared.Stunnable;
using Content.Shared.Throwing;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Helpers;
using Content.Server.Administration.Managers;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Localization;
using Robust.Shared.GameObjects;
using Robust.Shared.Network;
using Robust.Shared.IoC;
namespace Content.Server.UserInterface
{
[UsedImplicitly]
internal sealed class ActivatableUISystem : EntitySystem
{
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
[Dependency] private readonly IAdminManager _adminManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ActivatableUIComponent, ActivateInWorldEvent>(OnActivate);
SubscribeLocalEvent<ActivatableUIComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<ActivatableUIComponent, HandDeselectedEvent>((uid, aui, _) => CloseAll(uid, aui));
SubscribeLocalEvent<ActivatableUIComponent, UnequippedHandEvent>((uid, aui, _) => CloseAll(uid, aui));
// *THIS IS A BLATANT WORKAROUND!* RATIONALE: Microwaves need it
SubscribeLocalEvent<ActivatableUIComponent, EntParentChangedMessage>(OnParentChanged);
SubscribeLocalEvent<ActivatableUIComponent, BoundUIClosedEvent>(OnUIClose);
}
private void OnActivate(EntityUid uid, ActivatableUIComponent component, ActivateInWorldEvent args)
{
if (args.Handled) return;
if (component.InHandsOnly) return;
args.Handled = InteractUI(args.User, component);
}
private void OnUseInHand(EntityUid uid, ActivatableUIComponent component, UseInHandEvent args)
{
if (args.Handled) return;
args.Handled = InteractUI(args.User, 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 (args.UiKey != component.Key) return;
SetCurrentSingleUser(uid, null, component);
}
private bool InteractUI(IEntity user, ActivatableUIComponent aui)
{
if (!user.TryGetComponent(out ActorComponent? actor)) return false;
if (aui.AdminOnly && !_adminManager.IsAdmin(actor.PlayerSession)) return false;
if (!_actionBlockerSystem.CanInteract(user.Uid))
{
user.PopupMessageCursor(Loc.GetString("base-computer-ui-component-cannot-interact"));
return true;
}
var ui = aui.UserInterface;
if (ui == null) return false;
if (aui.SingleUser && (aui.CurrentSingleUser != null) && (actor.PlayerSession != aui.CurrentSingleUser))
{
// 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);
RaiseLocalEvent(aui.OwnerUid, oae, false);
if (oae.Cancelled) return false;
SetCurrentSingleUser(aui.OwnerUid, actor.PlayerSession, aui);
ui.Toggle(actor.PlayerSession);
return true;
}
public void SetCurrentSingleUser(EntityUid uid, IPlayerSession? v, ActivatableUIComponent? aui = null)
{
if (!Resolve(uid, ref aui))
return;
if (!aui.SingleUser)
return;
aui.CurrentSingleUser = v;
RaiseLocalEvent(uid, new ActivatableUIPlayerChangedEvent(), false);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
foreach (var component in EntityManager.EntityQuery<ActivatableUIComponent>(true))
{
var ui = component.UserInterface;
if (ui == null) continue;
// Done to skip an allocation on anything that's not in use.
if (ui.SubscribedSessions.Count == 0) continue;
// Must ToList in order to close things safely.
foreach (var session in ui.SubscribedSessions.ToArray())
{
if (session.AttachedEntityUid == null || !_actionBlockerSystem.CanInteract(session.AttachedEntityUid.Value))
{
ui.Close(session);
}
}
}
}
public void CloseAll(EntityUid uid, ActivatableUIComponent? aui = null)
{
if (!Resolve(uid, ref aui, false)) return;
aui.UserInterface?.CloseAll();
}
}
public class ActivatableUIOpenAttemptEvent : CancellableEntityEventArgs
{
public IEntity User { get; }
public ActivatableUIOpenAttemptEvent(IEntity who)
{
User = who;
}
}
public class ActivatableUIPlayerChangedEvent : EntityEventArgs
{
}
}