1
0

GhostRoleRadioMenu.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Content.Client.UserInterface.Controls;
  2. using Content.Shared.Ghost.Roles;
  3. using Content.Shared.Ghost.Roles.Components;
  4. using Robust.Client.UserInterface;
  5. using Robust.Client.UserInterface.Controls;
  6. using Robust.Client.UserInterface.XAML;
  7. using Robust.Shared.Prototypes;
  8. using System.Numerics;
  9. namespace Content.Client.Ghost;
  10. public sealed partial class GhostRoleRadioMenu : RadialMenu
  11. {
  12. [Dependency] private readonly EntityManager _entityManager = default!;
  13. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  14. public event Action<ProtoId<GhostRolePrototype>>? SendGhostRoleRadioMessageAction;
  15. public EntityUid Entity { get; set; }
  16. public GhostRoleRadioMenu()
  17. {
  18. IoCManager.InjectDependencies(this);
  19. RobustXamlLoader.Load(this);
  20. }
  21. public void SetEntity(EntityUid uid)
  22. {
  23. Entity = uid;
  24. RefreshUI();
  25. }
  26. private void RefreshUI()
  27. {
  28. // The main control that will contain all the clickable options
  29. var main = FindControl<RadialContainer>("Main");
  30. // The purpose of this radial UI is for ghost role radios that allow you to select
  31. // more than one potential option, such as with kobolds/lizards.
  32. // This means that it won't show anything if SelectablePrototypes is empty.
  33. if (!_entityManager.TryGetComponent<GhostRoleMobSpawnerComponent>(Entity, out var comp))
  34. return;
  35. foreach (var ghostRoleProtoString in comp.SelectablePrototypes)
  36. {
  37. // For each prototype we find we want to create a button that uses the name of the ghost role
  38. // as the hover tooltip, and the icon is taken from either the ghost role entityprototype
  39. // or the indicated icon entityprototype.
  40. if (!_prototypeManager.TryIndex<GhostRolePrototype>(ghostRoleProtoString, out var ghostRoleProto))
  41. continue;
  42. var button = new GhostRoleRadioMenuButton()
  43. {
  44. SetSize = new Vector2(64, 64),
  45. ToolTip = Loc.GetString(ghostRoleProto.Name),
  46. ProtoId = ghostRoleProto.ID,
  47. };
  48. var entProtoView = new EntityPrototypeView()
  49. {
  50. SetSize = new Vector2(48, 48),
  51. VerticalAlignment = VAlignment.Center,
  52. HorizontalAlignment = HAlignment.Center,
  53. Stretch = SpriteView.StretchMode.Fill
  54. };
  55. // pick the icon if it exists, otherwise fallback to the ghost role's entity
  56. if (_prototypeManager.TryIndex(ghostRoleProto.IconPrototype, out var iconProto))
  57. entProtoView.SetPrototype(iconProto);
  58. else
  59. entProtoView.SetPrototype(ghostRoleProto.EntityPrototype);
  60. button.AddChild(entProtoView);
  61. main.AddChild(button);
  62. AddGhostRoleRadioMenuButtonOnClickActions(main);
  63. }
  64. }
  65. private void AddGhostRoleRadioMenuButtonOnClickActions(Control control)
  66. {
  67. var mainControl = control as RadialContainer;
  68. if (mainControl == null)
  69. return;
  70. foreach (var child in mainControl.Children)
  71. {
  72. var castChild = child as GhostRoleRadioMenuButton;
  73. if (castChild == null)
  74. continue;
  75. castChild.OnButtonUp += _ =>
  76. {
  77. SendGhostRoleRadioMessageAction?.Invoke(castChild.ProtoId);
  78. Close();
  79. };
  80. }
  81. }
  82. }
  83. public sealed class GhostRoleRadioMenuButton : RadialMenuTextureButtonWithSector
  84. {
  85. public ProtoId<GhostRolePrototype> ProtoId { get; set; }
  86. }