SpawnExplosionWindow.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Numerics;
  2. using Content.Shared.Explosion;
  3. using JetBrains.Annotations;
  4. using Robust.Client.AutoGenerated;
  5. using Robust.Client.Console;
  6. using Robust.Client.GameObjects;
  7. using Robust.Client.Player;
  8. using Robust.Client.UserInterface.CustomControls;
  9. using Robust.Client.UserInterface.XAML;
  10. using Robust.Shared.Map;
  11. using Robust.Shared.Prototypes;
  12. using static Robust.Client.UserInterface.Controls.BaseButton;
  13. using static Robust.Client.UserInterface.Controls.OptionButton;
  14. namespace Content.Client.Administration.UI.SpawnExplosion;
  15. [GenerateTypedNameReferences]
  16. [UsedImplicitly]
  17. public sealed partial class SpawnExplosionWindow : DefaultWindow
  18. {
  19. [Dependency] private readonly IClientConsoleHost _conHost = default!;
  20. [Dependency] private readonly IMapManager _mapManager = default!;
  21. [Dependency] private readonly IPlayerManager _playerManager = default!;
  22. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  23. [Dependency] private readonly IEntityManager _entMan = default!;
  24. private readonly SharedTransformSystem _transform = default!;
  25. private readonly SpawnExplosionEui _eui;
  26. private List<MapId> _mapData = new();
  27. private List<string> _explosionTypes = new();
  28. /// <summary>
  29. /// Used to prevent unnecessary preview updates when setting fields (e.g., updating position)..
  30. /// </summary>
  31. private bool _pausePreview;
  32. public SpawnExplosionWindow(SpawnExplosionEui eui)
  33. {
  34. RobustXamlLoader.Load(this);
  35. IoCManager.InjectDependencies(this);
  36. _transform = _entMan.System<TransformSystem>();
  37. _eui = eui;
  38. ExplosionOption.OnItemSelected += ExplosionSelected;
  39. MapOptions.OnItemSelected += MapSelected;
  40. Recentre.OnPressed += (_) => SetLocation();
  41. Spawn.OnPressed += SubmitButtonOnOnPressed;
  42. Preview.OnToggled += (_) => UpdatePreview();
  43. MapX.OnValueChanged += (_) => UpdatePreview();
  44. MapY.OnValueChanged += (_) => UpdatePreview();
  45. Intensity.OnValueChanged += (_) => UpdatePreview();
  46. Slope.OnValueChanged += (_) => UpdatePreview();
  47. MaxIntensity.OnValueChanged += (_) => UpdatePreview();
  48. }
  49. private void ExplosionSelected(ItemSelectedEventArgs args)
  50. {
  51. ExplosionOption.SelectId(args.Id);
  52. UpdatePreview();
  53. }
  54. private void MapSelected(ItemSelectedEventArgs args)
  55. {
  56. MapOptions.SelectId(args.Id);
  57. UpdatePreview();
  58. }
  59. protected override void EnteredTree()
  60. {
  61. SetLocation();
  62. UpdateExplosionTypeOptions();
  63. }
  64. private void UpdateExplosionTypeOptions()
  65. {
  66. _explosionTypes.Clear();
  67. ExplosionOption.Clear();
  68. foreach (var type in _prototypeManager.EnumeratePrototypes<ExplosionPrototype>())
  69. {
  70. _explosionTypes.Add(type.ID);
  71. ExplosionOption.AddItem(type.ID);
  72. }
  73. }
  74. private void UpdateMapOptions()
  75. {
  76. _mapData.Clear();
  77. MapOptions.Clear();
  78. foreach (var map in _mapManager.GetAllMapIds())
  79. {
  80. _mapData.Add(map);
  81. MapOptions.AddItem(map.ToString());
  82. }
  83. }
  84. /// <summary>
  85. /// Set the current grid & indices based on the attached entities current location.
  86. /// </summary>
  87. private void SetLocation()
  88. {
  89. UpdateMapOptions();
  90. if (!_entMan.TryGetComponent(_playerManager.LocalEntity, out TransformComponent? transform))
  91. return;
  92. _pausePreview = true;
  93. MapOptions.Select(_mapData.IndexOf(transform.MapID));
  94. (MapX.Value, MapY.Value) = _transform.GetMapCoordinates(_playerManager.LocalEntity!.Value, xform: transform).Position;
  95. _pausePreview = false;
  96. UpdatePreview();
  97. }
  98. private void UpdatePreview()
  99. {
  100. if (_pausePreview)
  101. return;
  102. if (!Preview.Pressed)
  103. {
  104. _eui.ClearOverlay();
  105. return;
  106. }
  107. MapCoordinates coords = new(new Vector2(MapX.Value, MapY.Value), _mapData[MapOptions.SelectedId]);
  108. var explosionType = _explosionTypes[ExplosionOption.SelectedId];
  109. _eui.RequestPreviewData(coords, explosionType, Intensity.Value, Slope.Value, MaxIntensity.Value);
  110. }
  111. private void SubmitButtonOnOnPressed(ButtonEventArgs args)
  112. {
  113. // need to make room to view the fireworks
  114. Preview.Pressed = false;
  115. _eui.ClearOverlay();
  116. // for the actual explosion, we will just re-use the explosion command.
  117. // so assemble command arguments:
  118. var mapId = _mapData[MapOptions.SelectedId];
  119. var explosionType = _explosionTypes[ExplosionOption.SelectedId];
  120. var cmd = $"explosion {Intensity.Value} {Slope.Value} {MaxIntensity.Value} {MapX.Value} {MapY.Value} {mapId} {explosionType}";
  121. _conHost.ExecuteCommand(cmd);
  122. }
  123. }