Add a 'Copy' button to the fax UI (#22027)

* Add a 'Copy' button to the fax UI

* Add ValidatePrototypeId attribute

Co-authored-by: Kara <lunarautomaton6@gmail.com>

---------

Co-authored-by: Kara <lunarautomaton6@gmail.com>
This commit is contained in:
Arimah Greene
2023-12-25 02:08:15 +01:00
committed by GitHub
parent 28310a131c
commit b20fcf5141
6 changed files with 76 additions and 4 deletions

View File

@@ -22,6 +22,7 @@ using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Containers;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
namespace Content.Server.Fax;
@@ -43,6 +44,13 @@ public sealed class FaxSystem : EntitySystem
private const string PaperSlotId = "Paper";
/// <summary>
/// The prototype ID to use for faxed or copied entities if we can't get one from
/// the paper entity for whatever reason.
/// </summary>
[ValidatePrototypeId<EntityPrototype>]
private const string DefaultPaperPrototypeId = "Paper";
public override void Initialize()
{
base.Initialize();
@@ -63,6 +71,7 @@ public sealed class FaxSystem : EntitySystem
// UI
SubscribeLocalEvent<FaxMachineComponent, AfterActivatableUIOpenEvent>(OnToggleInterface);
SubscribeLocalEvent<FaxMachineComponent, FaxCopyMessage>(OnCopyButtonPressed);
SubscribeLocalEvent<FaxMachineComponent, FaxSendMessage>(OnSendButtonPressed);
SubscribeLocalEvent<FaxMachineComponent, FaxRefreshMessage>(OnRefreshButtonPressed);
SubscribeLocalEvent<FaxMachineComponent, FaxDestinationMessage>(OnDestinationSelected);
@@ -291,6 +300,11 @@ public sealed class FaxSystem : EntitySystem
UpdateUserInterface(uid, component);
}
private void OnCopyButtonPressed(EntityUid uid, FaxMachineComponent component, FaxCopyMessage args)
{
Copy(uid, component);
}
private void OnSendButtonPressed(EntityUid uid, FaxMachineComponent component, FaxSendMessage args)
{
Send(uid, component, args.Session.AttachedEntity);
@@ -329,7 +343,10 @@ public sealed class FaxSystem : EntitySystem
component.DestinationFaxAddress != null &&
component.SendTimeoutRemaining <= 0 &&
component.InsertingTimeRemaining <= 0;
var state = new FaxUiState(component.FaxName, component.KnownFaxes, canSend, isPaperInserted, component.DestinationFaxAddress);
var canCopy = isPaperInserted &&
component.SendTimeoutRemaining <= 0 &&
component.InsertingTimeRemaining <= 0;
var state = new FaxUiState(component.FaxName, component.KnownFaxes, canSend, canCopy, isPaperInserted, component.DestinationFaxAddress);
_userInterface.TrySetUiState(uid, FaxUiKey.Key, state);
}
@@ -369,9 +386,42 @@ public sealed class FaxSystem : EntitySystem
_deviceNetworkSystem.QueuePacket(uid, null, payload);
}
/// <summary>
/// Copies the paper in the fax. A timeout is set after copying,
/// which is shared by the send button.
/// </summary>
public void Copy(EntityUid uid, FaxMachineComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
var sendEntity = component.PaperSlot.Item;
if (sendEntity == null)
return;
if (!TryComp<MetaDataComponent>(sendEntity, out var metadata) ||
!TryComp<PaperComponent>(sendEntity, out var paper))
return;
// TODO: See comment in 'Send()' about not being able to copy whole entities
var printout = new FaxPrintout(paper.Content,
metadata.EntityName,
metadata.EntityPrototype?.ID ?? DefaultPaperPrototypeId,
paper.StampState,
paper.StampedBy);
component.PrintingQueue.Enqueue(printout);
component.SendTimeoutRemaining += component.SendTimeout;
// Don't play component.SendSound - it clashes with the printing sound, which
// will start immediately.
UpdateUserInterface(uid, component);
}
/// <summary>
/// Sends message to addressee if paper is set and a known fax is selected
/// A timeout is set after sending
/// A timeout is set after sending, which is shared by the copy button.
/// </summary>
public void Send(EntityUid uid, FaxMachineComponent? component = null, EntityUid? sender = null)
{
@@ -404,7 +454,7 @@ public sealed class FaxSystem : EntitySystem
// TODO: Ideally, we could just make a copy of the whole entity when it's
// faxed, in order to preserve visuals, etc.. This functionality isn't
// available yet, so we'll pass along the originating prototypeId and fall
// back to "Paper" in SpawnPaperFromQueue if we can't find one here.
// back to DefaultPaperPrototypeId in SpawnPaperFromQueue if we can't find one here.
payload[FaxConstants.FaxPaperPrototypeData] = metadata.EntityPrototype.ID;
}
@@ -454,7 +504,7 @@ public sealed class FaxSystem : EntitySystem
var printout = component.PrintingQueue.Dequeue();
var entityToSpawn = printout.PrototypeId.Length == 0 ? "Paper" : printout.PrototypeId;
var entityToSpawn = printout.PrototypeId.Length == 0 ? DefaultPaperPrototypeId : printout.PrototypeId;
var printed = EntityManager.SpawnEntity(entityToSpawn, Transform(uid).Coordinates);
if (TryComp<PaperComponent>(printed, out var paper))