1
0

AgentIDCardWindow.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Content.Client.Stylesheets;
  2. using Content.Shared.StatusIcon;
  3. using Robust.Client.AutoGenerated;
  4. using Robust.Client.GameObjects;
  5. using Robust.Client.UserInterface;
  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 System.Numerics;
  11. using System.Linq;
  12. namespace Content.Client.Access.UI
  13. {
  14. [GenerateTypedNameReferences]
  15. public sealed partial class AgentIDCardWindow : DefaultWindow
  16. {
  17. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  18. [Dependency] private readonly IEntitySystemManager _entitySystem = default!;
  19. private readonly SpriteSystem _spriteSystem;
  20. private const int JobIconColumnCount = 10;
  21. public event Action<string>? OnNameChanged;
  22. public event Action<string>? OnJobChanged;
  23. public event Action<ProtoId<JobIconPrototype>>? OnJobIconChanged;
  24. public AgentIDCardWindow()
  25. {
  26. RobustXamlLoader.Load(this);
  27. IoCManager.InjectDependencies(this);
  28. _spriteSystem = _entitySystem.GetEntitySystem<SpriteSystem>();
  29. NameLineEdit.OnTextEntered += e => OnNameChanged?.Invoke(e.Text);
  30. NameLineEdit.OnFocusExit += e => OnNameChanged?.Invoke(e.Text);
  31. JobLineEdit.OnTextEntered += e => OnJobChanged?.Invoke(e.Text);
  32. JobLineEdit.OnFocusExit += e => OnJobChanged?.Invoke(e.Text);
  33. }
  34. public void SetAllowedIcons(string currentJobIconId)
  35. {
  36. IconGrid.DisposeAllChildren();
  37. var jobIconButtonGroup = new ButtonGroup();
  38. var i = 0;
  39. var icons = _prototypeManager.EnumeratePrototypes<JobIconPrototype>().Where(icon => icon.AllowSelection).ToList();
  40. icons.Sort((x, y) => string.Compare(x.LocalizedJobName, y.LocalizedJobName, StringComparison.CurrentCulture));
  41. foreach (var jobIcon in icons)
  42. {
  43. String styleBase = StyleBase.ButtonOpenBoth;
  44. var modulo = i % JobIconColumnCount;
  45. if (modulo == 0)
  46. styleBase = StyleBase.ButtonOpenRight;
  47. else if (modulo == JobIconColumnCount - 1)
  48. styleBase = StyleBase.ButtonOpenLeft;
  49. // Generate buttons
  50. var jobIconButton = new Button
  51. {
  52. Access = AccessLevel.Public,
  53. StyleClasses = { styleBase },
  54. MaxSize = new Vector2(42, 28),
  55. Group = jobIconButtonGroup,
  56. Pressed = currentJobIconId == jobIcon.ID,
  57. ToolTip = jobIcon.LocalizedJobName
  58. };
  59. // Generate buttons textures
  60. var jobIconTexture = new TextureRect
  61. {
  62. Texture = _spriteSystem.Frame0(jobIcon.Icon),
  63. TextureScale = new Vector2(2.5f, 2.5f),
  64. Stretch = TextureRect.StretchMode.KeepCentered,
  65. };
  66. jobIconButton.AddChild(jobIconTexture);
  67. jobIconButton.OnPressed += _ => OnJobIconChanged?.Invoke(jobIcon.ID);
  68. IconGrid.AddChild(jobIconButton);
  69. i++;
  70. }
  71. }
  72. public void SetCurrentName(string name)
  73. {
  74. NameLineEdit.Text = name;
  75. }
  76. public void SetCurrentJob(string job)
  77. {
  78. JobLineEdit.Text = job;
  79. }
  80. }
  81. }