|
| 1 | +using Content.Client.Lathe.UI; |
| 2 | +using Content.Client.UserInterface.Controls; |
| 3 | +using Content.Shared.ListViewSelector; |
| 4 | +using JetBrains.Annotations; |
| 5 | +using Robust.Client.UserInterface.Controls; |
| 6 | +using Robust.Shared.Prototypes; |
| 7 | + |
| 8 | +// ReSharper disable InconsistentNaming |
| 9 | + |
| 10 | +namespace Content.Client.ListViewSelector; |
| 11 | + |
| 12 | +[UsedImplicitly] |
| 13 | +public sealed class ListViewSelectorBUI(EntityUid owner, Enum uiKey) : BoundUserInterface(owner, uiKey) |
| 14 | +{ |
| 15 | + [Dependency] private readonly IPrototypeManager _prototypeManager = default!; |
| 16 | + |
| 17 | + private FancyWindow _window = new(); |
| 18 | + private BoxContainer? _itemsContainer; |
| 19 | + private Dictionary<string, object> _metaData = new(); |
| 20 | + |
| 21 | + protected override void Open() |
| 22 | + { |
| 23 | + _window = FormWindow(); |
| 24 | + _window.OnClose += Close; |
| 25 | + _window.OpenCentered(); |
| 26 | + } |
| 27 | + |
| 28 | + protected override void UpdateState(BoundUserInterfaceState state) |
| 29 | + { |
| 30 | + base.UpdateState(state); |
| 31 | + if (state is not ListViewSelectorState listViewSelectorState) |
| 32 | + return; |
| 33 | + |
| 34 | + PopulateWindow(listViewSelectorState.Items); |
| 35 | + _metaData = listViewSelectorState.MetaData; |
| 36 | + } |
| 37 | + |
| 38 | + protected override void Dispose(bool disposing) |
| 39 | + { |
| 40 | + base.Dispose(disposing); |
| 41 | + |
| 42 | + if (disposing) |
| 43 | + _window.Close(); |
| 44 | + } |
| 45 | + |
| 46 | + private FancyWindow FormWindow() |
| 47 | + { |
| 48 | + var window = new FancyWindow |
| 49 | + { |
| 50 | + HorizontalExpand = true, |
| 51 | + VerticalExpand = true, |
| 52 | + MinWidth = 350, |
| 53 | + MinHeight = 400, |
| 54 | + Title = Loc.GetString("list-view-window-default-title") |
| 55 | + }; |
| 56 | + |
| 57 | + var scrollContainer = new ScrollContainer |
| 58 | + { |
| 59 | + HorizontalExpand = true, |
| 60 | + VerticalExpand = true |
| 61 | + }; |
| 62 | + |
| 63 | + var itemsContainer = new BoxContainer |
| 64 | + { |
| 65 | + Orientation = BoxContainer.LayoutOrientation.Vertical |
| 66 | + }; |
| 67 | + |
| 68 | + scrollContainer.AddChild(itemsContainer); |
| 69 | + window.AddChild(scrollContainer); |
| 70 | + |
| 71 | + _itemsContainer = itemsContainer; |
| 72 | + |
| 73 | + return window; |
| 74 | + } |
| 75 | + |
| 76 | + private void PopulateWindow(List<ListViewSelectorEntry> items) |
| 77 | + { |
| 78 | + if (_itemsContainer is null) |
| 79 | + return; |
| 80 | + |
| 81 | + _itemsContainer.Children.Clear(); |
| 82 | + |
| 83 | + foreach (var item in items) |
| 84 | + { |
| 85 | + var itemName = item.Name; |
| 86 | + var itemDesc = item.Description; |
| 87 | + if (_prototypeManager.TryIndex(item.Id, out var itemPrototype)) |
| 88 | + { |
| 89 | + itemName = itemPrototype.Name; |
| 90 | + itemDesc = itemPrototype.Description; |
| 91 | + } |
| 92 | + |
| 93 | + var button = new Button |
| 94 | + { |
| 95 | + Text = itemName, |
| 96 | + }; |
| 97 | + |
| 98 | + if (!string.IsNullOrEmpty(itemDesc)) |
| 99 | + button.TooltipSupplier = _ => new RecipeTooltip(itemDesc); |
| 100 | + |
| 101 | + button.OnButtonUp += _ => |
| 102 | + { |
| 103 | + var msg = new ListViewItemSelectedMessage(item, items.IndexOf(item), _metaData); |
| 104 | + SendMessage(msg); |
| 105 | + Close(); |
| 106 | + }; |
| 107 | + |
| 108 | + _itemsContainer.AddChild(button); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments