Delta-v/Content.Client/Nyanotrasen/ReverseEngineering/ReverseEngineeringMachineMe...

88 lines
3.0 KiB
C#

using Content.Client.UserInterface.Controls;
using Content.Shared.ReverseEngineering;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.State;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Timing;
namespace Content.Client.Nyanotrasen.ReverseEngineering;
[GenerateTypedNameReferences]
public sealed partial class ReverseEngineeringMachineMenu : FancyWindow
{
private readonly IEntityManager _entMan;
private readonly IGameTiming _timing;
private readonly SharedReverseEngineeringSystem _revEng;
private readonly Entity<ReverseEngineeringMachineComponent> _owner;
public event Action? OnScanButtonPressed;
public event Action? OnSafetyButtonToggled;
public event Action? OnAutoScanButtonToggled;
public event Action? OnStopButtonPressed;
public event Action? OnEjectButtonPressed;
public ReverseEngineeringMachineMenu(EntityUid owner, IEntityManager entMan, IGameTiming timing)
{
RobustXamlLoader.Load(this);
_entMan = entMan;
_timing = timing;
_revEng = entMan.System<SharedReverseEngineeringSystem>();
_owner = (owner, entMan.GetComponent<ReverseEngineeringMachineComponent>(owner));
ScanButton.OnPressed += _ => OnScanButtonPressed?.Invoke();
SafetyButton.OnToggled += _ => OnSafetyButtonToggled?.Invoke();
AutoScanButton.OnToggled += _ => OnAutoScanButtonToggled?.Invoke();
StopButton.OnPressed += _ => OnStopButtonPressed?.Invoke();
EjectButton.OnPressed += _ => OnEjectButtonPressed?.Invoke();
}
private void UpdateArtifactIcon(EntityUid? uid)
{
if (uid == null)
{
ItemDisplay.Visible = false;
return;
}
ItemDisplay.Visible = true;
ItemDisplay.SetEntity(uid);
}
public void UpdateState(ReverseEngineeringMachineState state)
{
Information.SetMessage(state.ScanMessage);
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
var scanning = _revEng.IsActive(_owner);
var item = _revEng.GetItem(_owner);
ScanButton.Disabled = scanning || item == null;
StopButton.Disabled = !scanning;
SafetyButton.Pressed = _owner.Comp.SafetyOn;
AutoScanButton.Pressed = _owner.Comp.AutoScan;
EjectButton.Disabled = ScanButton.Disabled;
UpdateArtifactIcon(item);
ProgressBox.Visible = scanning;
if (!_entMan.TryGetComponent<ActiveReverseEngineeringMachineComponent>(_owner, out var active)
|| !_entMan.TryGetComponent<ReverseEngineeringComponent>(item, out var rev))
return;
TotalProgressBar.Value = (float) rev.Progress;
var remaining = Math.Max(active.NextProbe.TotalSeconds - _timing.CurTime.TotalSeconds, 0.0);
ProgressLabel.Text = Loc.GetString("analysis-console-progress-text", ("seconds", (int) remaining));
ProgressBar.Value = 1f - (float) (remaining / _owner.Comp.AnalysisDuration.TotalSeconds);
}
}