RCDMenu.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using Content.Client.UserInterface.Controls;
  2. using Content.Shared.Popups;
  3. using Content.Shared.RCD;
  4. using Content.Shared.RCD.Components;
  5. using Robust.Client.AutoGenerated;
  6. using Robust.Client.GameObjects;
  7. using Robust.Client.Player;
  8. using Robust.Client.UserInterface;
  9. using Robust.Client.UserInterface.Controls;
  10. using Robust.Client.UserInterface.XAML;
  11. using Robust.Shared.Prototypes;
  12. using System.Numerics;
  13. namespace Content.Client.RCD;
  14. [GenerateTypedNameReferences]
  15. public sealed partial class RCDMenu : RadialMenu
  16. {
  17. [Dependency] private readonly EntityManager _entManager = default!;
  18. [Dependency] private readonly IPrototypeManager _protoManager = default!;
  19. [Dependency] private readonly IPlayerManager _playerManager = default!;
  20. private SharedPopupSystem _popup;
  21. private SpriteSystem _sprites;
  22. public event Action<ProtoId<RCDPrototype>>? SendRCDSystemMessageAction;
  23. private EntityUid _owner;
  24. public RCDMenu()
  25. {
  26. IoCManager.InjectDependencies(this);
  27. RobustXamlLoader.Load(this);
  28. _popup = _entManager.System<SharedPopupSystem>();
  29. _sprites = _entManager.System<SpriteSystem>();
  30. OnChildAdded += AddRCDMenuButtonOnClickActions;
  31. }
  32. public void SetEntity(EntityUid uid)
  33. {
  34. _owner = uid;
  35. Refresh();
  36. }
  37. public void Refresh()
  38. {
  39. // Find the main radial container
  40. var main = FindControl<RadialContainer>("Main");
  41. // Populate secondary radial containers
  42. if (!_entManager.TryGetComponent<RCDComponent>(_owner, out var rcd))
  43. return;
  44. foreach (var protoId in rcd.AvailablePrototypes)
  45. {
  46. if (!_protoManager.TryIndex(protoId, out var proto))
  47. continue;
  48. if (proto.Mode == RcdMode.Invalid)
  49. continue;
  50. var parent = FindControl<RadialContainer>(proto.Category);
  51. var tooltip = Loc.GetString(proto.SetName);
  52. if ((proto.Mode == RcdMode.ConstructTile || proto.Mode == RcdMode.ConstructObject) &&
  53. proto.Prototype != null && _protoManager.TryIndex(proto.Prototype, out var entProto, logError: false))
  54. {
  55. tooltip = Loc.GetString(entProto.Name);
  56. }
  57. tooltip = OopsConcat(char.ToUpper(tooltip[0]).ToString(), tooltip.Remove(0, 1));
  58. var button = new RCDMenuButton()
  59. {
  60. SetSize = new Vector2(64f, 64f),
  61. ToolTip = tooltip,
  62. ProtoId = protoId,
  63. };
  64. if (proto.Sprite != null)
  65. {
  66. var tex = new TextureRect()
  67. {
  68. VerticalAlignment = VAlignment.Center,
  69. HorizontalAlignment = HAlignment.Center,
  70. Texture = _sprites.Frame0(proto.Sprite),
  71. TextureScale = new Vector2(2f, 2f),
  72. };
  73. button.AddChild(tex);
  74. }
  75. parent.AddChild(button);
  76. // Ensure that the button that transitions the menu to the associated category layer
  77. // is visible in the main radial container (as these all start with Visible = false)
  78. foreach (var child in main.Children)
  79. {
  80. if (child is not RadialMenuTextureButton castChild)
  81. continue;
  82. if (castChild.TargetLayer == proto.Category)
  83. {
  84. castChild.Visible = true;
  85. break;
  86. }
  87. }
  88. }
  89. // Set up menu actions
  90. foreach (var child in Children)
  91. {
  92. AddRCDMenuButtonOnClickActions(child);
  93. }
  94. }
  95. private static string OopsConcat(string a, string b)
  96. {
  97. // This exists to prevent Roslyn being clever and compiling something that fails sandbox checks.
  98. return a + b;
  99. }
  100. private void AddRCDMenuButtonOnClickActions(Control control)
  101. {
  102. var radialContainer = control as RadialContainer;
  103. if (radialContainer == null)
  104. return;
  105. foreach (var child in radialContainer.Children)
  106. {
  107. var castChild = child as RCDMenuButton;
  108. if (castChild == null)
  109. continue;
  110. castChild.OnButtonUp += _ =>
  111. {
  112. SendRCDSystemMessageAction?.Invoke(castChild.ProtoId);
  113. if (_playerManager.LocalSession?.AttachedEntity != null &&
  114. _protoManager.TryIndex(castChild.ProtoId, out var proto))
  115. {
  116. var msg = Loc.GetString("rcd-component-change-mode", ("mode", Loc.GetString(proto.SetName)));
  117. if (proto.Mode == RcdMode.ConstructTile || proto.Mode == RcdMode.ConstructObject)
  118. {
  119. var name = Loc.GetString(proto.SetName);
  120. if (proto.Prototype != null &&
  121. _protoManager.TryIndex(proto.Prototype, out var entProto, logError: false))
  122. name = entProto.Name;
  123. msg = Loc.GetString("rcd-component-change-build-mode", ("name", name));
  124. }
  125. // Popup message
  126. _popup.PopupClient(msg, _owner, _playerManager.LocalSession.AttachedEntity);
  127. }
  128. Close();
  129. };
  130. }
  131. }
  132. }
  133. public sealed class RCDMenuButton : RadialMenuTextureButtonWithSector
  134. {
  135. public ProtoId<RCDPrototype> ProtoId { get; set; }
  136. }