MultiplayerConnectionViewportMessageSystemComponent.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <Atom/RPI.Public/ViewportContextBus.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzFramework/Font/FontInterface.h>
  12. #include <Multiplayer/IMultiplayer.h>
  13. #include <Multiplayer/MultiplayerEditorServerBus.h>
  14. namespace Multiplayer
  15. {
  16. //! System component that draws viewport messaging as the editor attempts connection to the editor-server while starting up game-mode.
  17. class MultiplayerConnectionViewportMessageSystemComponent final
  18. : public AZ::Component
  19. , public AZ::RPI::ViewportContextNotificationBus::Handler
  20. , MultiplayerEditorServerNotificationBus::Handler
  21. {
  22. public:
  23. static constexpr int MaxMessageLength = 256;
  24. static constexpr float ScrimAlpha = 0.6f;
  25. static constexpr AZ::TimeMs CenterViewportDebugToastTimePerWord = AZ::TimeMs{ 300 }; // Consider reading speed to be 200 words per minute (300 ms)
  26. static constexpr AZ::TimeMs CenterViewportDebugToastTimePrefix = AZ::TimeMs{ 2000 }; // Give viewers 2.0 seconds to notice the toast
  27. static constexpr AZ::TimeMs CenterViewportDebugToastTimeFade = AZ::TimeMs{ 1000 }; // Milliseconds toast takes to fade out
  28. // Messaging for client during editor play mode
  29. static constexpr char CenterViewportDebugTitle[] = "Multiplayer Editor";
  30. static constexpr char OnServerLaunchedMessage[] = "(1/4) Launching server...";
  31. static constexpr char OnServerLaunchFailMessage[] = "(1/4) Could not launch editor server.\nSee console for more info.";
  32. static constexpr char OnEditorConnectionAttemptMessage[] = "(2/4) Attempting to connect to server in order to send level data.\nAttempt %i of %i";
  33. static constexpr char OnEditorConnectionAttemptsFailedMessage[] = "(2/4) Failed to connect to server after %i attempts!\nPlease exit play mode and try again.";
  34. static constexpr char OnEditorSendingLevelDataMessage[] = "(3/4) Editor is sending the editor-server the level data packet.\nBytes %u / %u sent.";
  35. static constexpr char OnEditorSendingLevelDataFailedMessage[] =
  36. "(3/4) Editor failed to send the editor-server the level data packet.\nPlease exit play mode and try again.";
  37. static constexpr char OnEditorSendingLevelDataSuccessMessage[] =
  38. "(4/4) Waiting for editor-server to finish loading the level data.";
  39. static constexpr char OnConnectToSimulationFailMessage[] =
  40. "EditorServerReady packet was received, but connecting to the editor-server's network simulation failed! Is the editor and "
  41. "server using the same sv_port (%i)?";
  42. static constexpr char OnEditorServerStoppedUnexpectedly[] ="Editor server has unexpectedly stopped running!";
  43. // Messaging for clients
  44. static constexpr char ClientStatusTitle[] = "Multiplayer Client Status:";
  45. // Messaging common for both dedicated server and client-server
  46. static constexpr char ServerHostingPort[] = "Hosting on port %i";
  47. // Messaging for dedicated server
  48. static constexpr char DedicatedServerStatusTitle[] = "Multiplayer Dedicated Server Status:";
  49. static constexpr char DedicatedServerNotHosting[] = "Not Hosting";
  50. static constexpr char DedicatedServerHostingClientCount[] = "%i client(s)";
  51. // Messaging for client-server
  52. static constexpr char ClientServerStatusTitle[] = "Multiplayer Client-Server Status:";
  53. static constexpr char ClientServerHostingClientCount[] = "%i client(s) (including self)";
  54. // Toast Messages
  55. static constexpr char CenterViewportToastTitle[] = "Multiplayer Alert";
  56. static constexpr char OnBlockedLevelLoadMessage[] = "Blocked level load; see log for details.";
  57. static constexpr char OnNoServerLevelLoadedMessageClientSide[] = "Server accept message did not provide a level.\nEnsure server has level loaded before connecting.";
  58. static constexpr char OnNoServerLevelLoadedMessageServerSide[] = "A client has connected, but we're not in a level.\nPlease load a valid multiplayer level before accepting clients.";
  59. static constexpr char OnVersionMismatch[] = "Multiplayer Version Mismatch.\nEnsure server and client are both up to date.";
  60. AZ_COMPONENT(MultiplayerConnectionViewportMessageSystemComponent, "{7600cfcf-e380-4876-aa90-8120e57205e9}");
  61. static void Reflect(AZ::ReflectContext* context);
  62. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  63. //! AZ::Component overrides.
  64. //! @{
  65. void Activate() override;
  66. void Deactivate() override;
  67. //! @}
  68. private:
  69. //! AZ::RPI::ViewportContextNotificationBus::Handler overrides.
  70. //! @{
  71. void OnRenderTick() override;
  72. //! @}
  73. //! MultiplayerEditorServerNotificationBus::Handler overrides.
  74. //! @{
  75. void OnServerLaunched() override;
  76. void OnServerLaunchFail() override;
  77. void OnEditorConnectionAttempt(uint16_t connectionAttempts, uint16_t maxAttempts) override;
  78. void OnEditorConnectionAttemptsFailed(uint16_t failedAttempts) override;
  79. void OnEditorSendingLevelData(uint32_t bytesSent, uint32_t bytesTotal) override;
  80. void OnEditorSendingLevelDataFailed() override;
  81. void OnEditorSendingLevelDataSuccess() override;
  82. void OnConnectToSimulationSuccess() override;
  83. void OnConnectToSimulationFail(uint16_t serverPort) override;
  84. void OnPlayModeEnd() override;
  85. void OnEditorServerProcessStoppedUnexpectedly() override;
  86. //! @}
  87. //! IMultiplayer AZ::Event handlers.
  88. //! @{
  89. void OnBlockedLevelLoad();
  90. LevelLoadBlockedEvent::Handler m_levelLoadBlockedHandler = LevelLoadBlockedEvent::Handler([this](){OnBlockedLevelLoad();});
  91. void OnNoServerLevelLoadedEvent();
  92. NoServerLevelLoadedEvent::Handler m_noServerLevelLoadedHandler = NoServerLevelLoadedEvent::Handler([this](){OnNoServerLevelLoadedEvent();});
  93. void OnVersionMismatchEvent();
  94. VersionMismatchEvent::Handler m_versionMismatchEventHandler = VersionMismatchEvent::Handler([this](){OnVersionMismatchEvent();});
  95. //! @}
  96. void DrawConnectionStatus(AzNetworking::ConnectionState connectionState, const AzNetworking::IpAddress& hostAddress);
  97. void DrawConnectionStatusLine(const char* line, const AZ::Color& color);
  98. // Render a scrim (a gentle background shading) to create contrast in order that the debug text in the foreground is readable.
  99. // Make scrim most pronounced from the center of the screen and fade out towards the top and bottom on the screen.
  100. void DrawScrim(float alphaMultiplier = 1.0f) const;
  101. // Draws a message in the center of the viewport
  102. // @param title text is displayed over the message
  103. // @param title color is color of the title. Generally yellow or red (in the case of an error)
  104. // @param message to display
  105. // @param alpha value of the message (useful for fading out the message over time)
  106. void DrawCenterViewportMessage(const char* title, const AZ::Color& titleColor, const char* message, float alpha);
  107. AZStd::fixed_string<MaxMessageLength> m_centerViewportDebugText;
  108. AZ::Color m_centerViewportDebugTextColor = AZ::Colors::Yellow;
  109. AZStd::fixed_string<MaxMessageLength> m_centerViewportDebugToastText;
  110. AZ::TimeMs m_centerViewportDebugToastStartTime;
  111. AzFramework::FontDrawInterface* m_fontDrawInterface = nullptr;
  112. AzFramework::TextDrawParameters m_drawParams;
  113. float m_lineSpacing = 0.0f;
  114. AzNetworking::IpAddress m_hostIpAddress;
  115. int m_currentConnectionsDrawCount = 0;
  116. };
  117. }