metaforce/Runtime/ImGuiConsole.cpp

280 lines
9.2 KiB
C++
Raw Normal View History

2021-05-25 16:24:05 +00:00
#include "ImGuiConsole.hpp"
#include "CStateManager.hpp"
#include "GameGlobalObjects.hpp"
#include "MP1/MP1.hpp"
#include "imgui.h"
#include "TCastTo.hpp" // Generated file, do not modify include path
namespace metaforce {
2021-05-25 17:56:01 +00:00
static std::set<TUniqueId> inspectingEntities;
2021-05-25 16:24:05 +00:00
2021-05-25 19:58:18 +00:00
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)); }
2021-05-26 02:49:24 +00:00
static const std::vector<std::pair<std::string, CAssetId>> ListWorlds() {
2021-05-25 19:58:18 +00:00
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;
}
2021-05-26 02:49:24 +00:00
static const std::vector<std::pair<std::string, TAreaId>> ListAreas(CAssetId worldId) {
2021-05-25 19:58:18 +00:00
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;
}
2021-05-26 02:49:24 +00:00
static void Warp(const CAssetId worldId, TAreaId aId) {
2021-05-25 19:58:18 +00:00
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);
2021-05-26 02:49:24 +00:00
if (g_StateManager != nullptr) {
g_StateManager->SetWarping(true);
g_StateManager->SetShouldQuitGame(true);
} else {
// TODO warp from menu?
}
2021-05-25 19:58:18 +00:00
}
2021-05-26 14:00:57 +00:00
static bool stepFrame = false;
2021-05-25 17:56:01 +00:00
static void ShowMenuGame() {
2021-05-25 16:24:05 +00:00
static bool paused;
paused = g_Main->IsPaused();
if (ImGui::MenuItem("Paused", nullptr, &paused)) {
g_Main->SetPaused(paused);
}
2021-05-26 14:00:57 +00:00
if (ImGui::MenuItem("Step", nullptr, &stepFrame, paused)) {
g_Main->SetPaused(false);
}
2021-05-26 02:49:24 +00:00
if (ImGui::BeginMenu("Warp", g_StateManager != nullptr && g_ResFactory != nullptr &&
g_ResFactory->GetResLoader() != nullptr)) {
for (const auto& world : ListWorlds()) {
2021-05-25 19:58:18 +00:00
if (ImGui::BeginMenu(world.first.c_str())) {
2021-05-26 02:49:24 +00:00
for (const auto& area : ListAreas(world.second)) {
2021-05-25 19:58:18 +00:00
if (ImGui::MenuItem(area.first.c_str())) {
2021-05-26 02:49:24 +00:00
Warp(world.second, area.second);
2021-05-25 19:58:18 +00:00
}
}
ImGui::EndMenu();
}
}
ImGui::EndMenu();
}
2021-05-25 16:24:05 +00:00
if (ImGui::MenuItem("Quit", "Alt+F4")) {
g_Main->Quit();
}
}
2021-05-26 15:11:21 +00:00
static void ImGuiStringViewText(std::string_view text) {
ImGui::TextUnformatted(text.begin(), text.end());
}
static void LerpActorColor(CActor* act) {
act->m_debugAddColorTime += 1.f / 60.f;
float lerp = act->m_debugAddColorTime;
if (lerp > 2.f) {
lerp = 0.f;
act->m_debugAddColorTime = 0.f;
} else if (lerp > 1.f) {
lerp = 2.f - lerp;
}
act->m_debugAddColor = zeus::CColor::lerp(zeus::skClear, zeus::skBlue, lerp);
}
2021-05-25 16:24:05 +00:00
static void ShowInspectWindow(bool* isOpen) {
if (ImGui::Begin("Inspect", isOpen)) {
2021-05-26 02:49:24 +00:00
if (ImGui::BeginTable("Entities", 4,
2021-05-25 16:24:05 +00:00
ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_RowBg |
ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ScrollY)) {
ImGui::TableSetupColumn("ID",
ImGuiTableColumnFlags_PreferSortAscending | ImGuiTableColumnFlags_DefaultSort |
ImGuiTableColumnFlags_WidthFixed,
0, 'id');
2021-05-26 02:49:24 +00:00
ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed, 0, 'type');
2021-05-25 16:24:05 +00:00
ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthStretch, 0, 'name');
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableHeadersRow();
CObjectList& list = *g_StateManager->GetObjectList();
std::vector<CEntity*> items;
items.reserve(list.size());
for (auto* ent : list) {
items.push_back(ent);
}
if (ImGuiTableSortSpecs* sortSpecs = ImGui::TableGetSortSpecs()) {
for (int i = 0; i < sortSpecs->SpecsCount; ++i) {
const auto& specs = sortSpecs->Specs[i];
if (specs.ColumnUserID == 'id') {
std::sort(items.begin(), items.end(), [&](CEntity* a, CEntity* b) {
u16 aId = a->GetUniqueId().Value();
u16 bId = b->GetUniqueId().Value();
return specs.SortDirection == ImGuiSortDirection_Ascending ? aId < bId : aId > bId;
});
} else if (specs.ColumnUserID == 'name') {
std::sort(items.begin(), items.end(), [&](CEntity* a, CEntity* b) {
int compare = a->GetName().compare(b->GetName());
return specs.SortDirection == ImGuiSortDirection_Ascending ? compare < 0 : compare > 0;
});
2021-05-26 02:49:24 +00:00
} else if (specs.ColumnUserID == 'type') {
std::sort(items.begin(), items.end(), [&](CEntity* a, CEntity* b) {
int compare = a->ImGuiType().compare(b->ImGuiType());
return specs.SortDirection == ImGuiSortDirection_Ascending ? compare < 0 : compare > 0;
});
2021-05-25 16:24:05 +00:00
}
}
}
for (const auto& item : items) {
TUniqueId uid = item->GetUniqueId();
ImGui::PushID(uid.Value());
ImGui::TableNextRow();
if (ImGui::TableNextColumn()) {
2021-05-26 15:11:21 +00:00
auto text = fmt::format(FMT_STRING("{:x}"), uid.Value());
if (TCastToPtr<CActor> act = item) {
ImGui::Selectable(text.c_str(), &act->m_debugSelected, ImGuiSelectableFlags_SpanAllColumns);
if (act->m_debugSelected) {
LerpActorColor(act);
}
} else {
ImGui::TextUnformatted(text.c_str());
}
2021-05-25 16:24:05 +00:00
}
2021-05-26 02:49:24 +00:00
if (ImGui::TableNextColumn()) {
2021-05-26 15:11:21 +00:00
ImGuiStringViewText(item->ImGuiType());
2021-05-26 02:49:24 +00:00
}
2021-05-25 16:24:05 +00:00
if (ImGui::TableNextColumn()) {
2021-05-26 15:11:21 +00:00
ImGuiStringViewText(item->GetName());
2021-05-25 16:24:05 +00:00
}
if (ImGui::TableNextColumn()) {
if (ImGui::SmallButton("View")) {
2021-05-25 17:56:01 +00:00
inspectingEntities.insert(uid);
2021-05-25 16:24:05 +00:00
}
}
ImGui::PopID();
}
ImGui::EndTable();
}
}
2021-05-25 17:56:01 +00:00
ImGui::End();
2021-05-25 16:24:05 +00:00
}
2021-05-25 17:56:01 +00:00
static bool showEntityInfoWindow(TUniqueId uid) {
bool open = true;
CEntity* ent = g_StateManager->ObjectById(uid);
2021-05-25 16:24:05 +00:00
if (ent == nullptr) {
2021-05-25 17:56:01 +00:00
return false;
2021-05-25 16:24:05 +00:00
}
2021-05-25 17:56:01 +00:00
auto name = fmt::format(FMT_STRING("{}##{:x}"), !ent->GetName().empty() ? ent->GetName() : "Entity", uid.Value());
if (ImGui::Begin(name.c_str(), &open, ImGuiWindowFlags_AlwaysAutoResize)) {
2021-05-26 02:49:24 +00:00
ent->ImGuiInspect();
2021-05-25 16:24:05 +00:00
}
2021-05-25 17:56:01 +00:00
ImGui::End();
return open;
2021-05-25 16:24:05 +00:00
}
static void ShowAppMainMenuBar() {
static bool showInspectWindow = false;
static bool showDemoWindow = false;
2021-05-26 02:49:24 +00:00
bool canInspect = g_StateManager != nullptr && g_StateManager->GetObjectList();
2021-05-25 16:24:05 +00:00
if (ImGui::BeginMainMenuBar()) {
2021-05-25 17:56:01 +00:00
if (ImGui::BeginMenu("Game")) {
ShowMenuGame();
2021-05-25 16:24:05 +00:00
ImGui::EndMenu();
}
2021-05-25 17:56:01 +00:00
ImGui::Spacing();
2021-05-25 16:24:05 +00:00
if (ImGui::BeginMenu("Tools")) {
2021-05-26 02:49:24 +00:00
ImGui::MenuItem("Inspect", nullptr, &showInspectWindow, canInspect);
2021-05-25 16:24:05 +00:00
ImGui::Separator();
ImGui::MenuItem("Demo", nullptr, &showDemoWindow);
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
2021-05-26 02:49:24 +00:00
if (canInspect) {
if (showInspectWindow) {
ShowInspectWindow(&showInspectWindow);
}
2021-05-25 16:24:05 +00:00
auto iter = inspectingEntities.begin();
while (iter != inspectingEntities.end()) {
2021-05-25 17:56:01 +00:00
if (!showEntityInfoWindow(*iter)) {
2021-05-25 16:24:05 +00:00
iter = inspectingEntities.erase(iter);
} else {
iter++;
}
}
2021-05-26 02:49:24 +00:00
} else {
inspectingEntities.clear();
2021-05-25 16:24:05 +00:00
}
if (showDemoWindow) {
ImGui::ShowDemoWindow(&showDemoWindow);
}
}
2021-05-26 14:00:57 +00:00
void ImGuiConsole::proc() {
if (stepFrame) {
g_Main->SetPaused(true);
stepFrame = false;
}
ShowAppMainMenuBar();
}
2021-05-25 19:58:18 +00:00
ImGuiConsole::~ImGuiConsole() {
dummyWorlds.clear();
stringTables.clear();
}
2021-05-26 02:49:24 +00:00
} // namespace metaforce