From 6bf909f61c677a4eb67f69ba6c8d10e42a16fc83 Mon Sep 17 00:00:00 2001 From: OSMaxwell Date: Fri, 27 Oct 2023 14:48:39 +0200 Subject: [PATCH] Tree is parsed only once --- Intern/rayx-ui/src/Application.cpp | 10 +-- .../src/RenderSystem/UIRenderSystem.cpp | 71 ++++++++++++------- .../rayx-ui/src/RenderSystem/UIRenderSystem.h | 15 ++-- 3 files changed, 60 insertions(+), 36 deletions(-) diff --git a/Intern/rayx-ui/src/Application.cpp b/Intern/rayx-ui/src/Application.cpp index 7e2525583..b33b4369a 100644 --- a/Intern/rayx-ui/src/Application.cpp +++ b/Intern/rayx-ui/src/Application.cpp @@ -75,11 +75,8 @@ void Application::run() { // CLI Input std::string rmlPathCli = m_CommandParser.m_args.m_providedFile; - if (!rmlPathCli.empty()) { - updateScene(rmlPathCli, rObjects, rays, rayObj); - } + UIParameters uiParams{camController, rmlPathCli, !rmlPathCli.empty(), 0.0}; - UIParameters uiParams{camController, rmlPathCli, false, 0.0}; // Main loop while (!m_Window.shouldClose()) { // Skip rendering when minimized @@ -91,13 +88,12 @@ void Application::run() { if (auto commandBuffer = m_Renderer.beginFrame()) { // Params to pass to UI auto newTime = std::chrono::high_resolution_clock::now(); - float frameTime = std::chrono::duration(newTime - currentTime).count(); - uiParams.frameTime = frameTime; + uiParams.frameTime = std::chrono::duration(newTime - currentTime).count(); currentTime = newTime; // Update UI and camera m_ImGuiLayer.setupUI(uiParams); - camController.update(cam, m_Renderer.getAspectRatio()); + //camController.update(cam, m_Renderer.getAspectRatio()); if (uiParams.pathChanged) { updateScene(uiParams.rmlPath.string(), rObjects, rays, rayObj); uiParams.pathChanged = false; diff --git a/Intern/rayx-ui/src/RenderSystem/UIRenderSystem.cpp b/Intern/rayx-ui/src/RenderSystem/UIRenderSystem.cpp index 5e67c7384..84b33414a 100644 --- a/Intern/rayx-ui/src/RenderSystem/UIRenderSystem.cpp +++ b/Intern/rayx-ui/src/RenderSystem/UIRenderSystem.cpp @@ -4,8 +4,6 @@ #include #include -#include - #include "CanonicalizePath.h" void checkVkResult(VkResult result, const char* message) { @@ -239,7 +237,36 @@ void UIRenderSystem::showSettingsWindow() { ImGui::End(); } -void renderImGuiTreeFromRML(const std::filesystem::path& filename) { + +void renderImGuiTree(const UIRenderSystem::TreeNode& treeNode) { + for (auto& child : treeNode.children) { + if (child.children.empty()) { + if (ImGui::Selectable(child.name.c_str())) { + // Handle selection logic here + std::cout << "Selected object: " << child.name << std::endl; + } + } else { + if (ImGui::TreeNode(child.name.c_str())) { + renderImGuiTree(child); + ImGui::TreePop(); + } + } + } +} + +void buildTreeFromXMLNode(rapidxml::xml_node<>* node, UIRenderSystem::TreeNode& treeNode) { + for (rapidxml::xml_node<>* xmlChild = node->first_node(); xmlChild; xmlChild = xmlChild->next_sibling()) { + if (strcmp(xmlChild->name(), "object") == 0) { + treeNode.children.emplace_back(xmlChild->first_attribute("name")->value()); + } else if (strcmp(xmlChild->name(), "group") == 0) { + UIRenderSystem::TreeNode groupNode(xmlChild->first_attribute("name")->value()); + buildTreeFromXMLNode(xmlChild, groupNode); + treeNode.children.push_back(groupNode); + } + } +} + +void UIRenderSystem::renderImGuiTreeFromRML(const std::filesystem::path& filename) { // Check if file exists if (!std::filesystem::exists(filename)) { ImGui::Text("Choose a file to display the beamline outline."); @@ -247,14 +274,14 @@ void renderImGuiTreeFromRML(const std::filesystem::path& filename) { } // Read and parse the RML file - std::ifstream t(filename); - if (!t.is_open()) { + std::ifstream fileContent(filename); + if (!fileContent.is_open()) { ImGui::Text("Error: Could not open file."); return; } std::stringstream buffer; - buffer << t.rdbuf(); + buffer << fileContent.rdbuf(); std::string test = buffer.str(); // Check if the file is empty @@ -280,24 +307,9 @@ void renderImGuiTreeFromRML(const std::filesystem::path& filename) { } // Call recursive function to handle the object collection and render the ImGui tree - renderImGuiTreeFromXMLNode(xml_beamline); -} - -void renderImGuiTreeFromXMLNode(rapidxml::xml_node<>* node) { - for (rapidxml::xml_node<>* object = node->first_node(); object; object = object->next_sibling()) { - if (strcmp(object->name(), "object") == 0) { - if (ImGui::Selectable(object->first_attribute("name")->value())) { - // Handle object selection logic here - // e.g. print name to console - std::cout << "Selected object: " << object->first_attribute("name")->value() << std::endl; - } - } else if (strcmp(object->name(), "group") == 0) { - if (ImGui::TreeNode(object->first_attribute("name")->value())) { - renderImGuiTreeFromXMLNode(object); - ImGui::TreePop(); - } - } - } + m_pTreeRoot = std::make_unique("Root"); + buildTreeFromXMLNode(xml_beamline, *m_pTreeRoot); + renderImGuiTree(*m_pTreeRoot); } void UIRenderSystem::showBeamlineOutlineWindow(UIParameters& uiParams) { @@ -306,7 +318,16 @@ void UIRenderSystem::showBeamlineOutlineWindow(UIParameters& uiParams) { ImGui::Begin("Beamline Outline"); - renderImGuiTreeFromRML(uiParams.rmlPath); + if (uiParams.pathChanged) { + // Create and render new Tree + renderImGuiTreeFromRML(uiParams.rmlPath); + } else if (m_pTreeRoot == nullptr) { + // Do nothing + ImGui::Text("Choose a file to display the beamline outline."); + } else { + // Render same Tree + renderImGuiTree(*m_pTreeRoot); + } ImGui::End(); } diff --git a/Intern/rayx-ui/src/RenderSystem/UIRenderSystem.h b/Intern/rayx-ui/src/RenderSystem/UIRenderSystem.h index 79ba00c79..8f1c71f9d 100644 --- a/Intern/rayx-ui/src/RenderSystem/UIRenderSystem.h +++ b/Intern/rayx-ui/src/RenderSystem/UIRenderSystem.h @@ -32,6 +32,14 @@ class UIRenderSystem { VkClearValue getClearValue() const { return {m_ClearColor[0], m_ClearColor[1], m_ClearColor[2], m_ClearColor[3]}; } + // Simple TreeNode + struct TreeNode { + std::string name; + std::vector children; + + TreeNode(const char* nodeName) : name(nodeName) {} + }; + private: const Window& m_Window; const Device& m_Device; @@ -41,6 +49,7 @@ class UIRenderSystem { bool m_useLargeFont = false; ImFont* m_smallFont; ImFont* m_largeFont; + std::unique_ptr m_pTreeRoot; VkRenderPass m_RenderPass; VkDescriptorPool m_DescriptorPool; @@ -49,8 +58,6 @@ class UIRenderSystem { void showSceneEditorWindow(UIParameters& uiParams); void showSettingsWindow(); void showBeamlineOutlineWindow(UIParameters& uiParams); -}; -void renderImGuiTreeFromRML(const std::filesystem::path& filename); - -void renderImGuiTreeFromXMLNode(rapidxml::xml_node<>* node); + void renderImGuiTreeFromRML(const std::filesystem::path& filename); +};