Portable Generator Rework (#19302)
This commit is contained in:
committed by
GitHub
parent
50828363fe
commit
bf16698efa
@@ -1,3 +1,4 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Robust.Client.Graphics;
|
||||
@@ -51,4 +52,58 @@ public sealed class DoAfterSystem : SharedDoAfterSystem
|
||||
var handsQuery = GetEntityQuery<HandsComponent>();
|
||||
Update(playerEntity.Value, active, comp, time, xformQuery, handsQuery);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try to find an active do-after being executed by the local player.
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity the do after must be targeting (<see cref="DoAfterArgs.Target"/>)</param>
|
||||
/// <param name="doAfter">The found do-after.</param>
|
||||
/// <param name="event">The event to be raised on the found do-after when it completes.</param>
|
||||
/// <param name="progress">The progress of the found do-after, from 0 to 1.</param>
|
||||
/// <typeparam name="T">The type of event that must be raised by the found do-after.</typeparam>
|
||||
/// <returns>True if a do-after was found.</returns>
|
||||
public bool TryFindActiveDoAfter<T>(
|
||||
EntityUid entity,
|
||||
[NotNullWhen(true)] out Shared.DoAfter.DoAfter? doAfter,
|
||||
[NotNullWhen(true)] out T? @event,
|
||||
out float progress)
|
||||
where T : DoAfterEvent
|
||||
{
|
||||
var playerEntity = _player.LocalPlayer?.ControlledEntity;
|
||||
|
||||
doAfter = null;
|
||||
@event = null;
|
||||
progress = default;
|
||||
|
||||
if (!TryComp(playerEntity, out ActiveDoAfterComponent? active))
|
||||
return false;
|
||||
|
||||
if (_metadata.EntityPaused(playerEntity.Value))
|
||||
return false;
|
||||
|
||||
var comp = Comp<DoAfterComponent>(playerEntity.Value);
|
||||
|
||||
var time = GameTiming.CurTime;
|
||||
|
||||
foreach (var candidate in comp.DoAfters.Values)
|
||||
{
|
||||
if (candidate.Cancelled)
|
||||
continue;
|
||||
|
||||
if (candidate.Args.Target != entity)
|
||||
continue;
|
||||
|
||||
if (candidate.Args.Event is not T candidateEvent)
|
||||
continue;
|
||||
|
||||
@event = candidateEvent;
|
||||
doAfter = candidate;
|
||||
var elapsed = time - doAfter.StartTime;
|
||||
progress = (float) Math.Min(1, elapsed.TotalSeconds / doAfter.Args.Delay.TotalSeconds);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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">
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -369,13 +369,13 @@ namespace Content.Client.Stylesheets
|
||||
{
|
||||
BackgroundColor = new Color(0.25f, 0.25f, 0.25f)
|
||||
};
|
||||
progressBarBackground.SetContentMarginOverride(StyleBox.Margin.Vertical, 5);
|
||||
progressBarBackground.SetContentMarginOverride(StyleBox.Margin.Vertical, 14.5f);
|
||||
|
||||
var progressBarForeground = new StyleBoxFlat
|
||||
{
|
||||
BackgroundColor = new Color(0.25f, 0.50f, 0.25f)
|
||||
};
|
||||
progressBarForeground.SetContentMarginOverride(StyleBox.Margin.Vertical, 5);
|
||||
progressBarForeground.SetContentMarginOverride(StyleBox.Margin.Vertical, 14.5f);
|
||||
|
||||
// CheckBox
|
||||
var checkBoxTextureChecked = resCache.GetTexture("/Textures/Interface/Nano/checkbox_checked.svg.96dpi.png");
|
||||
|
||||
@@ -52,13 +52,13 @@ namespace Content.Client.Stylesheets
|
||||
{
|
||||
BackgroundColor = new Color(0.25f, 0.25f, 0.25f)
|
||||
};
|
||||
progressBarBackground.SetContentMarginOverride(StyleBox.Margin.Vertical, 5);
|
||||
progressBarBackground.SetContentMarginOverride(StyleBox.Margin.Vertical, 14.5f);
|
||||
|
||||
var progressBarForeground = new StyleBoxFlat
|
||||
{
|
||||
BackgroundColor = new Color(0.25f, 0.50f, 0.25f)
|
||||
};
|
||||
progressBarForeground.SetContentMarginOverride(StyleBox.Margin.Vertical, 5);
|
||||
progressBarForeground.SetContentMarginOverride(StyleBox.Margin.Vertical, 14.5f);
|
||||
|
||||
var textureInvertedTriangle = resCache.GetTexture("/Textures/Interface/Nano/inverted_triangle.svg.png");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user