Add Warp menu

This commit is contained in:
Luke Street 2021-05-25 15:58:18 -04:00
parent 72954c5019
commit 6da000e332
2 changed files with 94 additions and 1 deletions

View File

@ -12,12 +12,99 @@ namespace metaforce {
static std::set<TUniqueId> inspectingEntities; static std::set<TUniqueId> inspectingEntities;
static std::unordered_map<CAssetId, std::unique_ptr<CDummyWorld>> dummyWorlds;
static std::unordered_map<CAssetId, TCachedToken<CStringTable>> stringTables;
// utility wrapper to adapt locale-bound facets for wstring/wbuffer convert
template <class Facet>
struct deletable_facet : Facet {
template <class... Args>
deletable_facet(Args&&... args) : Facet(std::forward<Args>(args)...) {}
~deletable_facet() {}
};
static std::wstring_convert<deletable_facet<std::codecvt<char16_t, char, std::mbstate_t>>, char16_t> conv16;
std::string readUtf8String(CStringTable* tbl, int idx) { return conv16.to_bytes(tbl->GetString(idx)); }
static const std::vector<std::pair<std::string, CAssetId>> listWorlds() {
std::vector<std::pair<std::string, CAssetId>> worlds;
for (const auto& pak : g_ResFactory->GetResLoader()->GetPaks()) {
if (!pak->IsWorldPak()) {
continue;
}
CAssetId worldId = pak->GetMLVLId();
if (!dummyWorlds.contains(worldId)) {
dummyWorlds[worldId] = std::make_unique<CDummyWorld>(worldId, false);
}
auto& world = dummyWorlds[worldId];
bool complete = world->ICheckWorldComplete();
if (!complete) {
continue;
}
CAssetId stringId = world->IGetStringTableAssetId();
if (!stringId.IsValid()) {
continue;
}
if (!stringTables.contains(stringId)) {
stringTables[stringId] = g_SimplePool->GetObj(SObjectTag{SBIG('STRG'), stringId});
}
worlds.emplace_back(readUtf8String(stringTables[stringId].GetObj(), 0), worldId);
}
return worlds;
}
static const std::vector<std::pair<std::string, TAreaId>> listAreas(CAssetId worldId) {
std::vector<std::pair<std::string, TAreaId>> areas;
const auto& world = dummyWorlds[worldId];
for (int i = 0; i < world->IGetAreaCount(); ++i) {
const auto* area = world->IGetAreaAlways(i);
if (area == nullptr) {
continue;
}
CAssetId stringId = area->IGetStringTableAssetId();
if (!stringId.IsValid()) {
continue;
}
if (!stringTables.contains(stringId)) {
stringTables[stringId] = g_SimplePool->GetObj(SObjectTag{SBIG('STRG'), stringId});
}
areas.emplace_back(readUtf8String(stringTables[stringId].GetObj(), 0), TAreaId{i});
}
return areas;
}
static void warp(const CAssetId worldId, TAreaId aId) {
g_GameState->SetCurrentWorldId(worldId);
g_GameState->GetWorldTransitionManager()->DisableTransition();
if (aId >= g_GameState->CurrentWorldState().GetLayerState()->GetAreaCount()) {
aId = 0;
}
g_GameState->CurrentWorldState().SetAreaId(aId);
g_Main->SetFlowState(EFlowState::None);
g_StateManager->SetWarping(true);
g_StateManager->SetShouldQuitGame(true);
}
static void ShowMenuGame() { static void ShowMenuGame() {
static bool paused; static bool paused;
paused = g_Main->IsPaused(); paused = g_Main->IsPaused();
if (ImGui::MenuItem("Paused", nullptr, &paused)) { if (ImGui::MenuItem("Paused", nullptr, &paused)) {
g_Main->SetPaused(paused); g_Main->SetPaused(paused);
} }
if (ImGui::BeginMenu("Warp", g_ResFactory != nullptr && g_ResFactory->GetResLoader() != nullptr)) {
for (const auto& world : listWorlds()) {
if (ImGui::BeginMenu(world.first.c_str())) {
for (const auto& area : listAreas(world.second)) {
if (ImGui::MenuItem(area.first.c_str())) {
warp(world.second, area.second);
}
}
ImGui::EndMenu();
}
}
ImGui::EndMenu();
}
if (ImGui::MenuItem("Quit", "Alt+F4")) { if (ImGui::MenuItem("Quit", "Alt+F4")) {
g_Main->Quit(); g_Main->Quit();
} }
@ -138,4 +225,9 @@ static void ShowAppMainMenuBar() {
} }
void ImGuiConsole::proc() { ShowAppMainMenuBar(); } void ImGuiConsole::proc() { ShowAppMainMenuBar(); }
ImGuiConsole::~ImGuiConsole() {
dummyWorlds.clear();
stringTables.clear();
}
} // namespace metaforce } // namespace metaforce

View File

@ -3,6 +3,7 @@
namespace metaforce { namespace metaforce {
class ImGuiConsole { class ImGuiConsole {
public: public:
~ImGuiConsole();
void proc(); void proc();
}; };
} } // namespace metaforce