Cleaner BoundUserInterfaces (#17736)

This commit is contained in:
TemporalOroboros
2023-07-08 09:02:17 -07:00
committed by GitHub
parent 55b4fb1649
commit 3ac4cf85db
137 changed files with 1069 additions and 972 deletions

View File

@@ -37,6 +37,7 @@ namespace Content.Server.Communications
[Dependency] private readonly StationSystem _stationSystem = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
private const int MaxMessageLength = 256;
private const int MaxMessageNewlines = 2;
@@ -46,7 +47,7 @@ namespace Content.Server.Communications
{
// All events that refresh the BUI
SubscribeLocalEvent<AlertLevelChangedEvent>(OnAlertLevelChanged);
SubscribeLocalEvent<CommunicationsConsoleComponent, ComponentInit>((_, comp, _) => UpdateCommsConsoleInterface(comp));
SubscribeLocalEvent<CommunicationsConsoleComponent, ComponentInit>((uid, comp, _) => UpdateCommsConsoleInterface(uid, comp));
SubscribeLocalEvent<RoundEndSystemChangedEvent>(_ => OnGenericBroadcastEvent());
SubscribeLocalEvent<AlertLevelDelayFinishedEvent>(_ => OnGenericBroadcastEvent());
@@ -59,7 +60,8 @@ namespace Content.Server.Communications
public override void Update(float frameTime)
{
foreach (var comp in EntityQuery<CommunicationsConsoleComponent>())
var query = EntityQueryEnumerator<CommunicationsConsoleComponent>();
while (query.MoveNext(out var uid, out var comp))
{
// TODO refresh the UI in a less horrible way
if (comp.AnnouncementCooldownRemaining >= 0f)
@@ -74,8 +76,8 @@ namespace Content.Server.Communications
comp.UIUpdateAccumulator -= UIUpdateInterval;
if (comp.UserInterface is {} ui && ui.SubscribedSessions.Count > 0)
UpdateCommsConsoleInterface(comp);
if (comp.UserInterface is { } ui && ui.SubscribedSessions.Count > 0)
UpdateCommsConsoleInterface(uid, comp);
}
base.Update(frameTime);
@@ -86,9 +88,10 @@ namespace Content.Server.Communications
/// </summary>
private void OnGenericBroadcastEvent()
{
foreach (var comp in EntityQuery<CommunicationsConsoleComponent>())
var query = EntityQueryEnumerator<CommunicationsConsoleComponent>();
while (query.MoveNext(out var uid, out var comp))
{
UpdateCommsConsoleInterface(comp);
UpdateCommsConsoleInterface(uid, comp);
}
}
@@ -98,13 +101,12 @@ namespace Content.Server.Communications
/// <param name="args">Alert level changed event arguments</param>
private void OnAlertLevelChanged(AlertLevelChangedEvent args)
{
foreach (var comp in EntityQuery<CommunicationsConsoleComponent>(true))
var query = EntityQueryEnumerator<CommunicationsConsoleComponent>();
while (query.MoveNext(out var uid, out var comp))
{
var entStation = _stationSystem.GetOwningStation(comp.Owner);
var entStation = _stationSystem.GetOwningStation(uid);
if (args.Station == entStation)
{
UpdateCommsConsoleInterface(comp);
}
UpdateCommsConsoleInterface(uid, comp);
}
}
@@ -113,9 +115,10 @@ namespace Content.Server.Communications
/// </summary>
public void UpdateCommsConsoleInterface()
{
foreach (var comp in EntityQuery<CommunicationsConsoleComponent>())
var query = EntityQueryEnumerator<CommunicationsConsoleComponent>();
while (query.MoveNext(out var uid, out var comp))
{
UpdateCommsConsoleInterface(comp);
UpdateCommsConsoleInterface(uid, comp);
}
}
@@ -123,10 +126,8 @@ namespace Content.Server.Communications
/// Updates the UI for a particular comms console.
/// </summary>
/// <param name="comp"></param>
public void UpdateCommsConsoleInterface(CommunicationsConsoleComponent comp)
public void UpdateCommsConsoleInterface(EntityUid uid, CommunicationsConsoleComponent comp)
{
var uid = comp.Owner;
var stationUid = _stationSystem.GetOwningStation(uid);
List<string>? levels = null;
string currentLevel = default!;
@@ -154,19 +155,18 @@ namespace Content.Server.Communications
}
}
comp.UserInterface?.SetState(
new CommunicationsConsoleInterfaceState(
if (comp.UserInterface is not null)
UserInterfaceSystem.SetUiState(comp.UserInterface, new CommunicationsConsoleInterfaceState(
CanAnnounce(comp),
CanCallOrRecall(comp),
levels,
currentLevel,
currentDelay,
_roundEndSystem.ExpectedCountdownEnd
)
);
));
}
private bool CanAnnounce(CommunicationsConsoleComponent comp)
private static bool CanAnnounce(CommunicationsConsoleComponent comp)
{
return comp.AnnouncementCooldownRemaining <= 0f;
}
@@ -207,7 +207,7 @@ 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);
@@ -224,7 +224,8 @@ namespace Content.Server.Communications
private void OnAnnounceMessage(EntityUid uid, CommunicationsConsoleComponent comp,
CommunicationsConsoleAnnounceMessage message)
{
var msgChars = (message.Message.Length <= MaxMessageLength ? message.Message.Trim() : $"{message.Message.Trim().Substring(0, MaxMessageLength)}...").ToCharArray();
var msgWords = message.Message.Trim();
var msgChars = (msgWords.Length <= MaxMessageLength ? msgWords : $"{msgWords[0..MaxMessageLength]}...").ToCharArray();
var newlines = 0;
for (var i = 0; i < msgChars.Length; i++)
@@ -240,7 +241,7 @@ namespace Content.Server.Communications
var msg = new string(msgChars);
var author = Loc.GetString("comms-console-announcement-unknown-sender");
if (message.Session.AttachedEntity is {Valid: true} mob)
if (message.Session.AttachedEntity is { Valid: true } mob)
{
if (!CanAnnounce(comp))
{
@@ -260,7 +261,7 @@ namespace Content.Server.Communications
}
comp.AnnouncementCooldownRemaining = comp.DelayBetweenAnnouncements;
UpdateCommsConsoleInterface(comp);
UpdateCommsConsoleInterface(uid, comp);
// allow admemes with vv
Loc.TryGetString(comp.AnnouncementDisplayName, out var title);
@@ -285,7 +286,7 @@ 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 (message.Session.AttachedEntity is not { Valid: true } mob) return;
if (!CanUse(mob, uid))
{
_popupSystem.PopupEntity(Loc.GetString("comms-console-permission-denied"), uid, message.Session);
@@ -298,7 +299,7 @@ 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 (message.Session.AttachedEntity is not { Valid: true } mob) return;
if (!CanUse(mob, uid))
{
_popupSystem.PopupEntity(Loc.GetString("comms-console-permission-denied"), uid, message.Session);