Files
tbd-station-14/Content.Server/SensorMonitoring/SensorMonitoringConsoleSystem.UI.cs
Pieter-Jan Briers 123a4147de BUI bugfixes / improvements (#23881)
* Fix ActivatableUIRequiresPowerCellComponent stopping power draw when one of two people closes the UI.

Also fixes it to check UiKey properly.

* Remove unnecessary CrewManifestViewer on PDAs

This is for a pop-up crew manifest UI, which the PDA doesn't use.

* Fix BoundUIClosedEvents that didn't check UI key/not correctly at least.

Uses the new helper method in engine.

* Fix drone (cargo shuttle) pilot console UI breaking if two people open it and one person closes it.

* Fixes for disposal router/tagger UI.

Code was badly copy pasted without changing identifiers, never worked.

Also cleaned up some of the logic (text trimming, sounds).

Also removed the "refuse to work if you have something in your active hand" check like why.

* Avoid running most ActivatableUIComponent logic when closing a UI via toggle

Activating the UI while it's already open closes it via toggle. Except it still ran 99% of the "attempting to open" logic which makes no sense.

This probably fixes a bug or some other dumb behavior somewhere.

* Bitch
2024-01-14 18:18:39 +11:00

140 lines
4.7 KiB
C#

using Content.Server.DeviceNetwork.Components;
using Content.Shared.SensorMonitoring;
using Robust.Shared.Collections;
using ConsoleUIState = Content.Shared.SensorMonitoring.SensorMonitoringConsoleBoundInterfaceState;
using IncrementalUIState = Content.Shared.SensorMonitoring.SensorMonitoringIncrementalUpdate;
namespace Content.Server.SensorMonitoring;
public sealed partial class SensorMonitoringConsoleSystem
{
private void InitUI()
{
Subs.BuiEvents<SensorMonitoringConsoleComponent>(SensorMonitoringConsoleUiKey.Key, subs =>
{
subs.Event<BoundUIClosedEvent>(ConsoleUIClosed);
});
}
private void UpdateConsoleUI(EntityUid uid, SensorMonitoringConsoleComponent comp)
{
if (!_userInterface.TryGetUi(uid, SensorMonitoringConsoleUiKey.Key, out var ui))
return;
if (ui.SubscribedSessions.Count == 0)
return;
ConsoleUIState? fullState = null;
SensorMonitoringIncrementalUpdate? incrementalUpdate = null;
foreach (var session in ui.SubscribedSessions)
{
if (comp.InitialUIStateSent.Contains(session))
{
incrementalUpdate ??= CalculateIncrementalUpdate();
_userInterface.TrySendUiMessage(ui, incrementalUpdate, session);
}
else
{
fullState ??= CalculateFullState();
_userInterface.SetUiState(ui, fullState, session);
comp.InitialUIStateSent.Add(session);
}
}
comp.LastUIUpdate = _gameTiming.CurTime;
comp.RemovedSensors.Clear();
ConsoleUIState CalculateFullState()
{
var sensors = new ValueList<ConsoleUIState.SensorData>();
var streams = new ValueList<ConsoleUIState.SensorStream>();
foreach (var (ent, data) in comp.Sensors)
{
streams.Clear();
var name = MetaData(ent).EntityName;
var address = Comp<DeviceNetworkComponent>(ent).Address;
foreach (var (streamName, stream) in data.Streams)
{
streams.Add(new ConsoleUIState.SensorStream
{
NetId = stream.NetId,
Name = streamName,
Unit = stream.Unit,
Samples = stream.Samples.ToArray()
});
}
sensors.Add(new ConsoleUIState.SensorData
{
NetId = data.NetId,
Name = name,
Address = address,
DeviceType = data.DeviceType,
Streams = streams.ToArray()
});
}
return new ConsoleUIState
{
RetentionTime = comp.RetentionTime,
Sensors = sensors.ToArray()
};
}
SensorMonitoringIncrementalUpdate CalculateIncrementalUpdate()
{
var sensors = new ValueList<IncrementalUIState.SensorData>();
var streams = new ValueList<IncrementalUIState.SensorStream>();
var samples = new ValueList<SensorSample>();
foreach (var data in comp.Sensors.Values)
{
streams.Clear();
foreach (var stream in data.Streams.Values)
{
samples.Clear();
foreach (var (sampleTime, value) in stream.Samples)
{
if (sampleTime >= comp.LastUIUpdate)
samples.Add(new SensorSample(sampleTime - comp.LastUIUpdate, value));
}
streams.Add(new IncrementalUIState.SensorStream
{
NetId = stream.NetId,
Unit = stream.Unit,
Samples = samples.ToArray()
});
}
sensors.Add(new IncrementalUIState.SensorData { NetId = data.NetId, Streams = streams.ToArray() });
}
return new IncrementalUIState
{
RelTime = comp.LastUIUpdate,
RemovedSensors = comp.RemovedSensors.ToArray(),
Sensors = sensors.ToArray(),
};
}
}
private static void ConsoleUIClosed(
EntityUid uid,
SensorMonitoringConsoleComponent component,
BoundUIClosedEvent args)
{
if (!args.UiKey.Equals(SensorMonitoringConsoleUiKey.Key))
return;
if (args.Session is not { } player)
return;
component.InitialUIStateSent.Remove(player);
}
}