aboutsummaryrefslogtreecommitdiff
path: root/src/paramGui.h
blob: 2cef33509478171684a7ce685908057e3ffbd1aa (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440

static t_class *paramGui_class;
 
#define GUI_X_STEP 200
#define GUI_Y_STEP 18
 
 

typedef struct _paramGui
{
  t_object					x_obj;
  //t_outlet*					outlet;
  //t_symbol*					canvasname;
  t_canvas*					childcanvas;
  //t_symbol*                 fullpath;
  t_symbol*                 path;
  int                       path_l;
  int                       build;
  t_symbol*					root;
  
  t_symbol*					receive;
  int						waiting;
  //t_symbol*					target;
  //t_class*					empty_s;
  //t_symbol*					root;

} t_paramGui;


static void paramGui_buildCanvas(t_paramGui* x,int x_position,int y_position) {
	
			// Clear the canvas
            pd_typedmess((t_pd*)x->childcanvas,s_clear,0,NULL);
            
            int pos_x = 0;
            int pos_y = 0;
            
            t_atom atoms[22]; // This should be the maximum number of atoms
            
            // PINK HEADER
            SETSYMBOL(&atoms[0],s_obj);
			SETFLOAT(&atoms[1],pos_x);
			SETFLOAT(&atoms[2],pos_y);
			SETSYMBOL(&atoms[3],s_cnv);
			SETFLOAT(&atoms[4],15);
			SETFLOAT(&atoms[5],200);
			SETFLOAT(&atoms[6],20);
			SETSYMBOL(&atoms[7],s_empty);
			SETSYMBOL(&atoms[8],s_empty);
			SETSYMBOL(&atoms[9],x->path);
			SETFLOAT(&atoms[10],2);
			SETFLOAT(&atoms[11],12);
			SETFLOAT(&atoms[12],0);
			SETFLOAT(&atoms[13],8);
			SETFLOAT(&atoms[14],-258401);
			SETFLOAT(&atoms[15],-262144);
			SETFLOAT(&atoms[16],0);
			pd_forwardmess((t_pd*)x->childcanvas, 17, atoms);
            pos_y = pos_y + 23;
            
            
            int ac;
            t_atom* av;
            t_symbol* type;
            t_symbol* send;
            t_symbol* receive;
            
            int i;
            t_symbol* shortpath;
            
            int gui_built = 1;
            
            // ac & av for updating the values of the gui (p->get())
            // after it is created
            int ac_got = 0;
            t_atom* av_got;
            t_symbol* s_got;
             
            
             t_param* p = get_param_list(x->root);
             
             while (p) {
			
                gui_built = 1;
                    if (p->GUI && (strncmp(p->path->s_name,x->path->s_name,x->path_l)==0)) {
                        p->GUI(p->x,&ac,&av,&send,&receive);
                        if ( send == NULL ) send = s_empty;
                        if ( receive == NULL ) receive = s_empty;
                        
                        /* 
                        // This code alows the positioning of the guis, but creates
                        // too many problems
                        if ( IS_A_FLOAT(av,0) ) {
							pos_x = GUI_X_STEP * atom_getfloat(av);
							av++;
							ac--;
						}
						
						if ( IS_A_FLOAT(av,0) ) {
							pos_y = GUI_Y_STEP * atom_getfloat(av);
							av++;
							ac--;
						}
                        */
                        if ( IS_A_SYMBOL(av,0)) {
							
							// Make shortpath (removes what is common between paths)
							// Do not make shortpath if we are at the root (x->path_l==1)
							if ( x->path_l < 2) {
								shortpath = p->path;
							} else {
								shortpath = gensym(p->path->s_name + x->path_l);
							}
							
							
							
							
                           type = atom_getsymbol(av);
                           if ( type == s_nbx ) {
                                SETSYMBOL(&atoms[0],s_obj);
                                SETFLOAT(&atoms[1],pos_x);
                                SETFLOAT(&atoms[2],pos_y);
                                SETSYMBOL(&atoms[3],s_nbx);
                                SETFLOAT(&atoms[4],5);
                                SETFLOAT(&atoms[5],14);
                                SETFLOAT(&atoms[6],-1.0e+37);
                                SETFLOAT(&atoms[7],1.0e+37);
                                SETFLOAT(&atoms[8],0);
                                SETFLOAT(&atoms[9],0);
                                SETSYMBOL(&atoms[10],send);
                                SETSYMBOL(&atoms[11],receive);
                                SETSYMBOL(&atoms[12],shortpath);
                                SETFLOAT(&atoms[13],50);
                                SETFLOAT(&atoms[14],8);
                                SETFLOAT(&atoms[15],0);
                                SETFLOAT(&atoms[16],8);
                                SETFLOAT(&atoms[17],-262144);
                                SETFLOAT(&atoms[18],-1);
                                SETFLOAT(&atoms[19],-1);
                                SETFLOAT(&atoms[20],0);
                                SETFLOAT(&atoms[21],256);
                                pd_forwardmess((t_pd*)x->childcanvas, 22, atoms);
                                pos_y = pos_y + GUI_Y_STEP;
                                
                            } else if (type == s_bng) {
                                SETSYMBOL(&atoms[0],s_obj);
                                SETFLOAT(&atoms[1],pos_x);
                                SETFLOAT(&atoms[2],pos_y);
                                SETSYMBOL(&atoms[3],s_bng);
                                SETFLOAT(&atoms[4],15);
                                SETFLOAT(&atoms[5],250);
                                SETFLOAT(&atoms[6],50);
                                SETFLOAT(&atoms[7],0);
                                SETSYMBOL(&atoms[8],send);
                                SETSYMBOL(&atoms[9],receive);
                                SETSYMBOL(&atoms[10],shortpath);
                                SETFLOAT(&atoms[11],17);
                                SETFLOAT(&atoms[12],7);
                                SETFLOAT(&atoms[13],0);
                                SETFLOAT(&atoms[14],8);
                                SETFLOAT(&atoms[15],-262144);
                                SETFLOAT(&atoms[16],-1);
                                SETFLOAT(&atoms[17],-1);
                                pd_forwardmess((t_pd*)x->childcanvas, 18, atoms);
                                pos_y = pos_y + GUI_Y_STEP;
                            } else if ( (type == s_slider) || (type == s_knob) || (type == s_hsl) ) {
                                SETSYMBOL(&atoms[0],s_obj);
                                SETFLOAT(&atoms[1],pos_x);
                                SETFLOAT(&atoms[2],pos_y);
                                SETSYMBOL(&atoms[3],s_hsl);
                                SETFLOAT(&atoms[4],100);
                                SETFLOAT(&atoms[5],15);
                                if (ac > 1 && IS_A_FLOAT(av,1) ) {
                                     SETFLOAT(&atoms[6],atom_getfloat(av+1));
                                 } else {
                                     SETFLOAT(&atoms[6],0);
                                 }
                                 if (ac > 2 && IS_A_FLOAT(av,2) ) {
                                     SETFLOAT(&atoms[7],atom_getfloat(av+2));
                                 } else {
                                     SETFLOAT(&atoms[7],1);
                                 }
                                SETFLOAT(&atoms[8],0);
                                SETFLOAT(&atoms[9],0);
                                SETSYMBOL(&atoms[10],send);
                                SETSYMBOL(&atoms[11],receive);
                                SETSYMBOL(&atoms[12],shortpath);
                                SETFLOAT(&atoms[13],105);
                                SETFLOAT(&atoms[14],7);
                                SETFLOAT(&atoms[15],0);
                                SETFLOAT(&atoms[16],8);
                                SETFLOAT(&atoms[17],-262144);
                                SETFLOAT(&atoms[18],-1);
                                SETFLOAT(&atoms[19],-1);
                                SETFLOAT(&atoms[20],0);
                                SETFLOAT(&atoms[21],1);
                                pd_forwardmess((t_pd*)x->childcanvas, 22, atoms);
                                pos_y = pos_y + GUI_Y_STEP;
                                
                            } else if (type == s_tgl) {
                                SETSYMBOL(&atoms[0],s_obj);
                                SETFLOAT(&atoms[1],pos_x);
                                SETFLOAT(&atoms[2],pos_y);
                                SETSYMBOL(&atoms[3],s_tgl);
                                SETFLOAT(&atoms[4],15);
                                SETFLOAT(&atoms[5],0);
                                SETSYMBOL(&atoms[6],send);
                                SETSYMBOL(&atoms[7],receive);
                                SETSYMBOL(&atoms[8],shortpath);
                                SETFLOAT(&atoms[9],17);
                                SETFLOAT(&atoms[10],7);
                                SETFLOAT(&atoms[11],0);
                                SETFLOAT(&atoms[12],8);
                                SETFLOAT(&atoms[13],-262144);
                                SETFLOAT(&atoms[14],1);
                                SETFLOAT(&atoms[15],-1);
                                SETFLOAT(&atoms[16],0);
                                SETFLOAT(&atoms[17],1);
                                pd_forwardmess((t_pd*)x->childcanvas, 18, atoms);
                                pos_y = pos_y + GUI_Y_STEP;
                                
                                
                            } else if ( type == s_symbolatom || type == s_sym) {
                                SETSYMBOL(&atoms[0],s_symbolatom);
                                SETFLOAT(&atoms[1],pos_x);
                                SETFLOAT(&atoms[2],pos_y);
                                SETFLOAT(&atoms[3],17);
                                SETFLOAT(&atoms[4],0);
                                SETFLOAT(&atoms[5],0);
                                SETFLOAT(&atoms[6],1);
                                SETSYMBOL(&atoms[7],shortpath);
                                SETSYMBOL(&atoms[8],receive);
                                SETSYMBOL(&atoms[9],send);
                                pd_forwardmess((t_pd*)x->childcanvas, 10,atoms);
                                pos_y = pos_y + GUI_Y_STEP;
                            } else {
                                SETSYMBOL(&atoms[0],s_text);
                                SETFLOAT(&atoms[1],pos_x);
                                SETFLOAT(&atoms[2],pos_y);
                                SETSYMBOL(&atoms[3],shortpath);
                                pd_forwardmess((t_pd*)x->childcanvas, 4,atoms);
                                pos_y = pos_y + GUI_Y_STEP;
                                gui_built = 0;
                            }
                            
                            if ((gui_built) && (receive != s_empty) && (p->get)) {
                                p->get(p->x,&s_got,&ac_got,&av_got);
                                tof_send_anything_prepend(receive,s_got,ac_got,av_got,s_set);
                            }
                        }
                    }
                p = p->next;
            
            }
			
			// Try to resize the canvas
			 x->childcanvas->gl_screenx1 = x_position;
			 x->childcanvas->gl_screeny1 = y_position;
			 x->childcanvas->gl_screenx2 = pos_x + 300 + x->childcanvas->gl_screenx1;
			 x->childcanvas->gl_screeny2 = pos_y + 30 + x->childcanvas->gl_screeny1;
			 
			 
			 
			 // Change the build flag
			x->build = 0;
			
			// Show canvas
			t_atom a;
			SETFLOAT(&a,1);
			pd_typedmess((t_pd*)x->childcanvas,s_vis,1,&a);
	
}


static void paramGui_motion_callback(t_paramGui *x, t_float x_position, t_float y_position)
{
	if ( x->waiting ) {
		x->waiting = 0;
		paramGui_buildCanvas(x,(int) x_position,(int) y_position );
	}
}


static void paramGui_bang(t_paramGui *x) {
    
    if (x->childcanvas && !x->waiting) {
        
        if (x->build) {
            
            // query for the mouse pointers position 
            // one it is received, build the canvas 
            x->waiting = 1;
			sys_vgui("pd [concat %s motion [winfo pointerxy .] \\;]\n",x->receive->s_name);
        } else {
        
			
			
			// Show canvas
			t_atom a;
			SETFLOAT(&a,1);
			pd_typedmess((t_pd*)x->childcanvas,s_vis,1,&a);
		}
        
    }  else {
        
       pd_error(x,"No canvas to write to or mouse position not received!");
       
   }
}

static void paramGui_reset(t_paramGui *x) {
    x->build = 1;
    paramGui_bang(x);
}


static void paramGui_free(t_paramGui *x)
{
	if (x->childcanvas) {
		//post("Deleting it");
		pd_free((t_pd *)x->childcanvas);
	}
	x->childcanvas = NULL;
	
	if (x->receive) {
		pd_unbind(&x->x_obj.ob_pd,x->receive);
	}
}

/*
static void paramGui_properties(t_gobj*z, t_glist*owner) {
  t_iemguts_objlist*objs=objectsInCanvas((t_pd*)z);
   while(objs) {
    t_propertybang*x=(t_propertybang*)objs->obj;
    propertybang_bang(x);
    objs=objs->next;
  } 
}
*/

static void *paramGui_new(t_symbol *s, int ac, t_atom *av) {
  t_paramGui *x = (t_paramGui *)pd_new(paramGui_class);
  
  
  x->build = 1;
  
  
  
  char buf[MAXPDSTRING];
  sprintf(buf, "#%lx", (long)x);
  x->receive = gensym(buf);
  pd_bind(&x->x_obj.ob_pd, x->receive );
  
  x->waiting = 0;
  
  t_canvas* currentcanvas = tof_get_canvas();
  
  x->root = tof_get_dollarzero(tof_get_root_canvas(currentcanvas));
  
  
  x->path = param_get_path(currentcanvas, NULL);
  x->path_l = strlen(x->path->s_name);
  // Prepend $0 to path 
  t_symbol* dollarzero = tof_get_dollarzero(currentcanvas);
  int zeropath_len = strlen(dollarzero->s_name)+strlen(x->path->s_name)+1;
  char* zeropath = getbytes(zeropath_len * sizeof(* zeropath));
  strcpy(zeropath,dollarzero->s_name);
  strcat(zeropath,x->path->s_name);
  t_symbol* fullpath = gensym(zeropath);
  freebytes(zeropath,zeropath_len * sizeof(* zeropath));
  
  //post("path: %s",x->path->s_name);
  
  // create a new canvas
    
    
  t_atom a;
  SETSYMBOL(&a, fullpath);
  pd_typedmess(&pd_objectmaker,gensym("pd"),1,&a);
  
    // From this point on, we are hoping the "pd" object has been created
    x->childcanvas = (t_canvas*) pd_newest();
   
	
    // Hide the window (stupid way of doing this)
    if (x->childcanvas) {
		SETFLOAT(&a,0);
		pd_typedmess((t_pd*)x->childcanvas,gensym("vis"),1,&a);
    }
	
   
   inlet_new(&x->x_obj, &x->x_obj.ob_pd,&s_bang, gensym("reset"));

  

  // SET THE PROPERTIES FUNCTION
  //t_class *class = ((t_gobj*)currentcanvas)->g_pd;
  //class_setpropertiesfn(class, propertybang_properties);

    
  return (x);
}

/*
static void create_motion_proc(void)
{
    //sys_gui("if { ![::tof_param_class::proc_test motion]} {\n");
    sys_gui ("  proc ::tof_param_class::motion {x y} {\n");
    //sys_gui ("    if { $x != $::hcs_cursor_class::last_x \\\n");
    //sys_gui ("      || $y != $::hcs_cursor_class::last_y} {\n");
    sys_vgui("        pd [concat %s motion $x $y \\;]\n",
             cursor_receive_symbol->s_name);
    //sys_gui ("        set ::hcs_cursor_class::last_x $x\n");
    //sys_gui ("        set ::hcs_cursor_class::last_y $y\n");
    sys_gui ("    }\n");
    //sys_gui ("  }\n");
    //sys_gui ("}\n");
}
*/

void paramGui_setup(void) {
  paramGui_class = class_new(gensym("param gui"),
    (t_newmethod)paramGui_new, (t_method)paramGui_free,
    sizeof(t_paramGui), 0, A_GIMME, 0);

 class_addbang(paramGui_class, paramGui_bang);
 //class_addsymbol(paramGui_class, paramGui_symbol);
 
 // The mouse position callback
 class_addmethod(paramGui_class, (t_method)paramGui_motion_callback,\
                     gensym("motion"), A_DEFFLOAT, A_DEFFLOAT, 0);
 
 class_addmethod(paramGui_class, (t_method) paramGui_reset, gensym("reset"), 0);
 
 
 class_sethelpsymbol(paramGui_class,gensym("param"));
 
  //create_motion_proc();
 
}