Kill UserInterfaceHelpers (#20912)

This commit is contained in:
Kara
2023-10-11 02:17:59 -07:00
committed by GitHub
parent 756fd6f309
commit 14dac914ce
12 changed files with 104 additions and 98 deletions

View File

@@ -55,7 +55,5 @@ namespace Content.Server.Communications
/// </summary>
[DataField]
public SoundSpecifier Sound = new SoundPathSpecifier("/Audio/Announcements/announce.ogg");
public PlayerBoundUserInterface? UserInterface => Owner.GetUIOrNull(CommunicationsConsoleUiKey.Key);
}
}

View File

@@ -72,8 +72,8 @@ namespace Content.Server.Communications
comp.UIUpdateAccumulator -= UIUpdateInterval;
if (comp.UserInterface is { } ui && ui.SubscribedSessions.Count > 0)
UpdateCommsConsoleInterface(uid, comp);
if (_uiSystem.TryGetUi(uid, CommunicationsConsoleUiKey.Key, out var ui) && ui.SubscribedSessions.Count > 0)
UpdateCommsConsoleInterface(uid, comp, ui);
}
base.Update(frameTime);
@@ -121,9 +121,11 @@ namespace Content.Server.Communications
/// <summary>
/// Updates the UI for a particular comms console.
/// </summary>
/// <param name="comp"></param>
public void UpdateCommsConsoleInterface(EntityUid uid, CommunicationsConsoleComponent comp)
public void UpdateCommsConsoleInterface(EntityUid uid, CommunicationsConsoleComponent comp, PlayerBoundUserInterface? ui = null)
{
if (ui == null && !_uiSystem.TryGetUi(uid, CommunicationsConsoleUiKey.Key, out ui))
return;
var stationUid = _stationSystem.GetOwningStation(uid);
List<string>? levels = null;
string currentLevel = default!;
@@ -151,15 +153,14 @@ namespace Content.Server.Communications
}
}
if (comp.UserInterface is not null)
_uiSystem.SetUiState(comp.UserInterface, new CommunicationsConsoleInterfaceState(
CanAnnounce(comp),
CanCallOrRecall(comp),
levels,
currentLevel,
currentDelay,
_roundEndSystem.ExpectedCountdownEnd
));
_uiSystem.SetUiState(ui, new CommunicationsConsoleInterfaceState(
CanAnnounce(comp),
CanCallOrRecall(comp),
levels,
currentLevel,
currentDelay,
_roundEndSystem.ExpectedCountdownEnd
));
}
private static bool CanAnnounce(CommunicationsConsoleComponent comp)
@@ -203,7 +204,9 @@ namespace Content.Server.Communications
private void OnSelectAlertLevelMessage(EntityUid uid, CommunicationsConsoleComponent comp, CommunicationsConsoleSelectAlertLevelMessage message)
{
if (message.Session.AttachedEntity is not { Valid: true } mob) return;
if (message.Session.AttachedEntity is not { Valid: true } mob)
return;
if (!CanUse(mob, uid))
{
_popupSystem.PopupCursor(Loc.GetString("comms-console-permission-denied"), message.Session, PopupType.Medium);
@@ -284,8 +287,12 @@ namespace Content.Server.Communications
private void OnCallShuttleMessage(EntityUid uid, CommunicationsConsoleComponent comp, CommunicationsConsoleCallEmergencyShuttleMessage message)
{
if (!CanCallOrRecall(comp)) return;
if (message.Session.AttachedEntity is not { Valid: true } mob) return;
if (!CanCallOrRecall(comp))
return;
if (message.Session.AttachedEntity is not { Valid: true } mob)
return;
if (!CanUse(mob, uid))
{
_popupSystem.PopupEntity(Loc.GetString("comms-console-permission-denied"), uid, message.Session);
@@ -306,8 +313,12 @@ namespace Content.Server.Communications
private void OnRecallShuttleMessage(EntityUid uid, CommunicationsConsoleComponent comp, CommunicationsConsoleRecallEmergencyShuttleMessage message)
{
if (!CanCallOrRecall(comp)) return;
if (message.Session.AttachedEntity is not { Valid: true } mob) return;
if (!CanCallOrRecall(comp))
return;
if (message.Session.AttachedEntity is not { Valid: true } mob)
return;
if (!CanUse(mob, uid))
{
_popupSystem.PopupEntity(Loc.GetString("comms-console-permission-denied"), uid, message.Session);

View File

@@ -24,7 +24,5 @@ namespace Content.Server.Crayon
[ViewVariables(VVAccess.ReadWrite)]
[DataField("deleteEmpty")]
public bool DeleteEmpty = true;
[ViewVariables] public PlayerBoundUserInterface? UserInterface => Owner.GetUIOrNull(CrayonUiKey.Key);
}
}

View File

@@ -74,7 +74,8 @@ public sealed class CrayonSystem : SharedCrayonSystem
// Decrease "Ammo"
component.Charges--;
Dirty(component);
Dirty(uid, component);
_adminLogger.Add(LogType.CrayonDraw, LogImpact.Low, $"{EntityManager.ToPrettyString(args.User):user} drew a {component.Color:color} {component.SelectedState}");
args.Handled = true;
@@ -89,17 +90,16 @@ public sealed class CrayonSystem : SharedCrayonSystem
return;
if (!TryComp<ActorComponent>(args.User, out var actor) ||
component.UserInterface == null)
!_uiSystem.TryGetUi(uid, SharedCrayonComponent.CrayonUiKey.Key, out var ui))
{
return;
}
_uiSystem.ToggleUi(component.UserInterface, actor.PlayerSession);
if (component.UserInterface?.SubscribedSessions.Contains(actor.PlayerSession) == true)
_uiSystem.ToggleUi(ui, actor.PlayerSession);
if (ui.SubscribedSessions.Contains(actor.PlayerSession))
{
// Tell the user interface the selected stuff
_uiSystem.SetUiState(component.UserInterface, new CrayonBoundUserInterfaceState(component.SelectedState, component.SelectableColor, component.Color));
_uiSystem.SetUiState(ui, new CrayonBoundUserInterfaceState(component.SelectedState, component.SelectableColor, component.Color));
}
args.Handled = true;
@@ -108,22 +108,22 @@ public sealed class CrayonSystem : SharedCrayonSystem
private void OnCrayonBoundUI(EntityUid uid, CrayonComponent component, CrayonSelectMessage args)
{
// Check if the selected state is valid
if (!_prototypeManager.TryIndex<DecalPrototype>(args.State, out var prototype) || !prototype.Tags.Contains("crayon")) return;
if (!_prototypeManager.TryIndex<DecalPrototype>(args.State, out var prototype) || !prototype.Tags.Contains("crayon"))
return;
component.SelectedState = args.State;
Dirty(component);
Dirty(uid, component);
}
private void OnCrayonBoundUIColor(EntityUid uid, CrayonComponent component, CrayonColorMessage args)
{
// you still need to ensure that the given color is a valid color
if (component.SelectableColor && args.Color != component.Color)
{
component.Color = args.Color;
if (!component.SelectableColor || args.Color == component.Color)
return;
Dirty(component);
}
component.Color = args.Color;
Dirty(uid, component);
}
@@ -134,7 +134,7 @@ public sealed class CrayonSystem : SharedCrayonSystem
// Get the first one from the catalog and set it as default
var decal = _prototypeManager.EnumeratePrototypes<DecalPrototype>().FirstOrDefault(x => x.Tags.Contains("crayon"));
component.SelectedState = decal?.ID ?? string.Empty;
Dirty(component);
Dirty(uid, component);
}
private void OnCrayonDropped(EntityUid uid, CrayonComponent component, DroppedEvent args)

View File

@@ -20,8 +20,6 @@ public sealed partial class InstrumentComponent : SharedInstrumentComponent
public IPlayerSession? InstrumentPlayer =>
_entMan.GetComponentOrNull<ActivatableUIComponent>(Owner)?.CurrentSingleUser
?? _entMan.GetComponentOrNull<ActorComponent>(Owner)?.PlayerSession;
[ViewVariables] public PlayerBoundUserInterface? UserInterface => Owner.GetUIOrNull(InstrumentUiKey.Key);
}
[RegisterComponent]

View File

@@ -5,7 +5,6 @@ using Content.Server.Stunnable;
using Content.Shared.Administration;
using Content.Shared.Instruments;
using Content.Shared.Instruments.UI;
using Content.Shared.Interaction;
using Content.Shared.Physics;
using Content.Shared.Popups;
using JetBrains.Annotations;
@@ -17,7 +16,6 @@ using Robust.Shared.Configuration;
using Robust.Shared.Console;
using Robust.Shared.GameStates;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Server.Instruments;
@@ -435,9 +433,7 @@ public sealed partial class InstrumentSystem : SharedInstrumentSystem
// Just in case
Clean(uid);
if (instrument.UserInterface is not null)
_bui.CloseAll(instrument.UserInterface);
_bui.TryCloseAll(uid, InstrumentUiKey.Key);
}
instrument.Timer += frameTime;

View File

@@ -17,8 +17,6 @@ namespace Content.Server.Medical.Components
[DataField("scanDelay")]
public float ScanDelay = 0.8f;
public PlayerBoundUserInterface? UserInterface => Owner.GetUIOrNull(HealthAnalyzerUiKey.Key);
/// <summary>
/// Sound played on scanning begin
/// </summary>

View File

@@ -51,12 +51,12 @@ namespace Content.Server.Medical
args.Handled = true;
}
private void OpenUserInterface(EntityUid user, HealthAnalyzerComponent healthAnalyzer)
private void OpenUserInterface(EntityUid user, EntityUid analyzer)
{
if (!TryComp<ActorComponent>(user, out var actor) || healthAnalyzer.UserInterface == null)
if (!TryComp<ActorComponent>(user, out var actor) || !_uiSystem.TryGetUi(analyzer, HealthAnalyzerUiKey.Key, out var ui))
return;
_uiSystem.OpenUi(healthAnalyzer.UserInterface ,actor.PlayerSession);
_uiSystem.OpenUi(ui ,actor.PlayerSession);
}
public void UpdateScannedUser(EntityUid uid, EntityUid user, EntityUid? target, HealthAnalyzerComponent? healthAnalyzer)
@@ -64,7 +64,7 @@ namespace Content.Server.Medical
if (!Resolve(uid, ref healthAnalyzer))
return;
if (target == null || healthAnalyzer.UserInterface == null)
if (target == null || !_uiSystem.TryGetUi(uid, HealthAnalyzerUiKey.Key, out var ui))
return;
if (!HasComp<DamageableComponent>(target))
@@ -73,9 +73,9 @@ namespace Content.Server.Medical
TryComp<TemperatureComponent>(target, out var temp);
TryComp<BloodstreamComponent>(target, out var bloodstream);
OpenUserInterface(user, healthAnalyzer);
OpenUserInterface(user, uid);
_uiSystem.SendUiMessage(healthAnalyzer.UserInterface, new HealthAnalyzerScannedUserMessage(GetNetEntity(target), temp != null ? temp.CurrentTemperature : float.NaN,
_uiSystem.SendUiMessage(ui, new HealthAnalyzerScannedUserMessage(GetNetEntity(target), temp != null ? temp.CurrentTemperature : float.NaN,
bloodstream != null ? bloodstream.BloodSolution.FillFraction : float.NaN));
}
}

View File

@@ -11,8 +11,6 @@ namespace Content.Server.UserInterface
[ViewVariables]
public Enum? Key { get; set; }
[ViewVariables] public PlayerBoundUserInterface? UserInterface => (Key != null) ? Owner.GetUIOrNull(Key) : null;
[ViewVariables(VVAccess.ReadWrite)]
[DataField]
public bool InHandsOnly { get; set; } = false;

View File

@@ -76,7 +76,7 @@ public sealed partial class ActivatableUISystem : EntitySystem
return;
ActivationVerb verb = new();
verb.Act = () => InteractUI(args.User, component);
verb.Act = () => InteractUI(args.User, uid, component);
verb.Text = Loc.GetString(component.VerbText);
// TODO VERBS add "open UI" icon?
args.Verbs.Add(verb);
@@ -84,16 +84,24 @@ public sealed partial class ActivatableUISystem : EntitySystem
private void OnActivate(EntityUid uid, ActivatableUIComponent component, ActivateInWorldEvent args)
{
if (args.Handled) return;
if (component.InHandsOnly) return;
args.Handled = InteractUI(args.User, component);
if (args.Handled)
return;
if (component.InHandsOnly)
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;
args.Handled = InteractUI(args.User, component);
if (args.Handled)
return;
if (component.rightClickOnly)
return;
args.Handled = InteractUI(args.User, uid, component);
}
private void OnParentChanged(EntityUid uid, ActivatableUIComponent aui, ref EntParentChangedMessage args)
@@ -103,53 +111,64 @@ public sealed partial class ActivatableUISystem : EntitySystem
private void OnUIClose(EntityUid uid, ActivatableUIComponent component, BoundUIClosedEvent args)
{
if (args.Session != component.CurrentSingleUser) return;
if (args.UiKey != component.Key) return;
if (args.Session != component.CurrentSingleUser)
return;
if (!Equals(args.UiKey, component.Key))
return;
SetCurrentSingleUser(uid, null, component);
}
private bool InteractUI(EntityUid user, ActivatableUIComponent aui)
private bool InteractUI(EntityUid user, EntityUid uiEntity, ActivatableUIComponent aui)
{
if (!_blockerSystem.CanInteract(user, aui.Owner) && (!aui.AllowSpectator || !HasComp<GhostComponent>(user)))
if (!_blockerSystem.CanInteract(user, uiEntity) && (!aui.AllowSpectator || !HasComp<GhostComponent>(user)))
return false;
if (aui.RequireHands && !HasComp<HandsComponent>(user))
return false;
if (!EntityManager.TryGetComponent(user, out ActorComponent? actor)) return false;
if (!EntityManager.TryGetComponent(user, out ActorComponent? actor))
return false;
if (aui.AdminOnly && !_adminManager.IsAdmin(actor.PlayerSession)) return false;
if (aui.AdminOnly && !_adminManager.IsAdmin(actor.PlayerSession))
return false;
var ui = aui.UserInterface;
if (ui == null) return false;
if (aui.Key == null)
return false;
if (!_uiSystem.TryGetUi(uiEntity, aui.Key, out var ui))
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 (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, aui.Owner);
RaiseLocalEvent(user, uae, false);
RaiseLocalEvent((aui).Owner, oae, false);
if (oae.Cancelled || uae.Cancelled) return false;
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((aui).Owner, bae, false);
RaiseLocalEvent(uiEntity, bae);
SetCurrentSingleUser((aui).Owner, actor.PlayerSession, aui);
SetCurrentSingleUser(uiEntity, actor.PlayerSession, aui);
_uiSystem.ToggleUi(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((aui).Owner, aae, false);
RaiseLocalEvent(uiEntity, aae);
return true;
}
@@ -163,24 +182,28 @@ public sealed partial class ActivatableUISystem : EntitySystem
aui.CurrentSingleUser = v;
RaiseLocalEvent(uid, new ActivatableUIPlayerChangedEvent(), false);
RaiseLocalEvent(uid, new ActivatableUIPlayerChangedEvent());
}
public void CloseAll(EntityUid uid, ActivatableUIComponent? aui = null)
{
if (!Resolve(uid, ref aui, false))
return;
if (aui.UserInterface is null)
if (aui.Key == null || !_uiSystem.TryGetUi(uid, aui.Key, out var ui))
return;
_uiSystem.CloseAll(aui.UserInterface);
_uiSystem.CloseAll(ui);
}
private void OnHandDeselected(EntityUid uid, ActivatableUIComponent? aui, HandDeselectedEvent args)
{
if (!Resolve(uid, ref aui, false)) return;
if (!Resolve(uid, ref aui, false))
return;
if (!aui.CloseOnHandDeselect)
return;
CloseAll(uid, aui);
}
}

View File

@@ -1,5 +1,4 @@
using Content.Server.Actions;
using Content.Shared.Actions;
using Content.Shared.UserInterface;
using Robust.Server.GameObjects;
@@ -36,19 +35,19 @@ public sealed class IntrinsicUISystem : EntitySystem
if (key is null)
{
Logger.ErrorS("bui", $"Entity {ToPrettyString(uid)} has an invalid intrinsic UI.");
Log.Error($"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)}");
Log.Error($"Couldn't get UI {key} on {ToPrettyString(uid)}");
return false;
}
var attempt = new IntrinsicUIOpenAttemptEvent(uid, key);
RaiseLocalEvent(uid, attempt, false);
RaiseLocalEvent(uid, attempt);
if (attempt.Cancelled)
return false;
@@ -61,7 +60,7 @@ public sealed class IntrinsicUISystem : EntitySystem
if (!Resolve(uid, ref component))
return null;
return key is null ? null : uid.GetUIOrNull(key);
return key is null ? null : _uiSystem.GetUiOrNull(uid, key);
}
}

View File

@@ -1,13 +0,0 @@
using Robust.Server.GameObjects;
namespace Content.Server.UserInterface
{
public static class UserInterfaceHelpers
{
[Obsolete("Use UserInterfaceSystem")]
public static PlayerBoundUserInterface? GetUIOrNull(this EntityUid entity, Enum uiKey)
{
return IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<UserInterfaceSystem>().GetUiOrNull(entity, uiKey);
}
}
}