Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct TreeNode {
- TreeNode *next;
- TreeNode *child;
- String text;
- uint64_t id;
- };
- TreeNode* ParseTreeView(char *text) {
- int offset = 0;
- String line = ReadLine(text, &offset);
- while(*line.str && *line.str == ' ') {
- (*line.str)++;
- }
- int last_levels = 0;
- #define NODE_STACK_SIZE (256)
- TreeNode* stack[NODE_STACK_SIZE] = {0};
- int stack_offset = 0;
- #define pop() (assert(stack_offset > 0), stack[--stack_offset])
- #define push(...) (stack[stack_offset++] = __VA_ARGS__)
- TreeNode *root = 0;
- while(line.len > 0) {
- IndentResult indents = StripIndents(line);
- TreeNode *n = (TreeNode*) calloc(1, sizeof(TreeNode));
- n->text = indents.line;
- if (root == 0) {
- root = n;
- }
- TreeNode *hashParent = nullptr;
- if(stack_offset-1 > 0) {
- hashParent = stack[stack_offset-1];
- }
- HashTreenode(n, hashParent);
- int diff = indents.levels - last_levels;
- if(diff < 0) {
- if(stack_offset < 0) {
- assert(!"Indentation too low");
- }
- TreeNode *prev = pop(); // We are dedenting, screw the last node
- TreeNode *parent = pop(); // This should be our parent node
- for(int i = 1; i < (-diff); i++) {
- parent = pop();
- }
- parent->next = n;
- push(n);
- }
- else if(diff > 0) {
- TreeNode *top = stack[stack_offset-1];
- // Ignore multiple indents levels as that wouldnt make sense
- indents.levels = last_levels+1;
- top->child = n;
- push(n);
- } else {
- if(stack_offset >= 1) {
- TreeNode *prev = pop();
- prev->next = n;
- }
- push(n);
- }
- last_levels = indents.levels;
- line = ReadLine(text, &offset);
- }
- return root;
- }
- // Drawing the node
- void EmitNode(TreeNode *root) {
- TreeNode *n = root;
- while(n) {
- ImGuiTreeNodeFlags f = 0;
- if(ImGui::TreeNodeEx((void*)n->id, n->child == 0 ? f|ImGuiTreeNodeFlags_Leaf : f, "%.*s", n->text.len, n->text.str)) {
- TreeNode *c = n->child;
- EmitNode(c);
- ImGui::TreePop();
- }
- n = n->next;
- }
- }
- void DoTreeview(Element *e) {
- TreeNode *n = e->treeview.root;
- EmitNode(n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement