AccessLevelControl.xaml.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Linq;
  2. using Robust.Client.AutoGenerated;
  3. using Robust.Client.UserInterface;
  4. using Robust.Client.UserInterface.Controls;
  5. using Robust.Client.UserInterface.XAML;
  6. using Robust.Shared.Prototypes;
  7. using Content.Shared.Access;
  8. using Content.Shared.Access.Systems;
  9. namespace Content.Client.Access.UI;
  10. [GenerateTypedNameReferences]
  11. public sealed partial class AccessLevelControl : GridContainer
  12. {
  13. [Dependency] private readonly ILogManager _logManager = default!;
  14. private ISawmill _sawmill = default!;
  15. public readonly Dictionary<ProtoId<AccessLevelPrototype>, Button> ButtonsList = new();
  16. public AccessLevelControl()
  17. {
  18. RobustXamlLoader.Load(this);
  19. IoCManager.InjectDependencies(this);
  20. _sawmill = _logManager.GetSawmill("accesslevelcontrol");
  21. }
  22. public void Populate(List<ProtoId<AccessLevelPrototype>> accessLevels, IPrototypeManager prototypeManager)
  23. {
  24. foreach (var access in accessLevels)
  25. {
  26. if (!prototypeManager.TryIndex(access, out var accessLevel))
  27. {
  28. _sawmill.Error($"Unable to find accesslevel for {access}");
  29. continue;
  30. }
  31. var newButton = new Button
  32. {
  33. Text = accessLevel.GetAccessLevelName(),
  34. ToggleMode = true,
  35. };
  36. AddChild(newButton);
  37. ButtonsList.Add(accessLevel.ID, newButton);
  38. }
  39. }
  40. public void UpdateState(
  41. List<ProtoId<AccessLevelPrototype>> pressedList,
  42. List<ProtoId<AccessLevelPrototype>>? enabledList = null)
  43. {
  44. foreach (var (accessName, button) in ButtonsList)
  45. {
  46. button.Pressed = pressedList.Contains(accessName);
  47. button.Disabled = !(enabledList?.Contains(accessName) ?? true);
  48. }
  49. }
  50. }