Files
tbd-station-14/Content.Server/StationRecords/Systems/GeneralStationRecordConsoleSystem.cs
metalgearsloth 5896e68752 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
2024-04-26 18:16:24 +10:00

86 lines
3.4 KiB
C#

using System.Linq;
using Content.Server.Station.Systems;
using Content.Server.StationRecords.Components;
using Content.Shared.StationRecords;
using Robust.Server.GameObjects;
namespace Content.Server.StationRecords.Systems;
public sealed class GeneralStationRecordConsoleSystem : EntitySystem
{
[Dependency] private readonly UserInterfaceSystem _ui = default!;
[Dependency] private readonly StationSystem _station = default!;
[Dependency] private readonly StationRecordsSystem _stationRecords = default!;
public override void Initialize()
{
SubscribeLocalEvent<GeneralStationRecordConsoleComponent, RecordModifiedEvent>(UpdateUserInterface);
SubscribeLocalEvent<GeneralStationRecordConsoleComponent, AfterGeneralRecordCreatedEvent>(UpdateUserInterface);
SubscribeLocalEvent<GeneralStationRecordConsoleComponent, RecordRemovedEvent>(UpdateUserInterface);
Subs.BuiEvents<GeneralStationRecordConsoleComponent>(GeneralStationRecordConsoleKey.Key, subs =>
{
subs.Event<BoundUIOpenedEvent>(UpdateUserInterface);
subs.Event<SelectStationRecord>(OnKeySelected);
subs.Event<SetStationRecordFilter>(OnFiltersChanged);
});
}
private void UpdateUserInterface<T>(Entity<GeneralStationRecordConsoleComponent> ent, ref T args)
{
UpdateUserInterface(ent);
}
// TODO: instead of copy paste shitcode for each record console, have a shared records console comp they all use
// then have this somehow play nicely with creating ui state
// if that gets done put it in StationRecordsSystem console helpers section :)
private void OnKeySelected(Entity<GeneralStationRecordConsoleComponent> ent, ref SelectStationRecord msg)
{
ent.Comp.ActiveKey = msg.SelectedKey;
UpdateUserInterface(ent);
}
private void OnFiltersChanged(Entity<GeneralStationRecordConsoleComponent> ent, ref SetStationRecordFilter msg)
{
if (ent.Comp.Filter == null ||
ent.Comp.Filter.Type != msg.Type || ent.Comp.Filter.Value != msg.Value)
{
ent.Comp.Filter = new StationRecordsFilter(msg.Type, msg.Value);
UpdateUserInterface(ent);
}
}
private void UpdateUserInterface(Entity<GeneralStationRecordConsoleComponent> ent)
{
var (uid, console) = ent;
var owningStation = _station.GetOwningStation(uid);
if (!TryComp<StationRecordsComponent>(owningStation, out var stationRecords))
{
_ui.SetUiState(uid, GeneralStationRecordConsoleKey.Key, new GeneralStationRecordConsoleState());
return;
}
var listing = _stationRecords.BuildListing((owningStation.Value, stationRecords), console.Filter);
switch (listing.Count)
{
case 0:
_ui.SetUiState(uid, GeneralStationRecordConsoleKey.Key, new GeneralStationRecordConsoleState());
return;
case 1:
console.ActiveKey = listing.Keys.First();
break;
}
if (console.ActiveKey is not { } id)
return;
var key = new StationRecordKey(id, owningStation.Value);
_stationRecords.TryGetRecord<GeneralStationRecord>(key, out var record, stationRecords);
GeneralStationRecordConsoleState newState = new(id, record, listing, console.Filter);
_ui.SetUiState(uid, GeneralStationRecordConsoleKey.Key, newState);
}
}