RequirementsSelector.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Numerics;
  2. using Content.Client.Stylesheets;
  3. using Content.Client.UserInterface.Controls;
  4. using Content.Shared.Guidebook;
  5. using Robust.Client.AutoGenerated;
  6. using Robust.Client.UserInterface.Controls;
  7. using Robust.Client.UserInterface.CustomControls;
  8. using Robust.Client.UserInterface.XAML;
  9. using Robust.Shared.Prototypes;
  10. using Robust.Shared.Utility;
  11. namespace Content.Client.Lobby.UI.Roles;
  12. /// <summary>
  13. /// A generic locking selector.
  14. /// </summary>
  15. [GenerateTypedNameReferences]
  16. public sealed partial class RequirementsSelector : BoxContainer
  17. {
  18. private readonly RadioOptions<int> _options;
  19. private readonly StripeBack _lockStripe;
  20. private List<ProtoId<GuideEntryPrototype>>? _guides;
  21. public event Action<int>? OnSelected;
  22. public event Action<List<ProtoId<GuideEntryPrototype>>>? OnOpenGuidebook;
  23. public int Selected => _options.SelectedId;
  24. public RequirementsSelector()
  25. {
  26. RobustXamlLoader.Load(this);
  27. _options = new RadioOptions<int>(RadioOptionsLayout.Horizontal)
  28. {
  29. FirstButtonStyle = StyleBase.ButtonOpenRight,
  30. ButtonStyle = StyleBase.ButtonOpenBoth,
  31. LastButtonStyle = StyleBase.ButtonOpenLeft,
  32. HorizontalExpand = true,
  33. };
  34. //Override default radio option button width
  35. _options.GenerateItem = GenerateButton;
  36. _options.OnItemSelected += args =>
  37. {
  38. _options.Select(args.Id);
  39. OnSelected?.Invoke(args.Id);
  40. };
  41. var requirementsLabel = new Label()
  42. {
  43. Text = Loc.GetString("role-timer-locked"),
  44. Visible = true,
  45. HorizontalAlignment = HAlignment.Center,
  46. StyleClasses = {StyleBase.StyleClassLabelSubText},
  47. };
  48. _lockStripe = new StripeBack()
  49. {
  50. Visible = false,
  51. HorizontalExpand = true,
  52. HasMargins = false,
  53. MouseFilter = MouseFilterMode.Stop,
  54. Children =
  55. {
  56. requirementsLabel
  57. }
  58. };
  59. Help.OnPressed += _ =>
  60. {
  61. if (_guides != null)
  62. OnOpenGuidebook?.Invoke(_guides);
  63. };
  64. }
  65. /// <summary>
  66. /// Actually adds the controls.
  67. /// </summary>
  68. public void Setup(
  69. (string, int)[] items,
  70. string title,
  71. int titleSize,
  72. string? description,
  73. TextureRect? icon = null,
  74. List<ProtoId<GuideEntryPrototype>>? guides = null)
  75. {
  76. foreach (var (text, value) in items)
  77. {
  78. _options.AddItem(Loc.GetString(text), value);
  79. }
  80. Help.Visible = guides != null;
  81. _guides = guides;
  82. TitleLabel.Text = title;
  83. TitleLabel.MinSize = new Vector2(titleSize, 0f);
  84. TitleLabel.ToolTip = description;
  85. if (icon != null)
  86. {
  87. AddChild(icon);
  88. icon.SetPositionFirst();
  89. }
  90. OptionsContainer.AddChild(_options);
  91. OptionsContainer.AddChild(_lockStripe);
  92. }
  93. public void LockRequirements(FormattedMessage requirements)
  94. {
  95. var tooltip = new Tooltip();
  96. tooltip.SetMessage(requirements);
  97. _lockStripe.TooltipSupplier = _ => tooltip;
  98. _lockStripe.Visible = true;
  99. _options.Visible = false;
  100. }
  101. public void UnlockRequirements()
  102. {
  103. _lockStripe.Visible = false;
  104. _options.Visible = true;
  105. }
  106. private Button GenerateButton(string text, int value)
  107. {
  108. return new Button
  109. {
  110. Text = text,
  111. MinWidth = 90,
  112. HorizontalExpand = true,
  113. };
  114. }
  115. public void Select(int id)
  116. {
  117. _options.Select(id);
  118. }
  119. }