fish-station/Content.Client/SurveillanceCamera/UI/SurveillanceCameraMonitorBoundUi.cs
2024-07-22 04:38:50 +03:00

132 lines
4.1 KiB
C#

using Content.Client.Eye;
using Content.Shared.SurveillanceCamera;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface;
namespace Content.Client.SurveillanceCamera.UI;
public sealed class SurveillanceCameraMonitorBoundUserInterface : BoundUserInterface
{
private readonly EyeLerpingSystem _eyeLerpingSystem;
private readonly SurveillanceCameraMonitorSystem _surveillanceCameraMonitorSystem;
[ViewVariables]
private SurveillanceCameraMonitorWindow? _window;
[ViewVariables]
private EntityUid? _currentCamera;
private readonly IEntityManager _entManager; // Sunrise-edit
public SurveillanceCameraMonitorBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
_eyeLerpingSystem = EntMan.System<EyeLerpingSystem>();
_surveillanceCameraMonitorSystem = EntMan.System<SurveillanceCameraMonitorSystem>();
// Sunrise-start
IoCManager.InjectDependencies(this);
_entManager = IoCManager.Resolve<IEntityManager>();
_eyeLerpingSystem = _entManager.EntitySysManager.GetEntitySystem<EyeLerpingSystem>();
_surveillanceCameraMonitorSystem = _entManager.EntitySysManager.GetEntitySystem<SurveillanceCameraMonitorSystem>();
// Sunrise-end
}
protected override void Open()
{
base.Open();
_window = this.CreateWindow<SurveillanceCameraMonitorWindow>();
_window.CameraSelected += OnCameraSelected;
_window.CameraRefresh += OnCameraRefresh;
_window.SubnetRefresh += OnSubnetRefresh;
_window.CameraSwitchTimer += OnCameraSwitchTimer;
_window.CameraDisconnect += OnCameraDisconnect;
_window.SetEntity(Owner);
}
private void OnCameraSelected(string cameraAddress, string subnetAddress) // Sunrise-edit
{
SendMessage(new SurveillanceCameraMonitorSwitchMessage(cameraAddress, subnetAddress)); // Sunrise-edit
}
private void OnCameraSwitchTimer()
{
_surveillanceCameraMonitorSystem.AddTimer(Owner, _window!.OnSwitchTimerComplete);
}
private void OnCameraRefresh()
{
SendMessage(new SurveillanceCameraRefreshCamerasMessage());
}
private void OnSubnetRefresh()
{
SendMessage(new SurveillanceCameraRefreshSubnetsMessage());
}
private void OnCameraDisconnect()
{
SendMessage(new SurveillanceCameraDisconnectMessage());
}
protected override void UpdateState(BoundUserInterfaceState state)
{
if (_window == null || state is not SurveillanceCameraMonitorUiState cast)
{
return;
}
var active = EntMan.GetEntity(cast.ActiveCamera);
_entManager.TryGetComponent<TransformComponent>(Owner, out var xform); // Sunrise-edit
if (active == null)
{
_window.UpdateState(null, cast.ActiveAddress, cast.ActiveCamera); // Sunrise-edit
if (_currentCamera != null)
{
_surveillanceCameraMonitorSystem.RemoveTimer(Owner);
_eyeLerpingSystem.RemoveEye(_currentCamera.Value);
_currentCamera = null;
}
}
else
{
if (_currentCamera == null)
{
_eyeLerpingSystem.AddEye(active.Value);
_currentCamera = active;
}
else if (_currentCamera != active)
{
_eyeLerpingSystem.RemoveEye(_currentCamera.Value);
_eyeLerpingSystem.AddEye(active.Value);
_currentCamera = active;
}
if (EntMan.TryGetComponent<EyeComponent>(active, out var eye))
{
_window.UpdateState(eye.Eye, cast.ActiveAddress, cast.ActiveCamera); // Sunrise-edit
}
}
_window.ShowCameras(cast.Cameras, xform?.Coordinates); // Sunrise-edit
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (_currentCamera != null)
{
_eyeLerpingSystem.RemoveEye(_currentCamera.Value);
_currentCamera = null;
}
if (disposing)
{
_window?.Dispose();
}
}
}