Files
tbd-station-14/Content.Client/Anomaly/Ui/AnomalyGeneratorWindow.xaml.cs
Pieter-Jan Briers 68ce53ae17 Random spontaneous cleanup PR (#25131)
* Use new Subs.CVar helper

Removes manual config OnValueChanged calls, removes need to remember to manually unsubscribe.

This both reduces boilerplate and fixes many issues where subscriptions weren't removed on entity system shutdown.

* Fix a bunch of warnings

* More warning fixes

* Use new DateTime serializer to get rid of ISerializationHooks in changelog code.

* Get rid of some more ISerializationHooks for enums

* And a little more

* Apply suggestions from code review

Co-authored-by: 0x6273 <0x40@keemail.me>

---------

Co-authored-by: 0x6273 <0x40@keemail.me>
2024-02-13 16:48:39 -05:00

83 lines
2.4 KiB
C#

using Content.Client.Message;
using Content.Shared.Anomaly;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Timing;
using FancyWindow = Content.Client.UserInterface.Controls.FancyWindow;
namespace Content.Client.Anomaly.Ui;
[GenerateTypedNameReferences]
public sealed partial class AnomalyGeneratorWindow : FancyWindow
{
[Dependency] private readonly IGameTiming _timing = default!;
private TimeSpan _cooldownEnd = TimeSpan.Zero;
private bool _hasEnoughFuel;
public Action? OnGenerateButtonPressed;
public AnomalyGeneratorWindow(EntityUid gen)
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
EntityView.SetEntity(gen);
EntityView.SpriteOffset = false;
GenerateButton.OnPressed += _ => OnGenerateButtonPressed?.Invoke();
}
public void UpdateState(AnomalyGeneratorUserInterfaceState state)
{
_cooldownEnd = state.CooldownEndTime;
_hasEnoughFuel = state.FuelCost <= state.FuelAmount;
var fuelCompletion = Math.Clamp((float) state.FuelAmount / state.FuelCost, 0f, 1f);
FuelBar.Value = fuelCompletion;
var charges = state.FuelAmount / state.FuelCost;
FuelText.Text = Loc.GetString("anomaly-generator-charges", ("charges", charges));
UpdateTimer();
UpdateReady(); // yes this can trigger twice. no i don't care
}
public void UpdateTimer()
{
if (_timing.CurTime > _cooldownEnd)
{
CooldownLabel.SetMarkup(Loc.GetString("anomaly-generator-no-cooldown"));
}
else
{
var timeLeft = _cooldownEnd - _timing.CurTime;
var timeString = $"{timeLeft.Minutes:0}:{timeLeft.Seconds:00}";
CooldownLabel.SetMarkup(Loc.GetString("anomaly-generator-cooldown", ("time", timeString)));
UpdateReady();
}
}
public void UpdateReady()
{
var ready = _hasEnoughFuel && _timing.CurTime > _cooldownEnd;
var msg = ready
? Loc.GetString("anomaly-generator-yes-fire")
: Loc.GetString("anomaly-generator-no-fire");
ReadyLabel.SetMarkup(msg);
GenerateButton.Disabled = !ready;
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
UpdateTimer();
}
}