Revert "Remove some BUI boilerplate" (#30214)

Revert "Remove some BUI boilerplate (#28399)"

This reverts commit cbf329a82d.
This commit is contained in:
Nemanja
2024-07-20 20:42:27 -04:00
committed by GitHub
parent 6d664c9157
commit cb0ba66be3
137 changed files with 1755 additions and 1096 deletions

View File

@@ -9,39 +9,35 @@ 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 EntityUid _entity;
private readonly SharedPowerSwitchableSystem _switchable;
private readonly FuelGeneratorComponent? _component;
private PortableGeneratorComponentBuiState? _lastState;
public float? MaximumPower;
public event Action<int>? OnPower;
public event Action<bool>? OnState;
public event Action? OnSwitchOutput;
public event Action? OnEjectFuel;
public GeneratorWindow()
public GeneratorWindow(PortableGeneratorBoundUserInterface bui, EntityUid entity)
{
_entity = entity;
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_entityManager.TryGetComponent(entity, out _component);
_switchable = _entityManager.System<SharedPowerSwitchableSystem>();
EntityView.SetEntity(entity);
TargetPower.IsValid += IsValid;
TargetPower.ValueChanged += (args) =>
{
OnPower?.Invoke(args.Value);
bui.SetTargetPower(args.Value);
};
StartButton.OnPressed += _ => OnState?.Invoke(true);
StopButton.OnPressed += _ => OnState?.Invoke(false);
OutputSwitchButton.OnPressed += _ => OnSwitchOutput?.Invoke();
FuelEject.OnPressed += _ => OnEjectFuel?.Invoke();
}
public void SetEntity(EntityUid entity)
{
_entity = entity;
EntityView.SetEntity(entity);
StartButton.OnPressed += _ => bui.Start();
StopButton.OnPressed += _ => bui.Stop();
OutputSwitchButton.OnPressed += _ => bui.SwitchOutput();
FuelEject.OnPressed += _ => bui.EjectFuel();
}
private bool IsValid(int arg)
@@ -49,7 +45,7 @@ public sealed partial class GeneratorWindow : FancyWindow
if (arg < 0)
return false;
if (arg > (MaximumPower / 1000.0f ?? 0))
if (arg > (_lastState?.MaximumPower / 1000.0f ?? 0))
return false;
return true;
@@ -57,17 +53,16 @@ public sealed partial class GeneratorWindow : FancyWindow
public void Update(PortableGeneratorComponentBuiState state)
{
MaximumPower = state.MaximumPower;
if (!_entityManager.TryGetComponent(_entity, out FuelGeneratorComponent? component))
if (_component == null)
return;
_lastState = state;
if (!TargetPower.LineEditControl.HasKeyboardFocus())
TargetPower.OverrideValue((int)(state.TargetPower / 1000.0f));
var efficiency = SharedGeneratorSystem.CalcFuelEfficiency(state.TargetPower, state.OptimalPower, component);
var efficiency = SharedGeneratorSystem.CalcFuelEfficiency(state.TargetPower, state.OptimalPower, _component);
Efficiency.Text = efficiency.ToString("P1");
var burnRate = component.OptimalBurnRate / efficiency;
var burnRate = _component.OptimalBurnRate / efficiency;
var left = state.RemainingFuel / burnRate;
Eta.Text = Loc.GetString(
@@ -107,15 +102,14 @@ public sealed partial class GeneratorWindow : FancyWindow
}
var canSwitch = _entityManager.TryGetComponent(_entity, out PowerSwitchableComponent? switchable);
var switcher = _entityManager.System<SharedPowerSwitchableSystem>();
OutputSwitchLabel.Visible = canSwitch;
OutputSwitchButton.Visible = canSwitch;
if (switchable != null)
{
var voltage = switcher.VoltageString(switcher.GetVoltage(_entity, switchable));
var voltage = _switchable.VoltageString(_switchable.GetVoltage(_entity, switchable));
OutputSwitchLabel.Text = Loc.GetString("portable-generator-ui-current-output", ("voltage", voltage));
var nextVoltage = switcher.VoltageString(switcher.GetNextVoltage(_entity, switchable));
var nextVoltage = _switchable.VoltageString(_switchable.GetNextVoltage(_entity, switchable));
OutputSwitchButton.Text = Loc.GetString("power-switchable-switch-voltage", ("voltage", nextVoltage));
OutputSwitchButton.Disabled = state.On;
}