GhostTargetWindow.xaml.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Linq;
  2. using System.Numerics;
  3. using Content.Shared.Ghost;
  4. using Robust.Client.AutoGenerated;
  5. using Robust.Client.UserInterface.Controls;
  6. using Robust.Client.UserInterface.CustomControls;
  7. using Robust.Client.UserInterface.XAML;
  8. namespace Content.Client.UserInterface.Systems.Ghost.Controls
  9. {
  10. [GenerateTypedNameReferences]
  11. public sealed partial class GhostTargetWindow : DefaultWindow
  12. {
  13. private List<(string, NetEntity)> _warps = new();
  14. private string _searchText = string.Empty;
  15. public event Action<NetEntity>? WarpClicked;
  16. public event Action? OnGhostnadoClicked;
  17. public GhostTargetWindow()
  18. {
  19. RobustXamlLoader.Load(this);
  20. SearchBar.OnTextChanged += OnSearchTextChanged;
  21. GhostnadoButton.OnPressed += _ => OnGhostnadoClicked?.Invoke();
  22. }
  23. public void UpdateWarps(IEnumerable<GhostWarp> warps)
  24. {
  25. // Server COULD send these sorted but how about we just use the client to do it instead
  26. _warps = warps
  27. .OrderBy(w => w.IsWarpPoint)
  28. .ThenBy(w => w.DisplayName, Comparer<string>.Create(
  29. (x, y) => string.Compare(x, y, StringComparison.Ordinal)))
  30. .Select(w =>
  31. {
  32. var name = w.IsWarpPoint
  33. ? Loc.GetString("ghost-target-window-current-button", ("name", w.DisplayName))
  34. : w.DisplayName;
  35. return (name, w.Entity);
  36. })
  37. .ToList();
  38. }
  39. public void Populate()
  40. {
  41. ButtonContainer.DisposeAllChildren();
  42. AddButtons();
  43. }
  44. private void AddButtons()
  45. {
  46. foreach (var (name, warpTarget) in _warps)
  47. {
  48. var currentButtonRef = new Button
  49. {
  50. Text = name,
  51. TextAlign = Label.AlignMode.Right,
  52. HorizontalAlignment = HAlignment.Center,
  53. VerticalAlignment = VAlignment.Center,
  54. SizeFlagsStretchRatio = 1,
  55. MinSize = new Vector2(340, 20),
  56. ClipText = true,
  57. };
  58. currentButtonRef.OnPressed += _ => WarpClicked?.Invoke(warpTarget);
  59. currentButtonRef.Visible = ButtonIsVisible(currentButtonRef);
  60. ButtonContainer.AddChild(currentButtonRef);
  61. }
  62. }
  63. private bool ButtonIsVisible(Button button)
  64. {
  65. return string.IsNullOrEmpty(_searchText) || button.Text == null || button.Text.Contains(_searchText, StringComparison.OrdinalIgnoreCase);
  66. }
  67. private void UpdateVisibleButtons()
  68. {
  69. foreach (var child in ButtonContainer.Children)
  70. {
  71. if (child is Button button)
  72. button.Visible = ButtonIsVisible(button);
  73. }
  74. }
  75. private void OnSearchTextChanged(LineEdit.LineEditEventArgs args)
  76. {
  77. _searchText = args.Text;
  78. UpdateVisibleButtons();
  79. // Reset scroll bar so they can see the relevant results.
  80. GhostScroll.SetScrollValue(Vector2.Zero);
  81. }
  82. }
  83. }