initial arrow implementation - gramscii - A simple editor for ASCII box-and-arrow charts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 34a0ec991e4985d3ca4719ffc696f681370e14fc
 (DIR) parent 5dd00eaf46bd250099c368c7b4a66e0e4dfc4b6f
 (HTM) Author: KatolaZ <katolaz@freaknet.org>
       Date:   Fri, 19 Jul 2019 12:04:44 +0100
       
       initial arrow implementation
       
       Diffstat:
         M gramscii.c                          |     139 +++++++++++++++++++++++++++++--
       
       1 file changed, 132 insertions(+), 7 deletions(-)
       ---
 (DIR) diff --git a/gramscii.c b/gramscii.c
       @@ -17,6 +17,7 @@
        #define TEXT   0x04
        
        
       +#define DIR_N  0x00
        #define DIR_R  0x01
        #define DIR_U  0x02
        #define DIR_D  0x04
       @@ -50,12 +51,12 @@ int y;
        int step;
        char cursor;
        char corner;
       -char hlines[]  = {"-~=#@._"};
       -char vlines[]  = {"|H#@:;i"};
       +char hlines[]  = {"-~=#@._ "};
       +char vlines[]  = {"|H#@:;i "};
        char corners[] = {"+'H#@.\""};
       -int hlines_sz= sizeof(hlines);
       -int vlines_sz= sizeof(vlines);
       -int corners_sz = sizeof(corners);
       +int hlines_sz= sizeof(hlines) -1;
       +int vlines_sz= sizeof(vlines) -1;
       +int corners_sz = sizeof(corners) -1;
        int cur_hl, cur_vl, cur_corn;
        char line_h;
        char line_v;
       @@ -137,7 +138,6 @@ void redraw(){
                }
                status_bar();
                show_cursor();
       -        step = 1;
        }
        
        void update_current(){
       @@ -235,8 +235,8 @@ void draw_box(int x1, int y1, int fix){
        void get_box(){
                char c;
                int orig_x=x, orig_y=y;
       -        
                redraw();
       +        step = 1;
                draw_box(x,y,NOFIX);
                while((c=getchar())!=EOF && c != 27 && c!= 'b'){
                        switch(c){
       @@ -259,6 +259,7 @@ void get_box(){
                        }
                        check_bound();
                        redraw();
       +                step = 1;
                        draw_box(orig_x, orig_y, NOFIX);
                        status_bar();
                        show_cursor();
       @@ -270,6 +271,126 @@ void get_box(){
        }
        
        
       +int progr_x(int dir){
       +        switch(dir){
       +                case DIR_L:
       +                        return -1;
       +                case DIR_R:
       +                        return +1;
       +                default:
       +                        return 0;
       +        }
       +        return 0;
       +}
       +
       +
       +int progr_y(int dir){
       +        switch(dir){
       +                case DIR_D:
       +                        return +1;
       +                case DIR_U:
       +                        return -1;
       +                default:
       +                        return 0;
       +        }
       +        return 0;
       +}
       +
       +
       +/* FIXME: fix pointer position */
       +/* FIXME: set initial and final markers */
       +/* FIXME: draw "corner" as first char after change of dir */
       +void draw_arrow(int x, int y, char *a, int a_len, int fix){
       +
       +        int i, j;
       +        char line;
       +        void (*f)(int, int, char);
       +
       +                
       +        if (fix == FIX)
       +                f = set_xy;
       +        else
       +                f = draw_xy;
       +
       +        if (!a_len){
       +                f(x,y,corner);
       +                return;
       +        }
       +        line = (a[0] & DIR_L) || (a[0] & DIR_R) ? line_h : line_v;
       +        f(x,y,line);
       +        for (i=0; i<a_len; i+=2){
       +                for(j=0; j<a[i+1]; j++){
       +                        line = (a[i] & DIR_L) || (a[i] & DIR_R) ? line_h : line_v;
       +                        x += progr_x(a[i]);
       +                        y += progr_y(a[i]);
       +                        f(x, y, line); 
       +                }
       +        }
       +        show_cursor();
       +}
       +
       +void get_arrow(){
       +
       +        char c;
       +        int orig_x=x, orig_y=y, arrow_len;
       +        static char *arrow = NULL;
       +        static int arrow_sz;
       +
       +        if (!arrow){
       +                arrow_sz = 100;
       +                arrow = malloc(arrow_sz * sizeof(char));
       +        }
       +        arrow_len = 0;
       +        dir = DIR_N;
       +        
       +        redraw();
       +        step = 1;
       +        //draw_arrow(x,y,NOFIX);
       +        while((c=getchar())!=EOF && c != 27 && c!= 'a'){
       +                switch(c){
       +                        case 'H': step = 5;
       +                        case 'h': 
       +                                dir = DIR_L;
       +                                x -= step;
       +                                break;        
       +                        case 'J': step = 5;
       +                        case 'j':
       +                                dir = DIR_D;
       +                                y += step;
       +                                break;
       +                        case 'K': step = 5;
       +                        case 'k':
       +                                dir = DIR_U;
       +                                y -= step;
       +                                break;
       +                        case 'L': step = 5;
       +                        case 'l':
       +                                dir = DIR_R;
       +                                x += step;
       +                                break;
       +                        default:
       +                                continue;
       +                }
       +                check_bound();
       +                /* FIXME: if we are out of bound, do nothing? */
       +                if (arrow_len == arrow_sz){
       +                        arrow_sz *=2;
       +                        arrow = realloc(arrow, arrow_sz * sizeof(char));
       +                }
       +                arrow[arrow_len++] = dir;
       +                arrow[arrow_len++] = step;
       +                redraw();
       +                step = 1;
       +                draw_arrow(orig_x, orig_y, arrow, arrow_len, NOFIX);
       +                status_bar();
       +                show_cursor();
       +        }
       +        if (c == 'a')
       +                draw_arrow(orig_x, orig_y, arrow, arrow_len, FIX);
       +        redraw();
       +        state = MOVE;
       +}
       +
        void write_file(){
                char fname[256];
                FILE *f;
       @@ -333,6 +454,10 @@ void commands(){
                                        state = BOX;
                                        get_box();
                                        break;
       +                        case 'a':
       +                                state = ARROW;
       +                                get_arrow();
       +                                break;
                                case 'w':
                                        write_file();
                                        break;