Crew monitor revisit (#22240)

This commit is contained in:
chromiumboy
2023-12-09 23:38:50 -06:00
committed by GitHub
parent ffb9112dc5
commit b70c0845d0
28 changed files with 1871 additions and 1302 deletions

View File

@@ -1,275 +1,437 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Numerics;
using Content.Client.Pinpointer.UI;
using Content.Client.Stylesheets;
using Content.Client.UserInterface.Controls;
using Content.Shared.Medical.SuitSensor;
using Content.Shared.StatusIcon;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using static Robust.Client.UserInterface.Controls.BoxContainer;
namespace Content.Client.Medical.CrewMonitoring
namespace Content.Client.Medical.CrewMonitoring;
[GenerateTypedNameReferences]
public sealed partial class CrewMonitoringWindow : FancyWindow
{
[GenerateTypedNameReferences]
public sealed partial class CrewMonitoringWindow : FancyWindow
private List<Control> _rowsContent = new();
private readonly IEntityManager _entManager;
private readonly IPrototypeManager _prototypeManager;
private readonly SpriteSystem _spriteSystem;
private NetEntity? _trackedEntity;
private bool _tryToScrollToListFocus;
private Texture? _blipTexture;
public CrewMonitoringWindow(string stationName, EntityUid? mapUid)
{
private List<Control> _rowsContent = new();
private List<(DirectionIcon Icon, Vector2 Position)> _directionIcons = new();
private readonly IEntityManager _entManager;
private readonly IEyeManager _eye;
private EntityUid? _stationUid;
private CrewMonitoringButton? _trackedButton;
RobustXamlLoader.Load(this);
public static int IconSize = 16; // XAML has a `VSeparationOverride` of 20 for each row.
_entManager = IoCManager.Resolve<IEntityManager>();
_prototypeManager = IoCManager.Resolve<IPrototypeManager>();
_spriteSystem = _entManager.System<SpriteSystem>();
public CrewMonitoringWindow(EntityUid? mapUid)
_blipTexture = _spriteSystem.Frame0(new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/NavMap/beveled_circle.png")));
if (_entManager.TryGetComponent<TransformComponent>(mapUid, out var xform))
NavMap.MapUid = xform.GridUid;
else
NavMap.Visible = false;
StationName.AddStyleClass("LabelBig");
StationName.Text = stationName;
NavMap.TrackedEntitySelectedAction += SetTrackedEntityFromNavMap;
NavMap.ForceNavMapUpdate();
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (_tryToScrollToListFocus)
TryToScrollToFocus();
}
public void ShowSensors(List<SuitSensorStatus> sensors, EntityUid monitor, EntityCoordinates? monitorCoords)
{
ClearOutDatedData();
// No server label
if (sensors.Count == 0)
{
RobustXamlLoader.Load(this);
_eye = IoCManager.Resolve<IEyeManager>();
_entManager = IoCManager.Resolve<IEntityManager>();
_stationUid = mapUid;
if (_entManager.TryGetComponent<TransformComponent>(mapUid, out var xform))
{
NavMap.MapUid = xform.GridUid;
}
else
{
NavMap.Visible = false;
SetSize = new Vector2(775, 400);
MinSize = SetSize;
}
NoServerLabel.Visible = true;
return;
}
public void ShowSensors(List<SuitSensorStatus> stSensors, EntityCoordinates? monitorCoords, bool snap, float precision)
NoServerLabel.Visible = false;
// Order sensor data
var orderedSensors = sensors.OrderBy(n => n.Name).OrderBy(j => j.Job);
var assignedSensors = new HashSet<SuitSensorStatus>();
var departments = sensors.SelectMany(d => d.JobDepartments).Distinct().OrderBy(n => n);
// Create department labels and populate lists
foreach (var department in departments)
{
ClearAllSensors();
var departmentSensors = orderedSensors.Where(d => d.JobDepartments.Contains(department));
var monitorCoordsInStationSpace = _stationUid != null ? monitorCoords?.WithEntityId(_stationUid.Value, _entManager).Position : null;
if (departmentSensors == null || !departmentSensors.Any())
continue;
// TODO scroll container
// TODO filter by name & occupation
// TODO make each row a xaml-control. Get rid of some of this c# control creation.
if (stSensors.Count == 0)
foreach (var sensor in departmentSensors)
assignedSensors.Add(sensor);
if (SensorsTable.ChildCount > 0)
{
NoServerLabel.Visible = true;
return;
}
NoServerLabel.Visible = false;
// add a row for each sensor
foreach (var sensor in stSensors.OrderBy(a => a.Name))
{
var sensorEntity = _entManager.GetEntity(sensor.SuitSensorUid);
var coordinates = _entManager.GetCoordinates(sensor.Coordinates);
// add button with username
var nameButton = new CrewMonitoringButton()
var spacer = new Control()
{
SuitSensorUid = sensorEntity,
Coordinates = coordinates,
Text = sensor.Name,
Margin = new Thickness(5f, 5f),
SetHeight = 20,
};
if (sensorEntity == _trackedButton?.SuitSensorUid)
nameButton.AddStyleClass(StyleNano.StyleClassButtonColorGreen);
SetColorLabel(nameButton.Label, sensor.TotalDamage, sensor.IsAlive);
SensorsTable.AddChild(nameButton);
_rowsContent.Add(nameButton);
// add users job
// format: JobName
var jobLabel = new Label()
{
Text = sensor.Job,
HorizontalExpand = true
};
SetColorLabel(jobLabel, sensor.TotalDamage, sensor.IsAlive);
SensorsTable.AddChild(jobLabel);
_rowsContent.Add(jobLabel);
// add users status and damage
// format: IsAlive (TotalDamage)
var statusText = Loc.GetString(sensor.IsAlive ?
"crew-monitoring-user-interface-alive" :
"crew-monitoring-user-interface-dead");
if (sensor.TotalDamage != null)
{
statusText += $" ({sensor.TotalDamage})";
}
var statusLabel = new Label()
{
Text = statusText
};
SetColorLabel(statusLabel, sensor.TotalDamage, sensor.IsAlive);
SensorsTable.AddChild(statusLabel);
_rowsContent.Add(statusLabel);
// add users positions
// format: (x, y)
var box = GetPositionBox(sensor, monitorCoordsInStationSpace ?? Vector2.Zero, snap, precision);
SensorsTable.AddChild(box);
_rowsContent.Add(box);
if (coordinates != null && NavMap.Visible)
{
NavMap.TrackedCoordinates.TryAdd(coordinates.Value,
(true, sensorEntity == _trackedButton?.SuitSensorUid ? StyleNano.PointGreen : StyleNano.PointRed));
nameButton.OnButtonUp += args =>
{
if (_trackedButton != null && _trackedButton?.Coordinates != null)
//Make previous point red
NavMap.TrackedCoordinates[_trackedButton.Coordinates.Value] = (true, StyleNano.PointRed);
NavMap.TrackedCoordinates[coordinates.Value] = (true, StyleNano.PointGreen);
NavMap.CenterToCoordinates(coordinates.Value);
nameButton.AddStyleClass(StyleNano.StyleClassButtonColorGreen);
if (_trackedButton != null)
{ //Make previous button default
var previosButton = SensorsTable.GetChild(_trackedButton.IndexInTable);
previosButton.RemoveStyleClass(StyleNano.StyleClassButtonColorGreen);
}
_trackedButton = nameButton;
_trackedButton.IndexInTable = nameButton.GetPositionInParent();
};
}
SensorsTable.AddChild(spacer);
_rowsContent.Add(spacer);
}
// Show monitor point
if (monitorCoords != null)
NavMap.TrackedCoordinates.Add(monitorCoords.Value, (true, StyleNano.PointMagenta));
var deparmentLabel = new RichTextLabel()
{
Margin = new Thickness(10, 0),
HorizontalExpand = true,
};
deparmentLabel.SetMessage(department);
deparmentLabel.StyleClasses.Add(StyleNano.StyleClassTooltipActionDescription);
SensorsTable.AddChild(deparmentLabel);
_rowsContent.Add(deparmentLabel);
PopulateDepartmentList(departmentSensors);
}
private BoxContainer GetPositionBox(SuitSensorStatus sensor, Vector2 monitorCoordsInStationSpace, bool snap, float precision)
// Account for any non-station users
var remainingSensors = orderedSensors.Except(assignedSensors);
if (remainingSensors.Any())
{
EntityCoordinates? coordinates = _entManager.GetCoordinates(sensor.Coordinates);
var box = new BoxContainer() { Orientation = LayoutOrientation.Horizontal };
if (coordinates == null || _stationUid == null)
var spacer = new Control()
{
var dirIcon = new DirectionIcon()
{
SetSize = new Vector2(IconSize, IconSize),
Margin = new(0, 0, 4, 0)
};
box.AddChild(dirIcon);
box.AddChild(new Label() { Text = Loc.GetString("crew-monitoring-user-interface-no-info") });
}
else
SetHeight = 20,
};
SensorsTable.AddChild(spacer);
_rowsContent.Add(spacer);
var deparmentLabel = new RichTextLabel()
{
var local = coordinates.Value.WithEntityId(_stationUid.Value, _entManager).Position;
Margin = new Thickness(10, 0),
HorizontalExpand = true,
};
var displayPos = local.Floored();
var dirIcon = new DirectionIcon(snap, precision)
{
SetSize = new Vector2(IconSize, IconSize),
Margin = new(0, 0, 4, 0)
};
box.AddChild(dirIcon);
Label label = new Label() { Text = displayPos.ToString() };
SetColorLabel(label, sensor.TotalDamage, sensor.IsAlive);
box.AddChild(label);
_directionIcons.Add((dirIcon, local - monitorCoordsInStationSpace));
}
deparmentLabel.SetMessage(Loc.GetString("crew-monitoring-user-interface-no-department"));
deparmentLabel.StyleClasses.Add(StyleNano.StyleClassTooltipActionDescription);
return box;
SensorsTable.AddChild(deparmentLabel);
_rowsContent.Add(deparmentLabel);
PopulateDepartmentList(remainingSensors);
}
protected override void FrameUpdate(FrameEventArgs args)
// Show monitor on nav map
if (monitorCoords != null && _blipTexture != null)
{
// the window is separate from any specific viewport, so there is no real way to get an eye-rotation without
// using IEyeManager. Eventually this will have to be reworked for a station AI with multi-viewports.
// (From the future: Or alternatively, just disable the angular offset for station AIs?)
// An offsetAngle of zero here perfectly aligns directions to the station map.
// Note that the "relative angle" does this weird inverse-inverse thing.
// Could recalculate it all in world coordinates and then pass in eye directly... or do this.
var offsetAngle = Angle.Zero;
if (_entManager.TryGetComponent<TransformComponent>(_stationUid, out var xform))
{
// Apply the offset relative to the eye.
// For a station at 45 degrees rotation, the current eye rotation is -45 degrees.
// TODO: This feels sketchy. Is there something underlying wrong with eye rotation?
offsetAngle = -(_eye.CurrentEye.Rotation + xform.WorldRotation);
}
foreach (var (icon, pos) in _directionIcons)
{
icon.UpdateDirection(pos, offsetAngle);
}
}
private void ClearAllSensors()
{
foreach (var child in _rowsContent)
{
SensorsTable.RemoveChild(child);
}
_rowsContent.Clear();
_directionIcons.Clear();
NavMap.TrackedCoordinates.Clear();
}
private void SetColorLabel(Label label, int? totalDamage, bool isAlive)
{
var startColor = Color.White;
var critColor = Color.Yellow;
var endColor = Color.Red;
if (!isAlive)
{
label.FontColorOverride = endColor;
return;
}
//Convert from null to regular int
int damage;
if (totalDamage == null) return;
else damage = (int) totalDamage;
if (damage <= 0)
{
label.FontColorOverride = startColor;
}
else if (damage >= 200)
{
label.FontColorOverride = endColor;
}
else if (damage >= 0 && damage <= 100)
{
label.FontColorOverride = GetColorLerp(startColor, critColor, damage);
}
else if (damage >= 100 && damage <= 200)
{
//We need a number from 0 to 100. Divide the number from 100 to 200 by 2
damage /= 2;
label.FontColorOverride = GetColorLerp(critColor, endColor, damage);
}
}
private Color GetColorLerp(Color startColor, Color endColor, int damage)
{
//Smooth transition from one color to another depending on the percentage
var t = damage / 100f;
var r = MathHelper.Lerp(startColor.R, endColor.R, t);
var g = MathHelper.Lerp(startColor.G, endColor.G, t);
var b = MathHelper.Lerp(startColor.B, endColor.B, t);
var a = MathHelper.Lerp(startColor.A, endColor.A, t);
return new Color(r, g, b, a);
NavMap.TrackedEntities[_entManager.GetNetEntity(monitor)] = new NavMapBlip(monitorCoords.Value, _blipTexture, Color.Cyan, true, false);
}
}
public sealed class CrewMonitoringButton : Button
private void PopulateDepartmentList(IEnumerable<SuitSensorStatus> departmentSensors)
{
public int IndexInTable;
public EntityUid? SuitSensorUid;
public EntityCoordinates? Coordinates;
// Populate departments
foreach (var sensor in departmentSensors)
{
var coordinates = _entManager.GetCoordinates(sensor.Coordinates);
// Add a button that will hold a username and other details
NavMap.LocalizedNames.TryAdd(sensor.SuitSensorUid, sensor.Name + ", " + sensor.Job);
var sensorButton = new CrewMonitoringButton()
{
SuitSensorUid = sensor.SuitSensorUid,
Coordinates = coordinates,
Disabled = (coordinates == null),
HorizontalExpand = true,
};
if (sensor.SuitSensorUid == _trackedEntity)
sensorButton.AddStyleClass(StyleNano.StyleClassButtonColorGreen);
SensorsTable.AddChild(sensorButton);
_rowsContent.Add(sensorButton);
// Primary container to hold the button UI elements
var mainContainer = new BoxContainer()
{
Orientation = LayoutOrientation.Horizontal,
HorizontalExpand = true,
};
sensorButton.AddChild(mainContainer);
// User status container
var statusContainer = new BoxContainer()
{
SizeFlagsStretchRatio = 1.25f,
Orientation = LayoutOrientation.Horizontal,
HorizontalExpand = true,
};
mainContainer.AddChild(statusContainer);
// Suit coords indicator
var suitCoordsIndicator = new TextureRect()
{
Texture = _blipTexture,
TextureScale = new Vector2(0.25f, 0.25f),
Modulate = coordinates != null ? Color.LimeGreen : Color.DarkRed,
HorizontalAlignment = HAlignment.Center,
VerticalAlignment = VAlignment.Center,
};
statusContainer.AddChild(suitCoordsIndicator);
// Specify texture for the user status icon
var specifier = new SpriteSpecifier.Rsi(new ResPath("Interface/Alerts/human_crew_monitoring.rsi"), "alive");
if (!sensor.IsAlive)
{
specifier = new SpriteSpecifier.Rsi(new ResPath("Interface/Alerts/human_crew_monitoring.rsi"), "dead");
}
else if (sensor.TotalDamage != null)
{
var index = MathF.Round(4f * (sensor.TotalDamage.Value / 100f));
if (index >= 5)
specifier = new SpriteSpecifier.Rsi(new ResPath("Interface/Alerts/human_crew_monitoring.rsi"), "critical");
else
specifier = new SpriteSpecifier.Rsi(new ResPath("Interface/Alerts/human_crew_monitoring.rsi"), "health" + index);
}
// Status icon
var statusIcon = new AnimatedTextureRect
{
HorizontalAlignment = HAlignment.Center,
VerticalAlignment = VAlignment.Center,
Margin = new Thickness(0, 1, 3, 0),
};
statusIcon.SetFromSpriteSpecifier(specifier);
statusIcon.DisplayRect.TextureScale = new Vector2(2f, 2f);
statusContainer.AddChild(statusIcon);
// User name
var nameLabel = new Label()
{
Text = sensor.Name,
HorizontalExpand = true,
ClipText = true,
};
statusContainer.AddChild(nameLabel);
// User job container
var jobContainer = new BoxContainer()
{
Orientation = LayoutOrientation.Horizontal,
HorizontalExpand = true,
};
mainContainer.AddChild(jobContainer);
// Job icon
if (_prototypeManager.TryIndex<StatusIconPrototype>(sensor.JobIcon, out var proto))
{
var jobIcon = new TextureRect()
{
TextureScale = new Vector2(2f, 2f),
Stretch = TextureRect.StretchMode.KeepCentered,
Texture = _spriteSystem.Frame0(proto.Icon),
Margin = new Thickness(5, 0, 5, 0),
};
jobContainer.AddChild(jobIcon);
}
// Job name
var jobLabel = new Label()
{
Text = sensor.Job,
HorizontalExpand = true,
ClipText = true,
};
jobContainer.AddChild(jobLabel);
// Add user coordinates to the navmap
if (coordinates != null && NavMap.Visible && _blipTexture != null)
{
NavMap.TrackedEntities.TryAdd(sensor.SuitSensorUid,
new NavMapBlip
(coordinates.Value,
_blipTexture,
(_trackedEntity == null || sensor.SuitSensorUid == _trackedEntity) ? Color.LimeGreen : Color.LimeGreen * Color.DimGray,
sensor.SuitSensorUid == _trackedEntity));
NavMap.Focus = _trackedEntity;
// On button up
sensorButton.OnButtonUp += args =>
{
var prevTrackedEntity = _trackedEntity;
if (_trackedEntity == sensor.SuitSensorUid)
{
_trackedEntity = null;
}
else
{
_trackedEntity = sensor.SuitSensorUid;
NavMap.CenterToCoordinates(coordinates.Value);
}
NavMap.Focus = _trackedEntity;
UpdateSensorsTable(_trackedEntity, prevTrackedEntity);
};
}
}
}
private void SetTrackedEntityFromNavMap(NetEntity? netEntity)
{
var prevTrackedEntity = _trackedEntity;
_trackedEntity = netEntity;
if (_trackedEntity == prevTrackedEntity)
prevTrackedEntity = null;
NavMap.Focus = _trackedEntity;
_tryToScrollToListFocus = true;
UpdateSensorsTable(_trackedEntity, prevTrackedEntity);
}
private void UpdateSensorsTable(NetEntity? currTrackedEntity, NetEntity? prevTrackedEntity)
{
foreach (var sensor in SensorsTable.Children)
{
if (sensor is not CrewMonitoringButton)
continue;
var castSensor = (CrewMonitoringButton) sensor;
if (castSensor.SuitSensorUid == prevTrackedEntity)
castSensor.RemoveStyleClass(StyleNano.StyleClassButtonColorGreen);
else if (castSensor.SuitSensorUid == currTrackedEntity)
castSensor.AddStyleClass(StyleNano.StyleClassButtonColorGreen);
if (castSensor?.Coordinates == null)
continue;
if (NavMap.TrackedEntities.TryGetValue(castSensor.SuitSensorUid, out var data))
{
data = new NavMapBlip
(data.Coordinates,
data.Texture,
(currTrackedEntity == null || castSensor.SuitSensorUid == currTrackedEntity) ? Color.LimeGreen : Color.LimeGreen * Color.DimGray,
castSensor.SuitSensorUid == currTrackedEntity);
NavMap.TrackedEntities[castSensor.SuitSensorUid] = data;
}
}
}
private void TryToScrollToFocus()
{
if (!_tryToScrollToListFocus)
return;
if (!TryGetVerticalScrollbar(SensorScroller, out var vScrollbar))
return;
if (TryGetNextScrollPosition(out float? nextScrollPosition))
{
vScrollbar.ValueTarget = nextScrollPosition.Value;
if (MathHelper.CloseToPercent(vScrollbar.Value, vScrollbar.ValueTarget))
{
_tryToScrollToListFocus = false;
return;
}
}
}
private bool TryGetVerticalScrollbar(ScrollContainer scroll, [NotNullWhen(true)] out VScrollBar? vScrollBar)
{
vScrollBar = null;
foreach (var child in scroll.Children)
{
if (child is not VScrollBar)
continue;
vScrollBar = (VScrollBar) child;
return true;
}
return false;
}
private bool TryGetNextScrollPosition([NotNullWhen(true)] out float? nextScrollPosition)
{
nextScrollPosition = 0;
foreach (var sensor in SensorsTable.Children)
{
if (sensor is CrewMonitoringButton &&
((CrewMonitoringButton) sensor).SuitSensorUid == _trackedEntity)
return true;
nextScrollPosition += sensor.Height;
}
// Failed to find control
nextScrollPosition = null;
return false;
}
private void ClearOutDatedData()
{
SensorsTable.RemoveAllChildren();
_rowsContent.Clear();
NavMap.TrackedCoordinates.Clear();
NavMap.TrackedEntities.Clear();
NavMap.LocalizedNames.Clear();
}
}
public sealed class CrewMonitoringButton : Button
{
public int IndexInTable;
public NetEntity SuitSensorUid;
public EntityCoordinates? Coordinates;
}