UiStartMenuComponent.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include <Source/Components/UI/UiStartMenuComponent.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <LyShine/Bus/UiButtonBus.h>
  12. #include <LyShine/Bus/UiCursorBus.h>
  13. #include <LyShine/Bus/UiElementBus.h>
  14. #include <LyShine/Bus/UiTextInputBus.h>
  15. namespace MultiplayerSample
  16. {
  17. void UiStartMenuComponent::Reflect(AZ::ReflectContext* context)
  18. {
  19. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  20. {
  21. serializeContext->Class<UiStartMenuComponent, AZ::Component>()
  22. ->Version(1)
  23. ->Field("HostButton", &UiStartMenuComponent::m_hostButtonUi)
  24. ->Field("JoinButton", &UiStartMenuComponent::m_joinButtonUi)
  25. ->Field("ExitButton", &UiStartMenuComponent::m_exitButtonUi)
  26. ->Field("IPAddressTextInput", &UiStartMenuComponent::m_ipAddressTextInputUi)
  27. ->Field("AttemptConnectionBlockerUi", &UiStartMenuComponent::m_attemptConnectionBlockerUi)
  28. ->Field("ConnectToHostFailedUi", &UiStartMenuComponent::m_connectToHostFailedUi)
  29. ;
  30. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  31. {
  32. editContext->Class<UiStartMenuComponent>("UiStartMenuComponent", "Component to setup the start menu")
  33. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  34. ->Attribute(AZ::Edit::Attributes::Category, "Multiplayer Sample UI")
  35. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Component_Placeholder.svg")
  36. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("CanvasUI"))
  37. ->DataElement(AZ::Edit::UIHandlers::Default, &UiStartMenuComponent::m_hostButtonUi, "Host Button", "The ui button hosting a game (only available for unified launchers which can run as a client-host).")
  38. ->DataElement(AZ::Edit::UIHandlers::Default, &UiStartMenuComponent::m_joinButtonUi, "Join Button", "The ui button joining a multiplayer game based on the provided ip address.")
  39. ->DataElement(AZ::Edit::UIHandlers::Default, &UiStartMenuComponent::m_exitButtonUi, "Exit Button", "The ui button to quit the app.")
  40. ->DataElement(AZ::Edit::UIHandlers::Default, &UiStartMenuComponent::m_ipAddressTextInputUi, "IP Address TextInput", "The ui text input providing the ip address to join.")
  41. ->DataElement(AZ::Edit::UIHandlers::Default, &UiStartMenuComponent::m_attemptConnectionBlockerUi, "Attempt Connection Blocker", "Fullscreen ui for blocking user input while the client tries to connect.")
  42. ->DataElement(AZ::Edit::UIHandlers::Default, &UiStartMenuComponent::m_connectToHostFailedUi, "Connection To Host Failed", "Ui to inform the user that connecting to the host failed.")
  43. ;
  44. }
  45. }
  46. }
  47. void UiStartMenuComponent::Activate()
  48. {
  49. UiCursorBus::Broadcast(&UiCursorInterface::IncrementVisibleCounter);
  50. // Listen for button presses
  51. UiButtonBus::Event(m_exitButtonUi, &UiButtonInterface::SetOnClickCallback, [this](AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position) {OnButtonClicked(buttonEntityId); });
  52. UiButtonBus::Event(m_hostButtonUi, &UiButtonInterface::SetOnClickCallback, [this](AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position) {OnButtonClicked(buttonEntityId); });
  53. UiButtonBus::Event(m_joinButtonUi, &UiButtonInterface::SetOnClickCallback, [this](AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position) {OnButtonClicked(buttonEntityId); });
  54. UiButtonBus::Event(m_connectToHostFailedUi, &UiButtonInterface::SetOnClickCallback, [this](AZ::EntityId buttonEntityId, [[maybe_unused]] AZ::Vector2 position) {OnButtonClicked(buttonEntityId); });
  55. // Hide the host button if this app can't host
  56. #if !AZ_TRAIT_SERVER
  57. UiElementBus::Event(m_hostButtonUi, &UiElementInterface::SetIsEnabled, false);
  58. #endif
  59. // Hide the attempting connection ui until the player tries to connect
  60. UiElementBus::Event(m_attemptConnectionBlockerUi, &UiElementInterface::SetIsEnabled, false);
  61. // Listen for disconnect events to know if connecting to the host server failed
  62. AZ::Interface<Multiplayer::IMultiplayer>::Get()->AddEndpointDisconnectedHandler(m_onConnectToHostFailed);
  63. // Set default text of the ip-address input textbox based on 'cl_serverAddr'
  64. if (const auto console = AZ::Interface<AZ::IConsole>::Get())
  65. {
  66. AZ::CVarFixedString serverAddress;
  67. if (console->GetCvarValue("cl_serverAddr", serverAddress) == AZ::GetValueResult::Success)
  68. {
  69. UiTextInputBus::Event(m_ipAddressTextInputUi, &UiTextInputInterface::SetText, serverAddress.c_str());
  70. }
  71. else
  72. {
  73. AZ_Warning("UiStartMenuComponent", false, "Could not access cl_serveraddr cvar, so the ip-address input textfield won't be filled out. Please update UiStartMenuComponent.cpp to check for a valid cvar!")
  74. }
  75. }
  76. }
  77. void UiStartMenuComponent::Deactivate()
  78. {
  79. m_onConnectToHostFailed.Disconnect();
  80. UiCursorBus::Broadcast(&UiCursorInterface::DecrementVisibleCounter);
  81. }
  82. void UiStartMenuComponent::OnButtonClicked(AZ::EntityId buttonEntityId) const
  83. {
  84. const auto console = AZ::Interface<AZ::IConsole>::Get();
  85. if (!console)
  86. {
  87. AZ_Assert(false, "UiStartMenuComponent attempting to use console commands before AZ::Console is available.");
  88. return;
  89. }
  90. if (buttonEntityId == m_exitButtonUi)
  91. {
  92. console->PerformCommand("quit");
  93. }
  94. else if (buttonEntityId == m_hostButtonUi)
  95. {
  96. console->PerformCommand("host");
  97. console->PerformCommand(AZStd::string::format("loadlevel %s", MultiplayerLevelName).c_str());
  98. }
  99. else if (buttonEntityId == m_joinButtonUi)
  100. {
  101. UiElementBus::Event(m_attemptConnectionBlockerUi, &UiElementInterface::SetIsEnabled, true);
  102. AZStd::string ipAddress = "";
  103. UiTextInputBus::EventResult(ipAddress, m_ipAddressTextInputUi, &UiTextInputInterface::GetText);
  104. const AZStd::string connectCommand = AZStd::string::format("connect %s", ipAddress.c_str());
  105. console->PerformCommand(connectCommand.c_str());
  106. }
  107. else if (buttonEntityId == m_connectToHostFailedUi)
  108. {
  109. // Player acknowledged connection failed. Close the warning popup.
  110. UiElementBus::Event(m_connectToHostFailedUi, &UiElementInterface::SetIsEnabled, false);
  111. }
  112. }
  113. void UiStartMenuComponent::OnConnectToHostFailed()
  114. {
  115. UiElementBus::Event(m_attemptConnectionBlockerUi, &UiElementInterface::SetIsEnabled, false);
  116. UiElementBus::Event(m_connectToHostFailedUi, &UiElementInterface::SetIsEnabled, true);
  117. }
  118. } // namespace MultiplayerSample