#include "./scene_tools.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define LOGIGS(x) LOG("ImGuiSceneToolsService", x) namespace MM::Services { void ImGuiSceneToolsService::renderImGui(Engine& engine) { if (show_menu && ImGui::BeginMainMenuBar()) { if (ImGui::BeginMenu("Engine")) { if (ImGui::MenuItem("Stop Engine")) { engine.stop(); } ImGui::EndMenu(); } if (ImGui::BeginMenu("TextEditor")) { if (ImGui::MenuItem("New Editor")) { _text_editor_list.emplace_back(engine); } ImGui::EndMenu(); } if (ImGui::BeginMenu("Windows")) { ImGui::MenuItem("MM Metrics", NULL, &_show_mm_metrics); ImGui::MenuItem("ImGui Metrics", NULL, &_show_imgui_metrics); ImGui::MenuItem("Texture Loader", NULL, &_show_texture_loader); ImGui::MenuItem("Texture List", NULL, &_show_texture_list); ImGui::MenuItem("Entity Editor", NULL, &_show_entity_editor); ImGui::MenuItem("Camera Tool", NULL, &_show_camera_tool); ImGui::EndMenu(); } ImGui::Text("%.1fFPS", ImGui::GetIO().Framerate); ImGui::EndMainMenuBar(); } auto& scene = engine.tryService()->getScene(); if (_show_mm_metrics) { if (ImGui::Begin("Metrics##MM", &_show_mm_metrics)) { if (ImGui::CollapsingHeader("ECS")) { ImGui::Text("EnTT v%d.%d.%d", ENTT_VERSION_MAJOR, ENTT_VERSION_MINOR, ENTT_VERSION_PATCH); ImGui::Text("capacity: %lu", scene.capacity()); ImGui::Text("size: %lu", scene.size()); ImGui::Text("alive: %lu", scene.alive()); size_t orphans = 0; scene.orphans([&](auto) { orphans++; }); ImGui::Text("orphans: %lu", orphans); } } ImGui::End(); } if (_show_imgui_metrics) { ImGui::ShowMetricsWindow(&_show_imgui_metrics); } if (_show_texture_loader) { if (ImGui::Begin("Texture Loader", &_show_texture_loader)) { ImGuiWidgets::TextureResourceManagerLoader(engine); } ImGui::End(); } if (_show_texture_list) { if (ImGui::Begin("Texture List", &_show_texture_list)) { ImGuiWidgets::TextureResourceManagerList(); } ImGui::End(); } if (_show_entity_editor) { ImGui::SetNextWindowSize(ImVec2(750, 500), ImGuiCond_FirstUseEver); if (ImGui::Begin("Entity Editor", &_show_entity_editor, ImGuiWindowFlags_MenuBar)) { ImGui::BeginMenuBar(); ImGui::Checkbox("Show List", &_show_entity_list); ImGui::EndMenuBar(); if (_show_entity_list) { if (ImGui::BeginChild("list", {300, 0}, true)) { ImGuiWidgets::EntityTrashCan(scene); // TODO: remove? _entity_editor.renderEntityList(scene, _entity_comp_list); } ImGui::EndChild(); ImGui::SameLine(); } if (ImGui::BeginChild("editor")) { _entity_editor.renderEditor(scene, _e); } ImGui::EndChild(); } ImGui::End(); } if (_show_camera_tool) { if (ImGui::Begin("Camera3D Tool", &_show_camera_tool)) { ImGuiWidgets::Camera3D(scene); } ImGui::End(); } for (auto it = _text_editor_list.begin(); it != _text_editor_list.end();) { bool keep_open = true; it->renderImGui(&keep_open); if (!keep_open) { it = _text_editor_list.erase(it); continue; } it++; } } EntityEditor& ImGuiSceneToolsService::getEntityEditor(void) { return _entity_editor; } bool ImGuiSceneToolsService::enable(Engine& engine) { if (!engine.tryService()) { LOGIGS("error: rendering is not in engine"); return false; } if (!engine.tryService()) { LOGIGS("error: filesystem not initialized"); return false; } if (!engine.tryService()) { LOGIGS("error: no SceneServiceInterface"); return false; } // setup entity editor defaults { _entity_editor.show_window = false; _entity_editor.registerComponent("Name"); _entity_editor.registerComponent("Transform2D"); _entity_editor.registerComponent("Transform3D"); _entity_editor.registerComponent("Velocity2D"); _entity_editor.registerComponent("ViewDir2D"); _entity_editor.registerComponent("ViewDir3D"); _entity_editor.registerComponent("Color"); _entity_editor.registerComponent("Texture"); } auto* sdl_ss = engine.tryService(); if (sdl_ss) { _event_handle = sdl_ss->addEventHandler([this](const SDL_Event& e) -> bool { if (e.type == SDL_KEYDOWN && !e.key.repeat && e.key.keysym.sym == this->toggle_key) { this->show_menu = !this->show_menu; } return false; // invis }); } else { LOGIGS("Warn: no SDLService, skipping toggle hotkey"); } return true; } void ImGuiSceneToolsService::disable(Engine& engine) { if (_event_handle) { auto* sdl_ss = engine.tryService(); sdl_ss->removeEventHandler(_event_handle); _event_handle = nullptr; } } std::vector ImGuiSceneToolsService::registerUpdates(void) { return { { "ImGuiSceneToolsService::render"_hs, "ImGuiSceneToolsService::render", [this](Engine& e){ renderImGui(e); } } }; } } // namespace MM::Services