Fix forensic scanner UI. (#12398)

* Add missing Dispose method to ForensicScannerBoundUserInterface.

* Remove old code from ForensicScanner.

* Prevent forensic scanner from being used on the floor and allow its window to stay open when active hand is swapped.

* Use more standardized UI code for ForensicScanner.

* Add a delay to ForensicScanner printing.

* Show name of what was scanned on ForensicScanner UI.

* Add a print sound for ForensicScanner.

* Add more error reporting for ForensicScanner.

* Centralize common logic in ForensicScannerSystem.

* Allow ForensicScanner blank printouts.

* Tweak ForensicScanner audio parameters.
This commit is contained in:
Vordenburg
2022-11-08 16:06:09 -05:00
committed by GitHub
parent 754d3c1634
commit ed8141d333
10 changed files with 269 additions and 57 deletions

View File

@@ -1,12 +1,17 @@
using Content.Shared.Forensics;
using Robust.Client.GameObjects;
using Robust.Shared.Timing;
using Content.Shared.Forensics;
namespace Content.Client.Forensics
{
public sealed class ForensicScannerBoundUserInterface : BoundUserInterface
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
private ForensicScannerMenu? _window;
private TimeSpan _printCooldown;
public ForensicScannerBoundUserInterface(ClientUserInterfaceComponent owner, Enum uiKey) : base(owner, uiKey)
{
}
@@ -17,24 +22,61 @@ namespace Content.Client.Forensics
_window = new ForensicScannerMenu();
_window.OnClose += Close;
_window.Print.OnPressed += _ => Print();
_window.Clear.OnPressed += _ => Clear();
_window.OpenCentered();
}
private void Print()
{
SendMessage(new ForensicScannerPrintMessage());
_window?.Close();
if (_window != null)
_window.UpdatePrinterState(true);
// This UI does not require pinpoint accuracy as to when the Print
// button is available again, so spawning client-side timers is
// fine. The server will make sure the cooldown is honored.
Timer.Spawn(_printCooldown, () =>
{
if (_window != null)
_window.UpdatePrinterState(false);
});
}
protected override void ReceiveMessage(BoundUserInterfaceMessage message)
private void Clear()
{
SendMessage(new ForensicScannerClearMessage());
}
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
if (_window == null)
return;
if (message is not ForensicScannerUserMessage cast)
if (state is not ForensicScannerBoundUserInterfaceState cast)
return;
_window.Populate(cast);
_printCooldown = cast.PrintCooldown;
if (cast.PrintReadyAt > _gameTiming.CurTime)
Timer.Spawn(cast.PrintReadyAt - _gameTiming.CurTime, () =>
{
if (_window != null)
_window.UpdatePrinterState(false);
});
_window.UpdateState(cast);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing)
return;
_window?.Dispose();
}
}
}

View File

@@ -1,12 +1,25 @@
<DefaultWindow xmlns="https://spacestation14.io"
Title="{Loc 'forensic-scanner-interface-title'}"
MinSize="250 100"
SetSize="250 100">
MinSize="350 200"
SetSize="350 500">
<BoxContainer Orientation="Vertical">
<Button Name="Print"
<BoxContainer Orientation="Horizontal">
<Button Name="Print"
TextAlign="Center"
HorizontalExpand="True"
Access="Public"
Disabled="True"
Text="{Loc 'forensic-scanner-interface-print'}" />
<Button Name="Clear"
TextAlign="Center"
HorizontalExpand="True"
Access="Public"
Disabled="True"
Text="{Loc 'forensic-scanner-interface-clear'}" />
</BoxContainer>
<Label
Name="Name"
Align="Center" />
<Label
Name="Diagnostics"
Text="{Loc forensic-scanner-interface-no-data}"/>

View File

@@ -1,22 +1,44 @@
using System.Text;
using Content.Shared.Forensics;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Timing;
using Content.Shared.Forensics;
namespace Content.Client.Forensics
{
[GenerateTypedNameReferences]
public sealed partial class ForensicScannerMenu : DefaultWindow
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
public ForensicScannerMenu()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
}
public void Populate(ForensicScannerUserMessage msg)
public void UpdatePrinterState(bool disabled)
{
Print.Disabled = false;
Print.Disabled = disabled;
}
public void UpdateState(ForensicScannerBoundUserInterfaceState msg)
{
if (string.IsNullOrEmpty(msg.LastScannedName))
{
Print.Disabled = true;
Clear.Disabled = true;
Name.Text = string.Empty;
Diagnostics.Text = string.Empty;
return;
}
Print.Disabled = (msg.PrintReadyAt > _gameTiming.CurTime);
Clear.Disabled = false;
Name.Text = msg.LastScannedName;
var text = new StringBuilder();
text.AppendLine(Loc.GetString("forensic-scanner-interface-fingerprints"));
@@ -31,7 +53,6 @@ namespace Content.Client.Forensics
text.AppendLine(fiber);
}
Diagnostics.Text = text.ToString();
SetSize = (350, 600);
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Threading;
using Robust.Shared.Audio;
namespace Content.Server.Forensics
{
@@ -12,18 +13,58 @@ namespace Content.Server.Forensics
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public List<string> Fingerprints = new();
/// <summary>
/// A list of glove fibers that the forensic scanner found from the <see cref="ForensicsComponent"/> on an entity.
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public List<string> Fibers = new();
public string LastScanned = string.Empty;
/// <summary>
/// What is the name of the entity that was scanned last?
/// </summary>
/// <remarks>
/// This will be used for the title of the printout and displayed to players.
/// </remarks>
[ViewVariables(VVAccess.ReadOnly)]
public string LastScannedName = string.Empty;
/// <summary>
/// When will the scanner be ready to print again?
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public TimeSpan PrintReadyAt = TimeSpan.Zero;
/// <summary>
/// The time (in seconds) that it takes to scan an entity.
/// </summary>
[DataField("scanDelay")]
public float ScanDelay = 3.0f;
/// <summary>
/// How often can the scanner print out reports?
/// </summary>
[DataField("printCooldown")]
public TimeSpan PrintCooldown = TimeSpan.FromSeconds(5);
/// <summary>
/// The sound that's played when there's a match between a scan and an
/// inserted forensic pad.
/// </summary>
[DataField("soundMatch")]
public SoundSpecifier SoundMatch = new SoundPathSpecifier("/Audio/Machines/Nuke/angry_beep.ogg");
/// <summary>
/// The sound that's played when there's no match between a scan and an
/// inserted forensic pad.
/// </summary>
[DataField("soundNoMatch")]
public SoundSpecifier SoundNoMatch = new SoundPathSpecifier("/Audio/Machines/airlock_deny.ogg");
/// <summary>
/// The sound that's played when the scanner prints off a report.
/// </summary>
[DataField("soundPrint")]
public SoundSpecifier SoundPrint = new SoundPathSpecifier("/Audio/Machines/short_print_and_rip.ogg");
}
}

View File

@@ -1,43 +1,70 @@
using System.Linq;
using System.Text; // todo: remove this stinky LINQy
using System.Threading;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Timing;
using Content.Server.DoAfter;
using Content.Server.Paper;
using Content.Server.Popups;
using Content.Server.UserInterface;
using Content.Shared.Forensics;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Verbs;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Player;
namespace Content.Server.Forensics
{
public sealed class ForensicScannerSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly PaperSystem _paperSystem = default!;
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
private ISawmill _sawmill = default!;
public override void Initialize()
{
base.Initialize();
_sawmill = Logger.GetSawmill("forensics.scanner");
SubscribeLocalEvent<ForensicScannerComponent, AfterInteractEvent>(OnAfterInteract);
SubscribeLocalEvent<ForensicScannerComponent, AfterInteractUsingEvent>(OnAfterInteractUsing);
SubscribeLocalEvent<ForensicScannerComponent, BeforeActivatableUIOpenEvent>(OnBeforeActivatableUIOpen);
SubscribeLocalEvent<ForensicScannerComponent, GetVerbsEvent<UtilityVerb>>(OnUtilityVerb);
SubscribeLocalEvent<ForensicScannerComponent, ForensicScannerPrintMessage>(OnPrint);
SubscribeLocalEvent<ForensicScannerComponent, ForensicScannerClearMessage>(OnClear);
SubscribeLocalEvent<TargetScanSuccessfulEvent>(OnTargetScanSuccessful);
SubscribeLocalEvent<ScanCancelledEvent>(OnScanCancelled);
}
private void UpdateUserInterface(EntityUid uid, ForensicScannerComponent component)
{
var state = new ForensicScannerBoundUserInterfaceState(
component.Fingerprints,
component.Fibers,
component.LastScannedName,
component.PrintCooldown,
component.PrintReadyAt);
if (!_uiSystem.TrySetUiState(uid, ForensicScannerUiKey.Key, state))
{
_sawmill.Warning($"{ToPrettyString(uid)} was unable to set UI state.");
return;
}
}
private void OnScanCancelled(ScanCancelledEvent ev)
{
if (!EntityManager.TryGetComponent(ev.Scanner, out ForensicScannerComponent? scanner))
return;
scanner.CancelToken = null;
}
@@ -49,14 +76,38 @@ namespace Content.Server.Forensics
scanner.CancelToken = null;
if (!TryComp<ForensicsComponent>(ev.Target, out var forensics))
return;
{
scanner.Fingerprints = new();
scanner.Fibers = new();
}
else
{
scanner.Fingerprints = forensics.Fingerprints.ToList();
scanner.Fibers = forensics.Fibers.ToList();
}
scanner.LastScannedName = MetaData(ev.Target).EntityName;
scanner.Fingerprints = forensics.Fingerprints.ToList();
scanner.Fibers = forensics.Fibers.ToList();
scanner.LastScanned = MetaData(ev.Target).EntityName;
OpenUserInterface(ev.User, scanner);
}
/// <remarks>
/// Hosts logic common between OnUtilityVerb and OnAfterInteract.
/// </remarks>
private void StartScan(EntityUid uid, ForensicScannerComponent component, EntityUid user, EntityUid target)
{
component.CancelToken = new CancellationTokenSource();
_doAfterSystem.DoAfter(new DoAfterEventArgs(user, component.ScanDelay, component.CancelToken.Token, target: target)
{
BroadcastFinishedEvent = new TargetScanSuccessfulEvent(user, (EntityUid) target, component.Owner),
BroadcastCancelledEvent = new ScanCancelledEvent(component.Owner),
BreakOnTargetMove = true,
BreakOnUserMove = true,
BreakOnStun = true,
NeedHand = true
});
}
private void OnUtilityVerb(EntityUid uid, ForensicScannerComponent component, GetVerbsEvent<UtilityVerb> args)
{
if (!args.CanInteract || !args.CanAccess || component.CancelToken != null)
@@ -64,19 +115,8 @@ namespace Content.Server.Forensics
var verb = new UtilityVerb()
{
Act = () =>
{
component.CancelToken = new CancellationTokenSource();
_doAfterSystem.DoAfter(new DoAfterEventArgs(args.User, component.ScanDelay, component.CancelToken.Token, target: args.Target)
{
BroadcastFinishedEvent = new TargetScanSuccessfulEvent(args.User, (EntityUid) args.Target, component.Owner),
BroadcastCancelledEvent = new ScanCancelledEvent(component.Owner),
BreakOnTargetMove = true,
BreakOnUserMove = true,
BreakOnStun = true,
NeedHand = true
});
},
Act = () => StartScan(uid, component, args.User, args.Target),
IconEntity = uid,
Text = Loc.GetString("forensic-scanner-verb-text"),
Message = Loc.GetString("forensic-scanner-verb-message")
};
@@ -89,16 +129,7 @@ namespace Content.Server.Forensics
if (component.CancelToken != null || args.Target == null || !args.CanReach)
return;
component.CancelToken = new CancellationTokenSource();
_doAfterSystem.DoAfter(new DoAfterEventArgs(args.User, component.ScanDelay, component.CancelToken.Token, target: args.Target)
{
BroadcastFinishedEvent = new TargetScanSuccessfulEvent(args.User, (EntityUid) args.Target, component.Owner),
BroadcastCancelledEvent = new ScanCancelledEvent(component.Owner),
BreakOnTargetMove = true,
BreakOnUserMove = true,
BreakOnStun = true,
NeedHand = true
});
StartScan(uid, component, args.User, args.Target.Value);
}
private void OnAfterInteractUsing(EntityUid uid, ForensicScannerComponent component, AfterInteractUsingEvent args)
@@ -113,7 +144,7 @@ namespace Content.Server.Forensics
{
if (fiber == pad.Sample)
{
SoundSystem.Play("/Audio/Machines/Nuke/angry_beep.ogg", Filter.Pvs(uid), uid);
_audioSystem.PlayPvs(component.SoundMatch, uid);
_popupSystem.PopupEntity(Loc.GetString("forensic-scanner-match-fiber"), uid, Filter.Entities(args.User));
return;
}
@@ -123,38 +154,60 @@ namespace Content.Server.Forensics
{
if (fingerprint == pad.Sample)
{
SoundSystem.Play("/Audio/Machines/Nuke/angry_beep.ogg", Filter.Pvs(uid), uid);
_audioSystem.PlayPvs(component.SoundMatch, uid);
_popupSystem.PopupEntity(Loc.GetString("forensic-scanner-match-fingerprint"), uid, Filter.Entities(args.User));
return;
}
}
SoundSystem.Play("/Audio/Machines/airlock_deny.ogg", Filter.Pvs(uid), uid);
_audioSystem.PlayPvs(component.SoundNoMatch, uid);
_popupSystem.PopupEntity(Loc.GetString("forensic-scanner-match-none"), uid, Filter.Entities(args.User));
}
private void OnBeforeActivatableUIOpen(EntityUid uid, ForensicScannerComponent component, BeforeActivatableUIOpenEvent args)
{
UpdateUserInterface(uid, component);
}
private void OpenUserInterface(EntityUid user, ForensicScannerComponent component)
{
if (!TryComp<ActorComponent>(user, out var actor))
return;
var ui = _uiSystem.GetUi(component.Owner, ForensicScannerUiKey.Key);
UpdateUserInterface(component.Owner, component);
ui.Open(actor.PlayerSession);
ui.SendMessage(new ForensicScannerUserMessage(component.Fingerprints, component.Fibers, component.LastScanned));
_uiSystem.TryOpen(component.Owner, ForensicScannerUiKey.Key, actor.PlayerSession);
}
private void OnPrint(EntityUid uid, ForensicScannerComponent component, ForensicScannerPrintMessage args)
{
if (!args.Session.AttachedEntity.HasValue || (component.Fibers.Count == 0 && component.Fingerprints.Count == 0)) return;
if (!args.Session.AttachedEntity.HasValue)
{
_sawmill.Warning($"{ToPrettyString(uid)} got OnPrint without Session.AttachedEntity");
return;
}
// spawn a piece of paper.
var printed = EntityManager.SpawnEntity("Paper", Transform(args.Session.AttachedEntity.Value).Coordinates);
var user = args.Session.AttachedEntity.Value;
if (_gameTiming.CurTime < component.PrintReadyAt)
{
// This shouldn't occur due to the UI guarding against it, but
// if it does, tell the user why nothing happened.
_popupSystem.PopupEntity(Loc.GetString("forensic-scanner-printer-not-ready"), uid, Filter.Entities(user));
return;
}
// Spawn a piece of paper.
var printed = EntityManager.SpawnEntity("Paper", Transform(uid).Coordinates);
_handsSystem.PickupOrDrop(args.Session.AttachedEntity, printed, checkActionBlocker: false);
if (!TryComp<PaperComponent>(printed, out var paper))
{
_sawmill.Error("Printed paper did not have PaperComponent.");
return;
}
MetaData(printed).EntityName = Loc.GetString("forensic-scanner-report-title", ("entity", component.LastScanned));
MetaData(printed).EntityName = Loc.GetString("forensic-scanner-report-title", ("entity", component.LastScannedName));
var text = new StringBuilder();
@@ -171,6 +224,26 @@ namespace Content.Server.Forensics
}
_paperSystem.SetContent(printed, text.ToString());
_audioSystem.PlayPvs(component.SoundPrint, uid,
AudioParams.Default
.WithVariation(0.25f)
.WithVolume(3f)
.WithRolloffFactor(2.8f)
.WithMaxDistance(4.5f));
component.PrintReadyAt = _gameTiming.CurTime + component.PrintCooldown;
}
private void OnClear(EntityUid uid, ForensicScannerComponent component, ForensicScannerClearMessage args)
{
if (!args.Session.AttachedEntity.HasValue)
return;
component.Fingerprints = new();
component.Fibers = new();
component.LastScannedName = string.Empty;
UpdateUserInterface(uid, component);
}
private sealed class ScanCancelledEvent : EntityEventArgs

View File

@@ -3,17 +3,26 @@ using Robust.Shared.Serialization;
namespace Content.Shared.Forensics
{
[Serializable, NetSerializable]
public sealed class ForensicScannerUserMessage : BoundUserInterfaceMessage
public sealed class ForensicScannerBoundUserInterfaceState : BoundUserInterfaceState
{
public readonly List<string> Fingerprints = new();
public readonly List<string> Fibers = new();
public readonly string LastScanned = string.Empty;
public readonly string LastScannedName = string.Empty;
public readonly TimeSpan PrintCooldown = TimeSpan.Zero;
public readonly TimeSpan PrintReadyAt = TimeSpan.Zero;
public ForensicScannerUserMessage(List<string> fingerprints, List<string> fibers, string lastScanned)
public ForensicScannerBoundUserInterfaceState(
List<string> fingerprints,
List<string> fibers,
string lastScannedName,
TimeSpan printCooldown,
TimeSpan printReadyAt)
{
Fingerprints = fingerprints;
Fibers = fibers;
LastScanned = lastScanned;
LastScannedName = lastScannedName;
PrintCooldown = printCooldown;
PrintReadyAt = printReadyAt;
}
}
@@ -27,4 +36,9 @@ namespace Content.Shared.Forensics
public sealed class ForensicScannerPrintMessage : BoundUserInterfaceMessage
{
}
[Serializable, NetSerializable]
public sealed class ForensicScannerClearMessage : BoundUserInterfaceMessage
{
}
}

View File

@@ -0,0 +1,4 @@
- files: ["short_print_and_rip.ogg"]
license: "CC0-1.0"
copyright: "receipt printing.wav by 13F_Panska_Tlolkova_Matilda. This version is cleaned up, shortened, and converted to OGG."
source: "https://freesound.org/people/13F_Panska_Tlolkova_Matilda/sounds/378331"

Binary file not shown.

View File

@@ -3,6 +3,7 @@ forensic-scanner-interface-fingerprints = Fingerprints
forensic-scanner-interface-fibers = Fibers
forensic-scanner-interface-no-data = No scan data available
forensic-scanner-interface-print = Print
forensic-scanner-interface-clear = Clear
forensic-scanner-report-title = Forensics Report: {$entity}
forensic-pad-unused = It hasn't been used.
forensic-pad-sample = It has a sample: {$sample}
@@ -13,6 +14,7 @@ forensic-pad-already-used = This pad has already been used.
forensic-scanner-match-fiber = Match in fiber found!
forensic-scanner-match-fingerprint = Match in fingerprint found!
forensic-scanner-match-none = No matches found!
forensic-scanner-printer-not-ready = Printer is not ready yet.
forensic-scanner-verb-text = Scan
forensic-scanner-verb-message = Perform a forensic scan

View File

@@ -17,6 +17,8 @@
- Belt
- type: ActivatableUI
key: enum.ForensicScannerUiKey.Key
inHandsOnly: true
closeOnHandDeselect: false
- type: UserInterface
interfaces:
- key: enum.ForensicScannerUiKey.Key