Remove some BUI boilerplate (#28399)

* Remove some BUI boilerplate

- The disposals overrides got removed due to the helper method handling it.
- Replace window creation with CreateWindow helper.
- Fixed some stinky code which would cause exceptions.

* More

* moar

* weh

* done

* More BUIs

* More updates

* weh

* moar

* look who it is

* weh

* merge

* weh

* fixes
This commit is contained in:
metalgearsloth
2024-07-20 15:40:16 +10:00
committed by GitHub
parent 4aba9ec131
commit cbf329a82d
137 changed files with 1094 additions and 1753 deletions

View File

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