CatStation/Content.Client/SurveillanceCamera/UI/SurveillanceCameraMonitorWindow.xaml.cs
Rouden cc2c14f8e4
Device Network refactor part 1: API improvements (#44346)
* Refactor DeviceNetwork

* Singleton

* NetworkPayloads refactor

* Reorganize DeviceNetworkSystem

* Surveillance cameras

* Fix code

* Fix build

* Fix test fail by rewriting entire singleton logic

* Properly predict examine

* Update DeviceNetworkSystem.API.cs

* Update EntityTest.cs

* Update DeviceNetworkSystem.cs

* Alright I'll do this expensive check fine

* Fix disposals debug assert

* Implement device network static events support

* Use frozen dictionaries for performance

* Update SharedDeviceNetworkSystem.Payloads.cs

* Parallel payload handling support

* Fix some spagetti-code

* Optimize NetworkPayload cancellation

* Parallel payloads are now cancellable

* Fix screens

* Fix robotics console

* Allow new methods for shared

* Mail fix

* Turret fix

* Fix network system method usage + signal and borg fix

* finally fix atmos devices OH MY GOOOD

* Remove stupid parallelism from fax

* Fix surveiollance cameras

* Working on a single thing for 2 days straight award

* Fix the test?

* Update units.yml

* Add IDevice interface because why not

* Add more device network suffixes

* Fix camera router failing test

* yaml linter peak

* Improve docs for DeviceNetworkHandlers

* fix build

* Update CartridgeLoaderSystem.Relay.cs

* Remove parallel system and EntitySystem post init

* Add more docs and also events

* Remove unnecessary caching

* Update DevicePayloadComponentSystem.cs

* Back to entity events

* Implement DeviceData

* Remove serializable from payloads

* Update DeviceData.cs

* Add a prototype for a singleton

* Fix tests

* Fix air alarms

* Update DeviceNetworkSystem.cs

* Fix cameras

* Make camera router event-less

* Fix atmos sync payloads, add more docs

* Remove singleton + instant packets

* oops remove public access

* Fix tests?

* Why the hell overload effect makes puddle sparks

* Apparently it was redundant

* Update gates.yml

* Finish documentation

* Review

* Mark relay properties as obsolete

* Struct payloads support (#blazinglyfastmemorysafe)

* Update DeviceNetworkTest.cs

* Make device structs stuff better

* Move address prefix to DeviceNetworkComponent

* Locale fixes

* Fix tests

* Doesn't know boolean logic award

* Update AtmosAlarmableSystem.cs

* Fix address predicted examine

* Partial fix to air alarmables

* Fix deployable turrets UI

* Fix firelocks, maybe

* Apparently Activatable UI was enough lol

* Fix surveillance cameras

* Rename `QueuePacket` into `SendPacket`

* Simplify DeviceData as much as possible

* Update DeviceData.cs

* Update FaxMachineComponent.cs

* Remove DeviceData because its stupid

* Update ScreenSystem.cs

* Fix build (camera map broken still)

* Fix map

* fix usings

* Review changes

* Build fix
2026-08-31 19:42:56 +00:00

218 lines
7.8 KiB
C#

using System.Linq;
using Content.Client.Resources;
using Content.Shared.DeviceNetwork;
using Content.Shared.SurveillanceCamera.Components;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Graphics;
using Robust.Shared.Prototypes;
namespace Content.Client.SurveillanceCamera.UI;
[GenerateTypedNameReferences]
public sealed partial class SurveillanceCameraMonitorWindow : DefaultWindow
{
private static readonly ProtoId<ShaderPrototype> CameraStaticShader = "CameraStatic";
[Dependency] private IPrototypeManager _prototypeManager = default!;
[Dependency] private IResourceCache _resourceCache = default!;
[Dependency] private IEntityManager _entityManager = default!;
[Dependency] private IEntityNetworkManager _entityNetManager = default!;
/// <summary>
/// Triggered when a camera is selected.
/// First parameter contains the camera's address.
/// Second optional parameter contains a subnet - if possible, the monitor will switch to this subnet.
/// </summary>
public event Action<string, ProtoId<DeviceFrequencyPrototype>?>? CameraSelected;
public event Action<ProtoId<DeviceFrequencyPrototype>>? SubnetOpened;
public event Action? CameraRefresh;
public event Action? SubnetRefresh;
public event Action? CameraSwitchTimer;
public event Action? CameraDisconnect;
private string _currentAddress = string.Empty;
private bool _isSwitching;
private readonly FixedEye _defaultEye = new();
private readonly Dictionary<ProtoId<DeviceFrequencyPrototype>, int> _subnetMap = new();
private EntityUid? _mapUid;
public SurveillanceCameraMonitorWindow()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
// This could be done better. I don't want to deal with stylesheets at the moment.
var texture = _resourceCache.GetTexture("/Textures/Interface/Nano/square_black.png");
var shader = _prototypeManager.Index(CameraStaticShader).Instance().Duplicate();
CameraView.ViewportSize = new Vector2i(500, 500);
CameraView.Eye = _defaultEye; // sure
CameraViewBackground.Stretch = TextureRect.StretchMode.Scale;
CameraViewBackground.Texture = texture;
CameraViewBackground.ShaderOverride = shader;
SubnetList.OnItemSelected += OnSubnetListSelect;
SubnetSelector.OnItemSelected += args =>
{
// piss
SubnetOpened?.Invoke((ProtoId<DeviceFrequencyPrototype>)args.Button.GetItemMetadata(args.Id)!);
};
SubnetRefreshButton.OnPressed += _ => SubnetRefresh?.Invoke();
SubnetRefreshButtonMap.OnPressed += _ => SubnetRefresh?.Invoke();
CameraRefreshButton.OnPressed += _ => CameraRefresh?.Invoke();
CameraDisconnectButton.OnPressed += _ => CameraDisconnect?.Invoke();
CameraMap.EnableCameraSelection = true;
CameraMap.CameraSelected += OnCameraMapSelected;
}
// The UI class should get the eye from the entity, and then
// pass it here so that the UI can change its view.
public void UpdateState(IEye? eye,
HashSet<ProtoId<DeviceFrequencyPrototype>> subnets,
string activeAddress,
ProtoId<DeviceFrequencyPrototype>? activeSubnet,
Dictionary<string, string> cameras)
{
CameraMap.SetActiveCameraAddress(activeAddress);
CameraMap.SetAvailableSubnets(subnets);
_currentAddress = activeAddress;
SetCameraView(eye);
if (subnets.Count == 0)
{
SubnetSelector.AddItem(Loc.GetString("surveillance-camera-monitor-ui-no-subnets"));
SubnetSelector.Disabled = true;
return;
}
if (SubnetSelector.Disabled && subnets.Count != 0)
{
SubnetSelector.Clear();
SubnetSelector.Disabled = false;
}
// That way, we have *a* subnet selected if this is ever opened.
if (activeSubnet is null)
{
SubnetOpened!(subnets.First());
return;
}
// if the subnet count is unequal, that means
// we have to rebuild the subnet selector
if (SubnetSelector.ItemCount != subnets.Count)
{
SubnetSelector.Clear();
_subnetMap.Clear();
foreach (var subnet in subnets)
{
var id = AddSubnet(subnet);
_subnetMap.Add(subnet, id);
}
}
if (_subnetMap.TryGetValue(activeSubnet.Value, out var subnetId))
SubnetSelector.Select(subnetId);
PopulateCameraList(cameras);
}
private void PopulateCameraList(Dictionary<string, string> cameras)
{
var entries = cameras.Select(i => new ItemList.Item(SubnetList)
{
Text = $"{i.Value}: {i.Key}",
Metadata = i.Key
}).ToList();
entries.Sort((a, b) => string.Compare(a.Text, b.Text, StringComparison.Ordinal));
SubnetList.SetItems(entries, (a, b) => string.Compare(a.Text, b.Text));
}
private void SetCameraView(IEye? eye)
{
var eyeChanged = eye != CameraView.Eye || CameraView.Eye == null;
CameraView.Eye = eye ?? _defaultEye;
CameraView.Visible = !eyeChanged && !_isSwitching;
CameraDisconnectButton.Disabled = eye == null;
if (eye != null)
{
if (!eyeChanged)
{
return;
}
_isSwitching = true;
CameraViewBackground.Visible = true;
CameraStatus.Text = Loc.GetString("surveillance-camera-monitor-ui-status",
("status", Loc.GetString("surveillance-camera-monitor-ui-status-connecting")),
("address", _currentAddress));
CameraSwitchTimer!();
}
else
{
CameraViewBackground.Visible = true;
CameraStatus.Text = Loc.GetString("surveillance-camera-monitor-ui-status-disconnected");
}
}
public void OnSwitchTimerComplete()
{
_isSwitching = false;
CameraView.Visible = CameraView.Eye != _defaultEye;
CameraViewBackground.Visible = CameraView.Eye == _defaultEye;
CameraStatus.Text = Loc.GetString("surveillance-camera-monitor-ui-status",
("status", Loc.GetString("surveillance-camera-monitor-ui-status-connected")),
("address", _currentAddress));
}
private int AddSubnet(ProtoId<DeviceFrequencyPrototype> subnet)
{
var name = subnet;
if (_prototypeManager.TryIndex(subnet, out var frequency)
&& frequency.Name != null)
{
name = Loc.GetString(frequency.Name);
}
SubnetSelector.AddItem(name);
SubnetSelector.SetItemMetadata(SubnetSelector.ItemCount - 1, subnet);
return SubnetSelector.ItemCount - 1;
}
private void OnSubnetListSelect(ItemList.ItemListSelectedEventArgs args)
{
CameraSelected!((string)SubnetList[args.ItemIndex].Metadata!, null);
}
public void SetMap(EntityUid mapUid)
{
CameraMap.MapUid = _mapUid = mapUid;
}
private void OnCameraMapSelected(NetEntity netEntity)
{
if (_mapUid is null || !_entityManager.TryGetComponent<SurveillanceCameraMapComponent>(_mapUid.Value, out var mapComp))
return;
if (!mapComp.Cameras.TryGetValue(netEntity, out var marker) || !marker.Active)
return;
if (!string.IsNullOrEmpty(marker.Address))
CameraSelected?.Invoke(marker.Address, marker.Subnet);
else
_entityNetManager.SendSystemNetworkMessage(new RequestCameraMarkerUpdateMessage(netEntity));
}
}