| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- using Content.Shared.StationRecords;
- using Robust.Client.AutoGenerated;
- using Robust.Client.UserInterface.CustomControls;
- using Robust.Client.UserInterface.XAML;
- namespace Content.Client.StationRecords;
- [GenerateTypedNameReferences]
- public sealed partial class GeneralStationRecordConsoleWindow : DefaultWindow
- {
- public Action<uint?>? OnKeySelected;
- public Action<StationRecordFilterType, string>? OnFiltersChanged;
- public Action<uint>? OnDeleted;
- private bool _isPopulating;
- private StationRecordFilterType _currentFilterType;
- public GeneralStationRecordConsoleWindow()
- {
- RobustXamlLoader.Load(this);
- _currentFilterType = StationRecordFilterType.Name;
- foreach (var item in Enum.GetValues<StationRecordFilterType>())
- {
- StationRecordsFilterType.AddItem(GetTypeFilterLocals(item), (int)item);
- }
- RecordListing.OnItemSelected += args =>
- {
- if (_isPopulating || RecordListing[args.ItemIndex].Metadata is not uint cast)
- return;
- OnKeySelected?.Invoke(cast);
- };
- RecordListing.OnItemDeselected += _ =>
- {
- if (!_isPopulating)
- OnKeySelected?.Invoke(null);
- };
- StationRecordsFilterType.OnItemSelected += eventArgs =>
- {
- var type = (StationRecordFilterType) eventArgs.Id;
- if (_currentFilterType != type)
- {
- _currentFilterType = type;
- FilterListingOfRecords();
- }
- };
- StationRecordsFiltersValue.OnTextEntered += args =>
- {
- FilterListingOfRecords(args.Text);
- };
- StationRecordsFilters.OnPressed += _ =>
- {
- FilterListingOfRecords(StationRecordsFiltersValue.Text);
- };
- StationRecordsFiltersReset.OnPressed += _ =>
- {
- StationRecordsFiltersValue.Text = "";
- FilterListingOfRecords();
- };
- }
- public void UpdateState(GeneralStationRecordConsoleState state)
- {
- if (state.Filter != null)
- {
- if (state.Filter.Type != _currentFilterType)
- {
- _currentFilterType = state.Filter.Type;
- }
- if (state.Filter.Value != StationRecordsFiltersValue.Text)
- {
- StationRecordsFiltersValue.Text = state.Filter.Value;
- }
- }
- StationRecordsFilterType.SelectId((int)_currentFilterType);
- if (state.RecordListing == null)
- {
- RecordListingStatus.Visible = true;
- RecordListing.Visible = false;
- RecordListingStatus.Text = Loc.GetString("general-station-record-console-empty-state");
- RecordContainer.Visible = false;
- RecordContainerStatus.Visible = false;
- return;
- }
- RecordListingStatus.Visible = false;
- RecordListing.Visible = true;
- RecordContainer.Visible = true;
- PopulateRecordListing(state.RecordListing!, state.SelectedKey);
- RecordContainerStatus.Visible = state.Record == null;
- if (state.Record != null)
- {
- RecordContainerStatus.Visible = state.SelectedKey == null;
- RecordContainerStatus.Text = state.SelectedKey == null
- ? Loc.GetString("general-station-record-console-no-record-found")
- : Loc.GetString("general-station-record-console-select-record-info");
- PopulateRecordContainer(state.Record, state.CanDeleteEntries, state.SelectedKey);
- }
- else
- {
- RecordContainer.RemoveAllChildren();
- }
- }
- private void PopulateRecordListing(Dictionary<uint, string> listing, uint? selected)
- {
- RecordListing.Clear();
- RecordListing.ClearSelected();
- _isPopulating = true;
- foreach (var (key, name) in listing)
- {
- var item = RecordListing.AddItem(name);
- item.Metadata = key;
- item.Selected = key == selected;
- }
- _isPopulating = false;
- RecordListing.SortItemsByText();
- }
- private void PopulateRecordContainer(GeneralStationRecord record, bool enableDelete, uint? id)
- {
- RecordContainer.RemoveAllChildren();
- var newRecord = new GeneralRecord(record, enableDelete, id);
- newRecord.OnDeletePressed = OnDeleted;
- RecordContainer.AddChild(newRecord);
- }
- private void FilterListingOfRecords(string text = "")
- {
- if (!_isPopulating)
- {
- OnFiltersChanged?.Invoke(_currentFilterType, text);
- }
- }
- private string GetTypeFilterLocals(StationRecordFilterType type)
- {
- return Loc.GetString($"general-station-record-{type.ToString().ToLower()}-filter");
- }
- }
|