LoadoutWindow.xaml.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Numerics;
  2. using Content.Client.UserInterface.Controls;
  3. using Content.Shared.Dataset;
  4. using Content.Shared.Preferences;
  5. using Content.Shared.Preferences.Loadouts;
  6. using Content.Shared.Random.Helpers;
  7. using Robust.Client.AutoGenerated;
  8. using Robust.Client.UserInterface.XAML;
  9. using Robust.Shared.Player;
  10. using Robust.Shared.Prototypes;
  11. using Robust.Shared.Random;
  12. namespace Content.Client.Lobby.UI.Loadouts;
  13. [GenerateTypedNameReferences]
  14. public sealed partial class LoadoutWindow : FancyWindow
  15. {
  16. public event Action<string>? OnNameChanged;
  17. public event Action<ProtoId<LoadoutGroupPrototype>, ProtoId<LoadoutPrototype>>? OnLoadoutPressed;
  18. public event Action<ProtoId<LoadoutGroupPrototype>, ProtoId<LoadoutPrototype>>? OnLoadoutUnpressed;
  19. private List<LoadoutGroupContainer> _groups = new();
  20. public HumanoidCharacterProfile Profile;
  21. public LoadoutWindow(HumanoidCharacterProfile profile, RoleLoadout loadout, RoleLoadoutPrototype proto, ICommonSession session, IDependencyCollection collection)
  22. {
  23. RobustXamlLoader.Load(this);
  24. Profile = profile;
  25. var protoManager = collection.Resolve<IPrototypeManager>();
  26. RoleNameEdit.IsValid = text => text.Length <= HumanoidCharacterProfile.MaxLoadoutNameLength;
  27. // Hide if we can't edit the name.
  28. if (!proto.CanCustomizeName)
  29. {
  30. RoleNameBox.Visible = false;
  31. }
  32. else
  33. {
  34. var name = loadout.EntityName;
  35. LoadoutNameLabel.Text = proto.NameDataset == null ?
  36. Loc.GetString("loadout-name-edit-label") :
  37. Loc.GetString("loadout-name-edit-label-dataset");
  38. RoleNameEdit.ToolTip = Loc.GetString(
  39. "loadout-name-edit-tooltip",
  40. ("max", HumanoidCharacterProfile.MaxLoadoutNameLength));
  41. RoleNameEdit.Text = name ?? string.Empty;
  42. RoleNameEdit.OnTextChanged += args => OnNameChanged?.Invoke(args.Text);
  43. }
  44. // Hide if no groups
  45. if (proto.Groups.Count == 0)
  46. {
  47. LoadoutGroupsContainer.Visible = false;
  48. SetSize = Vector2.Zero;
  49. }
  50. else
  51. {
  52. foreach (var group in proto.Groups)
  53. {
  54. if (!protoManager.TryIndex(group, out var groupProto))
  55. continue;
  56. if (groupProto.Hidden)
  57. continue;
  58. var container = new LoadoutGroupContainer(profile, loadout, protoManager.Index(group), session, collection);
  59. LoadoutGroupsContainer.AddTab(container, Loc.GetString(groupProto.Name));
  60. _groups.Add(container);
  61. container.OnLoadoutPressed += args =>
  62. {
  63. OnLoadoutPressed?.Invoke(group, args);
  64. };
  65. container.OnLoadoutUnpressed += args =>
  66. {
  67. OnLoadoutUnpressed?.Invoke(group, args);
  68. };
  69. }
  70. }
  71. }
  72. public void RefreshLoadouts(RoleLoadout loadout, ICommonSession session, IDependencyCollection collection)
  73. {
  74. foreach (var group in _groups)
  75. {
  76. group.RefreshLoadouts(Profile, loadout, session, collection);
  77. }
  78. }
  79. }