GeneralStationRecordConsoleWindow.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using Content.Shared.StationRecords;
  2. using Robust.Client.AutoGenerated;
  3. using Robust.Client.UserInterface.CustomControls;
  4. using Robust.Client.UserInterface.XAML;
  5. namespace Content.Client.StationRecords;
  6. [GenerateTypedNameReferences]
  7. public sealed partial class GeneralStationRecordConsoleWindow : DefaultWindow
  8. {
  9. public Action<uint?>? OnKeySelected;
  10. public Action<StationRecordFilterType, string>? OnFiltersChanged;
  11. public Action<uint>? OnDeleted;
  12. private bool _isPopulating;
  13. private StationRecordFilterType _currentFilterType;
  14. public GeneralStationRecordConsoleWindow()
  15. {
  16. RobustXamlLoader.Load(this);
  17. _currentFilterType = StationRecordFilterType.Name;
  18. foreach (var item in Enum.GetValues<StationRecordFilterType>())
  19. {
  20. StationRecordsFilterType.AddItem(GetTypeFilterLocals(item), (int)item);
  21. }
  22. RecordListing.OnItemSelected += args =>
  23. {
  24. if (_isPopulating || RecordListing[args.ItemIndex].Metadata is not uint cast)
  25. return;
  26. OnKeySelected?.Invoke(cast);
  27. };
  28. RecordListing.OnItemDeselected += _ =>
  29. {
  30. if (!_isPopulating)
  31. OnKeySelected?.Invoke(null);
  32. };
  33. StationRecordsFilterType.OnItemSelected += eventArgs =>
  34. {
  35. var type = (StationRecordFilterType) eventArgs.Id;
  36. if (_currentFilterType != type)
  37. {
  38. _currentFilterType = type;
  39. FilterListingOfRecords();
  40. }
  41. };
  42. StationRecordsFiltersValue.OnTextEntered += args =>
  43. {
  44. FilterListingOfRecords(args.Text);
  45. };
  46. StationRecordsFilters.OnPressed += _ =>
  47. {
  48. FilterListingOfRecords(StationRecordsFiltersValue.Text);
  49. };
  50. StationRecordsFiltersReset.OnPressed += _ =>
  51. {
  52. StationRecordsFiltersValue.Text = "";
  53. FilterListingOfRecords();
  54. };
  55. }
  56. public void UpdateState(GeneralStationRecordConsoleState state)
  57. {
  58. if (state.Filter != null)
  59. {
  60. if (state.Filter.Type != _currentFilterType)
  61. {
  62. _currentFilterType = state.Filter.Type;
  63. }
  64. if (state.Filter.Value != StationRecordsFiltersValue.Text)
  65. {
  66. StationRecordsFiltersValue.Text = state.Filter.Value;
  67. }
  68. }
  69. StationRecordsFilterType.SelectId((int)_currentFilterType);
  70. if (state.RecordListing == null)
  71. {
  72. RecordListingStatus.Visible = true;
  73. RecordListing.Visible = false;
  74. RecordListingStatus.Text = Loc.GetString("general-station-record-console-empty-state");
  75. RecordContainer.Visible = false;
  76. RecordContainerStatus.Visible = false;
  77. return;
  78. }
  79. RecordListingStatus.Visible = false;
  80. RecordListing.Visible = true;
  81. RecordContainer.Visible = true;
  82. PopulateRecordListing(state.RecordListing!, state.SelectedKey);
  83. RecordContainerStatus.Visible = state.Record == null;
  84. if (state.Record != null)
  85. {
  86. RecordContainerStatus.Visible = state.SelectedKey == null;
  87. RecordContainerStatus.Text = state.SelectedKey == null
  88. ? Loc.GetString("general-station-record-console-no-record-found")
  89. : Loc.GetString("general-station-record-console-select-record-info");
  90. PopulateRecordContainer(state.Record, state.CanDeleteEntries, state.SelectedKey);
  91. }
  92. else
  93. {
  94. RecordContainer.RemoveAllChildren();
  95. }
  96. }
  97. private void PopulateRecordListing(Dictionary<uint, string> listing, uint? selected)
  98. {
  99. RecordListing.Clear();
  100. RecordListing.ClearSelected();
  101. _isPopulating = true;
  102. foreach (var (key, name) in listing)
  103. {
  104. var item = RecordListing.AddItem(name);
  105. item.Metadata = key;
  106. item.Selected = key == selected;
  107. }
  108. _isPopulating = false;
  109. RecordListing.SortItemsByText();
  110. }
  111. private void PopulateRecordContainer(GeneralStationRecord record, bool enableDelete, uint? id)
  112. {
  113. RecordContainer.RemoveAllChildren();
  114. var newRecord = new GeneralRecord(record, enableDelete, id);
  115. newRecord.OnDeletePressed = OnDeleted;
  116. RecordContainer.AddChild(newRecord);
  117. }
  118. private void FilterListingOfRecords(string text = "")
  119. {
  120. if (!_isPopulating)
  121. {
  122. OnFiltersChanged?.Invoke(_currentFilterType, text);
  123. }
  124. }
  125. private string GetTypeFilterLocals(StationRecordFilterType type)
  126. {
  127. return Loc.GetString($"general-station-record-{type.ToString().ToLower()}-filter");
  128. }
  129. }