Portable Generator Rework (#19302)

This commit is contained in:
Pieter-Jan Briers
2023-08-25 20:40:42 +02:00
committed by GitHub
parent 50828363fe
commit bf16698efa
73 changed files with 1933 additions and 473 deletions

View File

@@ -1,22 +1,39 @@
<controls:FancyWindow xmlns="https://spacestation14.io"
xmlns:cc="clr-namespace:Content.Client.Administration.UI.CustomControls"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
MinSize="350 130"
SetSize="360 180"
Title="{Loc 'generator-ui-title'}">
MinSize="450 235"
SetSize="450 235"
Resizable="False"
Title="{Loc 'portable-generator-ui-title'}">
<BoxContainer Margin="4 0" Orientation="Horizontal">
<BoxContainer Orientation="Vertical" HorizontalExpand="True" SizeFlagsStretchRatio="2" VerticalAlignment="Center" Margin="5">
<BoxContainer Orientation="Vertical" HorizontalExpand="True" SizeFlagsStretchRatio="2" VerticalAlignment="Top" Margin="5">
<GridContainer Margin="2 0 0 0" Columns="2" HorizontalExpand="True">
<Label Name="StatusLabel" Text="{Loc 'portable-generator-ui-power-switch'}" HorizontalExpand="True" />
<Control MinWidth="120">
<Button Name="StartButton" Text="{Loc 'portable-generator-ui-start'}" />
<Button Name="StopButton" Text="{Loc 'portable-generator-ui-stop'}" />
<ProgressBar Name="StartProgress" MaxValue="1" />
<Label Name="LabelUnanchored" Text="{Loc 'portable-generator-ui-unanchored'}" />
</Control>
<!-- Power -->
<Label Text="{Loc 'generator-ui-target-power-label'}"/>
<Label Text="{Loc 'portable-generator-ui-target-power-label'}"/>
<SpinBox Name="TargetPower" HorizontalExpand="True"/>
<Label Text="{Loc 'generator-ui-efficiency-label'}"/>
<Label Name="Efficiency" Text="???%" HorizontalExpand="True"/>
<Label Text="{Loc 'generator-ui-fuel-use-label'}"/>
<Label Text="{Loc 'portable-generator-ui-efficiency-label'}"/>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Label Name="Efficiency" Text="???%" />
<Label Name="Eta" HorizontalExpand="True" Margin="4 0 0 0" />
</BoxContainer>
<Label Text="{Loc 'portable-generator-ui-fuel-use-label'}"/>
<ProgressBar Name="FuelFraction" MinValue="0" MaxValue="1" HorizontalExpand="True"/>
<Label Text="{Loc 'generator-ui-fuel-left-label'}"/>
<Label Name="FuelLeft" Text="0" HorizontalExpand="True"/>
<Label Text="{Loc 'portable-generator-ui-fuel-left-label'}"/>
<BoxContainer Orientation="Horizontal" HorizontalExpand="True">
<Label Name="FuelLeft" Text="0" HorizontalExpand="True"/>
<Button Name="FuelEject" Text="{Loc 'portable-generator-ui-eject'}" />
</BoxContainer>
<Label Name="OutputSwitchLabel" Text="{Loc 'portable-generator-ui-switch'}" Visible="False" />
<Button Name="OutputSwitchButton" Visible="False" />
</GridContainer>
<Label Margin="2 0 0 0" Name="CloggedLabel" FontColorOverride="Red" Text="{Loc 'portable-generator-ui-clogged'}" />
</BoxContainer>
<cc:VSeparator StyleClasses="LowDivider"/>
<PanelContainer Margin="12 0 0 0" StyleClasses="Inset" VerticalAlignment="Center">

View File

@@ -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);
}
}

View File

@@ -0,0 +1,62 @@
using Content.Shared.Power.Generator;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
namespace Content.Client.Power.Generator;
[UsedImplicitly]
public sealed class PortableGeneratorBoundUserInterface : BoundUserInterface
{
private GeneratorWindow? _window;
public PortableGeneratorBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
_window = new GeneratorWindow(this, Owner);
_window.OpenCenteredLeft();
_window.OnClose += Close;
}
protected override void UpdateState(BoundUserInterfaceState state)
{
if (state is not PortableGeneratorComponentBuiState msg)
return;
_window?.Update(msg);
}
protected override void Dispose(bool disposing)
{
_window?.Dispose();
}
public void SetTargetPower(int target)
{
SendMessage(new PortableGeneratorSetTargetPowerMessage(target));
}
public void Start()
{
SendMessage(new PortableGeneratorStartMessage());
}
public void Stop()
{
SendMessage(new PortableGeneratorStopMessage());
}
public void SwitchOutput()
{
SendMessage(new PortableGeneratorSwitchOutputMessage());
}
public void EjectFuel()
{
SendMessage(new PortableGeneratorEjectFuelMessage());
}
}

View File

@@ -1,42 +0,0 @@
using Content.Shared.Power.Generator;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
namespace Content.Client.Power.Generator;
[UsedImplicitly]
public sealed class SolidFuelGeneratorBoundUserInterface : BoundUserInterface
{
private GeneratorWindow? _window;
public SolidFuelGeneratorBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
_window = new GeneratorWindow(this, Owner);
_window.OpenCenteredLeft();
_window.OnClose += Close;
}
protected override void UpdateState(BoundUserInterfaceState state)
{
if (state is not SolidFuelGeneratorComponentBuiState msg)
return;
_window?.Update(msg);
}
protected override void Dispose(bool disposing)
{
_window?.Dispose();
}
public void SetTargetPower(int target)
{
SendMessage(new SetTargetPowerMessage(target));
}
}