LoadoutGroupContainer.xaml.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Linq;
  2. using Content.Shared.Clothing;
  3. using Content.Shared.Preferences;
  4. using Content.Shared.Preferences.Loadouts;
  5. using Robust.Client.AutoGenerated;
  6. using Robust.Client.UserInterface.Controls;
  7. using Robust.Client.UserInterface.XAML;
  8. using Robust.Shared.Player;
  9. using Robust.Shared.Prototypes;
  10. namespace Content.Client.Lobby.UI.Loadouts;
  11. [GenerateTypedNameReferences]
  12. public sealed partial class LoadoutGroupContainer : BoxContainer
  13. {
  14. private readonly LoadoutGroupPrototype _groupProto;
  15. public event Action<ProtoId<LoadoutPrototype>>? OnLoadoutPressed;
  16. public event Action<ProtoId<LoadoutPrototype>>? OnLoadoutUnpressed;
  17. public LoadoutGroupContainer(HumanoidCharacterProfile profile, RoleLoadout loadout, LoadoutGroupPrototype groupProto, ICommonSession session, IDependencyCollection collection)
  18. {
  19. RobustXamlLoader.Load(this);
  20. _groupProto = groupProto;
  21. RefreshLoadouts(profile, loadout, session, collection);
  22. }
  23. /// <summary>
  24. /// Updates button availabilities and buttons.
  25. /// </summary>
  26. public void RefreshLoadouts(HumanoidCharacterProfile profile, RoleLoadout loadout, ICommonSession session, IDependencyCollection collection)
  27. {
  28. var protoMan = collection.Resolve<IPrototypeManager>();
  29. var loadoutSystem = collection.Resolve<IEntityManager>().System<LoadoutSystem>();
  30. RestrictionsContainer.DisposeAllChildren();
  31. if (_groupProto.MinLimit > 0)
  32. {
  33. RestrictionsContainer.AddChild(new Label()
  34. {
  35. Text = Loc.GetString("loadouts-min-limit", ("count", _groupProto.MinLimit)),
  36. Margin = new Thickness(5, 0, 5, 5),
  37. });
  38. }
  39. if (_groupProto.MaxLimit > 0)
  40. {
  41. RestrictionsContainer.AddChild(new Label()
  42. {
  43. Text = Loc.GetString("loadouts-max-limit", ("count", _groupProto.MaxLimit)),
  44. Margin = new Thickness(5, 0, 5, 5),
  45. });
  46. }
  47. if (protoMan.TryIndex(loadout.Role, out var roleProto) && roleProto.Points != null && loadout.Points != null)
  48. {
  49. RestrictionsContainer.AddChild(new Label()
  50. {
  51. Text = Loc.GetString("loadouts-points-limit", ("count", loadout.Points.Value), ("max", roleProto.Points.Value)),
  52. Margin = new Thickness(5, 0, 5, 5),
  53. });
  54. }
  55. LoadoutsContainer.DisposeAllChildren();
  56. // Didn't use options because this is more robust in future.
  57. var selected = loadout.SelectedLoadouts[_groupProto.ID];
  58. foreach (var loadoutProto in _groupProto.Loadouts)
  59. {
  60. if (!protoMan.TryIndex(loadoutProto, out var loadProto))
  61. continue;
  62. var matchingLoadout = selected.FirstOrDefault(e => e.Prototype == loadoutProto);
  63. var pressed = matchingLoadout != null;
  64. var enabled = loadout.IsValid(profile, session, loadoutProto, collection, out var reason);
  65. var loadoutContainer = new LoadoutContainer(loadoutProto, !enabled, reason);
  66. loadoutContainer.Select.Pressed = pressed;
  67. loadoutContainer.Text = loadoutSystem.GetName(loadProto);
  68. loadoutContainer.Select.OnPressed += args =>
  69. {
  70. if (args.Button.Pressed)
  71. OnLoadoutPressed?.Invoke(loadoutProto);
  72. else
  73. OnLoadoutUnpressed?.Invoke(loadoutProto);
  74. };
  75. LoadoutsContainer.AddChild(loadoutContainer);
  76. }
  77. }
  78. }