StationAiMenu.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Numerics;
  2. using Content.Client.UserInterface.Controls;
  3. using Content.Shared.Silicons.StationAi;
  4. using Robust.Client.AutoGenerated;
  5. using Robust.Client.GameObjects;
  6. using Robust.Client.Graphics;
  7. using Robust.Client.UserInterface.Controls;
  8. using Robust.Client.UserInterface.XAML;
  9. using Robust.Shared.Timing;
  10. namespace Content.Client.Silicons.StationAi;
  11. [GenerateTypedNameReferences]
  12. public sealed partial class StationAiMenu : RadialMenu
  13. {
  14. [Dependency] private readonly IClyde _clyde = default!;
  15. [Dependency] private readonly IEntityManager _entManager = default!;
  16. public event Action<BaseStationAiAction>? OnAiRadial;
  17. private EntityUid _tracked;
  18. public StationAiMenu()
  19. {
  20. IoCManager.InjectDependencies(this);
  21. RobustXamlLoader.Load(this);
  22. }
  23. public void Track(EntityUid owner)
  24. {
  25. _tracked = owner;
  26. if (!_entManager.EntityExists(_tracked))
  27. {
  28. Close();
  29. return;
  30. }
  31. BuildButtons();
  32. UpdatePosition();
  33. }
  34. private void BuildButtons()
  35. {
  36. var ev = new GetStationAiRadialEvent();
  37. _entManager.EventBus.RaiseLocalEvent(_tracked, ref ev);
  38. var main = FindControl<RadialContainer>("Main");
  39. main.DisposeAllChildren();
  40. var sprites = _entManager.System<SpriteSystem>();
  41. foreach (var action in ev.Actions)
  42. {
  43. // TODO: This radial boilerplate is quite annoying
  44. var button = new StationAiMenuButton(action.Event)
  45. {
  46. SetSize = new Vector2(64f, 64f),
  47. ToolTip = action.Tooltip != null ? Loc.GetString(action.Tooltip) : null,
  48. };
  49. if (action.Sprite != null)
  50. {
  51. var texture = sprites.Frame0(action.Sprite);
  52. var scale = Vector2.One;
  53. if (texture.Width <= 32)
  54. {
  55. scale *= 2;
  56. }
  57. var tex = new TextureRect
  58. {
  59. VerticalAlignment = VAlignment.Center,
  60. HorizontalAlignment = HAlignment.Center,
  61. Texture = texture,
  62. TextureScale = scale,
  63. };
  64. button.AddChild(tex);
  65. }
  66. button.OnPressed += args =>
  67. {
  68. OnAiRadial?.Invoke(action.Event);
  69. Close();
  70. };
  71. main.AddChild(button);
  72. }
  73. }
  74. protected override void FrameUpdate(FrameEventArgs args)
  75. {
  76. base.FrameUpdate(args);
  77. UpdatePosition();
  78. }
  79. private void UpdatePosition()
  80. {
  81. if (!_entManager.TryGetComponent(_tracked, out TransformComponent? xform))
  82. {
  83. Close();
  84. return;
  85. }
  86. if (!xform.Coordinates.IsValid(_entManager))
  87. {
  88. Close();
  89. return;
  90. }
  91. var coords = _entManager.System<SpriteSystem>().GetSpriteScreenCoordinates((_tracked, null, xform));
  92. if (!coords.IsValid)
  93. {
  94. Close();
  95. return;
  96. }
  97. OpenScreenAt(coords.Position, _clyde);
  98. }
  99. }
  100. public sealed class StationAiMenuButton(BaseStationAiAction action) : RadialMenuTextureButtonWithSector
  101. {
  102. public BaseStationAiAction Action = action;
  103. }