1
0

DockingScreen.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Linq;
  2. using System.Numerics;
  3. using System.Text;
  4. using Content.Shared.Shuttles.BUIStates;
  5. using Content.Shared.Shuttles.Systems;
  6. using Robust.Client.AutoGenerated;
  7. using Robust.Client.UserInterface;
  8. using Robust.Client.UserInterface.Controls;
  9. using Robust.Client.UserInterface.XAML;
  10. using Robust.Shared.Utility;
  11. namespace Content.Client.Shuttles.UI;
  12. [GenerateTypedNameReferences]
  13. public sealed partial class DockingScreen : BoxContainer
  14. {
  15. [Dependency] private readonly IEntityManager _entManager = default!;
  16. private readonly SharedShuttleSystem _shuttles;
  17. /// <summary>
  18. /// Stored by GridID then by docks
  19. /// </summary>
  20. public Dictionary<NetEntity, List<DockingPortState>> Docks = new();
  21. /// <summary>
  22. /// Store the dock buttons for the side buttons.
  23. /// </summary>
  24. private readonly Dictionary<NetEntity, Button> _ourDockButtons = new();
  25. public event Action<NetEntity, NetEntity>? DockRequest;
  26. public event Action<NetEntity>? UndockRequest;
  27. public DockingScreen()
  28. {
  29. RobustXamlLoader.Load(this);
  30. IoCManager.InjectDependencies(this);
  31. _shuttles = _entManager.System<SharedShuttleSystem>();
  32. DockingControl.OnViewDock += OnView;
  33. DockingControl.DockRequest += (entity, netEntity) =>
  34. {
  35. DockRequest?.Invoke(entity, netEntity);
  36. };
  37. DockingControl.UndockRequest += entity =>
  38. {
  39. UndockRequest?.Invoke(entity);
  40. };
  41. }
  42. private void OnView(NetEntity obj)
  43. {
  44. if (_ourDockButtons.TryGetValue(obj, out var viewed))
  45. {
  46. viewed.Pressed = true;
  47. }
  48. }
  49. public void UpdateState(EntityUid? shuttle, DockingInterfaceState state)
  50. {
  51. Docks = state.Docks;
  52. DockingControl.DockState = state;
  53. DockingControl.GridEntity = shuttle;
  54. BuildDocks(shuttle);
  55. }
  56. private void BuildDocks(EntityUid? shuttle)
  57. {
  58. DockingControl.BuildDocks(shuttle);
  59. var currentDock = DockingControl.ViewedDock;
  60. // DockedWith.DisposeAllChildren();
  61. DockPorts.DisposeAllChildren();
  62. _ourDockButtons.Clear();
  63. if (shuttle == null)
  64. {
  65. DockingControl.SetViewedDock(null);
  66. return;
  67. }
  68. var shuttleNent = _entManager.GetNetEntity(shuttle.Value);
  69. if (!Docks.TryGetValue(shuttleNent, out var shuttleDocks) || shuttleDocks.Count <= 0)
  70. return;
  71. var dockText = new StringBuilder();
  72. var buttonGroup = new ButtonGroup();
  73. var idx = 0;
  74. var selected = false;
  75. // Build the dock buttons for our docks.
  76. foreach (var dock in shuttleDocks)
  77. {
  78. idx++;
  79. dockText.Clear();
  80. dockText.Append(dock.Name);
  81. var button = new Button()
  82. {
  83. Text = dockText.ToString(),
  84. ToggleMode = true,
  85. Group = buttonGroup,
  86. Margin = new Thickness(0f, 3f),
  87. };
  88. button.OnMouseEntered += args =>
  89. {
  90. DockingControl.HighlightedDock = dock.Entity;
  91. };
  92. button.OnMouseExited += args =>
  93. {
  94. DockingControl.HighlightedDock = null;
  95. };
  96. button.Label.Margin = new Thickness(3f);
  97. if (currentDock == dock.Entity)
  98. {
  99. selected = true;
  100. button.Pressed = true;
  101. }
  102. button.OnPressed += args =>
  103. {
  104. OnDockPress(dock);
  105. };
  106. _ourDockButtons[dock.Entity] = button;
  107. DockPorts.AddChild(button);
  108. }
  109. // Button group needs one selected so just show the first one.
  110. if (!selected)
  111. {
  112. var buttonOne = shuttleDocks[0];
  113. OnDockPress(buttonOne);
  114. }
  115. var shuttleContainers = new Dictionary<NetEntity, DockObject>();
  116. foreach (var dock in shuttleDocks.OrderBy(x => x.GridDockedWith))
  117. {
  118. if (dock.GridDockedWith == null)
  119. continue;
  120. DockObject? dockContainer;
  121. if (!shuttleContainers.TryGetValue(dock.GridDockedWith.Value, out dockContainer))
  122. {
  123. dockContainer = new DockObject();
  124. shuttleContainers[dock.GridDockedWith.Value] = dockContainer;
  125. var dockGrid = _entManager.GetEntity(dock.GridDockedWith);
  126. string? iffLabel = null;
  127. if (_entManager.EntityExists(dockGrid))
  128. {
  129. iffLabel = _shuttles.GetIFFLabel(dockGrid.Value);
  130. }
  131. iffLabel ??= Loc.GetString("shuttle-console-unknown");
  132. dockContainer.SetName(iffLabel);
  133. // DockedWith.AddChild(dockContainer);
  134. }
  135. dockContainer.AddDock(dock, DockingControl);
  136. }
  137. }
  138. private void OnDockPress(DockingPortState state)
  139. {
  140. DockingControl.SetViewedDock(state);
  141. }
  142. }