StoreWithdrawWindow.xaml.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Linq;
  2. using Robust.Client.AutoGenerated;
  3. using Robust.Client.UserInterface.CustomControls;
  4. using Robust.Client.UserInterface.XAML;
  5. using Content.Shared.FixedPoint;
  6. using Content.Shared.Store;
  7. using Robust.Client.UserInterface.Controls;
  8. using Robust.Shared.Prototypes;
  9. namespace Content.Client.Store.Ui;
  10. /// <summary>
  11. /// Window to select amount TC to withdraw from Uplink account
  12. /// Used as sub-window in Uplink UI
  13. /// </summary>
  14. [GenerateTypedNameReferences]
  15. public sealed partial class StoreWithdrawWindow : DefaultWindow
  16. {
  17. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  18. private Dictionary<CurrencyPrototype, FixedPoint2> _validCurrencies = new();
  19. private HashSet<CurrencyWithdrawButton> _buttons = new();
  20. public event Action<BaseButton.ButtonEventArgs, string, int>? OnWithdrawAttempt;
  21. public StoreWithdrawWindow()
  22. {
  23. RobustXamlLoader.Load(this);
  24. IoCManager.InjectDependencies(this);
  25. }
  26. public void CreateCurrencyButtons(Dictionary<ProtoId<CurrencyPrototype>, FixedPoint2> balance)
  27. {
  28. _validCurrencies.Clear();
  29. foreach (var currency in balance)
  30. {
  31. if (!_prototypeManager.TryIndex(currency.Key, out var proto))
  32. continue;
  33. _validCurrencies.Add(proto, currency.Value);
  34. }
  35. //this shouldn't ever happen but w/e
  36. if (_validCurrencies.Count < 1)
  37. return;
  38. ButtonContainer.Children.Clear();
  39. _buttons.Clear();
  40. foreach (var currency in _validCurrencies)
  41. {
  42. if (!currency.Key.CanWithdraw)
  43. continue;
  44. var button = new CurrencyWithdrawButton()
  45. {
  46. Id = currency.Key.ID,
  47. Amount = currency.Value,
  48. MinHeight = 20,
  49. Text = Loc.GetString("store-withdraw-button-ui", ("currency",Loc.GetString(currency.Key.DisplayName, ("amount", currency.Value)))),
  50. Disabled = false,
  51. };
  52. button.OnPressed += args =>
  53. {
  54. OnWithdrawAttempt?.Invoke(args, button.Id, WithdrawSlider.Value);
  55. Close();
  56. };
  57. _buttons.Add(button);
  58. ButtonContainer.AddChild(button);
  59. }
  60. var maxWithdrawAmount = _validCurrencies.Values.Max().Int();
  61. // setup withdraw slider
  62. WithdrawSlider.MinValue = 1;
  63. WithdrawSlider.MaxValue = maxWithdrawAmount;
  64. WithdrawSlider.OnValueChanged += OnValueChanged;
  65. OnValueChanged(WithdrawSlider.Value);
  66. }
  67. public void OnValueChanged(int i)
  68. {
  69. foreach (var button in _buttons)
  70. {
  71. button.Disabled = button.Amount < WithdrawSlider.Value;
  72. }
  73. }
  74. private sealed class CurrencyWithdrawButton : Button
  75. {
  76. public string? Id;
  77. public FixedPoint2 Amount = FixedPoint2.Zero;
  78. }
  79. }