Untitled
unknown
plain_text
2 years ago
2.2 kB
8
Indexable
using json = nlohmann::json;
enum NodeType {
FILE_NODE,
DIRECTORY_NODE
};
struct TreeNode {
std::string name;
NodeType type;
TreeNode* child; // NULL if it's a file, points to child node if it's a directory
// Constructor for file node
TreeNode(const std::string& name, NodeType type) : name(name), type(type), child(nullptr) {}
// Constructor for directory node
TreeNode(const std::string& name) : name(name), type(DIRECTORY_NODE), child(nullptr) {}
// Destructor to recursively delete child nodes
~TreeNode() {
if (type == DIRECTORY_NODE && child != nullptr) {
delete child;
}
}
};
void ParseJSON(const json& nodeData, TreeNode& treeNode) {
if (nodeData.contains("name") && nodeData.contains("type")) {
treeNode.name = nodeData["name"];
std::string type = nodeData["type"];
std::cout << "Parsing node: " << treeNode.name << ", Type: " << type << std::endl;
if (type == "directory") {
treeNode.type = DIRECTORY_NODE;
if (nodeData.contains("children")) {
treeNode.child = new TreeNode("");
for (const auto& child : nodeData["children"]) {
ParseJSON(child, *treeNode.child);
}
}
}
else if (type == "file") {
treeNode.type = FILE_NODE;
}
}
}
void DisplayTreeNode(const TreeNode& node) {
if (node.type == DIRECTORY_NODE) {
if (ImGui::TreeNode(node.name.c_str())) {
if (node.child) {
DisplayTreeNode(*node.child);
}
ImGui::TreePop();
}
}
else {
ImGui::Selectable(node.name.c_str());
}
}
std::ifstream file("project.json");
json fileTreeJson;
file >> fileTreeJson;
TreeNode root(fileTreeJson["project"]);
ParseJSON(fileTreeJson["root"], root);
int width, height;
glfwGetWindowSize(window, &width, &height);
ImGui::SetNextWindowSize(ImVec2(300, 600), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowPos(ImVec2(width-300, 20), ImGuiCond_FirstUseEver);
ImGui::Begin("Project Explorer");
DisplayTreeNode(root);
ImGui::End();Editor is loading...
Leave a Comment