-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgeom_rect.c
154 lines (141 loc) · 3.63 KB
/
geom_rect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "db.h"
#include "xwin.h"
#include "token.h"
#include "rubber.h"
#include "rlgetc.h"
#include "opt_parse.h"
/*
*
* +--------------+---------<--+---------------+
* v v ^ |
* (ADD)--0---(R)------+-1------------+--|xy|---|xy|----|prim|->
* | | | | |cmd |
* +-(R)<layer>-+ +-(:W<width>-+
* | |
* +-(:FILL)----+
*/
static double x1, y1;
void draw_box();
OPTS ropts;
int add_rect(LEXER *lp, int *layer)
{
enum {START,NUM1,NUM2,END} state = START;
int done=0;
TOKEN token;
char *word;
double x2,y2;
int debug=0;
if (debug) printf("layer %d\n",*layer);
rl_saveprompt();
rl_setprompt("ADD_RECT> ");
opt_set_defaults(&ropts);
while (!done) {
token = token_look(lp,&word);
if (debug) printf("got %s: %s\n", tok2str(token), word);
if (token==CMD) {
state=END;
}
switch(state) {
case START: /* get option or first xy pair */
db_checkpoint(lp);
if (token == OPT ) {
token_get(lp,&word);
if (opt_parse(word, RECT_OPTS, &ropts) == -1) {
state = END;
} else {
state = START;
}
} else if (token == NUMBER) {
state = NUM1;
} else if (token == EOL || token == EOC) {
token_get(lp,&word); /* just eat it up */
state = START;
} else if (token == EOF) {
state = END;
} else {
token_err("RECT", lp, "expected OPT or NUMBER", token);
state = END; /* error */
}
break;
case NUM1: /* get pair of xy coordinates */
if (token == NUMBER) {
if (getnum(lp, "RECT", &x1, &y1)) {
rubber_set_callback(draw_box);
state = NUM2;
} else {
state = END;
}
} else if (token == EOL) {
token_get(lp,&word); /* just ignore it */
} else if (token == EOC) {
printf(" cancelling ADD RECT\n");
rubber_clear_callback();
need_redraw++;
state = START;
} else {
token_err("RECT", lp, "expected NUMBER", token);
state = END;
}
break;
case NUM2: /* get pair of xy coordinates */
if (token == NUMBER) {
if (getnum(lp, "RECT", &x2, &y2)) {
state = START;
if (x1 == x2 || y1 == y2) {
printf(" Can't add a rectangle of zero width or height: %g %g %g %g\n",
x1, y1, x2, y2);
} else {
db_add_rect(currep, *layer, opt_copy(&ropts), x1, y1, x2, y2);
}
rubber_clear_callback();
need_redraw++;
} else {
state = END;
}
} else if (token == EOL) {
token_get(lp,&word); /* just ignore it */
} else if (token == EOC) {
printf("ADD RECT: cancelling ADD RECT\n");
rubber_clear_callback();
need_redraw++;
state = START;
} else {
token_err("RECT", lp, "expected NUMBER", token);
state = END;
}
break;
case END:
default:
if (token == EOC || token == CMD) {
;
} else {
// token_flush_EOL(lp);
}
done++;
break;
}
}
rubber_clear_callback();
rl_restoreprompt();
return(1);
}
void draw_box(double x2, double y2, int count)
{
static double x1old, x2old, y1old, y2old;
BOUNDS bb;
bb.init=0;
if (count == 0) { /* first call */
db_drawbounds(x1,y1,x2,y2,D_RUBBER); /* draw new shape */
} else if (count > 0) { /* intermediate calls */
db_drawbounds(x1old,y1old,x2old,y2old,D_RUBBER); /* erase old shape */
db_drawbounds(x1,y1,x2,y2,D_RUBBER); /* draw new shape */
} else { /* last call, cleanup */
db_drawbounds(x1old,y1old,x2old,y2old,D_RUBBER); /* erase old shape */
}
/* save old values */
x1old=x1;
y1old=y1;
x2old=x2;
y2old=y2;
jump(&bb, D_RUBBER);
}