Advertisement
TheBirkIsReal

Untitled

Dec 15th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. struct TreeNode {
  2.     TreeNode *next;
  3.     TreeNode *child;
  4.     String text;
  5.     uint64_t id;
  6. };
  7.  
  8. TreeNode* ParseTreeView(char *text) {
  9.     int offset = 0;
  10.     String line = ReadLine(text, &offset);
  11.     while(*line.str && *line.str == ' ') {
  12.         (*line.str)++;
  13.     }
  14.     int last_levels = 0;
  15.  
  16. #define NODE_STACK_SIZE (256)
  17.     TreeNode* stack[NODE_STACK_SIZE] = {0};
  18.     int stack_offset = 0;
  19. #define pop() (assert(stack_offset > 0), stack[--stack_offset])
  20. #define push(...) (stack[stack_offset++] = __VA_ARGS__)
  21.  
  22.     TreeNode *root = 0;
  23.  
  24.     while(line.len > 0) {
  25.         IndentResult indents = StripIndents(line);
  26.        
  27.         TreeNode *n = (TreeNode*) calloc(1, sizeof(TreeNode));
  28.         n->text = indents.line;
  29.         if (root == 0) {
  30.             root = n;
  31.         }
  32.  
  33.         TreeNode *hashParent = nullptr;
  34.         if(stack_offset-1 > 0) {
  35.             hashParent = stack[stack_offset-1];
  36.         }
  37.         HashTreenode(n, hashParent);
  38.  
  39.         int diff = indents.levels - last_levels;
  40.         if(diff < 0) {
  41.             if(stack_offset < 0) {
  42.                 assert(!"Indentation too low");
  43.             }
  44.  
  45.             TreeNode *prev = pop(); // We are dedenting, screw the last node
  46.             TreeNode *parent = pop(); // This should be our parent node
  47.             for(int i = 1; i < (-diff); i++) {
  48.                 parent = pop();
  49.             }
  50.             parent->next = n;
  51.             push(n);
  52.         }
  53.         else if(diff > 0) {
  54.             TreeNode *top = stack[stack_offset-1];
  55.             // Ignore multiple indents levels as that wouldnt make sense
  56.             indents.levels = last_levels+1;
  57.             top->child = n;
  58.             push(n);
  59.         } else {
  60.             if(stack_offset >= 1) {
  61.                 TreeNode *prev = pop();
  62.                 prev->next = n;
  63.             }
  64.             push(n);
  65.         }
  66.  
  67.         last_levels = indents.levels;
  68.         line = ReadLine(text, &offset);
  69.     }
  70.  
  71.     return root;
  72. }
  73.  
  74. // Drawing the node
  75. void EmitNode(TreeNode *root) {
  76.     TreeNode *n = root;
  77.     while(n) {
  78.         ImGuiTreeNodeFlags f = 0;
  79.         if(ImGui::TreeNodeEx((void*)n->id, n->child == 0 ? f|ImGuiTreeNodeFlags_Leaf : f, "%.*s", n->text.len, n->text.str)) {
  80.             TreeNode *c = n->child;
  81.             EmitNode(c);
  82.             ImGui::TreePop();
  83.         }
  84.  
  85.         n = n->next;
  86.     }
  87. }
  88.  
  89. void DoTreeview(Element *e) {
  90.     TreeNode *n = e->treeview.root;
  91.     EmitNode(n);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement