From 6fa78218d251e83a6f69ea5106eaeec7bb68559e Mon Sep 17 00:00:00 2001 From: mbalfour Date: Fri, 21 May 2021 17:32:55 -0500 Subject: [PATCH] [LYN-3548] Change IMGUI to use list of levels with prefab LoadLevel instead of text box There was an intermittent crash related to garbage in the level name box with prefabs, possibly due to levelName being uninitialized. This change improves the UX by listing out all the possible choices, instead of making people type it in. --- .../Source/LYCommonMenu/ImGuiLYCommonMenu.cpp | 42 ++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/Gems/ImGui/Code/Source/LYCommonMenu/ImGuiLYCommonMenu.cpp b/Gems/ImGui/Code/Source/LYCommonMenu/ImGuiLYCommonMenu.cpp index 8e3e2663d5..5107a02835 100644 --- a/Gems/ImGui/Code/Source/LYCommonMenu/ImGuiLYCommonMenu.cpp +++ b/Gems/ImGui/Code/Source/LYCommonMenu/ImGuiLYCommonMenu.cpp @@ -15,6 +15,7 @@ #ifdef IMGUI_ENABLED #include +#include #include #include #include @@ -251,15 +252,37 @@ namespace ImGui if (usePrefabSystemForLevels) { - char levelName[256]; + // Run through all the assets in the asset catalog and gather up the list of level assets + + AZ::Data::AssetType levelAssetType = lvlSystem->GetLevelAssetType(); + AZStd::vector levelNames; + auto enumerateCB = + [levelAssetType, &levelNames]([[maybe_unused]] const AZ::Data::AssetId id, const AZ::Data::AssetInfo& assetInfo) + { + if (assetInfo.m_assetType == levelAssetType) + { + levelNames.emplace_back(assetInfo.m_relativePath); + } + }; + + AZ::Data::AssetCatalogRequestBus::Broadcast( + &AZ::Data::AssetCatalogRequestBus::Events::EnumerateAssets, nullptr, enumerateCB, nullptr); + + AZStd::sort(levelNames.begin(), levelNames.end()); + + // Create a menu item for each level asset, with an action to load it if selected. + ImGui::TextColored(ImGui::Colors::s_PlainLabelColor, "Load Level: "); - bool result = ImGui::InputText("", levelName, sizeof(levelName), ImGuiInputTextFlags_EnterReturnsTrue); - if (result) + for (int i = 0; i < levelNames.size(); i++) { - AZ_TracePrintf("Imgui", "Attempting to load level '%s'\n", levelName); - AZ::TickBus::QueueFunction([lvlSystem, levelName]() { - lvlSystem->LoadLevel(levelName); - }); + if (ImGui::MenuItem(AZStd::string::format("%d- %s", i, levelNames[i].c_str()).c_str())) + { + AZ::TickBus::QueueFunction( + [lvlSystem, levelNames, i]() + { + lvlSystem->LoadLevel(levelNames[i].c_str()); + }); + } } } else @@ -269,9 +292,8 @@ namespace ImGui { if (ImGui::MenuItem(AZStd::string::format("%d- %s", i, lvlSystem->GetLevelInfo(i)->GetName()).c_str())) { - AZStd::string mapCommandString = AZStd::string::format("map %s", lvlSystem->GetLevelInfo(i)->GetName()); - AZ::TickBus::QueueFunction([mapCommandString]() { - gEnv->pConsole->ExecuteString(mapCommandString.c_str()); + AZ::TickBus::QueueFunction([lvlSystem, i]() { + lvlSystem->LoadLevel(lvlSystem->GetLevelInfo(i)->GetName()); }); } }