Fax machines can print from text file (#23262)
* added * checks tweaking * fixed what sloth wanted * fixed? * dialog diposing fix * checks tweaking * more changes * dispose streamreader * Update Content.Client/Fax/UI/FaxBoundUi.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> * Update Content.Server/Fax/FaxSystem.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> * fix minor typo --------- Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
@@ -1,15 +1,22 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Content.Shared.Fax;
|
using Content.Shared.Fax;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Robust.Client.GameObjects;
|
using Robust.Client.GameObjects;
|
||||||
|
using Robust.Client.UserInterface;
|
||||||
|
|
||||||
namespace Content.Client.Fax.UI;
|
namespace Content.Client.Fax.UI;
|
||||||
|
|
||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
public sealed class FaxBoundUi : BoundUserInterface
|
public sealed class FaxBoundUi : BoundUserInterface
|
||||||
{
|
{
|
||||||
|
[Dependency] private readonly IFileDialogManager _fileDialogManager = default!;
|
||||||
|
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
private FaxWindow? _window;
|
private FaxWindow? _window;
|
||||||
|
|
||||||
|
private bool _dialogIsOpen = false;
|
||||||
|
|
||||||
public FaxBoundUi(EntityUid owner, Enum uiKey) : base(owner, uiKey)
|
public FaxBoundUi(EntityUid owner, Enum uiKey) : base(owner, uiKey)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -22,12 +29,33 @@ public sealed class FaxBoundUi : BoundUserInterface
|
|||||||
_window.OpenCentered();
|
_window.OpenCentered();
|
||||||
|
|
||||||
_window.OnClose += Close;
|
_window.OnClose += Close;
|
||||||
|
_window.FileButtonPressed += OnFileButtonPressed;
|
||||||
_window.CopyButtonPressed += OnCopyButtonPressed;
|
_window.CopyButtonPressed += OnCopyButtonPressed;
|
||||||
_window.SendButtonPressed += OnSendButtonPressed;
|
_window.SendButtonPressed += OnSendButtonPressed;
|
||||||
_window.RefreshButtonPressed += OnRefreshButtonPressed;
|
_window.RefreshButtonPressed += OnRefreshButtonPressed;
|
||||||
_window.PeerSelected += OnPeerSelected;
|
_window.PeerSelected += OnPeerSelected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async void OnFileButtonPressed()
|
||||||
|
{
|
||||||
|
if (_dialogIsOpen)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_dialogIsOpen = true;
|
||||||
|
var filters = new FileDialogFilters(new FileDialogFilters.Group("txt"));
|
||||||
|
await using var file = await _fileDialogManager.OpenFile(filters);
|
||||||
|
_dialogIsOpen = false;
|
||||||
|
|
||||||
|
if (_window == null || _window.Disposed || file == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
using var reader = new StreamReader(file);
|
||||||
|
var content = await reader.ReadToEndAsync();
|
||||||
|
SendMessage(new FaxFileMessage(content[..Math.Min(content.Length, FaxFileMessageValidation.MaxContentSize)], _window.OfficePaper));
|
||||||
|
}
|
||||||
|
|
||||||
private void OnSendButtonPressed()
|
private void OnSendButtonPressed()
|
||||||
{
|
{
|
||||||
SendMessage(new FaxSendMessage());
|
SendMessage(new FaxSendMessage());
|
||||||
|
|||||||
@@ -19,6 +19,14 @@
|
|||||||
<OptionButton Name="PeerSelector" HorizontalExpand="True" />
|
<OptionButton Name="PeerSelector" HorizontalExpand="True" />
|
||||||
</BoxContainer>
|
</BoxContainer>
|
||||||
<Control HorizontalExpand="True" MinHeight="20" />
|
<Control HorizontalExpand="True" MinHeight="20" />
|
||||||
|
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
|
||||||
|
<Button Name="FileButton"
|
||||||
|
Text="{Loc 'fax-machine-ui-file-button'}"
|
||||||
|
HorizontalExpand="False"/>
|
||||||
|
<Button Name="PaperButton"
|
||||||
|
Text="{Loc 'fax-machine-ui-paper-button-normal'}"
|
||||||
|
HorizontalExpand="False"/>
|
||||||
|
</BoxContainer>
|
||||||
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
|
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
|
||||||
<Button Name="CopyButton"
|
<Button Name="CopyButton"
|
||||||
Text="{Loc 'fax-machine-ui-copy-button'}"
|
Text="{Loc 'fax-machine-ui-copy-button'}"
|
||||||
|
|||||||
@@ -3,21 +3,30 @@ using Content.Shared.Fax;
|
|||||||
using Robust.Client.AutoGenerated;
|
using Robust.Client.AutoGenerated;
|
||||||
using Robust.Client.UserInterface.CustomControls;
|
using Robust.Client.UserInterface.CustomControls;
|
||||||
using Robust.Client.UserInterface.XAML;
|
using Robust.Client.UserInterface.XAML;
|
||||||
|
using Robust.Client.UserInterface;
|
||||||
|
|
||||||
namespace Content.Client.Fax.UI;
|
namespace Content.Client.Fax.UI;
|
||||||
|
|
||||||
[GenerateTypedNameReferences]
|
[GenerateTypedNameReferences]
|
||||||
public sealed partial class FaxWindow : DefaultWindow
|
public sealed partial class FaxWindow : DefaultWindow
|
||||||
{
|
{
|
||||||
|
public event Action? FileButtonPressed;
|
||||||
|
public event Action? PaperButtonPressed;
|
||||||
public event Action? CopyButtonPressed;
|
public event Action? CopyButtonPressed;
|
||||||
public event Action? SendButtonPressed;
|
public event Action? SendButtonPressed;
|
||||||
public event Action? RefreshButtonPressed;
|
public event Action? RefreshButtonPressed;
|
||||||
public event Action<string>? PeerSelected;
|
public event Action<string>? PeerSelected;
|
||||||
|
|
||||||
|
public bool OfficePaper = false;
|
||||||
|
|
||||||
public FaxWindow()
|
public FaxWindow()
|
||||||
{
|
{
|
||||||
RobustXamlLoader.Load(this);
|
RobustXamlLoader.Load(this);
|
||||||
|
|
||||||
|
PaperButtonPressed += OnPaperButtonPressed;
|
||||||
|
|
||||||
|
FileButton.OnPressed += _ => FileButtonPressed?.Invoke();
|
||||||
|
PaperButton.OnPressed += _ => PaperButtonPressed?.Invoke();
|
||||||
CopyButton.OnPressed += _ => CopyButtonPressed?.Invoke();
|
CopyButton.OnPressed += _ => CopyButtonPressed?.Invoke();
|
||||||
SendButton.OnPressed += _ => SendButtonPressed?.Invoke();
|
SendButton.OnPressed += _ => SendButtonPressed?.Invoke();
|
||||||
RefreshButton.OnPressed += _ => RefreshButtonPressed?.Invoke();
|
RefreshButton.OnPressed += _ => RefreshButtonPressed?.Invoke();
|
||||||
@@ -80,4 +89,14 @@ public sealed partial class FaxWindow : DefaultWindow
|
|||||||
PeerSelector.SetItemMetadata(PeerSelector.ItemCount - 1, address);
|
PeerSelector.SetItemMetadata(PeerSelector.ItemCount - 1, address);
|
||||||
return PeerSelector.ItemCount - 1;
|
return PeerSelector.ItemCount - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnPaperButtonPressed()
|
||||||
|
{
|
||||||
|
OfficePaper = !OfficePaper;
|
||||||
|
|
||||||
|
if(OfficePaper)
|
||||||
|
PaperButton.Text = Loc.GetString("fax-machine-ui-paper-button-office");
|
||||||
|
else
|
||||||
|
PaperButton.Text = Loc.GetString("fax-machine-ui-paper-button-normal");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,9 @@ public sealed class FaxSystem : EntitySystem
|
|||||||
[ValidatePrototypeId<EntityPrototype>]
|
[ValidatePrototypeId<EntityPrototype>]
|
||||||
private const string DefaultPaperPrototypeId = "Paper";
|
private const string DefaultPaperPrototypeId = "Paper";
|
||||||
|
|
||||||
|
[ValidatePrototypeId<EntityPrototype>]
|
||||||
|
private const string OfficePaperPrototypeId = "PaperOffice";
|
||||||
|
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
base.Initialize();
|
base.Initialize();
|
||||||
@@ -72,6 +75,7 @@ public sealed class FaxSystem : EntitySystem
|
|||||||
|
|
||||||
// UI
|
// UI
|
||||||
SubscribeLocalEvent<FaxMachineComponent, AfterActivatableUIOpenEvent>(OnToggleInterface);
|
SubscribeLocalEvent<FaxMachineComponent, AfterActivatableUIOpenEvent>(OnToggleInterface);
|
||||||
|
SubscribeLocalEvent<FaxMachineComponent, FaxFileMessage>(OnFileButtonPressed);
|
||||||
SubscribeLocalEvent<FaxMachineComponent, FaxCopyMessage>(OnCopyButtonPressed);
|
SubscribeLocalEvent<FaxMachineComponent, FaxCopyMessage>(OnCopyButtonPressed);
|
||||||
SubscribeLocalEvent<FaxMachineComponent, FaxSendMessage>(OnSendButtonPressed);
|
SubscribeLocalEvent<FaxMachineComponent, FaxSendMessage>(OnSendButtonPressed);
|
||||||
SubscribeLocalEvent<FaxMachineComponent, FaxRefreshMessage>(OnRefreshButtonPressed);
|
SubscribeLocalEvent<FaxMachineComponent, FaxRefreshMessage>(OnRefreshButtonPressed);
|
||||||
@@ -301,6 +305,12 @@ public sealed class FaxSystem : EntitySystem
|
|||||||
UpdateUserInterface(uid, component);
|
UpdateUserInterface(uid, component);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnFileButtonPressed(EntityUid uid, FaxMachineComponent component, FaxFileMessage args)
|
||||||
|
{
|
||||||
|
args.Content = args.Content[..Math.Min(args.Content.Length, FaxFileMessageValidation.MaxContentSize)];
|
||||||
|
PrintFile(uid, component, args);
|
||||||
|
}
|
||||||
|
|
||||||
private void OnCopyButtonPressed(EntityUid uid, FaxMachineComponent component, FaxCopyMessage args)
|
private void OnCopyButtonPressed(EntityUid uid, FaxMachineComponent component, FaxCopyMessage args)
|
||||||
{
|
{
|
||||||
Copy(uid, component);
|
Copy(uid, component);
|
||||||
@@ -387,6 +397,27 @@ public sealed class FaxSystem : EntitySystem
|
|||||||
_deviceNetworkSystem.QueuePacket(uid, null, payload);
|
_deviceNetworkSystem.QueuePacket(uid, null, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Makes fax print from a file from the computer. A timeout is set after copying,
|
||||||
|
/// which is shared by the send button.
|
||||||
|
/// </summary>
|
||||||
|
public void PrintFile(EntityUid uid, FaxMachineComponent component, FaxFileMessage args)
|
||||||
|
{
|
||||||
|
string prototype;
|
||||||
|
if (args.OfficePaper)
|
||||||
|
prototype = OfficePaperPrototypeId;
|
||||||
|
else
|
||||||
|
prototype = DefaultPaperPrototypeId;
|
||||||
|
|
||||||
|
var name = Loc.GetString("fax-machine-printed-paper-name");
|
||||||
|
|
||||||
|
var printout = new FaxPrintout(args.Content, name, prototype);
|
||||||
|
component.PrintingQueue.Enqueue(printout);
|
||||||
|
component.SendTimeoutRemaining += component.SendTimeout;
|
||||||
|
|
||||||
|
UpdateUserInterface(uid, component);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Copies the paper in the fax. A timeout is set after copying,
|
/// Copies the paper in the fax. A timeout is set after copying,
|
||||||
/// which is shared by the send button.
|
/// which is shared by the send button.
|
||||||
|
|||||||
@@ -34,6 +34,24 @@ public sealed class FaxUiState : BoundUserInterfaceState
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Serializable, NetSerializable]
|
||||||
|
public sealed class FaxFileMessage : BoundUserInterfaceMessage
|
||||||
|
{
|
||||||
|
public string Content;
|
||||||
|
public bool OfficePaper;
|
||||||
|
|
||||||
|
public FaxFileMessage(string content, bool officePaper)
|
||||||
|
{
|
||||||
|
Content = content;
|
||||||
|
OfficePaper = officePaper;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class FaxFileMessageValidation
|
||||||
|
{
|
||||||
|
public const int MaxContentSize = 10000;
|
||||||
|
}
|
||||||
|
|
||||||
[Serializable, NetSerializable]
|
[Serializable, NetSerializable]
|
||||||
public sealed class FaxCopyMessage : BoundUserInterfaceMessage
|
public sealed class FaxCopyMessage : BoundUserInterfaceMessage
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ fax-machine-dialog-rename = Rename
|
|||||||
fax-machine-dialog-field-name = Name
|
fax-machine-dialog-field-name = Name
|
||||||
|
|
||||||
fax-machine-ui-window = Fax Machine
|
fax-machine-ui-window = Fax Machine
|
||||||
|
fax-machine-ui-file-button = Print File
|
||||||
|
fax-machine-ui-paper-button-normal = Normal Paper
|
||||||
|
fax-machine-ui-paper-button-office = Office Paper
|
||||||
fax-machine-ui-copy-button = Copy
|
fax-machine-ui-copy-button = Copy
|
||||||
fax-machine-ui-send-button = Send
|
fax-machine-ui-send-button = Send
|
||||||
fax-machine-ui-refresh-button = Refresh
|
fax-machine-ui-refresh-button = Refresh
|
||||||
@@ -19,3 +22,5 @@ fax-machine-ui-paper-inserted = Paper in tray
|
|||||||
fax-machine-ui-paper-not-inserted = No paper
|
fax-machine-ui-paper-not-inserted = No paper
|
||||||
|
|
||||||
fax-machine-chat-notify = Received new fax message from "{$fax}" fax
|
fax-machine-chat-notify = Received new fax message from "{$fax}" fax
|
||||||
|
|
||||||
|
fax-machine-printed-paper-name = printed paper
|
||||||
|
|||||||
Reference in New Issue
Block a user