Tracer des diagrammes dans un terminal - retour accueil
git clone git://bebou.netlib.re/boxes
Log | Files | Refs | README |
boxes.c (5928B)
1 #define TB_IMPL 2 #include "termbox2.h" 3 4 #define MAX_BOX_NB 100 5 6 struct box { int x; int y; int h; int w; }; 7 struct arrow { int startx; int starty; int endx; int endy; }; 8 struct boxpile { struct box* list; int count; }; 9 struct arrowpile { struct arrow* list; int count; }; 10 11 void drawboxcoord(int x, int y, int w, int h) { 12 if(w<0) { x=x+w; w=-w; } 13 if(h<0) { y=y+h; h=-h; } 14 tb_printf(x+w,y+h,TB_DEFAULT,TB_DEFAULT,"╯"); 15 tb_printf(x+w,y,TB_DEFAULT,TB_DEFAULT,"╮"); 16 tb_printf(x,y+h,TB_DEFAULT,TB_DEFAULT,"╰"); 17 tb_printf(x,y,TB_DEFAULT,TB_DEFAULT,"╭"); 18 for (int i=1;i<w;i++) { 19 tb_printf(x+i,y,TB_DEFAULT,TB_DEFAULT, "─"); 20 tb_printf(x+i,y+h,TB_DEFAULT,TB_DEFAULT, "─"); 21 } 22 for (int i=1;i<h;i++) { 23 tb_printf(x,y+i,TB_DEFAULT,TB_DEFAULT, "│"); 24 tb_printf(x+w,y+i,TB_DEFAULT,TB_DEFAULT, "│"); 25 } 26 tb_printf(0,0,TB_DEFAULT,TB_DEFAULT,"x: %d, y: %d\nw: %d, h: %d",x,y,w,h); 27 } 28 29 void drawbox(struct box b) { drawboxcoord(b.x,b.y,b.w,b.h); } 30 31 void drawarrowcoord(int startx, int starty, int endx, int endy) { 32 int i=0; int j=0; signed int nx; signed int ny; 33 if(endx<startx) nx=-1; else nx=1; 34 if(endy<starty) ny=-1; else ny=1; 35 for(i=0;i<abs(startx-endx);i++) { 36 tb_printf(startx+nx*i,starty+ny*j,0,0,"─"); 37 } 38 if(ny==1 && nx==1) tb_printf(startx+nx*i,starty,0,0,"╮"); 39 else if(ny==1 && nx==-1) tb_printf(startx+nx*i,starty,0,0,"╭"); 40 else if(ny==-1 && nx==1) tb_printf(startx+nx*i,starty,0,0,"╯"); 41 else tb_printf(startx+nx*i,starty,0,0,"╰"); 42 for(int j=1;j<abs(starty-endy);j++) { 43 tb_printf(startx+nx*i,starty+ny*j,0,0,"│"); 44 } 45 tb_printf(endx,endy,0,0,"→"); 46 tb_printf(0,0,TB_DEFAULT,TB_DEFAULT,"start: x%d,y%d, end: x%d,y%d",startx,starty,endx,endy); 47 } 48 49 void drawarrow(struct arrow a) { drawarrowcoord(a.startx,a.starty,a.endx,a.endy); } 50 51 struct arrow createarrow(int startx, int starty, int endx, int endy) { 52 struct arrow a; 53 a.startx=startx; a.starty=starty; a.endx=endx; a.endy=endy; return a; 54 } 55 56 void addarrow(struct arrowpile *arrows, struct arrow a) { 57 arrows->list[arrows->count]=a; 58 arrows->count++; 59 } 60 61 void deletearrow(struct arrowpile *arrows, int index) { 62 for (int i=index;i<arrows->count-1;i++) arrows->list[i]=arrows->list[i+1]; 63 if(arrows->count>0) arrows->count--; 64 } 65 66 struct box createbox(int x, int y, int w, int h) { 67 struct box b; b.x=x; b.y=y; b.w=w; b.h=h; return b; 68 } 69 70 void addbox(struct boxpile *boxes, struct box b) { 71 boxes->list[boxes->count]=b; 72 boxes->count++; 73 } 74 75 void deletebox(struct boxpile *boxes, int index) { 76 for (int i=index;i<boxes->count-1;i++) boxes->list[i]=boxes->list[i+1]; 77 if(boxes->count>0) boxes->count--; 78 } 79 80 int main(int argc, char **argv) { 81 82 int toparsecount=0; 83 char line[1000]; 84 char **toparse; 85 86 struct boxpile boxes; boxes.list=malloc(MAX_BOX_NB*sizeof(struct box)); boxes.count=0; 87 struct arrowpile arrows; arrows.list=malloc(MAX_BOX_NB*sizeof(struct arrow)); arrows.count=0; 88 89 if(!isatty(fileno(stdin))) { 90 while (fgets(line, sizeof(line), stdin)) { 91 if(strlen(line)==1) continue; 92 toparse=realloc(toparse,(toparsecount+1)*sizeof(void*)); 93 toparse[toparsecount] = malloc(1000); 94 strcpy(toparse[toparsecount], line); 95 toparsecount++; 96 } 97 98 char *delimiter=" "; 99 char* token; 100 for (int i=0;i<toparsecount;i++) { 101 token = strtok(toparse[i], delimiter); 102 char *type=token; 103 int args[4]; int j=0; 104 while (token) { 105 token = strtok(NULL, delimiter); 106 if(token!=NULL) args[j++]=(int)strtol(token, NULL, 10); 107 } 108 if(strcmp(type,"arrow")==0) addarrow(&arrows,createarrow(args[0],args[1],args[2],args[3])); 109 else if (strcmp(type,"box")==0) addbox(&boxes,createbox(args[0],args[1],args[2],args[3])); 110 } 111 } 112 113 114 tb_init(); 115 tb_hide_cursor(); 116 tb_set_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE); 117 int mode=0; 118 struct tb_event ev; 119 int curx; int cury; 120 121 122 while(1) { 123 tb_clear(); 124 for (int i=0;i<boxes.count;i++) drawbox(boxes.list[i]); 125 for (int i=0;i<arrows.count;i++) drawarrow(arrows.list[i]); 126 tb_printf(0,tb_height()-1,TB_DEFAULT,TB_DEFAULT,"count: %d, mode: %d",boxes.count,mode); 127 tb_present(); 128 tb_poll_event(&ev); 129 switch(ev.ch) { 130 case 97: /*a*/ 131 mode=1; 132 break; 133 case 98: /*b*/ 134 mode=0; 135 break; 136 } 137 switch(ev.key) { 138 case 13: /*Enter*/ 139 tb_shutdown(); 140 for (int i=0;i<arrows.count;i++) 141 printf("arrow %d %d %d %d\n",arrows.list[i].startx,arrows.list[i].starty,arrows.list[i].endx,arrows.list[i].endy); 142 for (int i=0;i<boxes.count;i++) 143 printf("box %d %d %d %d\n",boxes.list[i].x,boxes.list[i].y,boxes.list[i].w,boxes.list[i].h); 144 free(boxes.list); 145 return 0; 146 case 65512: /*MouseLeft*/ 147 switch(mode) { 148 case 0: 149 if(boxes.count==MAX_BOX_NB) break; 150 curx=ev.x; cury=ev.y; 151 while(ev.key!=65509) { /*MouseRelease*/ 152 tb_clear(); 153 for (int i=0;i<boxes.count;i++) drawbox(boxes.list[i]); 154 for (int i=0;i<arrows.count;i++) drawarrow(arrows.list[i]); 155 drawboxcoord(curx,cury,ev.x-curx,ev.y-cury); 156 tb_present(); 157 tb_poll_event(&ev); 158 } 159 addbox(&boxes,createbox(curx,cury,ev.x-curx,ev.y-cury)); 160 break; 161 case 1: 162 if(arrows.count==MAX_BOX_NB) break; 163 curx=ev.x; cury=ev.y; 164 while(ev.key!=65509) { /*MouseRelease*/ 165 tb_clear(); 166 for (int i=0;i<boxes.count;i++) drawbox(boxes.list[i]); 167 for (int i=0;i<arrows.count;i++) drawarrow(arrows.list[i]); 168 drawarrowcoord(curx,cury,ev.x,ev.y); 169 tb_present(); 170 tb_poll_event(&ev); 171 } 172 addarrow(&arrows,createarrow(curx,cury,ev.x,ev.y)); 173 break; 174 } 175 break; 176 case 26: /*ctrl+z*/ 177 switch(mode) { 178 case 0: 179 deletebox(&boxes,boxes.count-1); 180 break; 181 case 1: 182 deletearrow(&arrows,arrows.count-1); 183 break; 184 } 185 break; 186 case 65511: /*MouseRight*/ 187 for(int i=0;i<boxes.count;i++) 188 if(ev.x==boxes.list[i].x && ev.y==boxes.list[i].y) { 189 deletebox(&boxes,i); 190 break; 191 } 192 break; 193 } 194 } 195 return 0; 196 }