Skip to content

Commit

Permalink
Tree is parsed only once
Browse files Browse the repository at this point in the history
  • Loading branch information
OSMaxwell committed Oct 27, 2023
1 parent 8b60b64 commit 6bf909f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 36 deletions.
10 changes: 3 additions & 7 deletions Intern/rayx-ui/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<float, std::chrono::seconds::period>(newTime - currentTime).count();
uiParams.frameTime = frameTime;
uiParams.frameTime = std::chrono::duration<float, std::chrono::seconds::period>(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;
Expand Down
71 changes: 46 additions & 25 deletions Intern/rayx-ui/src/RenderSystem/UIRenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <imgui_impl_glfw.h>
#include <imgui_impl_vulkan.h>

#include <glm/gtx/quaternion.hpp>

#include "CanonicalizePath.h"

void checkVkResult(VkResult result, const char* message) {
Expand Down Expand Up @@ -239,22 +237,51 @@ 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.");
return;
}

// 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
Expand All @@ -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<UIRenderSystem::TreeNode>("Root");
buildTreeFromXMLNode(xml_beamline, *m_pTreeRoot);
renderImGuiTree(*m_pTreeRoot);
}

void UIRenderSystem::showBeamlineOutlineWindow(UIParameters& uiParams) {
Expand All @@ -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();
}
15 changes: 11 additions & 4 deletions Intern/rayx-ui/src/RenderSystem/UIRenderSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<TreeNode> children;

TreeNode(const char* nodeName) : name(nodeName) {}
};

private:
const Window& m_Window;
const Device& m_Device;
Expand All @@ -41,6 +49,7 @@ class UIRenderSystem {
bool m_useLargeFont = false;
ImFont* m_smallFont;
ImFont* m_largeFont;
std::unique_ptr<TreeNode> m_pTreeRoot;

VkRenderPass m_RenderPass;
VkDescriptorPool m_DescriptorPool;
Expand All @@ -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);
};

0 comments on commit 6bf909f

Please sign in to comment.