EmotesUIController.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Content.Client.Chat.UI;
  2. using Content.Client.Gameplay;
  3. using Content.Client.UserInterface.Controls;
  4. using Content.Shared.Chat;
  5. using Content.Shared.Chat.Prototypes;
  6. using Content.Shared.Input;
  7. using JetBrains.Annotations;
  8. using Robust.Client.Graphics;
  9. using Robust.Client.Input;
  10. using Robust.Client.UserInterface.Controllers;
  11. using Robust.Client.UserInterface.Controls;
  12. using Robust.Shared.Input.Binding;
  13. using Robust.Shared.Prototypes;
  14. namespace Content.Client.UserInterface.Systems.Emotes;
  15. [UsedImplicitly]
  16. public sealed class EmotesUIController : UIController, IOnStateChanged<GameplayState>
  17. {
  18. [Dependency] private readonly IEntityManager _entityManager = default!;
  19. [Dependency] private readonly IClyde _displayManager = default!;
  20. [Dependency] private readonly IInputManager _inputManager = default!;
  21. private MenuButton? EmotesButton => UIManager.GetActiveUIWidgetOrNull<MenuBar.Widgets.GameTopMenuBar>()?.EmotesButton;
  22. private EmotesMenu? _menu;
  23. public void OnStateEntered(GameplayState state)
  24. {
  25. CommandBinds.Builder
  26. .Bind(ContentKeyFunctions.OpenEmotesMenu,
  27. InputCmdHandler.FromDelegate(_ => ToggleEmotesMenu(false)))
  28. .Register<EmotesUIController>();
  29. }
  30. public void OnStateExited(GameplayState state)
  31. {
  32. CommandBinds.Unregister<EmotesUIController>();
  33. }
  34. private void ToggleEmotesMenu(bool centered)
  35. {
  36. if (_menu == null)
  37. {
  38. // setup window
  39. _menu = UIManager.CreateWindow<EmotesMenu>();
  40. _menu.OnClose += OnWindowClosed;
  41. _menu.OnOpen += OnWindowOpen;
  42. _menu.OnPlayEmote += OnPlayEmote;
  43. if (EmotesButton != null)
  44. EmotesButton.SetClickPressed(true);
  45. if (centered)
  46. {
  47. _menu.OpenCentered();
  48. }
  49. else
  50. {
  51. // Open the menu, centered on the mouse
  52. var vpSize = _displayManager.ScreenSize;
  53. _menu.OpenCenteredAt(_inputManager.MouseScreenPosition.Position / vpSize);
  54. }
  55. }
  56. else
  57. {
  58. _menu.OnClose -= OnWindowClosed;
  59. _menu.OnOpen -= OnWindowOpen;
  60. _menu.OnPlayEmote -= OnPlayEmote;
  61. if (EmotesButton != null)
  62. EmotesButton.SetClickPressed(false);
  63. CloseMenu();
  64. }
  65. }
  66. public void UnloadButton()
  67. {
  68. if (EmotesButton == null)
  69. return;
  70. EmotesButton.OnPressed -= ActionButtonPressed;
  71. }
  72. public void LoadButton()
  73. {
  74. if (EmotesButton == null)
  75. return;
  76. EmotesButton.OnPressed += ActionButtonPressed;
  77. }
  78. private void ActionButtonPressed(BaseButton.ButtonEventArgs args)
  79. {
  80. ToggleEmotesMenu(true);
  81. }
  82. private void OnWindowClosed()
  83. {
  84. if (EmotesButton != null)
  85. EmotesButton.Pressed = false;
  86. CloseMenu();
  87. }
  88. private void OnWindowOpen()
  89. {
  90. if (EmotesButton != null)
  91. EmotesButton.Pressed = true;
  92. }
  93. private void CloseMenu()
  94. {
  95. if (_menu == null)
  96. return;
  97. _menu.Dispose();
  98. _menu = null;
  99. }
  100. private void OnPlayEmote(ProtoId<EmotePrototype> protoId)
  101. {
  102. _entityManager.RaisePredictiveEvent(new PlayEmoteMessage(protoId));
  103. }
  104. }