|
| 1 | +// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt |
| 2 | + |
| 3 | +using System.Numerics; |
| 4 | +using Content.Client.UserInterface.Controls; |
| 5 | +using Content.Shared.SS220.Pinpointer; |
| 6 | +using Robust.Client.AutoGenerated; |
| 7 | +using Robust.Client.UserInterface.Controls; |
| 8 | +using Robust.Client.UserInterface.XAML; |
| 9 | + |
| 10 | +namespace Content.Client.SS220.Pinpointer.UI; |
| 11 | + |
| 12 | +[GenerateTypedNameReferences] |
| 13 | +public sealed partial class PinpointerUplinkMenu : FancyWindow |
| 14 | +{ |
| 15 | + public FancyWindow? DnaWindow; |
| 16 | + |
| 17 | + public HashSet<TrackedItem> ItemListSet = []; |
| 18 | + |
| 19 | + public Action<NetEntity>? OnTargetPicked; |
| 20 | + public Action<string>? OnDnaPicked; |
| 21 | + |
| 22 | + private string _searchText = string.Empty; |
| 23 | + |
| 24 | + public PinpointerUplinkMenu() |
| 25 | + { |
| 26 | + RobustXamlLoader.Load(this); |
| 27 | + SearchBar.OnTextChanged += OnSearchChanged; |
| 28 | + DNASearch.OnPressed += OnDNASearch; |
| 29 | + } |
| 30 | + |
| 31 | + private void OnSearchChanged(LineEdit.LineEditEventArgs args) |
| 32 | + { |
| 33 | + _searchText = args.Text.Trim().ToLower(); |
| 34 | + PopulateList(); |
| 35 | + } |
| 36 | + |
| 37 | + private void OnDNASearch(BaseButton.ButtonEventArgs args) |
| 38 | + { |
| 39 | + if (DnaWindow != null) |
| 40 | + { |
| 41 | + DnaWindow.Open(); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + DnaWindow = new FancyWindow |
| 46 | + { |
| 47 | + Title = Loc.GetString("pinpointer-ui-uplink-search-by-dna"), |
| 48 | + SetSize = new Vector2(200, 200), |
| 49 | + }; |
| 50 | + |
| 51 | + |
| 52 | + var boxContainer = new BoxContainer |
| 53 | + { |
| 54 | + Orientation = BoxContainer.LayoutOrientation.Vertical, |
| 55 | + HorizontalAlignment = HAlignment.Center, |
| 56 | + VerticalAlignment = VAlignment.Center, |
| 57 | + }; |
| 58 | + |
| 59 | + var lineEdit = new LineEdit |
| 60 | + { |
| 61 | + PlaceHolder = Loc.GetString("pinpointer-ui-uplink-enter-dna"), |
| 62 | + SetSize = new Vector2(150, 30), |
| 63 | + }; |
| 64 | + |
| 65 | + var button = new Button() |
| 66 | + { |
| 67 | + Text = Loc.GetString("pinpointer-ui-uplink-accept"), |
| 68 | + Modulate = Color.LimeGreen, |
| 69 | + StyleClasses = { "OpenBoth" }, |
| 70 | + SetSize = new Vector2(150, 30), |
| 71 | + }; |
| 72 | + |
| 73 | + button.OnPressed += _ => |
| 74 | + { |
| 75 | + if (string.IsNullOrEmpty(lineEdit.Text)) |
| 76 | + return; |
| 77 | + |
| 78 | + OnDnaPicked?.Invoke(lineEdit.Text); |
| 79 | + }; |
| 80 | + |
| 81 | + boxContainer.AddChild(lineEdit); |
| 82 | + boxContainer.AddChild(button); |
| 83 | + DnaWindow.AddChild(boxContainer); |
| 84 | + DnaWindow.OpenCentered(); |
| 85 | + } |
| 86 | + |
| 87 | + public void PopulateList() |
| 88 | + { |
| 89 | + ItemList.RemoveAllChildren(); |
| 90 | + |
| 91 | + foreach (var entry in ItemListSet) |
| 92 | + { |
| 93 | + var button = CreateTargetButton(entry.Name, () => OnTargetPicked?.Invoke(entry.Entity)); |
| 94 | + |
| 95 | + if (button == null) |
| 96 | + continue; |
| 97 | + |
| 98 | + ItemList.AddChild(button); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private Button? CreateTargetButton(string fullName, Action onClick) |
| 103 | + { |
| 104 | + if (!fullName.Contains(_searchText, StringComparison.CurrentCultureIgnoreCase)) |
| 105 | + return null; |
| 106 | + |
| 107 | + var button = new Button |
| 108 | + { |
| 109 | + ToolTip = fullName, |
| 110 | + StyleClasses = { "OpenBoth" }, |
| 111 | + SetHeight = 30, |
| 112 | + Margin = new Thickness(5, 0, 5, 5), |
| 113 | + HorizontalExpand = true, |
| 114 | + }; |
| 115 | + |
| 116 | + var label = new Label |
| 117 | + { |
| 118 | + Text = fullName, |
| 119 | + HorizontalExpand = true, |
| 120 | + HorizontalAlignment = HAlignment.Center, |
| 121 | + }; |
| 122 | + |
| 123 | + button.OnPressed += _ => onClick(); |
| 124 | + button.AddChild(label); |
| 125 | + |
| 126 | + return button; |
| 127 | + } |
| 128 | +} |
| 129 | + |
0 commit comments