1
0

IdCardConsoleWindow.xaml.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System.Linq;
  2. using Content.Shared.Access;
  3. using Content.Shared.Access.Systems;
  4. using Content.Shared.Roles;
  5. using Robust.Client.AutoGenerated;
  6. using Robust.Client.UserInterface.Controls;
  7. using Robust.Client.UserInterface.CustomControls;
  8. using Robust.Client.UserInterface.XAML;
  9. using Robust.Shared.Prototypes;
  10. using static Content.Shared.Access.Components.IdCardConsoleComponent;
  11. namespace Content.Client.Access.UI
  12. {
  13. [GenerateTypedNameReferences]
  14. public sealed partial class IdCardConsoleWindow : DefaultWindow
  15. {
  16. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  17. [Dependency] private readonly ILogManager _logManager = default!;
  18. private readonly ISawmill _logMill = default!;
  19. private readonly IdCardConsoleBoundUserInterface _owner;
  20. private AccessLevelControl _accessButtons = new();
  21. private readonly List<string> _jobPrototypeIds = new();
  22. private string? _lastFullName;
  23. private string? _lastJobTitle;
  24. private string? _lastJobProto;
  25. // The job that will be picked if the ID doesn't have a job on the station.
  26. private static ProtoId<JobPrototype> _defaultJob = "Nomad";
  27. public IdCardConsoleWindow(IdCardConsoleBoundUserInterface owner, IPrototypeManager prototypeManager,
  28. List<ProtoId<AccessLevelPrototype>> accessLevels)
  29. {
  30. RobustXamlLoader.Load(this);
  31. IoCManager.InjectDependencies(this);
  32. _logMill = _logManager.GetSawmill(SharedIdCardConsoleSystem.Sawmill);
  33. _owner = owner;
  34. FullNameLineEdit.OnTextEntered += _ => SubmitData();
  35. FullNameLineEdit.OnTextChanged += _ =>
  36. {
  37. FullNameSaveButton.Disabled = FullNameSaveButton.Text == _lastFullName;
  38. };
  39. FullNameSaveButton.OnPressed += _ => SubmitData();
  40. JobTitleLineEdit.OnTextEntered += _ => SubmitData();
  41. JobTitleLineEdit.OnTextChanged += _ =>
  42. {
  43. JobTitleSaveButton.Disabled = JobTitleLineEdit.Text == _lastJobTitle;
  44. };
  45. JobTitleSaveButton.OnPressed += _ => SubmitData();
  46. var jobs = _prototypeManager.EnumeratePrototypes<JobPrototype>().ToList();
  47. jobs.Sort((x, y) => string.Compare(x.LocalizedName, y.LocalizedName, StringComparison.CurrentCulture));
  48. foreach (var job in jobs)
  49. {
  50. if (!job.OverrideConsoleVisibility.GetValueOrDefault(job.SetPreference))
  51. {
  52. continue;
  53. }
  54. _jobPrototypeIds.Add(job.ID);
  55. JobPresetOptionButton.AddItem(Loc.GetString(job.Name), _jobPrototypeIds.Count - 1);
  56. }
  57. JobPresetOptionButton.OnItemSelected += SelectJobPreset;
  58. _accessButtons.Populate(accessLevels, prototypeManager);
  59. AccessLevelControlContainer.AddChild(_accessButtons);
  60. foreach (var (id, button) in _accessButtons.ButtonsList)
  61. {
  62. button.OnPressed += _ => SubmitData();
  63. }
  64. }
  65. private void ClearAllAccess()
  66. {
  67. foreach (var button in _accessButtons.ButtonsList.Values)
  68. {
  69. if (button.Pressed)
  70. {
  71. button.Pressed = false;
  72. }
  73. }
  74. }
  75. private void SelectJobPreset(OptionButton.ItemSelectedEventArgs args)
  76. {
  77. if (!_prototypeManager.TryIndex(_jobPrototypeIds[args.Id], out JobPrototype? job))
  78. {
  79. return;
  80. }
  81. JobTitleLineEdit.Text = Loc.GetString(job.Name);
  82. args.Button.SelectId(args.Id);
  83. ClearAllAccess();
  84. // this is a sussy way to do this
  85. foreach (var access in job.Access)
  86. {
  87. if (_accessButtons.ButtonsList.TryGetValue(access, out var button) && !button.Disabled)
  88. {
  89. button.Pressed = true;
  90. }
  91. }
  92. foreach (var group in job.AccessGroups)
  93. {
  94. if (!_prototypeManager.TryIndex(group, out AccessGroupPrototype? groupPrototype))
  95. {
  96. continue;
  97. }
  98. foreach (var access in groupPrototype.Tags)
  99. {
  100. if (_accessButtons.ButtonsList.TryGetValue(access, out var button) && !button.Disabled)
  101. {
  102. button.Pressed = true;
  103. }
  104. }
  105. }
  106. SubmitData();
  107. }
  108. public void UpdateState(IdCardConsoleBoundUserInterfaceState state)
  109. {
  110. PrivilegedIdButton.Text = state.IsPrivilegedIdPresent
  111. ? Loc.GetString("id-card-console-window-eject-button")
  112. : Loc.GetString("id-card-console-window-insert-button");
  113. PrivilegedIdLabel.Text = state.PrivilegedIdName;
  114. TargetIdButton.Text = state.IsTargetIdPresent
  115. ? Loc.GetString("id-card-console-window-eject-button")
  116. : Loc.GetString("id-card-console-window-insert-button");
  117. TargetIdLabel.Text = state.TargetIdName;
  118. var interfaceEnabled =
  119. state.IsPrivilegedIdPresent && state.IsPrivilegedIdAuthorized && state.IsTargetIdPresent;
  120. var fullNameDirty = _lastFullName != null && FullNameLineEdit.Text != state.TargetIdFullName;
  121. var jobTitleDirty = _lastJobTitle != null && JobTitleLineEdit.Text != state.TargetIdJobTitle;
  122. FullNameLabel.Modulate = interfaceEnabled ? Color.White : Color.Gray;
  123. FullNameLineEdit.Editable = interfaceEnabled;
  124. if (!fullNameDirty)
  125. {
  126. FullNameLineEdit.Text = state.TargetIdFullName ?? string.Empty;
  127. }
  128. FullNameSaveButton.Disabled = !interfaceEnabled || !fullNameDirty;
  129. JobTitleLabel.Modulate = interfaceEnabled ? Color.White : Color.Gray;
  130. JobTitleLineEdit.Editable = interfaceEnabled;
  131. if (!jobTitleDirty)
  132. {
  133. JobTitleLineEdit.Text = state.TargetIdJobTitle ?? string.Empty;
  134. }
  135. JobTitleSaveButton.Disabled = !interfaceEnabled || !jobTitleDirty;
  136. JobPresetOptionButton.Disabled = !interfaceEnabled;
  137. _accessButtons.UpdateState(state.TargetIdAccessList?.ToList() ??
  138. new List<ProtoId<AccessLevelPrototype>>(),
  139. state.AllowedModifyAccessList?.ToList() ??
  140. new List<ProtoId<AccessLevelPrototype>>());
  141. var jobIndex = _jobPrototypeIds.IndexOf(state.TargetIdJobPrototype);
  142. // If the job index is < 0 that means they don't have a job registered in the station records.
  143. // For example, a new ID from a box would have no job index.
  144. if (jobIndex < 0)
  145. {
  146. jobIndex = _jobPrototypeIds.IndexOf(_defaultJob);
  147. }
  148. JobPresetOptionButton.SelectId(jobIndex);
  149. _lastFullName = state.TargetIdFullName;
  150. _lastJobTitle = state.TargetIdJobTitle;
  151. _lastJobProto = state.TargetIdJobPrototype;
  152. }
  153. private void SubmitData()
  154. {
  155. // Don't send this if it isn't dirty.
  156. var jobProtoDirty = _lastJobProto != null &&
  157. _jobPrototypeIds[JobPresetOptionButton.SelectedId] != _lastJobProto;
  158. _owner.SubmitData(
  159. FullNameLineEdit.Text,
  160. JobTitleLineEdit.Text,
  161. // Iterate over the buttons dictionary, filter by `Pressed`, only get key from the key/value pair
  162. _accessButtons.ButtonsList.Where(x => x.Value.Pressed).Select(x => x.Key).ToList(),
  163. jobProtoDirty ? _jobPrototypeIds[JobPresetOptionButton.SelectedId] : string.Empty);
  164. }
  165. }
  166. }