GameStateMainMenu.inl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 <AzCore/Component/TickBus.h>
  9. #include <AzCore/IO/Path/Path.h>
  10. #include <AzCore/Asset/AssetManagerBus.h>
  11. #include <AzFramework/Spawnable/Spawnable.h>
  12. #include <GameState/GameStateRequestBus.h>
  13. #include <GameStateSamples/GameStateMainMenu.h>
  14. #include <GameStateSamples/GameStateOptionsMenu.h>
  15. #include <GameStateSamples/GameStateLevelLoading.h>
  16. #include <GameStateSamples/GameStatePrimaryUserSelection.h>
  17. #include <GameStateSamples/GameStateSamples_Traits_Platform.h>
  18. #include <LocalUser/LocalUserRequestBus.h>
  19. #include <LyShine/Bus/UiButtonBus.h>
  20. #include <LyShine/Bus/UiCanvasBus.h>
  21. #include <LyShine/Bus/UiCanvasManagerBus.h>
  22. #include <LyShine/Bus/UiCursorBus.h>
  23. #include <LyShine/Bus/UiDynamicLayoutBus.h>
  24. #include <LyShine/Bus/UiElementBus.h>
  25. #include <SaveData/SaveDataRequestBus.h>
  26. #include <ILevelSystem.h>
  27. ////////////////////////////////////////////////////////////////////////////////////////////////////
  28. namespace GameStateSamples
  29. {
  30. ////////////////////////////////////////////////////////////////////////////////////////////////
  31. inline void GameStateMainMenu::OnPushed()
  32. {
  33. // We could load the UI canvas here and keep it cached until OnPopped is called in order to
  34. // speed up re-entering this game state, but doing so would consume memory for the lifetime
  35. // of the process that is only needed while this state is active (which is not very often).
  36. bool createLocalUserLobbySubState = false;
  37. #if AZ_TRAIT_GAMESTATESAMPLES_LOCAL_USER_LOBBY_ENABLED
  38. createLocalUserLobbySubState = true;
  39. #else
  40. createLocalUserLobbySubState = false;
  41. #endif // AZ_TRAIT_GAMESTATESAMPLES_LOCAL_USER_LOBBY_ENABLED
  42. ISystem* iSystem = GetISystem();
  43. IConsole* iConsole = iSystem ? iSystem->GetIConsole() : nullptr;
  44. if (iConsole && iConsole->GetCVar("sys_localUserLobbyEnabled"))
  45. {
  46. switch (iConsole->GetCVar("sys_localUserLobbyEnabled")->GetIVal())
  47. {
  48. case 0: { createLocalUserLobbySubState = false; } break;
  49. case 1: { createLocalUserLobbySubState = true; } break;
  50. default: break; // Use the default value that was set above
  51. }
  52. }
  53. if (createLocalUserLobbySubState)
  54. {
  55. m_localUserLobbySubState.reset(aznew GameStateLocalUserLobby());
  56. m_localUserLobbySubState->OnPushed();
  57. }
  58. LoadGameOptionsFromPersistentStorage();
  59. }
  60. ////////////////////////////////////////////////////////////////////////////////////////////////
  61. inline void GameStateMainMenu::OnPopped()
  62. {
  63. // See comment above in OnPushed
  64. if (m_localUserLobbySubState)
  65. {
  66. m_localUserLobbySubState->OnPopped();
  67. m_localUserLobbySubState.reset();
  68. }
  69. }
  70. ////////////////////////////////////////////////////////////////////////////////////////////////
  71. inline void GameStateMainMenu::OnEnter()
  72. {
  73. LoadMainMenuCanvas();
  74. if (m_localUserLobbySubState)
  75. {
  76. m_localUserLobbySubState->OnEnter();
  77. }
  78. if (ISystem* iSystem = GetISystem())
  79. {
  80. iSystem->GetISystemEventDispatcher()->RegisterListener(this);
  81. }
  82. }
  83. ////////////////////////////////////////////////////////////////////////////////////////////////
  84. inline void GameStateMainMenu::OnExit()
  85. {
  86. if (ISystem* iSystem = GetISystem())
  87. {
  88. iSystem->GetISystemEventDispatcher()->RemoveListener(this);
  89. }
  90. if (m_localUserLobbySubState)
  91. {
  92. m_localUserLobbySubState->OnExit();
  93. }
  94. UnloadMainMenuCanvas();
  95. }
  96. ////////////////////////////////////////////////////////////////////////////////////////////////
  97. inline void GameStateMainMenu::OnUpdate()
  98. {
  99. // This should be called directly from LoadMainMenuCanvas (or right after it from OnEnter),
  100. // but at that point in our convoluted startup sequence the level system doesn't exist yet.
  101. if (m_shouldRefreshLevelListDisplay)
  102. {
  103. m_shouldRefreshLevelListDisplay = false;
  104. RefreshLevelListDisplay();
  105. }
  106. if (m_localUserLobbySubState)
  107. {
  108. m_localUserLobbySubState->OnUpdate();
  109. }
  110. }
  111. ////////////////////////////////////////////////////////////////////////////////////////////////
  112. inline void OnLevelButtonClicked(AZ::EntityId entityId, AZ::Vector2)
  113. {
  114. // Load the selected level
  115. AZStd::string levelName;
  116. UiButtonBus::EventResult(levelName, entityId, &UiButtonInterface::GetOnClickActionName);
  117. ISystem* iSystem = GetISystem();
  118. ILevelSystem* levelSystem = iSystem ? iSystem->GetILevelSystem() : nullptr;
  119. if (levelSystem && !levelName.empty())
  120. {
  121. // This command gets delayed by one frame, so we check for the
  122. // actual level load start in GameStateMainMenu::OnSystemEvent
  123. AZ::TickBus::QueueFunction([levelSystem, levelName]() {
  124. levelSystem->UnloadLevel();
  125. levelSystem->LoadLevel(levelName.c_str());
  126. });
  127. }
  128. }
  129. ////////////////////////////////////////////////////////////////////////////////////////////////
  130. inline void OnOptionsButtonClicked([[maybe_unused]] AZ::EntityId entityId, AZ::Vector2)
  131. {
  132. AZ_Assert(GameState::GameStateRequests::IsActiveGameStateOfType<GameStateMainMenu>(),
  133. "The active game state is not an instance of GameStateMainMenu");
  134. GameState::GameStateRequests::CreateAndPushNewOverridableGameStateOfType<GameStateOptionsMenu>();
  135. }
  136. ////////////////////////////////////////////////////////////////////////////////////////////////
  137. inline void OnBackButtonClicked([[maybe_unused]] AZ::EntityId entityId, AZ::Vector2)
  138. {
  139. AZ_Assert(GameState::GameStateRequests::DoesStackContainGameStateOfType<GameStatePrimaryUserSelection>(),
  140. "The game state stack doesn't contain an instance of GameStatePrimaryUserSelection");
  141. GameState::GameStateRequests::PopActiveGameStateUntilOfType<GameStatePrimaryUserSelection>();
  142. }
  143. ////////////////////////////////////////////////////////////////////////////////////////////////
  144. inline void GameStateMainMenu::OnSystemEvent(ESystemEvent event, UINT_PTR, UINT_PTR)
  145. {
  146. // If the user happens to initiate a level load outside the context of these game states,
  147. // for example via executing the 'map' command from the debug console or in autoexec.cfg,
  148. // this will also be detected by checking for the ESYSTEM_EVENT_LEVEL_LOAD_PREPARE event.
  149. if (event == ESYSTEM_EVENT_LEVEL_LOAD_PREPARE)
  150. {
  151. // Push the level loading game state
  152. AZ_Assert(!GameState::GameStateRequests::DoesStackContainGameStateOfType<GameStateLevelLoading>(),
  153. "The game state stack already contains an instance of GameStateLevelLoading");
  154. GameState::GameStateRequests::CreateAndPushNewOverridableGameStateOfType<GameStateLevelLoading>();
  155. }
  156. }
  157. ////////////////////////////////////////////////////////////////////////////////////////////////
  158. inline void GameStateMainMenu::LoadMainMenuCanvas()
  159. {
  160. // Load the UI canvas
  161. const char* uiCanvasAssetPath = GetMainMenuCanvasAssetPath();
  162. UiCanvasManagerBus::BroadcastResult(m_mainMenuCanvasEntityId,
  163. &UiCanvasManagerInterface::LoadCanvas,
  164. uiCanvasAssetPath);
  165. if (!m_mainMenuCanvasEntityId.IsValid())
  166. {
  167. AZ_Warning("GameStateMainMenu", false, "Could not load %s", uiCanvasAssetPath);
  168. return;
  169. }
  170. // Display the main menu and set it to stay loaded when a level unloads
  171. UiCanvasBus::Event(m_mainMenuCanvasEntityId, &UiCanvasInterface::SetEnabled, true);
  172. UiCanvasBus::Event(m_mainMenuCanvasEntityId, &UiCanvasInterface::SetKeepLoadedOnLevelUnload, true);
  173. // Display the UI cursor
  174. UiCursorBus::Broadcast(&UiCursorInterface::IncrementVisibleCounter);
  175. // Setup the 'Options' button to open the options menu
  176. AZ::EntityId optionsButtonElementId;
  177. UiCanvasBus::EventResult(optionsButtonElementId,
  178. m_mainMenuCanvasEntityId,
  179. &UiCanvasInterface::FindElementEntityIdByName,
  180. "OptionsButton");
  181. UiButtonBus::Event(optionsButtonElementId, &UiButtonInterface::SetOnClickCallback, OnOptionsButtonClicked);
  182. // Setup the 'Back' button to return to the primary user selection screen
  183. AZ::EntityId backButtonElementId;
  184. UiCanvasBus::EventResult(backButtonElementId,
  185. m_mainMenuCanvasEntityId,
  186. &UiCanvasInterface::FindElementEntityIdByName,
  187. "BackButton");
  188. const bool enableBackButton = GameState::GameStateRequests::DoesStackContainGameStateOfType<GameStatePrimaryUserSelection>();
  189. UiElementBus::Event(backButtonElementId, &UiElementInterface::SetIsEnabled, enableBackButton);
  190. if (enableBackButton)
  191. {
  192. UiButtonBus::Event(backButtonElementId, &UiButtonInterface::SetOnClickCallback, OnBackButtonClicked);
  193. }
  194. else
  195. {
  196. // Use the wide version of the 'Options' button.
  197. UiElementBus::Event(optionsButtonElementId, &UiElementInterface::SetIsEnabled, false);
  198. UiCanvasBus::EventResult(optionsButtonElementId,
  199. m_mainMenuCanvasEntityId,
  200. &UiCanvasInterface::FindElementEntityIdByName,
  201. "OptionsButtonWide");
  202. UiElementBus::Event(optionsButtonElementId, &UiElementInterface::SetIsEnabled, true);
  203. UiButtonBus::Event(optionsButtonElementId, &UiButtonInterface::SetOnClickCallback, OnOptionsButtonClicked);
  204. }
  205. // RefreshLevelListDisplay() should be called directly here (or right after it from OnEnter),
  206. // but at this point in our messed up startup sequence the level system doesn't exist yet.
  207. m_shouldRefreshLevelListDisplay = true;
  208. }
  209. ////////////////////////////////////////////////////////////////////////////////////////////////
  210. inline void GameStateMainMenu::UnloadMainMenuCanvas()
  211. {
  212. m_shouldRefreshLevelListDisplay = false;
  213. if (m_mainMenuCanvasEntityId.IsValid())
  214. {
  215. // Hide the UI cursor
  216. UiCursorBus::Broadcast(&UiCursorInterface::DecrementVisibleCounter);
  217. // Unload the main menu
  218. UiCanvasManagerBus::Broadcast(&UiCanvasManagerInterface::UnloadCanvas,
  219. m_mainMenuCanvasEntityId);
  220. m_mainMenuCanvasEntityId.SetInvalid();
  221. }
  222. }
  223. ////////////////////////////////////////////////////////////////////////////////////////////////
  224. inline const char* GameStateMainMenu::GetMainMenuCanvasAssetPath()
  225. {
  226. return "@products@/ui/canvases/defaultmainmenuscreen.uicanvas";
  227. }
  228. ////////////////////////////////////////////////////////////////////////////////////////////////
  229. inline void GameStateMainMenu::RefreshLevelListDisplay()
  230. {
  231. // Get the dynamic layout UI element
  232. AZ::EntityId dynamicLayoutElementId;
  233. UiCanvasBus::EventResult(dynamicLayoutElementId,
  234. m_mainMenuCanvasEntityId,
  235. &UiCanvasInterface::FindElementEntityIdByName,
  236. "DynamicColumn");
  237. if (dynamicLayoutElementId.IsValid())
  238. {
  239. ISystem* iSystem = GetISystem();
  240. ILevelSystem* levelSystem = iSystem ? iSystem->GetILevelSystem() : nullptr;
  241. // Run through all the assets in the asset catalog and gather up the list of level assets
  242. AZ::Data::AssetType levelAssetType = levelSystem->GetLevelAssetType();
  243. AZStd::vector<AZStd::string> levelNames;
  244. auto enumerateCB = [levelAssetType, &levelNames](
  245. [[maybe_unused]] const AZ::Data::AssetId id,
  246. const AZ::Data::AssetInfo& assetInfo)
  247. {
  248. if (assetInfo.m_assetType == levelAssetType)
  249. {
  250. levelNames.emplace_back(assetInfo.m_relativePath);
  251. }
  252. };
  253. AZ::Data::AssetCatalogRequestBus::Broadcast(
  254. &AZ::Data::AssetCatalogRequestBus::Events::EnumerateAssets, nullptr, enumerateCB, nullptr);
  255. // Add all the levels into the UI as buttons
  256. UiDynamicLayoutBus::Event(dynamicLayoutElementId, &UiDynamicLayoutInterface::SetNumChildElements, static_cast<int>(levelNames.size()));
  257. for (int i = 0; i < levelNames.size(); ++i)
  258. {
  259. AZ::IO::PathView level(levelNames[i].c_str());
  260. // Get the button element id
  261. AZ::EntityId buttonElementId;
  262. UiElementBus::EventResult(buttonElementId, dynamicLayoutElementId, &UiElementInterface::GetChildEntityId, i);
  263. // Get the text element id
  264. AZ::EntityId textElementId;
  265. UiElementBus::EventResult(textElementId, buttonElementId, &UiElementInterface::FindChildEntityIdByName, "Text");
  266. // Set the name, on-click callback, and on-click action name for each button
  267. UiTextBus::Event(textElementId, &UiTextInterface::SetText, level.Filename().Native());
  268. UiButtonBus::Event(buttonElementId, &UiButtonInterface::SetOnClickCallback, OnLevelButtonClicked);
  269. UiButtonBus::Event(buttonElementId, &UiButtonInterface::SetOnClickActionName, levelNames[i]);
  270. if (i == 0)
  271. {
  272. // Force the first level to be selected
  273. UiCanvasBus::Event(m_mainMenuCanvasEntityId, &UiCanvasInterface::ForceHoverInteractable, buttonElementId);
  274. }
  275. }
  276. }
  277. }
  278. ////////////////////////////////////////////////////////////////////////////////////////////////
  279. inline void GameStateMainMenu::LoadGameOptionsFromPersistentStorage()
  280. {
  281. SaveData::SaveDataRequests::SaveOrLoadObjectParams<GameOptions> loadObjectParams;
  282. GameOptionRequestBus::BroadcastResult(loadObjectParams.serializableObject,
  283. &GameOptionRequests::GetGameOptions);
  284. loadObjectParams.dataBufferName = GameOptions::SaveDataBufferName;
  285. loadObjectParams.localUserId = LocalUser::LocalUserRequests::GetPrimaryLocalUserId();
  286. loadObjectParams.callback = [](const SaveData::SaveDataRequests::SaveOrLoadObjectParams<GameOptions>& params,
  287. [[maybe_unused]] SaveData::SaveDataNotifications::Result result)
  288. {
  289. params.serializableObject->OnLoadedFromPersistentData();
  290. };
  291. SaveData::SaveDataRequests::LoadObject(loadObjectParams);
  292. }
  293. }