58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
using Content.Client.UserInterface.Controls;
|
|
using Content.Shared.Power.Generator;
|
|
using Robust.Client.AutoGenerated;
|
|
using Robust.Client.UserInterface.XAML;
|
|
|
|
namespace Content.Client.Power.Generator;
|
|
|
|
[GenerateTypedNameReferences]
|
|
public sealed partial class GeneratorWindow : FancyWindow
|
|
{
|
|
[Dependency] private readonly IEntityManager _entityManager = default!;
|
|
|
|
private readonly FuelGeneratorComponent? _component;
|
|
private SolidFuelGeneratorComponentBuiState? _lastState;
|
|
|
|
public GeneratorWindow(SolidFuelGeneratorBoundUserInterface bui, EntityUid vis)
|
|
{
|
|
RobustXamlLoader.Load(this);
|
|
IoCManager.InjectDependencies(this);
|
|
|
|
_entityManager.TryGetComponent(vis, out _component);
|
|
|
|
EntityView.SetEntity(vis);
|
|
TargetPower.IsValid += IsValid;
|
|
TargetPower.ValueChanged += (args) =>
|
|
{
|
|
bui.SetTargetPower(args.Value);
|
|
};
|
|
}
|
|
|
|
private bool IsValid(int arg)
|
|
{
|
|
if (arg < 0)
|
|
return false;
|
|
|
|
if (arg > (_lastState?.MaximumPower / 1000.0f ?? 0))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
public void Update(SolidFuelGeneratorComponentBuiState state)
|
|
{
|
|
if (_component == null)
|
|
return;
|
|
|
|
var oldState = _lastState;
|
|
_lastState = state;
|
|
// ReSharper disable once CompareOfFloatsByEqualityOperator
|
|
if (oldState?.TargetPower != state.TargetPower)
|
|
TargetPower.OverrideValue((int)(state.TargetPower / 1000.0f));
|
|
Efficiency.Text = SharedGeneratorSystem.CalcFuelEfficiency(state.TargetPower, state.OptimalPower, _component).ToString("P1");
|
|
FuelFraction.Value = state.RemainingFuel - (int) state.RemainingFuel;
|
|
FuelLeft.Text = ((int) MathF.Floor(state.RemainingFuel)).ToString();
|
|
|
|
}
|
|
}
|