AnomalyGeneratorWindow.xaml.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Content.Client.Message;
  2. using Content.Shared.Anomaly;
  3. using Robust.Client.AutoGenerated;
  4. using Robust.Client.GameObjects;
  5. using Robust.Client.UserInterface.XAML;
  6. using Robust.Shared.Timing;
  7. using FancyWindow = Content.Client.UserInterface.Controls.FancyWindow;
  8. namespace Content.Client.Anomaly.Ui;
  9. [GenerateTypedNameReferences]
  10. public sealed partial class AnomalyGeneratorWindow : FancyWindow
  11. {
  12. [Dependency] private readonly IGameTiming _timing = default!;
  13. private TimeSpan _cooldownEnd = TimeSpan.Zero;
  14. private bool _hasEnoughFuel;
  15. public Action? OnGenerateButtonPressed;
  16. public AnomalyGeneratorWindow()
  17. {
  18. RobustXamlLoader.Load(this);
  19. IoCManager.InjectDependencies(this);
  20. EntityView.SpriteOffset = false;
  21. GenerateButton.OnPressed += _ => OnGenerateButtonPressed?.Invoke();
  22. }
  23. public void SetEntity(EntityUid uid)
  24. {
  25. EntityView.SetEntity(uid);
  26. }
  27. public void UpdateState(AnomalyGeneratorUserInterfaceState state)
  28. {
  29. _cooldownEnd = state.CooldownEndTime;
  30. _hasEnoughFuel = state.FuelCost <= state.FuelAmount;
  31. var fuelCompletion = Math.Clamp((float) state.FuelAmount / state.FuelCost, 0f, 1f);
  32. FuelBar.Value = fuelCompletion;
  33. var charges = state.FuelAmount / state.FuelCost;
  34. FuelText.Text = Loc.GetString("anomaly-generator-charges", ("charges", charges));
  35. UpdateTimer();
  36. UpdateReady(); // yes this can trigger twice. no i don't care
  37. }
  38. public void UpdateTimer()
  39. {
  40. if (_timing.CurTime > _cooldownEnd)
  41. {
  42. CooldownLabel.SetMarkup(Loc.GetString("anomaly-generator-no-cooldown"));
  43. }
  44. else
  45. {
  46. var timeLeft = _cooldownEnd - _timing.CurTime;
  47. var timeString = $"{timeLeft.Minutes:0}:{timeLeft.Seconds:00}";
  48. CooldownLabel.SetMarkup(Loc.GetString("anomaly-generator-cooldown", ("time", timeString)));
  49. UpdateReady();
  50. }
  51. }
  52. public void UpdateReady()
  53. {
  54. var ready = _hasEnoughFuel && _timing.CurTime > _cooldownEnd;
  55. var msg = ready
  56. ? Loc.GetString("anomaly-generator-yes-fire")
  57. : Loc.GetString("anomaly-generator-no-fire");
  58. ReadyLabel.SetMarkup(msg);
  59. GenerateButton.Disabled = !ready;
  60. }
  61. protected override void FrameUpdate(FrameEventArgs args)
  62. {
  63. base.FrameUpdate(args);
  64. UpdateTimer();
  65. }
  66. }