GameStatePrimaryUserSignedOut.inl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <GameState/GameStateRequestBus.h>
  9. #include <GameStateSamples/GameStatePrimaryUserSignedOut.h>
  10. #include <GameStateSamples/GameStatePrimaryUserSelection.h>
  11. #include <MessagePopup/MessagePopupBus.h>
  12. #include <LocalUser/LocalUserRequestBus.h>
  13. #include <AzFramework/Input/Utils/IsAnyKeyOrButton.h>
  14. #include <ILocalizationManager.h>
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////
  16. namespace GameStateSamples
  17. {
  18. ////////////////////////////////////////////////////////////////////////////////////////////////
  19. inline void GameStatePrimaryUserSignedOut::OnEnter()
  20. {
  21. ShowPrimaryUserSignedOutPopup();
  22. AzFramework::InputChannelEventListener::Connect();
  23. LocalUser::LocalUserNotificationBus::Handler::BusConnect();
  24. }
  25. ////////////////////////////////////////////////////////////////////////////////////////////////
  26. inline void GameStatePrimaryUserSignedOut::OnExit()
  27. {
  28. LocalUser::LocalUserNotificationBus::Handler::BusDisconnect();
  29. AzFramework::InputChannelEventListener::Disconnect();
  30. HidePrimaryUserSignedOutPopup();
  31. }
  32. ////////////////////////////////////////////////////////////////////////////////////////////////
  33. inline AZ::s32 GameStatePrimaryUserSignedOut::GetPriority() const
  34. {
  35. // Re-establishing a primary user takes precedence over everything else
  36. return AzFramework::InputChannelEventListener::GetPriorityFirst();
  37. }
  38. ////////////////////////////////////////////////////////////////////////////////////////////////
  39. inline bool GameStatePrimaryUserSignedOut::OnInputChannelEventFiltered(const AzFramework::InputChannel & inputChannel)
  40. {
  41. if (inputChannel.IsStateEnded() &&
  42. AzFramework::IsAnyKeyOrButton(inputChannel))
  43. {
  44. const AzFramework::LocalUserId assignedLocalUserId = inputChannel.GetInputDevice().GetAssignedLocalUserId();
  45. const AzFramework::LocalUserId primaryLocalUserId = LocalUser::LocalUserRequests::GetPrimaryLocalUserId();
  46. if (assignedLocalUserId == primaryLocalUserId)
  47. {
  48. // We received input from the primary user, so just pop this state
  49. AZ_Assert(GameState::GameStateRequests::IsActiveGameStateOfType<GameStatePrimaryUserSignedOut>(),
  50. "The active game state is not an instance of GameStatePrimaryUserSignedOut");
  51. GameState::GameStateRequestBus::Broadcast(&GameState::GameStateRequests::PopActiveGameState);
  52. }
  53. else if (assignedLocalUserId == AzFramework::LocalUserIdAny ||
  54. assignedLocalUserId == AzFramework::LocalUserIdNone)
  55. {
  56. // We received input from a device that is not associated with a user, so prompt for user sign-in
  57. inputChannel.GetInputDevice().PromptLocalUserSignIn();
  58. }
  59. else
  60. {
  61. // We received input from a different user confirming we want to select a new one
  62. AZ_Assert(GameState::GameStateRequests::IsActiveGameStateOfType<GameStatePrimaryUserSignedOut>(),
  63. "The active game state is not an instance of GameStatePrimaryUserSignedOut");
  64. GameState::GameStateRequests::PopActiveGameStateUntilOfType<GameStatePrimaryUserSelection>();
  65. }
  66. }
  67. return true; // Consume all input while this game state is active
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////////////////////
  70. inline void GameStatePrimaryUserSignedOut::OnLocalUserSignedIn(AzFramework::LocalUserId localUserId)
  71. {
  72. const AzFramework::LocalUserId primaryLocalUserId = LocalUser::LocalUserRequests::GetPrimaryLocalUserId();
  73. if (primaryLocalUserId == localUserId)
  74. {
  75. // The primary user signed back in
  76. AZ_Assert(GameState::GameStateRequests::IsActiveGameStateOfType<GameStatePrimaryUserSignedOut>(),
  77. "The active game state is not an instance of GameStatePrimaryUserSignedOut");
  78. GameState::GameStateRequestBus::Broadcast(&GameState::GameStateRequests::PopActiveGameState);
  79. }
  80. }
  81. ////////////////////////////////////////////////////////////////////////////////////////////////
  82. inline void GameStatePrimaryUserSignedOut::ShowPrimaryUserSignedOutPopup()
  83. {
  84. if (m_primaryUserSignedOutPopupId != 0)
  85. {
  86. // We're already displaying the message popup
  87. return;
  88. }
  89. AZStd::string localizedMessage;
  90. const char* localizationKey = "@PRIMARY_CONTROLLER_DISCONNECTED_LOC_KEY";
  91. bool wasLocalized = false;
  92. LocalizationManagerRequestBus::BroadcastResult(wasLocalized,
  93. &LocalizationManagerRequestBus::Events::LocalizeString_ch,
  94. localizationKey,
  95. localizedMessage,
  96. false);
  97. const char* popupMessage = wasLocalized && localizedMessage != localizationKey ?
  98. localizedMessage.c_str() :
  99. "Primary profile signed out.\n\nEither sign in again with the same profile, or press any button while signed into a different profile to return to the main menu.\n\n(any unsaved progress will be lost)";
  100. MessagePopup::MessagePopupRequestBus::BroadcastResult(m_primaryUserSignedOutPopupId,
  101. &MessagePopup::MessagePopupRequests::ShowPopup,
  102. popupMessage,
  103. MessagePopup::EPopupButtons_NoButtons);
  104. }
  105. ////////////////////////////////////////////////////////////////////////////////////////////////
  106. inline void GameStatePrimaryUserSignedOut::HidePrimaryUserSignedOutPopup()
  107. {
  108. if (m_primaryUserSignedOutPopupId != 0)
  109. {
  110. MessagePopup::MessagePopupRequestBus::Broadcast(&MessagePopup::MessagePopupRequests::HidePopup,
  111. m_primaryUserSignedOutPopupId, 0);
  112. m_primaryUserSignedOutPopupId = 0;
  113. }
  114. }
  115. }