Debian Codegraph @

grid.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* Copyright (c) 1997-1999 Miller Puckette.
2 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
3 * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
4
5 /* g_grid.c written by Yves Degoyon 2002 */
6 /* grid control object : two dimensionnal grid */
7 /* thanks to Thomas Musil, Miller Puckette, Guenther Geiger and Krzystof Czaja */
8
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <math.h>
14 #include <ctype.h>
15 #include "m_pd.h"
16 #include "m_imp.h"
17 #include "g_canvas.h"
18
19 #include "g_grid.h"
20
21 #ifdef _WIN32
22 #include <io.h>
23 #else
24 #include <unistd.h>
25 #endif
26
27 #define DEFAULT_GRID_WIDTH 200
28 #define DEFAULT_GRID_HEIGHT 200
29 #define DEFAULT_GRID_NBLINES 10
30
31 t_widgetbehavior grid_widgetbehavior;
32 static t_class *grid_class;
33 static int gridcount=0;
34
35 static int guidebug=0;
36 static int pointsize = 5;
37
38 static char *grid_version = "grid: version 0.9, written by Yves Degoyon ([email protected])";
39
40 #define GRID_SYS_VGUI2(a,b) if (guidebug) \
41 post(a,b);\
42 sys_vgui(a,b)
43
44 #define GRID_SYS_VGUI3(a,b,c) if (guidebug) \
45 post(a,b,c);\
46 sys_vgui(a,b,c)
47
48 #define GRID_SYS_VGUI4(a,b,c,d) if (guidebug) \
49 post(a,b,c,d);\
50 sys_vgui(a,b,c,d)
51
52 #define GRID_SYS_VGUI7(a,b,c,d,e,f,g) if (guidebug) \
53 post(a,b,c,d,e,f,g );\
54 sys_vgui(a,b,c,d,e,f,g)
55
56 #define GRID_SYS_VGUI8(a,b,c,d,e,f,g,h) if (guidebug) \
57 post(a,b,c,d,e,f,g,h);\
58 sys_vgui(a,b,c,d,e,f,g,h)
59
60 #define GRID_SYS_VGUI9(a,b,c,d,e,f,g,h,i) if (guidebug) \
61 post(a,b,c,d,e,f,g,h,i );\
62 sys_vgui(a,b,c,d,e,f,g,h,i)
63
64 /* drawing functions */
65 static void grid_draw_update(t_grid *x, t_glist *glist)
66 {
67 t_canvas *canvas=glist_getcanvas(glist);
68 t_int xpoint=x->x_current, ypoint=x->y_current;
69
70 // later : try to figure out what's this test for ??
71 // if (glist_isvisible(glist))
72 // {
73 // delete previous point if existing
74 if (x->x_point)
75 {
76 GRID_SYS_VGUI3(".x%lx.c delete %lxPOINT\n", canvas, x);
77 }
78
79 if ( x->x_current < text_xpix(&x->x_obj, glist) ) xpoint = text_xpix(&x->x_obj, glist);
80 if ( x->x_current > text_xpix(&x->x_obj, glist) + x->x_width - pointsize )
81 xpoint = text_xpix(&x->x_obj, glist) + x->x_width - pointsize;
82 if ( x->y_current < text_ypix(&x->x_obj, glist) ) ypoint = text_ypix(&x->x_obj, glist);
83 if ( x->y_current > text_ypix(&x->x_obj, glist) + x->x_height - pointsize )
84 ypoint = text_ypix(&x->x_obj, glist) + x->x_height - pointsize;
85 // draw the selected point
86 GRID_SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -fill #FF0000 -tags %lxPOINT\n",
87 canvas, xpoint, ypoint, xpoint+pointsize, ypoint+pointsize, x);
88 x->x_point = 1;
89 // }
90 // else
91 // {
92 // post( "grid : position updated in an invisible grid" );
93 // }
94 }
95
96 static void grid_draw_new(t_grid *x, t_glist *glist)
97 {
98 t_canvas *canvas=glist_getcanvas(glist);
99
100 GRID_SYS_VGUI8(".x%lx.c create rectangle %d %d %d %d -fill %s -tags %lxGRID\n",
101 canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
102 text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height,
103 x->x_bgcolor, x);
104 GRID_SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -tags %lxo0\n",
105 canvas, text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist) + x->x_height+1,
106 text_xpix(&x->x_obj, glist)+7, text_ypix(&x->x_obj, glist) + x->x_height+2,
107 x);
108 GRID_SYS_VGUI7(".x%lx.c create rectangle %d %d %d %d -tags %lxo1\n",
109 canvas, text_xpix(&x->x_obj, glist)+x->x_width-7, text_ypix(&x->x_obj, glist) + x->x_height+1,
110 text_xpix(&x->x_obj, glist)+x->x_width, text_ypix(&x->x_obj, glist) + x->x_height+2,
111 x);
112
113 if ( x->x_grid )
114 {
115 int xlpos = text_xpix(&x->x_obj, glist)+x->x_width/x->x_xlines;
116 int ylpos = text_ypix(&x->x_obj, glist)+x->x_height/x->x_ylines;
117 int xcount = 1;
118 int ycount = 1;
119 while ( xlpos < text_xpix(&x->x_obj, glist)+x->x_width )
120 {
121 GRID_SYS_VGUI9(".x%lx.c create line %d %d %d %d -fill #FFFFFF -tags %lxLINE%d%d\n",
122 canvas, xlpos, text_ypix(&x->x_obj, glist),
123 xlpos, text_ypix(&x->x_obj, glist)+x->x_height,
124 x, xcount, 0 );
125 xlpos+=x->x_width/x->x_xlines;
126 xcount++;
127 }
128 while ( ylpos < text_ypix(&x->x_obj, glist)+x->x_height )
129 {
130 GRID_SYS_VGUI9(".x%lx.c create line %d %d %d %d -fill #FFFFFF -tags %lxLINE%d%d\n",
131 canvas, text_xpix(&x->x_obj, glist), ylpos,
132 text_xpix(&x->x_obj, glist)+x->x_width, ylpos,
133 x, 0, ycount);
134 ylpos+=x->x_height/x->x_ylines;
135 ycount++;
136 }
137 }
138 canvas_fixlinesfor( canvas, (t_text*)x );
139 }
140
141 static void grid_draw_move(t_grid *x, t_glist *glist)
142 {
143 t_canvas *canvas=glist_getcanvas(glist);
144
145 GRID_SYS_VGUI7(".x%lx.c coords %lxGRID %d %d %d %d\n",
146 canvas, x,
147 text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
148 text_xpix(&x->x_obj, glist)+x->x_width, text_ypix(&x->x_obj, glist)+x->x_height);
149 GRID_SYS_VGUI7(".x%lx.c coords %lxo0 %d %d %d %d\n",
150 canvas, x,
151 text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist) + x->x_height+1,
152 text_xpix(&x->x_obj, glist)+7, text_ypix(&x->x_obj, glist) + x->x_height+2 );
153 GRID_SYS_VGUI7(".x%lx.c coords %lxo1 %d %d %d %d\n",
154 canvas, x,
155 text_xpix(&x->x_obj, glist)+x->x_width-7, text_ypix(&x->x_obj, glist) + x->x_height+1,
156 text_xpix(&x->x_obj, glist)+x->x_width, text_ypix(&x->x_obj, glist) + x->x_height+2 );
157 if ( x->x_point )
158 {
159 grid_draw_update(x, glist);
160 }
161 if ( x->x_grid )
162 {
163 int xlpos = text_xpix(&x->x_obj, glist)+x->x_width/x->x_xlines;
164 int ylpos = text_ypix(&x->x_obj, glist)+x->x_height/x->x_ylines;
165 int xcount = 1;
166 int ycount = 1;
167 while ( xlpos < text_xpix(&x->x_obj, glist)+x->x_width )
168 {
169 GRID_SYS_VGUI9(".x%lx.c coords %lxLINE%d%d %d %d %d %d\n",
170 canvas, x, xcount, 0, xlpos, text_ypix(&x->x_obj, glist),
171 xlpos, text_ypix(&x->x_obj, glist) + x->x_height);
172 xlpos+=x->x_width/x->x_xlines;
173 xcount++;
174 }
175 while ( ylpos < text_ypix(&x->x_obj, glist)+x->x_height )
176 {
177 GRID_SYS_VGUI9(".x%lx.c coords %lxLINE%d%d %d %d %d %d\n",
178 canvas, x, 0, ycount, text_xpix(&x->x_obj, glist), ylpos,
179 text_xpix(&x->x_obj, glist) + x->x_width, ylpos);
180 ylpos+=x->x_height/x->x_ylines;
181 ycount++;
182 }
183 }
184 canvas_fixlinesfor( canvas, (t_text*)x );
185 }
186
187 static void grid_draw_erase(t_grid* x,t_glist* glist)
188 {
189 t_canvas *canvas=glist_getcanvas(glist);
190 int i;
191
192 GRID_SYS_VGUI3(".x%lx.c delete %lxGRID\n", canvas, x);
193 GRID_SYS_VGUI3(".x%lx.c delete %lxo0\n", canvas, x);
194 GRID_SYS_VGUI3(".x%lx.c delete %lxo1\n", canvas, x);
195 if (x->x_grid)
196 {
197 for (i=1; i<x->x_xlines; i++ )
198 {
199 GRID_SYS_VGUI4(".x%lx.c delete %lxLINE%d0\n", canvas, x, i);
200 }
201 for (i=1; i<x->x_ylines; i++ )
202 {
203 GRID_SYS_VGUI4(".x%lx.c delete %lxLINE0%d\n", canvas, x, i);
204 }
205 }
206 if (x->x_point)
207 {
208 GRID_SYS_VGUI3(".x%lx.c delete %lxPOINT\n", canvas, x);
209 x->x_point = 0;
210 }
211 }
212
213 static void grid_draw_select(t_grid* x,t_glist* glist)
214 {
215 t_canvas *canvas=glist_getcanvas(glist);
216
217 if(x->x_selected)
218 {
219 pd_bind(&x->x_obj.ob_pd, x->x_name);
220 /* sets the item in blue */
221 GRID_SYS_VGUI3(".x%lx.c itemconfigure %lxGRID -outline #0000FF\n", canvas, x);
222 }
223 else
224 {
225 pd_unbind(&x->x_obj.ob_pd, x->x_name);
226 GRID_SYS_VGUI3(".x%lx.c itemconfigure %lxGRID -outline #000000\n", canvas, x);
227 }
228 }
229
230 static void grid_output_current(t_grid* x)
231 {
232 t_float xvalue, yvalue;
233 t_float xmodstep, ymodstep;
234
235 xvalue = x->x_min + (x->x_current - text_xpix(&x->x_obj, x->x_glist)) * (x->x_max-x->x_min) / x->x_width ;
236 if (xvalue < x->x_min ) xvalue = x->x_min;
237 if (xvalue > x->x_max ) xvalue = x->x_max;
238 xmodstep = ((float)((int)(xvalue*10000) % (int)(x->x_xstep*10000))/10000.);
239 xvalue = xvalue - xmodstep;
240 outlet_float( x->x_xoutlet, xvalue );
241
242 yvalue = x->y_max - (x->y_current - text_ypix(&x->x_obj, x->x_glist) ) * (x->y_max-x->y_min) / x->x_height ;
243 if (yvalue < x->y_min ) yvalue = x->y_min;
244 if (yvalue > x->y_max ) yvalue = x->y_max;
245 ymodstep = ((float)((int)(yvalue*10000) % (int)(x->x_ystep*10000))/10000.);
246 yvalue = yvalue - ymodstep;
247 outlet_float( x->x_youtlet, yvalue );
248 }
249
250 /* ------------------------ grid widgetbehaviour----------------------------- */
251
252
253 static void grid_getrect(t_gobj *z, t_glist *owner,
254 int *xp1, int *yp1, int *xp2, int *yp2)
255 {
256 t_grid* x = (t_grid*)z;
257
258 *xp1 = text_xpix(&x->x_obj, x->x_glist);
259 *yp1 = text_ypix(&x->x_obj, x->x_glist);
260 *xp2 = text_xpix(&x->x_obj, x->x_glist)+x->x_width;
261 *yp2 = text_ypix(&x->x_obj, x->x_glist)+x->x_height;
262 }
263
264 static void grid_save(t_gobj *z, t_binbuf *b)
265 {
266 t_grid *x = (t_grid *)z;
267
268 // post( "saving grid : %s", x->x_name->s_name );
269 binbuf_addv(b, "ssiissiffiffiffiiff", gensym("#X"),gensym("obj"),
270 (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix,
271 atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)),
272 x->x_name, x->x_width, x->x_min,
273 x->x_max, x->x_height,
274 x->y_min, x->y_max,
275 x->x_grid, x->x_xstep,
276 x->x_ystep, x->x_xlines, x->x_ylines,
277 x->x_current, x->y_current );
278 binbuf_addv(b, ";");
279 }
280
281 static void grid_properties(t_gobj *z, t_glist *owner)
282 {
283 char buf[800];
284 t_grid *x=(t_grid *)z;
285
286 sprintf(buf, "pdtk_grid_dialog %%s %s %d %.2f %.2f %d %.2f %.2f %d %.2f %.2f %d %d\n",
287 x->x_name->s_name, x->x_width, x->x_min, x->x_max, x->x_height,
288 x->y_min, x->y_max, x->x_grid, x->x_xstep, x->x_ystep,
289 x->x_xlines, x->x_ylines );
290 // post("grid_properties : %s", buf );
291 gfxstub_new(&x->x_obj.ob_pd, x, buf);
292 }
293
294 static void grid_select(t_gobj *z, t_glist *glist, int selected)
295 {
296 t_grid *x = (t_grid *)z;
297
298 x->x_selected = selected;
299 grid_draw_select( x, glist );
300 }
301
302 static void grid_vis(t_gobj *z, t_glist *glist, int vis)
303 {
304 t_grid *x = (t_grid *)z;
305
306 if (vis)
307 {
308 grid_draw_new( x, glist );
309 grid_draw_update( x, glist );
310 grid_output_current(x);
311 }
312 else
313 {
314 grid_draw_erase( x, glist );
315 }
316 }
317
318 static void grid_dialog(t_grid *x, t_symbol *s, int argc, t_atom *argv)
319 {
320 if ( !x )
321 {
322 post( "grid : error :tried to set properties on an unexisting object" );
323 }
324 if ( argc != 12 )
325 {
326 pd_error(x, "grid : error in the number of arguments ( %d instead of 12 )", argc );
327 return;
328 }
329 if ( argv[0].a_type != A_SYMBOL || argv[1].a_type != A_FLOAT ||
330 argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT ||
331 argv[4].a_type != A_FLOAT || argv[5].a_type != A_FLOAT ||
332 argv[6].a_type != A_FLOAT || argv[7].a_type != A_FLOAT ||
333 argv[8].a_type != A_FLOAT || argv[9].a_type != A_FLOAT ||
334 argv[10].a_type != A_FLOAT || argv[11].a_type != A_FLOAT )
335 {
336 pd_error(x, "grid : wrong arguments" );
337 return;
338 }
339 x->x_name = argv[0].a_w.w_symbol;
340 x->x_width = (int)argv[1].a_w.w_float;
341 x->x_min = argv[2].a_w.w_float;
342 x->x_max = argv[3].a_w.w_float;
343 x->x_height = (int)argv[4].a_w.w_float;
344 x->y_min = argv[5].a_w.w_float;
345 x->y_max = argv[6].a_w.w_float;
346 x->x_grid = argv[7].a_w.w_float;
347 x->x_xstep = argv[8].a_w.w_float;
348 x->x_ystep = argv[9].a_w.w_float;
349 x->x_xlines = argv[10].a_w.w_float;
350 x->x_ylines = argv[11].a_w.w_float;
351 grid_draw_erase(x, x->x_glist);
352 grid_draw_new(x, x->x_glist);
353 }
354
355 static void grid_delete(t_gobj *z, t_glist *glist)
356 {
357 canvas_deletelinesfor(glist, (t_text *)z);
358 }
359
360 static void grid_displace(t_gobj *z, t_glist *glist, int dx, int dy)
361 {
362 t_grid *x = (t_grid *)z;
363 int xold = text_xpix(&x->x_obj, glist);
364 int yold = text_ypix(&x->x_obj, glist);
365
366 // post( "grid_displace dx=%d dy=%d", dx, dy );
367
368 x->x_obj.te_xpix += dx;
369 x->x_current += dx;
370 x->x_obj.te_ypix += dy;
371 x->y_current += dy;
372 if(xold != text_xpix(&x->x_obj, glist) || yold != text_ypix(&x->x_obj, glist))
373 {
374 grid_draw_move(x, x->x_glist);
375 }
376 }
377
378 static void grid_motion(t_grid *x, t_floatarg dx, t_floatarg dy)
379 {
380 int xold = x->x_current;
381 int yold = x->y_current;
382
383 // post( "grid_motion dx=%f dy=%f", dx, dy );
384
385 x->x_current += dx;
386 x->y_current += dy;
387 if(xold != x->x_current || yold != x->y_current)
388 {
389 grid_output_current(x);
390 grid_draw_update(x, x->x_glist);
391 }
392 }
393
394 static int grid_click(t_gobj *z, struct _glist *glist,
395 int xpix, int ypix, int shift, int alt, int dbl, int doit)
396 {
397 t_grid* x = (t_grid *)z;
398
399 // post( "grid_click doit=%d x=%d y=%d", doit, xpix, ypix );
400 if ( doit)
401 {
402 x->x_current = xpix;
403 x->y_current = ypix;
404 grid_output_current(x);
405 grid_draw_update(x, glist);
406 glist_grab(glist, &x->x_obj.te_g, (t_glistmotionfn)grid_motion,
407 0, xpix, ypix);
408 }
409 return (1);
410 }
411
412 static void grid_goto(t_grid *x, t_floatarg newx, t_floatarg newy)
413 {
414 int xold = x->x_current;
415 int yold = x->y_current;
416
417 if ( newx > x->x_width-1 ) newx = x->x_width-1;
418 if ( newx < 0 ) newx = 0;
419 if ( newy > x->x_height-1 ) newy = x->x_height-1;
420 if ( newy < 0 ) newy = 0;
421
422 // post( "grid_set x=%f y=%f", newx, newy );
423
424 x->x_current = newx + text_xpix(&x->x_obj, x->x_glist);
425 x->y_current = newy + text_ypix(&x->x_obj, x->x_glist);
426 if(xold != x->x_current || yold != x->y_current)
427 {
428 grid_output_current(x);
429 grid_draw_update(x, x->x_glist);
430 }
431 }
432
433
434 static void grid_new_color(t_grid *x, t_floatarg color1, t_floatarg color2, t_floatarg color3)
435 {
436 char col1[10], col2[10], col3[10];
437
438 if ( ( color1 < 0 ) || ( color1 > 255 ) )
439 {
440 pd_error(x, "wrong color component : %d", (int) color1 );
441 }
442 if ( ( color2 < 0 ) || ( color2 > 255 ) )
443 {
444 pd_error(x, "wrong color component : %d", (int) color2 );
445 }
446 if ( ( color3 < 0 ) || ( color3 > 255 ) )
447 {
448 pd_error(x, "wrong color component : %d", (int) color3 );
449 }
450
451 if (color1 < 17)
452 sprintf(col1,"0%X",(int) color1);
453 else
454 sprintf(col1,"%X",(int) color1);
455
456 if (color2 < 17)
457 sprintf(col2,"0%X",(int) color2);
458 else
459 sprintf(col2,"%X",(int) color2);
460
461 if (color3 < 17)
462 sprintf(col3,"0%X",(int) color3);
463 else
464 sprintf(col3,"%X",(int) color3);
465 sprintf( x->x_bgcolor, "#%s%s%s", col1, col2, col3);
466
467 grid_draw_erase( x, x->x_glist);
468 grid_draw_new( x, x->x_glist );
469 }
470
471 static void grid_values(t_grid* x, t_floatarg xvalue, t_floatarg yvalue)
472 {
473 int xold = x->x_current;
474 int yold = x->y_current;
475
476 if (xvalue < x->x_min ) xvalue = x->x_min;
477 if (xvalue > x->x_max ) xvalue = x->x_max;
478
479 x->x_current = text_xpix(&x->x_obj, x->x_glist)
480 + abs(((xvalue - x->x_min) / (x->x_max - x->x_min)) * x->x_width);
481
482 if (yvalue < x->y_min ) yvalue = x->y_min;
483 if (yvalue > x->y_max ) yvalue = x->y_max;
484
485 x->y_current = text_ypix(&x->x_obj, x->x_glist)
486 + abs((1 - (yvalue - x->y_min) / (x->y_max - x->y_min)) * x->x_height);
487
488 if(xold != x->x_current || yold != x->y_current)
489 {
490 grid_output_current(x);
491 grid_draw_update(x, x->x_glist);
492 }
493 }
494
495 static void grid_xvalues(t_grid* x, t_floatarg xvalue, t_floatarg yvalue)
496 {
497 int xold = x->x_current;
498 int yold = x->y_current;
499
500 if (xvalue < x->x_min ) xvalue = x->x_min;
501 if (xvalue > x->x_max ) xvalue = x->x_max;
502
503 x->x_current = text_xpix(&x->x_obj, x->x_glist)
504 + abs(((xvalue - x->x_min) / (x->x_max - x->x_min)) * x->x_width);
505
506 if (yvalue < x->y_min ) yvalue = x->y_min;
507 if (yvalue > x->y_max ) yvalue = x->y_max;
508
509 x->y_current = text_ypix(&x->x_obj, x->x_glist)
510 + abs((1 - (yvalue - x->y_min) / (x->y_max - x->y_min)) * x->x_height);
511
512 if(xold != x->x_current || yold != x->y_current)
513 {
514 grid_draw_update(x, x->x_glist);
515 }
516 }
517
518 static void grid_valuemotion(t_grid* x, t_floatarg dx, t_floatarg dy)
519 {
520 int xold = x->x_current;
521 int yold = x->y_current;
522 t_float xvalue, yvalue;
523
524 xvalue = x->x_min + (x->x_current - text_xpix(&x->x_obj, x->x_glist)) * (x->x_max-x->x_min) / x->x_width ;
525 if (xvalue < x->x_min ) xvalue = x->x_min;
526 if (xvalue > x->x_max ) xvalue = x->x_max;
527
528 yvalue = x->y_max - (x->y_current - text_ypix(&x->x_obj, x->x_glist) ) * (x->y_max-x->y_min) / x->x_height ;
529 if (yvalue < x->y_min ) yvalue = x->y_min;
530 if (yvalue > x->y_max ) yvalue = x->y_max;
531
532 xvalue += dx;
533 yvalue += dy;
534
535 if (xvalue < x->x_min ) xvalue = x->x_min;
536 if (xvalue > x->x_max ) xvalue = x->x_max;
537
538 x->x_current = text_xpix(&x->x_obj, x->x_glist)
539 + abs(((xvalue - x->x_min) / (x->x_max - x->x_min)) * x->x_width);
540
541 if (yvalue < x->y_min ) yvalue = x->y_min;
542 if (yvalue > x->y_max ) yvalue = x->y_max;
543
544 x->y_current = text_ypix(&x->x_obj, x->x_glist)
545 + abs((1 - (yvalue - x->y_min) / (x->y_max - x->y_min)) * x->x_height);
546
547 if(xold != x->x_current || yold != x->y_current)
548 {
549 grid_output_current(x);
550 grid_draw_update(x, x->x_glist);
551 }
552 }
553
554 static void grid_xvaluemotion(t_grid* x, t_floatarg dx, t_floatarg dy)
555 {
556 int xold = x->x_current;
557 int yold = x->y_current;
558 t_float xvalue, yvalue;
559
560 xvalue = x->x_min + (x->x_current - text_xpix(&x->x_obj, x->x_glist)) * (x->x_max-x->x_min) / x->x_width ;
561 if (xvalue < x->x_min ) xvalue = x->x_min;
562 if (xvalue > x->x_max ) xvalue = x->x_max;
563
564 yvalue = x->y_max - (x->y_current - text_ypix(&x->x_obj, x->x_glist) ) * (x->y_max-x->y_min) / x->x_height ;
565 if (yvalue < x->y_min ) yvalue = x->y_min;
566 if (yvalue > x->y_max ) yvalue = x->y_max;
567
568 xvalue += dx;
569 yvalue += dy;
570
571 if (xvalue < x->x_min ) xvalue = x->x_min;
572 if (xvalue > x->x_max ) xvalue = x->x_max;
573
574 x->x_current = text_xpix(&x->x_obj, x->x_glist)
575 + abs(((xvalue - x->x_min) / (x->x_max - x->x_min)) * x->x_width);
576
577 if (yvalue < x->y_min ) yvalue = x->y_min;
578 if (yvalue > x->y_max ) yvalue = x->y_max;
579
580 x->y_current = text_ypix(&x->x_obj, x->x_glist)
581 + abs((1 - (yvalue - x->y_min) / (x->y_max - x->y_min)) * x->x_height);
582
583 if(xold != x->x_current || yold != x->y_current)
584 {
585 grid_draw_update(x, x->x_glist);
586 }
587 }
588
589 static void grid_bang(t_grid *x)
590 {
591 grid_output_current(x);
592 }
593
594 static t_grid *grid_new(t_symbol *s, int argc, t_atom *argv)
595 {
596 int zz;
597 t_grid *x;
598 char *str;
599
600 // post( "grid_new : create : %s argc =%d", s->s_name, argc );
601
602 x = (t_grid *)pd_new(grid_class);
603 // new grid created from the gui
604 if ( argc != 0 )
605 {
606 if ( argc != 14 )
607 {
608 pd_error(x, "grid : error in the number of arguments ( %d instead of 14 )", argc );
609 return NULL;
610 }
611 if ( argv[0].a_type != A_SYMBOL || argv[1].a_type != A_FLOAT ||
612 argv[2].a_type != A_FLOAT || argv[3].a_type != A_FLOAT ||
613 argv[4].a_type != A_FLOAT || argv[5].a_type != A_FLOAT ||
614 argv[6].a_type != A_FLOAT || argv[7].a_type != A_FLOAT ||
615 argv[8].a_type != A_FLOAT || argv[9].a_type != A_FLOAT ||
616 argv[10].a_type != A_FLOAT || argv[11].a_type != A_FLOAT ||
617 argv[12].a_type != A_FLOAT || argv[13].a_type != A_FLOAT )
618 {
619 pd_error(x, "grid : wrong arguments" );
620 return NULL;
621 }
622
623 // update grid count
624 if (!strncmp((str = argv[0].a_w.w_symbol->s_name), "grid", 5)
625 && (zz = atoi(str + 5)) > gridcount)
626 {
627 gridcount = zz;
628 }
629 x->x_name = argv[0].a_w.w_symbol;
630 pd_bind(&x->x_obj.ob_pd, x->x_name);
631 x->x_width = argv[1].a_w.w_float;
632 x->x_min = argv[2].a_w.w_float;
633 x->x_max = argv[3].a_w.w_float;
634 x->x_height = argv[4].a_w.w_float;
635 x->y_min = argv[5].a_w.w_float;
636 x->y_max = argv[6].a_w.w_float;
637 x->x_grid = argv[7].a_w.w_float;
638 x->x_xstep = argv[8].a_w.w_float;
639 x->x_ystep = argv[9].a_w.w_float;
640 x->x_xlines = argv[10].a_w.w_float;
641 x->x_ylines = argv[11].a_w.w_float;
642 x->x_current = argv[12].a_w.w_float;
643 x->y_current = argv[13].a_w.w_float;
644 x->x_point = 1;
645 }
646 else
647 {
648 char buf[40];
649
650 sprintf(buf, "grid%d", ++gridcount);
651 s = gensym(buf);
652
653 x->x_name = s;
654 pd_bind(&x->x_obj.ob_pd, x->x_name);
655
656 x->x_width = DEFAULT_GRID_WIDTH;
657 x->x_min = 0;
658 x->x_max = DEFAULT_GRID_WIDTH - 1;
659 x->x_height = DEFAULT_GRID_HEIGHT;
660 x->y_min = 0;
661 x->y_max = DEFAULT_GRID_HEIGHT - 1;
662 x->x_grid = 1;
663 x->x_xstep = 1.0;
664 x->x_ystep = 1.0;
665 x->x_xlines = DEFAULT_GRID_NBLINES;
666 x->x_ylines = DEFAULT_GRID_NBLINES;
667 x->x_current = 0;
668 x->y_current = 0;
669
670 }
671
672 // common fields for new and restored grids
673 x->x_point = 0;
674 x->x_selected = 0;
675 x->x_glist = (t_glist *) canvas_getcurrent();
676 x->x_xoutlet = outlet_new(&x->x_obj, &s_float );
677 x->x_youtlet = outlet_new(&x->x_obj, &s_float );
678
679 x->x_bgcolor = (char*)getbytes(12);
680 strcpy( x->x_bgcolor, "#123589" );
681
682 // post( "grid_new name : %s width: %d height : %d", x->x_name->s_name, x->x_width, x->x_height );
683
684 return (x);
685 }
686
687 static void grid_free(t_grid *x)
688 {
689 }
690
691 void grid_setup(void)
692 {
•693 logpost(NULL, 4, "%s", grid_version );
694 grid_class = class_new(gensym("grid"), (t_newmethod)grid_new,
695 (t_method)grid_free, sizeof(t_grid), 0, A_GIMME, 0);
696 class_addmethod(grid_class, (t_method)grid_click, gensym("click"),
697 A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0);
698 class_addmethod(grid_class, (t_method)grid_motion, gensym("motion"),
699 A_FLOAT, A_FLOAT, 0);
700 class_addmethod(grid_class, (t_method)grid_bang, gensym("bang"), 0);
701 class_addmethod(grid_class, (t_method)grid_values, gensym("values"),
702 A_FLOAT, A_FLOAT, 0);
703 class_addmethod(grid_class, (t_method)grid_valuemotion, gensym("valuemotion"),
704 A_FLOAT, A_FLOAT, 0);
705 class_addmethod(grid_class, (t_method)grid_xvalues, gensym("xvalues"),
706 A_FLOAT, A_FLOAT, 0);
707 class_addmethod(grid_class, (t_method)grid_xvaluemotion, gensym("xvaluemotion"),
708 A_FLOAT, A_FLOAT, 0);
709 class_addmethod(grid_class, (t_method)grid_goto, gensym("goto"), A_FLOAT, A_FLOAT, 0);
710 class_addmethod(grid_class, (t_method)grid_dialog, gensym("dialog"), A_GIMME, 0);
711 class_addmethod(grid_class, (t_method)grid_new_color, gensym("color"), A_FLOAT, A_FLOAT, A_FLOAT, 0);
712 grid_widgetbehavior.w_getrectfn = grid_getrect;
713 grid_widgetbehavior.w_displacefn = grid_displace;
714 grid_widgetbehavior.w_selectfn = grid_select;
715 grid_widgetbehavior.w_activatefn = NULL;
716 grid_widgetbehavior.w_deletefn = grid_delete;
717 grid_widgetbehavior.w_visfn = grid_vis;
718 grid_widgetbehavior.w_clickfn = grid_click;
719
720 #if PD_MINOR_VERSION >= 37
721 class_setpropertiesfn(grid_class, grid_properties);
722 class_setsavefn(grid_class, grid_save);
723 #else
724 grid_widgetbehavior.w_propertiesfn = grid_properties;
725 grid_widgetbehavior.w_savefn = grid_save;
726 #endif
727
728 class_setwidget(grid_class, &grid_widgetbehavior);
729
730 sys_vgui("eval [read [open {%s/%s.tcl}]]\n",
731 grid_class->c_externdir->s_name, grid_class->c_name->s_name);
732 }