Portable Generator Rework (#19302)
This commit is contained in:
committed by
GitHub
parent
50828363fe
commit
bf16698efa
@@ -1,4 +1,5 @@
|
||||
using Content.Client.UserInterface.Controls;
|
||||
using Content.Client.DoAfter;
|
||||
using Content.Client.UserInterface.Controls;
|
||||
using Content.Shared.Power.Generator;
|
||||
using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.UserInterface.XAML;
|
||||
@@ -8,24 +9,33 @@ namespace Content.Client.Power.Generator;
|
||||
[GenerateTypedNameReferences]
|
||||
public sealed partial class GeneratorWindow : FancyWindow
|
||||
{
|
||||
private readonly EntityUid _entity;
|
||||
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
[Dependency] private readonly ILocalizationManager _loc = default!;
|
||||
|
||||
private readonly FuelGeneratorComponent? _component;
|
||||
private SolidFuelGeneratorComponentBuiState? _lastState;
|
||||
private PortableGeneratorComponentBuiState? _lastState;
|
||||
|
||||
public GeneratorWindow(SolidFuelGeneratorBoundUserInterface bui, EntityUid vis)
|
||||
public GeneratorWindow(PortableGeneratorBoundUserInterface bui, EntityUid entity)
|
||||
{
|
||||
_entity = entity;
|
||||
RobustXamlLoader.Load(this);
|
||||
IoCManager.InjectDependencies(this);
|
||||
|
||||
_entityManager.TryGetComponent(vis, out _component);
|
||||
_entityManager.TryGetComponent(entity, out _component);
|
||||
|
||||
EntityView.SetEntity(vis);
|
||||
EntityView.SetEntity(entity);
|
||||
TargetPower.IsValid += IsValid;
|
||||
TargetPower.ValueChanged += (args) =>
|
||||
{
|
||||
bui.SetTargetPower(args.Value);
|
||||
};
|
||||
|
||||
StartButton.OnPressed += _ => bui.Start();
|
||||
StopButton.OnPressed += _ => bui.Stop();
|
||||
OutputSwitchButton.OnPressed += _ => bui.SwitchOutput();
|
||||
FuelEject.OnPressed += _ => bui.EjectFuel();
|
||||
}
|
||||
|
||||
private bool IsValid(int arg)
|
||||
@@ -39,19 +49,76 @@ public sealed partial class GeneratorWindow : FancyWindow
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Update(SolidFuelGeneratorComponentBuiState state)
|
||||
public void Update(PortableGeneratorComponentBuiState state)
|
||||
{
|
||||
if (_component == null)
|
||||
return;
|
||||
|
||||
var oldState = _lastState;
|
||||
_lastState = state;
|
||||
// ReSharper disable once CompareOfFloatsByEqualityOperator
|
||||
if (oldState?.TargetPower != state.TargetPower)
|
||||
if (!TargetPower.LineEditControl.HasKeyboardFocus())
|
||||
TargetPower.OverrideValue((int)(state.TargetPower / 1000.0f));
|
||||
Efficiency.Text = SharedGeneratorSystem.CalcFuelEfficiency(state.TargetPower, state.OptimalPower, _component).ToString("P1");
|
||||
var efficiency = SharedGeneratorSystem.CalcFuelEfficiency(state.TargetPower, state.OptimalPower, _component);
|
||||
Efficiency.Text = efficiency.ToString("P1");
|
||||
|
||||
var burnRate = _component.OptimalBurnRate / efficiency;
|
||||
var left = state.RemainingFuel / burnRate;
|
||||
|
||||
Eta.Text = Loc.GetString(
|
||||
"portable-generator-ui-eta",
|
||||
("minutes", Math.Ceiling(left / 60.0)));
|
||||
FuelFraction.Value = state.RemainingFuel - (int) state.RemainingFuel;
|
||||
FuelLeft.Text = ((int) MathF.Floor(state.RemainingFuel)).ToString();
|
||||
|
||||
var progress = 0f;
|
||||
|
||||
var unanchored = !_entityManager.GetComponent<TransformComponent>(_entity).Anchored;
|
||||
var starting = !unanchored && TryGetStartProgress(out progress);
|
||||
var on = !unanchored && !starting && state.On;
|
||||
var off = !unanchored && !starting && !state.On;
|
||||
|
||||
LabelUnanchored.Visible = unanchored;
|
||||
StartProgress.Visible = starting;
|
||||
StopButton.Visible = on;
|
||||
StartButton.Visible = off;
|
||||
|
||||
if (starting)
|
||||
{
|
||||
StatusLabel.Text = _loc.GetString("portable-generator-ui-status-starting");
|
||||
StatusLabel.SetOnlyStyleClass("Caution");
|
||||
|
||||
StartProgress.Value = progress;
|
||||
}
|
||||
else if (on)
|
||||
{
|
||||
StatusLabel.Text = _loc.GetString("portable-generator-ui-status-running");
|
||||
StatusLabel.SetOnlyStyleClass("Good");
|
||||
}
|
||||
else
|
||||
{
|
||||
StatusLabel.Text = _loc.GetString("portable-generator-ui-status-stopped");
|
||||
StatusLabel.SetOnlyStyleClass("Danger");
|
||||
}
|
||||
|
||||
var canSwitch = _entityManager.TryGetComponent(_entity, out PowerSwitchableGeneratorComponent? switchable);
|
||||
OutputSwitchLabel.Visible = canSwitch;
|
||||
OutputSwitchButton.Visible = canSwitch;
|
||||
|
||||
if (canSwitch)
|
||||
{
|
||||
var isHV = switchable!.ActiveOutput == PowerSwitchableGeneratorOutput.HV;
|
||||
OutputSwitchLabel.Text =
|
||||
Loc.GetString(isHV ? "portable-generator-ui-switch-hv" : "portable-generator-ui-switch-mv");
|
||||
OutputSwitchButton.Text =
|
||||
Loc.GetString(isHV ? "portable-generator-ui-switch-to-mv" : "portable-generator-ui-switch-to-hv");
|
||||
OutputSwitchButton.Disabled = state.On;
|
||||
}
|
||||
|
||||
CloggedLabel.Visible = state.Clogged;
|
||||
}
|
||||
|
||||
private bool TryGetStartProgress(out float progress)
|
||||
{
|
||||
var doAfterSystem = _entityManager.EntitySysManager.GetEntitySystem<DoAfterSystem>();
|
||||
return doAfterSystem.TryFindActiveDoAfter<GeneratorStartedEvent>(_entity, out _, out _, out progress);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user