GeneratorWindow.xaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Content.Client.DoAfter;
  2. using Content.Client.UserInterface.Controls;
  3. using Content.Shared.Power.Generator;
  4. using Robust.Client.AutoGenerated;
  5. using Robust.Client.UserInterface.XAML;
  6. using Robust.Shared.Network;
  7. namespace Content.Client.Power.Generator;
  8. [GenerateTypedNameReferences]
  9. public sealed partial class GeneratorWindow : FancyWindow
  10. {
  11. [Dependency] private readonly IEntityManager _entityManager = default!;
  12. [Dependency] private readonly ILocalizationManager _loc = default!;
  13. private EntityUid _entity;
  14. public float? MaximumPower;
  15. public event Action<int>? OnPower;
  16. public event Action<bool>? OnState;
  17. public event Action? OnSwitchOutput;
  18. public event Action? OnEjectFuel;
  19. public GeneratorWindow()
  20. {
  21. RobustXamlLoader.Load(this);
  22. IoCManager.InjectDependencies(this);
  23. TargetPower.IsValid += IsValid;
  24. TargetPower.ValueChanged += (args) =>
  25. {
  26. OnPower?.Invoke(args.Value);
  27. };
  28. StartButton.OnPressed += _ => OnState?.Invoke(true);
  29. StopButton.OnPressed += _ => OnState?.Invoke(false);
  30. OutputSwitchButton.OnPressed += _ => OnSwitchOutput?.Invoke();
  31. FuelEject.OnPressed += _ => OnEjectFuel?.Invoke();
  32. }
  33. public void SetEntity(EntityUid entity)
  34. {
  35. _entity = entity;
  36. EntityView.SetEntity(entity);
  37. }
  38. private bool IsValid(int arg)
  39. {
  40. if (arg < 0)
  41. return false;
  42. if (arg > (MaximumPower / 1000.0f ?? 0))
  43. return false;
  44. return true;
  45. }
  46. public void Update(PortableGeneratorComponentBuiState state)
  47. {
  48. MaximumPower = state.MaximumPower;
  49. if (!_entityManager.TryGetComponent(_entity, out FuelGeneratorComponent? component))
  50. return;
  51. if (!TargetPower.LineEditControl.HasKeyboardFocus())
  52. TargetPower.OverrideValue((int)(state.TargetPower / 1000.0f));
  53. var efficiency = SharedGeneratorSystem.CalcFuelEfficiency(state.TargetPower, state.OptimalPower, component);
  54. Efficiency.Text = efficiency.ToString("P1");
  55. var burnRate = component.OptimalBurnRate / efficiency;
  56. var left = state.RemainingFuel / burnRate;
  57. Eta.Text = Loc.GetString(
  58. "portable-generator-ui-eta",
  59. ("minutes", Math.Ceiling(left / 60.0)));
  60. FuelFraction.Value = state.RemainingFuel - (int) state.RemainingFuel;
  61. FuelLeft.Text = ((int) MathF.Floor(state.RemainingFuel)).ToString();
  62. var progress = 0f;
  63. var unanchored = !_entityManager.GetComponent<TransformComponent>(_entity).Anchored;
  64. var starting = !unanchored && TryGetStartProgress(out progress);
  65. var on = !unanchored && !starting && state.On;
  66. var off = !unanchored && !starting && !state.On;
  67. LabelUnanchored.Visible = unanchored;
  68. StartProgress.Visible = starting;
  69. StopButton.Visible = on;
  70. StartButton.Visible = off;
  71. if (starting)
  72. {
  73. StatusLabel.Text = _loc.GetString("portable-generator-ui-status-starting");
  74. StatusLabel.SetOnlyStyleClass("Caution");
  75. StartProgress.Value = progress;
  76. }
  77. else if (on)
  78. {
  79. StatusLabel.Text = _loc.GetString("portable-generator-ui-status-running");
  80. StatusLabel.SetOnlyStyleClass("Good");
  81. }
  82. else
  83. {
  84. StatusLabel.Text = _loc.GetString("portable-generator-ui-status-stopped");
  85. StatusLabel.SetOnlyStyleClass("Danger");
  86. }
  87. var canSwitch = _entityManager.TryGetComponent(_entity, out PowerSwitchableComponent? switchable);
  88. var switcher = _entityManager.System<SharedPowerSwitchableSystem>();
  89. OutputSwitchLabel.Visible = canSwitch;
  90. OutputSwitchButton.Visible = canSwitch;
  91. if (switchable != null)
  92. {
  93. var voltage = switcher.VoltageString(switcher.GetVoltage(_entity, switchable));
  94. OutputSwitchLabel.Text = Loc.GetString("portable-generator-ui-current-output", ("voltage", voltage));
  95. var nextVoltage = switcher.VoltageString(switcher.GetNextVoltage(_entity, switchable));
  96. OutputSwitchButton.Text = Loc.GetString("power-switchable-switch-voltage", ("voltage", nextVoltage));
  97. OutputSwitchButton.Disabled = state.On;
  98. }
  99. CloggedLabel.Visible = state.Clogged;
  100. if (state.NetworkStats is { } netStats)
  101. {
  102. NetworkStats.Text = Loc.GetString(
  103. "portable-generator-ui-network-stats-value",
  104. ("load", netStats.Load),
  105. ("supply", netStats.Supply));
  106. var good = netStats.Load <= netStats.Supply;
  107. NetworkStats.SetOnlyStyleClass(good ? "Good" : "Caution");
  108. }
  109. else
  110. {
  111. NetworkStats.Text = Loc.GetString("portable-generator-ui-network-stats-not-connected");
  112. NetworkStats.StyleClasses.Clear();
  113. }
  114. }
  115. private bool TryGetStartProgress(out float progress)
  116. {
  117. // Try to check progress of auto-revving first
  118. if (_entityManager.TryGetComponent<ActiveGeneratorRevvingComponent>(_entity, out var activeGeneratorRevvingComponent) && _entityManager.TryGetComponent<PortableGeneratorComponent>(_entity, out var portableGeneratorComponent))
  119. {
  120. var calculatedProgress = activeGeneratorRevvingComponent.CurrentTime / portableGeneratorComponent.StartTime;
  121. progress = (float) calculatedProgress;
  122. return true;
  123. }
  124. var doAfterSystem = _entityManager.EntitySysManager.GetEntitySystem<DoAfterSystem>();
  125. return doAfterSystem.TryFindActiveDoAfter<GeneratorStartedEvent>(_entity, out _, out _, out progress);
  126. }
  127. }