mirror of
https://github.com/wega-team/ss14-wega.git
synced 2026-02-14 19:30:01 +01:00
* Camera map * I hope this helps * Review 1 * Review 2 * Review 3 * Review 4 * Review 5 * Colorblind mode support * Review 6 * Change design * Map wire * Logic fix * Fix a terrible mistake * Fix * Fix 2 * Small rename * More fix * Better removal * And another fix * Will it work? * It is literally pointless * some small things
230 lines
7.9 KiB
C#
230 lines
7.9 KiB
C#
using System.Linq;
|
|
using Content.Client.Resources;
|
|
using Content.Client.Viewport;
|
|
using Content.Shared.DeviceNetwork;
|
|
using Content.Shared.SurveillanceCamera;
|
|
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 readonly IPrototypeManager _prototypeManager = default!;
|
|
[Dependency] private readonly IResourceCache _resourceCache = default!;
|
|
[Dependency] private readonly IEntityManager _entityManager = 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, string?>? CameraSelected;
|
|
|
|
public event Action<string>? 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<string, int> _subnetMap = new();
|
|
private EntityUid? _mapUid;
|
|
|
|
private string? SelectedSubnet
|
|
{
|
|
get
|
|
{
|
|
if (SubnetSelector.ItemCount == 0
|
|
|| SubnetSelector.SelectedMetadata == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return (string) SubnetSelector.SelectedMetadata;
|
|
}
|
|
}
|
|
|
|
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((string) 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<string> subnets, string activeAddress, string 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 (string.IsNullOrEmpty(activeSubnet))
|
|
{
|
|
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, 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(string subnet)
|
|
{
|
|
var name = subnet;
|
|
if (_prototypeManager.TryIndex<DeviceFrequencyPrototype>(subnet, out var frequency))
|
|
{
|
|
name = Loc.GetString(frequency.Name ?? subnet);
|
|
}
|
|
|
|
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
|
|
_entityManager.RaisePredictiveEvent(new RequestCameraMarkerUpdateMessage(netEntity));
|
|
}
|
|
}
|