From 8e3d23b921d9bbcb7c53017bacff8a3050a34b55 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Sat, 27 Jul 2019 06:57:12 +0100 Subject: reorganise code --- draw.c | 324 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 324 insertions(+) create mode 100644 draw.c (limited to 'draw.c') diff --git a/draw.c b/draw.c new file mode 100644 index 0000000..9e20601 --- /dev/null +++ b/draw.c @@ -0,0 +1,324 @@ +#include + +#include "gramscii.h" +#include "config.h" + +/*** drawing-related functions ***/ + +/*** Lines and markers ***/ + +void toggle_hline(){ + + cur_hl = (cur_hl + 1) % hlines_sz; + line_h = hlines[cur_hl]; + +} + +void toggle_corner(){ + + cur_corn = (cur_corn + 1 ) % corners_sz; + corner = corners[cur_corn]; + +} + +void toggle_vline(){ + + cur_vl = (cur_vl + 1) % vlines_sz; + line_v = vlines[cur_vl]; + +} + +void toggle_st_mark(){ + + cur_start = (cur_start + 1 ) % stmarks_sz; + mark_st = st_marks[cur_start]; +} + +void toggle_end_mark(){ + + cur_end = (cur_end+ 1 ) % endmarks_sz; + mark_end = end_marks[cur_end]; +} + +int change_style(char c){ + switch(c){ + case '-': + toggle_hline(); + break; + case '|': + toggle_vline(); + break; + case '+': + toggle_corner(); + break; + case '<': + toggle_st_mark(); + break; + case '>': + toggle_end_mark(); + break; + case '.': + reset_styles(); + break; + default: + return 0; + } + return c; +} + + + + +/***** text, box, arrows *****/ + +void get_text(FILE *fc){ + char c; + int orig_x = x; + + redraw(); + while((c=fgetc(fc))!=EOF && c != 27){ + if(c=='\n'){ + set_cur(BG); + y += 1; + x = orig_x; + } + else { + set_cur(c); + update_current(); + modified = 1; + x += 1; + if (x >= WIDTH) + x = orig_x; + } + check_bound(); + status_bar(); + show_cursor(); + } + mode=MOVE; +} + +void draw_box(int x1, int y1, int fix){ + + int xmin, ymin, xmax, ymax; + int i; + void (*f)(int, int, char); + + if (fix == FIX) + f = set_xy; + else + f = draw_xy; + + xmin = MIN(x, x1); + xmax = MAX(x, x1); + ymin = MIN(y, y1); + ymax = MAX(y, y1); + + for(i=xmin+1; i<=xmax; i++){ + f(i, ymin, line_h); + f(i, ymax, line_h); + } + for(i=ymin+1; i<=ymax; i++){ + f(xmin, i, line_v); + f(xmax, i, line_v); + } + f(xmin, ymin, corner); + f(xmin, ymax, corner); + f(xmax, ymin, corner); + f(xmax, ymax, corner); + show_cursor(); +} + +void get_box(FILE *fc){ + char c; + int orig_x=x, orig_y=y; + redraw(); + step = 1; + draw_box(x,y,NOFIX); + while((c=fgetc(fc))!=EOF && c != 27 && c!= 'b' && c != '\n'){ + if (change_style(c)) + goto update_box; + if (!move_around(c, fc)) + continue; + check_bound(); + redraw(); + step = 1; +update_box: + draw_box(orig_x, orig_y, NOFIX); + status_bar(); + show_cursor(); + } + if (c == 'b' || c == '\n'){ + draw_box(orig_x, orig_y, FIX); + modified = 1; + } + redraw(); + mode = MOVE; +} + +void draw_arrow(int x, int y, char *a, int a_len, int fix){ + + int i, j, cur_dir; + char line; + void (*f)(int, int, char); + + + if (fix == FIX) + f = set_xy; + else + f = draw_xy; + + f(x,y,mark_st); + if (!a_len){ + show_cursor(); + return; + } + cur_dir=DIR_N; + for (i=0; i0) { + /* If we are switching between horizontal and vertical, put a "corner" */ + if (((cur_dir & DIR_HOR) && (a[i] & DIR_VER)) || + ((cur_dir & DIR_VER) && (a[i] & DIR_HOR))){ + f(x,y,corner); + show_cursor(); + } + } + for(j=0; j=x; i--) set_xy(i,y,BG); + break; + case DIR_U: + for(i=y1; i>=y; i--) set_xy(x,i,BG); + break; + case DIR_D: + for(i=y1; i<=y; i++) set_xy(x,i,BG); + break; + } +} + + +void erase(FILE *fc){ + char c; + int orig_x = x, orig_y = y; + status_bar(); + show_cursor(); + while((c=fgetc(fc))!=EOF && c!=27 && c!= 'x' && c != '\n'){ + if (!move_around(c, fc)) continue; + check_bound(); + do_erase(orig_x, orig_y); + step = 1; + modified = 1; + orig_x = x; + orig_y = y; + redraw(); + status_bar(); + show_cursor(); + } + mode = MOVE; +} + + +/*** Visual ***/ + + +void visual_box(FILE *fc){ + int orig_x =x, orig_y = y; + char c, f = BG; + + redraw(); + step = 1; + set_video(VIDEO_REV); + draw_box(x,y,NOFIX); + while((c=fgetc(fc))!=EOF && c != 27 && c!= 'v' && c != '\n'){ + if (!move_around(c, fc)) switch(c){ + case 'f':/* fill */ + f = get_key(fc, "fill char: "); /** FALLTHROUGH **/ + case 'x':/* erase */ + erase_box(orig_x, orig_y, f); + modified = 1; + goto vis_exit; + break; + } + check_bound(); + set_video(VIDEO_NRM); + redraw(); + step = 1; + f = BG; + set_video(VIDEO_REV); + draw_box(orig_x, orig_y, NOFIX); + status_bar(); + show_cursor(); + } +vis_exit: + set_video(VIDEO_NRM); + redraw(); + mode = MOVE; +} -- cgit v1.2.3