soc-2008-mxcurioni: merged changes to revision 15705
[blender.git] / source / blender / src / drawnode.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2005 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): David Millan Escriva, Juho Vepsäläinen
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "BLI_blenlib.h"
35 #include "BLI_arithb.h"
36
37 #include "DNA_action_types.h"
38 #include "DNA_color_types.h"
39 #include "DNA_customdata_types.h"
40 #include "DNA_gpencil_types.h"
41 #include "DNA_ipo_types.h"
42 #include "DNA_ID.h"
43 #include "DNA_image_types.h"
44 #include "DNA_material_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_node_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_space_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_texture_types.h"
52 #include "DNA_text_types.h"
53 #include "DNA_userdef_types.h"
54
55 #include "BKE_global.h"
56 #include "BKE_image.h"
57 #include "BKE_library.h"
58 #include "BKE_main.h"
59 #include "BKE_material.h"
60 #include "BKE_node.h"
61 #include "BKE_object.h"
62 #include "BKE_texture.h"
63 #include "BKE_text.h"
64 #include "BKE_utildefines.h"
65
66 #include "CMP_node.h"
67 #include "SHD_node.h"
68
69 #include "BDR_gpencil.h"
70
71 #include "BIF_gl.h"
72 #include "BIF_glutil.h"
73 #include "BIF_drawgpencil.h"
74 #include "BIF_interface.h"
75 #include "BIF_interface_icons.h"
76 #include "BIF_language.h"
77 #include "BIF_mywindow.h"
78 #include "BIF_previewrender.h"
79 #include "BIF_resources.h"
80 #include "BIF_screen.h"
81 #include "BIF_space.h"
82
83 #include "BSE_drawipo.h"
84 #include "BSE_node.h"
85 #include "BSE_view.h"
86
87 #include "BMF_Api.h"
88
89 #include "MEM_guardedalloc.h"
90
91 #include "RE_pipeline.h"
92 #include "IMB_imbuf_types.h"
93
94 #include "blendef.h"
95 #include "butspace.h"
96 #include "interface.h"  /* urm...  for rasterpos_safe, roundbox */
97 #include "mydevice.h"
98
99 extern void autocomplete_uv(char *str, void *arg_v);
100 extern int verify_valid_uv_name(char *str);
101
102 /* autocomplete callback for buttons */
103 static void autocomplete_vcol(char *str, void *arg_v)
104 {
105         Mesh *me;
106         CustomDataLayer *layer;
107         AutoComplete *autocpl;
108         int a;
109
110         if(str[0]==0)
111                 return;
112
113         autocpl= autocomplete_begin(str, 32);
114                 
115         /* search if str matches the beginning of name */
116         for(me= G.main->mesh.first; me; me=me->id.next)
117                 for(a=0, layer= me->fdata.layers; a<me->fdata.totlayer; a++, layer++)
118                         if(layer->type == CD_MCOL)
119                                 autocomplete_do_name(autocpl, layer->name);
120         
121         autocomplete_end(autocpl, str);
122 }
123
124 static int verify_valid_vcol_name(char *str)
125 {
126         Mesh *me;
127         CustomDataLayer *layer;
128         int a;
129         
130         if(str[0]==0)
131                 return 1;
132
133         /* search if str matches the name */
134         for(me= G.main->mesh.first; me; me=me->id.next)
135                 for(a=0, layer= me->fdata.layers; a<me->fdata.totlayer; a++, layer++)
136                         if(layer->type == CD_MCOL)
137                                 if(strcmp(layer->name, str)==0)
138                                         return 1;
139         
140         return 0;
141 }
142
143 static void snode_drawstring(SpaceNode *snode, char *str, int okwidth)
144 {
145         char drawstr[NODE_MAXSTR];
146         int width;
147         
148         if(str[0]==0 || okwidth<4) return;
149         
150         BLI_strncpy(drawstr, str, NODE_MAXSTR);
151         width= snode->aspect*BIF_GetStringWidth(snode->curfont, drawstr, 0);
152
153         if(width > okwidth) {
154                 int len= strlen(drawstr)-1;
155                 
156                 while(width > okwidth && len>=0) {
157                         drawstr[len]= 0;
158                         
159                         width= snode->aspect*BIF_GetStringWidth(snode->curfont, drawstr, 0);
160                         len--;
161                 }
162                 if(len==0) return;
163         }
164         BIF_DrawString(snode->curfont, drawstr, 0);
165
166 }
167
168 /* **************  Socket callbacks *********** */
169
170 static void socket_vector_menu_cb(void *node_v, void *ntree_v)
171 {
172         if(node_v && ntree_v) {
173                 NodeTagChanged(ntree_v, node_v); 
174                 addqueue(curarea->win, UI_BUT_EVENT, B_NODE_EXEC+((bNode *)node_v)->nr);
175         }
176 }
177
178 /* NOTE: this is a block-menu, needs 0 events, otherwise the menu closes */
179 static uiBlock *socket_vector_menu(void *socket_v)
180 {
181         SpaceNode *snode= curarea->spacedata.first;
182         bNode *node;
183         bNodeSocket *sock= socket_v;
184         bNodeStack *ns= &sock->ns;
185         uiBlock *block;
186         uiBut *bt;
187         
188         /* a bit ugly... retrieve the node the socket comes from */
189         for(node= snode->nodetree->nodes.first; node; node= node->next) {
190                 bNodeSocket *sockt;
191                 for(sockt= node->inputs.first; sockt; sockt= sockt->next)
192                         if(sockt==sock)
193                                 break;
194                 if(sockt)
195                         break;
196         }
197         
198         block= uiNewBlock(&curarea->uiblocks, "socket menu", UI_EMBOSS, UI_HELV, curarea->win);
199
200         /* use this for a fake extra empy space around the buttons */
201         uiDefBut(block, LABEL, 0, "",                   -4, -4, 188, 68, NULL, 0, 0, 0, 0, "");
202         
203         uiBlockBeginAlign(block);
204         bt= uiDefButF(block, NUMSLI, 0, "X ",    0,40,180,20, ns->vec, ns->min, ns->max, 10, 0, "");
205         uiButSetFunc(bt, socket_vector_menu_cb, node, snode->nodetree);
206         bt= uiDefButF(block, NUMSLI, 0, "Y ",    0,20,180,20, ns->vec+1, ns->min, ns->max, 10, 0, "");
207         uiButSetFunc(bt, socket_vector_menu_cb, node, snode->nodetree);
208         bt= uiDefButF(block, NUMSLI, 0, "Z ",    0,0,180,20, ns->vec+2, ns->min, ns->max, 10, 0, "");
209         uiButSetFunc(bt, socket_vector_menu_cb, node, snode->nodetree);
210         
211         uiBlockSetDirection(block, UI_TOP);
212         
213         allqueue(REDRAWNODE, 0);
214         
215         return block;
216 }
217
218 static void node_sync_cb(void *snode_v, void *node_v)
219 {
220         SpaceNode *snode= snode_v;
221         
222         if(snode->treetype==NTREE_SHADER) {
223                 nodeShaderSynchronizeID(node_v, 1);
224                 allqueue(REDRAWBUTSSHADING, 0);
225         }
226 }
227
228 /* ****************** GENERAL CALLBACKS FOR NODES ***************** */
229
230 static void node_ID_title_cb(void *node_v, void *unused_v)
231 {
232         bNode *node= node_v;
233         
234         if(node->id) {
235                 test_idbutton(node->id->name+2);        /* library.c, verifies unique name */
236                 BLI_strncpy(node->name, node->id->name+2, 21);
237                 
238                 allqueue(REDRAWBUTSSHADING, 0);
239                 allqueue(REDRAWNODE, 0);
240                 allqueue(REDRAWOOPS, 0);
241         }
242 }
243
244
245 static void node_but_title_cb(void *node_v, void *but_v)
246 {
247         bNode *node= node_v;
248         uiBut *bt= but_v;
249         BLI_strncpy(node->name, bt->drawstr, NODE_MAXSTR);
250         
251         allqueue(REDRAWNODE, 0);
252 }
253
254 static void node_group_alone_cb(void *node_v, void *unused_v)
255 {
256         bNode *node= node_v;
257         
258         nodeCopyGroup(node);
259
260         allqueue(REDRAWNODE, 0);
261 }
262
263 /* ****************** BUTTON CALLBACKS FOR ALL TREES ***************** */
264
265 static int node_buts_group(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
266 {
267         if(block && node->id) {
268                 uiBut *bt;
269                 short width;
270                 
271                 uiBlockBeginAlign(block);
272                 
273                 /* name button */
274                 width= (short)(butr->xmax-butr->xmin - (node->id->us>1?19.0f:0.0f));
275                 bt= uiDefBut(block, TEX, B_NOP, "NT:",
276                                          butr->xmin, butr->ymin, width, 19, 
277                                          node->id->name+2, 0.0, 19.0, 0, 0, "NodeTree name");
278                 uiButSetFunc(bt, node_ID_title_cb, node, NULL);
279                 
280                 /* user amount */
281                 if(node->id->us>1) {
282                         char str1[32];
283                         sprintf(str1, "%d", node->id->us);
284                         bt= uiDefBut(block, BUT, B_NOP, str1, 
285                                                  butr->xmax-19, butr->ymin, 19, 19, 
286                                                  NULL, 0, 0, 0, 0, "Displays number of users.");
287                         uiButSetFunc(bt, node_group_alone_cb, node, NULL);
288                 }
289                 
290                 uiBlockEndAlign(block);
291         }       
292         return 19;
293 }
294
295 static int node_buts_value(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
296 {
297         if(block) {
298                 bNodeSocket *sock= node->outputs.first;         /* first socket stores value */
299                 
300                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", 
301                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
302                                   sock->ns.vec, sock->ns.min, sock->ns.max, 10, 2, "");
303                 
304         }
305         return 20;
306 }
307
308 static int node_buts_rgb(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
309 {
310         if(block) {
311                 bNodeSocket *sock= node->outputs.first;         /* first socket stores value */
312                 if(sock) {
313                         /* enforce square box drawing */
314                         uiBlockSetEmboss(block, UI_EMBOSSP);
315                         
316                         uiDefButF(block, HSVCUBE, B_NODE_EXEC+node->nr, "", 
317                                           butr->xmin, butr->ymin, butr->xmax-butr->xmin, 12, 
318                                           sock->ns.vec, 0.0f, 1.0f, 3, 0, "");
319                         uiDefButF(block, HSVCUBE, B_NODE_EXEC+node->nr, "", 
320                                           butr->xmin, butr->ymin+15, butr->xmax-butr->xmin, butr->ymax-butr->ymin -15 -15, 
321                                           sock->ns.vec, 0.0f, 1.0f, 2, 0, "");
322                         uiDefButF(block, COL, B_NOP, "",                
323                                           butr->xmin, butr->ymax-12, butr->xmax-butr->xmin, 12, 
324                                           sock->ns.vec, 0.0, 0.0, -1, 0, "");
325                         /* the -1 above prevents col button to popup a color picker */
326                         
327                         uiBlockSetEmboss(block, UI_EMBOSS);
328                 }
329         }
330         return 30 + (int)(node->width-NODE_DY);
331 }
332
333 static int node_buts_mix_rgb(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
334 {
335         if(block) {
336                 uiBut *bt;
337                 int a_but= (ntree->type==NTREE_COMPOSIT);
338                 
339                 /* blend type */
340                 uiBlockBeginAlign(block);
341                 bt=uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Mix %x0|Add %x1|Subtract %x3|Multiply %x2|Screen %x4|Overlay %x9|Divide %x5|Difference %x6|Darken %x7|Lighten %x8|Dodge %x10|Burn %x11|Color %x15|Value %x14|Saturation %x13|Hue %x12",
342                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin -(a_but?20:0), 20, 
343                                          &node->custom1, 0, 0, 0, 0, "");
344                 uiButSetFunc(bt, node_but_title_cb, node, bt);
345                 /* Alpha option, composite */
346                 if(a_but)
347                         uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "A",
348                                   butr->xmax-20, butr->ymin, 20, 20, 
349                                   &node->custom2, 0, 0, 0, 0, "Include Alpha of 2nd input in this operation");
350         }
351         return 20;
352 }
353
354 static int node_buts_time(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
355 {
356         if(block) {
357                 CurveMapping *cumap= node->storage;
358                 short dx= (short)((butr->xmax-butr->xmin)/2);
359                 butr->ymin += 26;
360
361                 curvemap_buttons(block, node->storage, 's', B_NODE_EXEC+node->nr, B_REDR, butr);
362                 
363                 if(cumap) {
364                         cumap->flag |= CUMA_DRAW_CFRA;
365                         if(node->custom1<node->custom2)
366                                 cumap->sample[0]= (float)(CFRA - node->custom1)/(float)(node->custom2-node->custom1);
367                 }
368
369                 uiBlockBeginAlign(block);
370                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Sta:",
371                                   butr->xmin, butr->ymin-22, dx, 19, 
372                                   &node->custom1, 1.0, 20000.0, 0, 0, "Start frame");
373                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "End:",
374                                   butr->xmin+dx, butr->ymin-22, dx, 19, 
375                                   &node->custom2, 1.0, 20000.0, 0, 0, "End frame");
376         }
377         
378         return node->width-NODE_DY;
379 }
380
381 static int node_buts_valtorgb(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
382 {
383         if(block) {
384                 if(node->storage) {
385                         draw_colorband_buts_small(block, node->storage, butr, B_NODE_EXEC+node->nr);
386                 }
387         }
388         return 40;
389 }
390
391 static int node_buts_curvevec(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
392 {
393         if(block) {
394                 curvemap_buttons(block, node->storage, 'v', B_NODE_EXEC+node->nr, B_REDR, butr);
395         }       
396         return (int)(node->width-NODE_DY);
397 }
398
399 static float *_sample_col= NULL;        // bad bad, 2.5 will do better?
400 void node_curvemap_sample(float *col)
401 {
402         _sample_col= col;
403 }
404
405 static int node_buts_curvecol(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
406 {
407         if(block) {
408                 CurveMapping *cumap= node->storage;
409                 if(_sample_col) {
410                         cumap->flag |= CUMA_DRAW_SAMPLE;
411                         VECCOPY(cumap->sample, _sample_col);
412                 }
413                 else 
414                         cumap->flag &= ~CUMA_DRAW_SAMPLE;
415
416                 curvemap_buttons(block, node->storage, 'c', B_NODE_EXEC+node->nr, B_REDR, butr);
417         }       
418         return (int)(node->width-NODE_DY);
419 }
420
421 static int node_buts_normal(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
422 {
423         if(block) {
424                 bNodeSocket *sock= node->outputs.first;         /* first socket stores normal */
425                 
426                 uiDefButF(block, BUT_NORMAL, B_NODE_EXEC+node->nr, "", 
427                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, butr->ymax-butr->ymin, 
428                                   sock->ns.vec, 0.0f, 1.0f, 0, 0, "");
429                 
430         }       
431         return (int)(node->width-NODE_DY);
432 }
433
434 static void node_browse_tex_cb(void *ntree_v, void *node_v)
435 {
436         bNodeTree *ntree= ntree_v;
437         bNode *node= node_v;
438         Tex *tex;
439         
440         if(node->menunr<1) return;
441         
442         if(node->id) {
443                 node->id->us--;
444                 node->id= NULL;
445         }
446         tex= BLI_findlink(&G.main->tex, node->menunr-1);
447
448         node->id= &tex->id;
449         id_us_plus(node->id);
450         BLI_strncpy(node->name, node->id->name+2, 21);
451         
452         nodeSetActive(ntree, node);
453         
454         allqueue(REDRAWBUTSSHADING, 0);
455         allqueue(REDRAWNODE, 0);
456         NodeTagChanged(ntree, node); 
457         
458         node->menunr= 0;
459 }
460
461 static void node_dynamic_update_cb(void *ntree_v, void *node_v)
462 {
463         Material *ma;
464         bNode *node= (bNode *)node_v;
465         ID *id= node->id;
466         int error= 0;
467
468         if (BTST(node->custom1, NODE_DYNAMIC_ERROR)) error= 1;
469
470         /* Users only have to press the "update" button in one pynode
471          * and we also update all others sharing the same script */
472         for (ma= G.main->mat.first; ma; ma= ma->id.next) {
473                 if (ma->nodetree) {
474                         bNode *nd;
475                         for (nd= ma->nodetree->nodes.first; nd; nd= nd->next) {
476                                 if ((nd->type == NODE_DYNAMIC) && (nd->id == id)) {
477                                         nd->custom1= 0;
478                                         nd->custom1= BSET(nd->custom1, NODE_DYNAMIC_REPARSE);
479                                         nd->menunr= 0;
480                                         if (error)
481                                                 nd->custom1= BSET(nd->custom1, NODE_DYNAMIC_ERROR);
482                                 }
483                         }
484                 }
485         }
486
487         allqueue(REDRAWBUTSSHADING, 0);
488         allqueue(REDRAWNODE, 0);
489         BIF_preview_changed(ID_MA);
490 }
491
492 static int node_buts_texture(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
493 {
494         if(block) {
495                 uiBut *bt;
496                 char *strp;
497                 
498                 /* browse button texture */
499                 uiBlockBeginAlign(block);
500                 IDnames_to_pupstring(&strp, NULL, "", &(G.main->tex), NULL, NULL);
501                 node->menunr= 0;
502                 bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, strp, 
503                                           butr->xmin, butr->ymin, 20, 19, 
504                                           &node->menunr, 0, 0, 0, 0, "Browse texture");
505                 uiButSetFunc(bt, node_browse_tex_cb, ntree, node);
506                 if(strp) MEM_freeN(strp);
507                 
508                 if(node->id) {
509                         bt= uiDefBut(block, TEX, B_NOP, "TE:",
510                                                  butr->xmin+19, butr->ymin, butr->xmax-butr->xmin-19, 19, 
511                                                  node->id->name+2, 0.0, 19.0, 0, 0, "Texture name");
512                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
513                 }
514                 
515         }       
516         return 19;
517 }
518
519 static int node_buts_math(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
520
521         if(block) { 
522                 uiBut *bt; 
523
524                 bt=uiDefButS(block, MENU, B_NODE_EXEC,  "Add %x0|Subtract %x1|Multiply %x2|Divide %x3|Sine %x4|Cosine %x5|Tangent %x6|Arcsine %x7|Arccosine %x8|Arctangent %x9|Power %x10|Logarithm %x11|Minimum %x12|Maximum %x13|Round %x14|Less Than %x15|Greater Than %x16", butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, &node->custom1, 0, 0, 0, 0, ""); 
525                 uiButSetFunc(bt, node_but_title_cb, node, bt); 
526         } 
527         return 20; 
528 }
529
530
531 /* ****************** BUTTON CALLBACKS FOR SHADER NODES ***************** */
532
533 static void node_browse_text_cb(void *ntree_v, void *node_v)
534 {
535         bNodeTree *ntree= ntree_v;
536         bNode *node= node_v;
537         ID *oldid;
538         
539         if(node->menunr<1) return;
540         
541         if(node->id) {
542                 node->id->us--;
543         }
544         oldid= node->id;
545         node->id= BLI_findlink(&G.main->text, node->menunr-1);
546         id_us_plus(node->id);
547         BLI_strncpy(node->name, node->id->name+2, 21); /* huh? why 21? */
548
549         node->custom1= BSET(node->custom1, NODE_DYNAMIC_NEW);
550         
551         nodeSetActive(ntree, node);
552
553         allqueue(REDRAWBUTSSHADING, 0);
554         allqueue(REDRAWNODE, 0);
555
556         node->menunr= 0;
557 }
558
559 static void node_mat_alone_cb(void *node_v, void *unused)
560 {
561         bNode *node= node_v;
562         
563         node->id= (ID *)copy_material((Material *)node->id);
564         
565         BIF_undo_push("Single user material");
566         allqueue(REDRAWBUTSSHADING, 0);
567         allqueue(REDRAWNODE, 0);
568         allqueue(REDRAWOOPS, 0);
569 }
570
571 static void node_browse_mat_cb(void *ntree_v, void *node_v)
572 {
573         bNodeTree *ntree= ntree_v;
574         bNode *node= node_v;
575         
576         if(node->menunr<1) return;
577         
578         if(node->menunr==32767) {       /* code for Add New */
579                 if(node->id) {
580                         /* make copy, but make sure it doesnt have the node tag nor nodes */
581                         Material *ma= (Material *)node->id;
582                         ma->id.us--;
583                         ma= copy_material(ma);
584                         ma->use_nodes= 0;
585                         if(ma->nodetree) {
586                                 ntreeFreeTree(ma->nodetree);
587                                 MEM_freeN(ma->nodetree);
588                         }
589                         ma->nodetree= NULL;
590                         node->id= (ID *)ma;
591                 }
592                 else node->id= (ID *)add_material("MatNode");
593         }
594         else {
595                 if(node->id) node->id->us--;
596                 node->id= BLI_findlink(&G.main->mat, node->menunr-1);
597                 id_us_plus(node->id);
598         }
599         BLI_strncpy(node->name, node->id->name+2, 21);
600         
601         nodeSetActive(ntree, node);
602
603         allqueue(REDRAWBUTSSHADING, 0);
604         allqueue(REDRAWNODE, 0);
605         BIF_preview_changed(ID_MA);
606
607         node->menunr= 0;
608 }
609
610 static void node_new_mat_cb(void *ntree_v, void *node_v)
611 {
612         bNodeTree *ntree= ntree_v;
613         bNode *node= node_v;
614         
615         node->id= (ID *)add_material("MatNode");
616         BLI_strncpy(node->name, node->id->name+2, 21);
617
618         nodeSetActive(ntree, node);
619
620         allqueue(REDRAWBUTSSHADING, 0);
621         allqueue(REDRAWNODE, 0);
622         BIF_preview_changed(ID_MA);
623
624 }
625
626 static void node_texmap_cb(void *texmap_v, void *unused_v)
627 {
628         init_mapping(texmap_v);
629 }
630
631 static int node_shader_buts_material(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
632 {
633         if(block) {
634                 uiBut *bt;
635                 short dx= (short)((butr->xmax-butr->xmin)/3.0f), has_us= (node->id && node->id->us>1);
636                 short dy= (short)butr->ymin;
637                 char *strp;
638                 
639                 /* WATCH IT: we use this callback in material buttons, but then only want first row */
640                 if(butr->ymax-butr->ymin > 21.0f) dy+= 19;
641                 
642                 uiBlockBeginAlign(block);
643                 if(node->id==NULL) uiBlockSetCol(block, TH_REDALERT);
644                 else if(has_us) uiBlockSetCol(block, TH_BUT_SETTING1);
645                 else uiBlockSetCol(block, TH_BUT_SETTING2);
646                 
647                 /* browse button */
648                 IDnames_to_pupstring(&strp, NULL, "ADD NEW %x32767", &(G.main->mat), NULL, NULL);
649                 node->menunr= 0;
650                 bt= uiDefButS(block, MENU, B_NOP, strp, 
651                                   butr->xmin, dy, 19, 19, 
652                                   &node->menunr, 0, 0, 0, 0, "Browses existing choices or adds NEW");
653                 uiButSetFunc(bt, node_browse_mat_cb, ntree, node);
654                 if(strp) MEM_freeN(strp);
655                 
656                 /* Add New button */
657                 if(node->id==NULL) {
658                         bt= uiDefBut(block, BUT, B_NOP, "Add New",
659                                                  butr->xmin+19, dy, (short)(butr->xmax-butr->xmin-19.0f), 19, 
660                                                  NULL, 0.0, 0.0, 0, 0, "Add new Material");
661                         uiButSetFunc(bt, node_new_mat_cb, ntree, node);
662                         uiBlockSetCol(block, TH_AUTO);
663                 }
664                 else {
665                         /* name button */
666                         short width= (short)(butr->xmax-butr->xmin-19.0f - (has_us?19.0f:0.0f));
667                         bt= uiDefBut(block, TEX, B_NOP, "MA:",
668                                                   butr->xmin+19, dy, width, 19, 
669                                                   node->id->name+2, 0.0, 19.0, 0, 0, "Material name");
670                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
671                         
672                         /* user amount */
673                         if(has_us) {
674                                 char str1[32];
675                                 sprintf(str1, "%d", node->id->us);
676                                 bt= uiDefBut(block, BUT, B_NOP, str1, 
677                                                           butr->xmax-19, dy, 19, 19, 
678                                                           NULL, 0, 0, 0, 0, "Displays number of users. Click to make a single-user copy.");
679                                 uiButSetFunc(bt, node_mat_alone_cb, node, NULL);
680                         }
681                         
682                         /* WATCH IT: we use this callback in material buttons, but then only want first row */
683                         if(butr->ymax-butr->ymin > 21.0f) {
684                                 /* node options */
685                                 uiBlockSetCol(block, TH_AUTO);
686                                 uiDefButBitS(block, TOG, SH_NODE_MAT_DIFF, B_NODE_EXEC+node->nr, "Diff",
687                                                          butr->xmin, butr->ymin, dx, 19, 
688                                                          &node->custom1, 0, 0, 0, 0, "Material Node outputs Diffuse");
689                                 uiDefButBitS(block, TOG, SH_NODE_MAT_SPEC, B_NODE_EXEC+node->nr, "Spec",
690                                                          butr->xmin+dx, butr->ymin, dx, 19, 
691                                                          &node->custom1, 0, 0, 0, 0, "Material Node outputs Specular");
692                                 uiDefButBitS(block, TOG, SH_NODE_MAT_NEG, B_NODE_EXEC+node->nr, "Neg Normal",
693                                                          butr->xmax-dx, butr->ymin, dx, 19,
694                                                          &node->custom1, 0, 0, 0, 0, "Material Node uses inverted Normal");
695                         }
696                 }
697                 uiBlockEndAlign(block);
698         }       
699         return 38;
700 }
701
702 static int node_shader_buts_mapping(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
703 {
704         if(block) {
705                 TexMapping *texmap= node->storage;
706                 short dx= (short)((butr->xmax-butr->xmin)/7.0f);
707                 short dy= (short)(butr->ymax-19);
708                 
709                 uiBlockSetFunc(block, node_texmap_cb, texmap, NULL);    /* all buttons get this */
710                 
711                 uiBlockBeginAlign(block);
712                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->loc, -1000.0f, 1000.0f, 10, 2, "");
713                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->loc+1, -1000.0f, 1000.0f, 10, 2, "");
714                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->loc+2, -1000.0f, 1000.0f, 10, 2, "");
715                 dy-= 19;
716                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->rot, -1000.0f, 1000.0f, 1000, 1, "");
717                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->rot+1, -1000.0f, 1000.0f, 1000, 1, "");
718                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->rot+2, -1000.0f, 1000.0f, 1000, 1, "");
719                 dy-= 19;
720                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->size, -1000.0f, 1000.0f, 10, 2, "");
721                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->size+1, -1000.0f, 1000.0f, 10, 2, "");
722                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->size+2, -1000.0f, 1000.0f, 10, 2, "");
723                 dy-= 25;
724                 uiBlockBeginAlign(block);
725                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->min, -10.0f, 10.0f, 100, 2, "");
726                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->min+1, -10.0f, 10.0f, 100, 2, "");
727                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->min+2, -10.0f, 10.0f, 100, 2, "");
728                 dy-= 19;
729                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->max, -10.0f, 10.0f, 10, 2, "");
730                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->max+1, -10.0f, 10.0f, 10, 2, "");
731                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->max+2, -10.0f, 10.0f, 10, 2, "");
732                 uiBlockEndAlign(block);
733                 
734                 /* labels/options */
735                 
736                 dy= (short)(butr->ymax-19);
737                 uiDefBut(block, LABEL, B_NOP, "Loc", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
738                 dy-= 19;
739                 uiDefBut(block, LABEL, B_NOP, "Rot", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
740                 dy-= 19;
741                 uiDefBut(block, LABEL, B_NOP, "Size", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
742                 dy-= 25;
743                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MIN, B_NODE_EXEC+node->nr, "Min", butr->xmin, dy, dx-4, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
744                 dy-= 19;
745                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MAX, B_NODE_EXEC+node->nr, "Max", butr->xmin, dy, dx-4, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
746                 
747         }       
748         return 5*19 + 6;
749 }
750
751 static int node_shader_buts_vect_math(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
752
753         if(block) { 
754                 uiBut *bt; 
755         
756                 bt=uiDefButS(block, MENU, B_NODE_EXEC,  "Add %x0|Subtract %x1|Average %x2|Dot Product %x3 |Cross Product %x4|Normalize %x5", butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, &node->custom1, 0, 0, 0, 0, ""); 
757                 uiButSetFunc(bt, node_but_title_cb, node, bt); 
758         } 
759         return 20; 
760 }
761
762 static int node_shader_buts_geometry(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
763 {
764         if(block) {
765                 uiBut *but;
766                 NodeGeometry *ngeo= (NodeGeometry*)node->storage;
767
768                 if(!verify_valid_uv_name(ngeo->uvname))
769                         uiBlockSetCol(block, TH_REDALERT);
770                 but= uiDefBut(block, TEX, B_NODE_EXEC+node->nr, "UV:", butr->xmin, butr->ymin+20, butr->xmax-butr->xmin, 20, ngeo->uvname, 0, 31, 0, 0, "Set name of UV layer to use, default is active UV layer");
771                 uiButSetCompleteFunc(but, autocomplete_uv, NULL);
772                 uiBlockSetCol(block, TH_AUTO);
773
774                 if(!verify_valid_vcol_name(ngeo->colname))
775                         uiBlockSetCol(block, TH_REDALERT);
776                 but= uiDefBut(block, TEX, B_NODE_EXEC+node->nr, "Col:", butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, ngeo->colname, 0, 31, 0, 0, "Set name of vertex color layer to use, default is active vertex color layer");
777                 uiButSetCompleteFunc(but, autocomplete_vcol, NULL);
778                 uiBlockSetCol(block, TH_AUTO);
779         }
780
781         return 40;
782 }
783
784 static int node_shader_buts_dynamic(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
785
786         if (block) { 
787                 uiBut *bt;
788                 SpaceNode *snode= curarea->spacedata.first;
789                 short dy= (short)butr->ymin;
790                 int xoff=0;
791
792                 /* B_NODE_EXEC is handled in butspace.c do_node_buts */
793                 if(!node->id) {
794                                 char *strp;
795                                 IDnames_to_pupstring(&strp, NULL, "", &(G.main->text), NULL, NULL);
796                                 node->menunr= 0;
797                                 bt= uiDefButS(block, MENU, B_NODE_EXEC/*+node->nr*/, strp, 
798                                                                 butr->xmin, dy, 19, 19, 
799                                                                 &node->menunr, 0, 0, 0, 0, "Browses existing choices");
800                                 uiButSetFunc(bt, node_browse_text_cb, ntree, node);
801                                 xoff=19;
802                                 if(strp) MEM_freeN(strp);       
803                 }
804                 else {
805                         bt = uiDefBut(block, BUT, B_NOP, "Update",
806                                         butr->xmin+xoff, butr->ymin+20, 50, 19,
807                                         &node->menunr, 0.0, 19.0, 0, 0, "Refresh this node (and all others that use the same script)");
808                         uiButSetFunc(bt, node_dynamic_update_cb, ntree, node);
809
810                         if (BTST(node->custom1, NODE_DYNAMIC_ERROR)) {
811                                 BIF_ThemeColor(TH_REDALERT);
812                                 ui_rasterpos_safe(butr->xmin + xoff, butr->ymin + 5, snode->aspect);
813                                 snode_drawstring(snode, "Error! Check console...", butr->xmax - butr->xmin);
814                         }
815                 }
816         }
817         return 20+19; 
818 }
819
820 /* only once called */
821 static void node_shader_set_butfunc(bNodeType *ntype)
822 {
823         switch(ntype->type) {
824                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
825
826                 case SH_NODE_MATERIAL:
827                 case SH_NODE_MATERIAL_EXT:
828                         ntype->butfunc= node_shader_buts_material;
829                         break;
830                 case SH_NODE_TEXTURE:
831                         ntype->butfunc= node_buts_texture;
832                         break;
833                 case SH_NODE_NORMAL:
834                         ntype->butfunc= node_buts_normal;
835                         break;
836                 case SH_NODE_CURVE_VEC:
837                         ntype->butfunc= node_buts_curvevec;
838                         break;
839                 case SH_NODE_CURVE_RGB:
840                         ntype->butfunc= node_buts_curvecol;
841                         break;
842                 case SH_NODE_MAPPING:
843                         ntype->butfunc= node_shader_buts_mapping;
844                         break;
845                 case SH_NODE_VALUE:
846                         ntype->butfunc= node_buts_value;
847                         break;
848                 case SH_NODE_RGB:
849                         ntype->butfunc= node_buts_rgb;
850                         break;
851                 case SH_NODE_MIX_RGB:
852                         ntype->butfunc= node_buts_mix_rgb;
853                         break;
854                 case SH_NODE_VALTORGB:
855                         ntype->butfunc= node_buts_valtorgb;
856                         break;
857                 case SH_NODE_MATH: 
858                         ntype->butfunc= node_buts_math;
859                         break; 
860                 case SH_NODE_VECT_MATH: 
861                         ntype->butfunc= node_shader_buts_vect_math;
862                         break; 
863                 case SH_NODE_GEOMETRY:
864                         ntype->butfunc= node_shader_buts_geometry;
865                         break;
866                 case NODE_DYNAMIC:
867                         ntype->butfunc= node_shader_buts_dynamic;
868                         break;
869                 default:
870                         ntype->butfunc= NULL;
871         }
872 }
873
874 /* ****************** BUTTON CALLBACKS FOR COMPOSITE NODES ***************** */
875
876
877
878 static void node_browse_image_cb(void *ntree_v, void *node_v)
879 {
880         bNodeTree *ntree= ntree_v;
881         bNode *node= node_v;
882         
883         nodeSetActive(ntree, node);
884         
885         if(node->menunr<1) return;
886         if(node->menunr==32767) {       /* code for Load New */
887                 addqueue(curarea->win, UI_BUT_EVENT, B_NODE_LOADIMAGE);
888         }
889         else {
890                 if(node->id) node->id->us--;
891                 node->id= BLI_findlink(&G.main->image, node->menunr-1);
892                 id_us_plus(node->id);
893
894                 BLI_strncpy(node->name, node->id->name+2, 21);
895
896                 NodeTagChanged(ntree, node); 
897                 BKE_image_signal((Image *)node->id, node->storage, IMA_SIGNAL_USER_NEW_IMAGE);
898                 addqueue(curarea->win, UI_BUT_EVENT, B_NODE_EXEC+node->nr);
899         }
900         node->menunr= 0;
901 }
902
903 static void node_active_cb(void *ntree_v, void *node_v)
904 {
905         nodeSetActive(ntree_v, node_v);
906 }
907 static void node_image_type_cb(void *node_v, void *unused)
908 {
909         
910         allqueue(REDRAWNODE, 1);
911 }
912
913 static char *node_image_type_pup(void)
914 {
915         char *str= MEM_mallocN(256, "image type pup");
916         int a;
917         
918         str[0]= 0;
919         
920         a= sprintf(str, "Image Type %%t|");
921         a+= sprintf(str+a, "  Image %%x%d %%i%d|", IMA_SRC_FILE, ICON_IMAGE_DEHLT);
922         a+= sprintf(str+a, "  Movie %%x%d %%i%d|", IMA_SRC_MOVIE, ICON_SEQUENCE);
923         a+= sprintf(str+a, "  Sequence %%x%d %%i%d|", IMA_SRC_SEQUENCE, ICON_IMAGE_COL);
924         a+= sprintf(str+a, "  Generated %%x%d %%i%d", IMA_SRC_GENERATED, ICON_BLANK1);
925         
926         return str;
927 }
928
929 /* copy from buttons_shading.c */
930 static char *layer_menu(RenderResult *rr)
931 {
932         RenderLayer *rl;
933         int len= 40 + 40*BLI_countlist(&rr->layers);
934         short a, nr;
935         char *str= MEM_callocN(len, "menu layers");
936         
937         strcpy(str, "Layer %t");
938         a= strlen(str);
939         for(nr=0, rl= rr->layers.first; rl; rl= rl->next, nr++) {
940                 a+= sprintf(str+a, "|%s %%x%d", rl->name, nr);
941         }
942         
943         return str;
944 }
945
946 static void image_layer_cb(void *ima_v, void *iuser_v)
947 {
948         
949         ntreeCompositForceHidden(G.scene->nodetree);
950         BKE_image_multilayer_index(ima_v, iuser_v);
951         allqueue(REDRAWNODE, 0);
952 }
953
954 static int node_composit_buts_image(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
955 {
956         ImageUser *iuser= node->storage;
957         
958         if(block) {
959                 uiBut *bt;
960                 short dy= (short)butr->ymax-19;
961                 char *strp;
962                 
963                 uiBlockBeginAlign(block);
964                 uiBlockSetCol(block, TH_BUT_SETTING2);
965                 
966                 /* browse button */
967                 IMAnames_to_pupstring(&strp, NULL, "LOAD NEW %x32767", &(G.main->image), NULL, NULL);
968                 node->menunr= 0;
969                 bt= uiDefButS(block, MENU, B_NOP, strp, 
970                                           butr->xmin, dy, 19, 19, 
971                                           &node->menunr, 0, 0, 0, 0, "Browses existing choices");
972                 uiButSetFunc(bt, node_browse_image_cb, ntree, node);
973                 if(strp) MEM_freeN(strp);
974                 
975                 /* Add New button */
976                 if(node->id==NULL) {
977                         bt= uiDefBut(block, BUT, B_NODE_LOADIMAGE, "Load New",
978                                                  butr->xmin+19, dy, (short)(butr->xmax-butr->xmin-19.0f), 19, 
979                                                  NULL, 0.0, 0.0, 0, 0, "Add new Image");
980                         uiButSetFunc(bt, node_active_cb, ntree, node);
981                         uiBlockSetCol(block, TH_AUTO);
982                 }
983                 else {
984                         /* name button + type */
985                         Image *ima= (Image *)node->id;
986                         short xmin= (short)butr->xmin, xmax= (short)butr->xmax;
987                         short width= xmax - xmin - 45;
988                         short icon= ICON_IMAGE_DEHLT;
989                         
990                         if(ima->source==IMA_SRC_MOVIE) icon= ICON_SEQUENCE;
991                         else if(ima->source==IMA_SRC_SEQUENCE) icon= ICON_IMAGE_COL;
992                         else if(ima->source==IMA_SRC_GENERATED) icon= ICON_BLANK1;
993                         
994                         bt= uiDefBut(block, TEX, B_NOP, "IM:",
995                                                  xmin+19, dy, width, 19, 
996                                                  node->id->name+2, 0.0, 19.0, 0, 0, "Image name");
997                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
998                         
999                         /* buffer type option */
1000                         strp= node_image_type_pup();
1001                         bt= uiDefIconTextButS(block, MENU, B_NOP, icon, strp,
1002                                                  xmax-26, dy, 26, 19, 
1003                                                  &ima->source, 0.0, 19.0, 0, 0, "Image type");
1004                         uiButSetFunc(bt, node_image_type_cb, node, ima);
1005                         MEM_freeN(strp);
1006                         
1007                         if( ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE) ) {
1008                                 width= (xmax-xmin)/2;
1009                                 
1010                                 dy-= 19;
1011                                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "Frs:",
1012                                                   xmin, dy, width, 19, 
1013                                                   &iuser->frames, 1.0, MAXFRAMEF, 0, 0, "Amount of images used in animation");
1014                                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "SFra:",
1015                                                   xmin+width, dy, width, 19, 
1016                                                   &iuser->sfra, 1.0, MAXFRAMEF, 0, 0, "Start frame of animation");
1017                                 dy-= 19;
1018                                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "Offs:",
1019                                                   xmin, dy, width, 19, 
1020                                                   &iuser->offset, -MAXFRAMEF, MAXFRAMEF, 0, 0, "Offsets the number of the frame to use in the animation");
1021                                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Cycl",
1022                                                   xmin+width, dy, width-20, 19, 
1023                                                   &iuser->cycl, 0.0, 0.0, 0, 0, "Make animation go cyclic");
1024                                 uiDefIconButBitS(block, TOG, IMA_ANIM_ALWAYS, B_NODE_EXEC+node->nr, ICON_AUTO,
1025                                                   xmax-20, dy, 20, 19, 
1026                                                   &iuser->flag, 0.0, 0.0, 0, 0, "Always refresh Image on frame changes");
1027                         }
1028                         if( ima->type==IMA_TYPE_MULTILAYER && ima->rr) {
1029                                 RenderLayer *rl= BLI_findlink(&ima->rr->layers, iuser->layer);
1030                                 if(rl) {
1031                                         width= (xmax-xmin);
1032                                         dy-= 19;
1033                                         strp= layer_menu(ima->rr);
1034                                         bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, strp,
1035                                                           xmin, dy, width, 19, 
1036                                                           &iuser->layer, 0.0, 10000.0, 0, 0, "Layer");
1037                                         uiButSetFunc(bt, image_layer_cb, ima->rr, node->storage);
1038                                         MEM_freeN(strp);
1039                                 }
1040                         }
1041                 }
1042                 
1043         }       
1044         if(node->id) {
1045                 Image *ima= (Image *)node->id;
1046                 int retval= 19;
1047                 
1048                 /* for each draw we test for anim refresh event */
1049                 if(iuser->flag & IMA_ANIM_REFRESHED) {
1050                         iuser->flag &= ~IMA_ANIM_REFRESHED;
1051                         addqueue(curarea->win, UI_BUT_EVENT, B_NODE_EXEC+node->nr);
1052                 }
1053                 
1054                 if( ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE) )
1055                         retval+= 38;
1056                 if( ima->type==IMA_TYPE_MULTILAYER)
1057                         retval+= 19;
1058                 return retval;
1059         }
1060         else
1061                 return 19;
1062 }
1063
1064 /* if we use render layers from other scene, we make a nice title */
1065 static void set_render_layers_title(void *node_v, void *unused)
1066 {
1067         bNode *node= node_v;
1068         Scene *sce;
1069         SceneRenderLayer *srl;
1070         char str[64];
1071         
1072         if(node->id) {
1073                 BLI_strncpy(str, node->id->name+2, 21);
1074                 strcat(str, "|");
1075                 sce= (Scene *)node->id;
1076         }
1077         else {
1078                 str[0]= 0;
1079                 sce= G.scene;
1080         }
1081         srl= BLI_findlink(&sce->r.layers, node->custom1);
1082         if(srl==NULL) {
1083                 node->custom1= 0;
1084                 srl= sce->r.layers.first;
1085         }
1086         
1087         strcat(str, srl->name);
1088         BLI_strncpy(node->name, str, 32);
1089 }
1090
1091 static char *scene_layer_menu(Scene *sce)
1092 {
1093         SceneRenderLayer *srl;
1094         int len= 40 + 40*BLI_countlist(&sce->r.layers);
1095         short a, nr;
1096         char *str= MEM_callocN(len, "menu layers");
1097         
1098         strcpy(str, "Active Layer %t");
1099         a= strlen(str);
1100         for(nr=0, srl= sce->r.layers.first; srl; srl= srl->next, nr++) {
1101                 a+= sprintf(str+a, "|%s %%x%d", srl->name, nr);
1102         }
1103         
1104         return str;
1105 }
1106
1107 static void node_browse_scene_cb(void *ntree_v, void *node_v)
1108 {
1109         bNodeTree *ntree= ntree_v;
1110         bNode *node= node_v;
1111         Scene *sce;
1112         
1113         if(node->menunr<1) return;
1114         
1115         if(node->id) {
1116                 node->id->us--;
1117                 node->id= NULL;
1118         }
1119         sce= BLI_findlink(&G.main->scene, node->menunr-1);
1120         if(sce!=G.scene) {
1121                 node->id= &sce->id;
1122                 id_us_plus(node->id);
1123         }
1124         
1125         set_render_layers_title(node, NULL);
1126         nodeSetActive(ntree, node);
1127
1128         allqueue(REDRAWBUTSSHADING, 0);
1129         allqueue(REDRAWNODE, 0);
1130         NodeTagChanged(ntree, node); 
1131
1132         node->menunr= 0;
1133 }
1134
1135
1136 static int node_composit_buts_renderlayers(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1137 {
1138         if(block) {
1139                 uiBut *bt;
1140                 char *strp;
1141                 
1142                 /* browse button scene */
1143                 uiBlockBeginAlign(block);
1144                 IDnames_to_pupstring(&strp, NULL, "", &(G.main->scene), NULL, NULL);
1145                 node->menunr= 0;
1146                 bt= uiDefButS(block, MENU, B_NOP, strp, 
1147                                           butr->xmin, butr->ymin, 20, 19, 
1148                                           &node->menunr, 0, 0, 0, 0, "Browse Scene to use RenderLayer from");
1149                 uiButSetFunc(bt, node_browse_scene_cb, ntree, node);
1150                 if(strp) MEM_freeN(strp);
1151                 
1152                 /* browse button layer */
1153                 strp= scene_layer_menu(node->id?(Scene *)node->id:G.scene);
1154                 if(node->id)
1155                         bt= uiDefIconTextButS(block, MENU, B_NODE_EXEC+node->nr, ICON_SCENE_DEHLT, strp, 
1156                                   butr->xmin+20, butr->ymin, (butr->xmax-butr->xmin)-40, 19, 
1157                                   &node->custom1, 0, 0, 0, 0, "Choose Render Layer");
1158                 else
1159                         bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, strp, 
1160                                   butr->xmin+20, butr->ymin, (butr->xmax-butr->xmin)-40, 19, 
1161                                   &node->custom1, 0, 0, 0, 0, "Choose Render Layer");
1162                 uiButSetFunc(bt, set_render_layers_title, node, NULL);
1163                 MEM_freeN(strp);
1164                 
1165                 /* re-render */
1166                 /* uses custom2, not the best implementation of the world... but we need it to work now :) */
1167                 bt= uiDefIconButS(block, TOG, B_NODE_EXEC+node->nr, ICON_SCENE, 
1168                                   butr->xmax-20, butr->ymin, 20, 19, 
1169                                   &node->custom2, 0, 0, 0, 0, "Re-render this Layer");
1170                 
1171         }
1172         return 19;
1173 }
1174
1175 static void node_blur_relative_cb(void *node, void *poin2)
1176 {
1177         bNode *nodev= node;
1178         NodeBlurData *nbd= nodev->storage;
1179         if(nbd->image_in_width != 0){
1180                 if(nbd->relative){ /* convert absolute values to relative */
1181                         nbd->percentx= (float)(nbd->sizex)/nbd->image_in_width;
1182                         nbd->percenty= (float)(nbd->sizey)/nbd->image_in_height;
1183                 }else{ /* convert relative values to absolute */
1184                         nbd->sizex= (int)(nbd->percentx*nbd->image_in_width);
1185                         nbd->sizey= (int)(nbd->percenty*nbd->image_in_height);
1186                 }
1187         }
1188         allqueue(REDRAWNODE, 0);
1189 }
1190 static void node_blur_update_sizex_cb(void *node, void *poin2)
1191 {
1192         bNode *nodev= node;
1193         NodeBlurData *nbd= nodev->storage;
1194
1195         nbd->sizex= (int)(nbd->percentx*nbd->image_in_width);
1196 }
1197 static void node_blur_update_sizey_cb(void *node, void *poin2)
1198 {
1199         bNode *nodev= node;
1200         NodeBlurData *nbd= nodev->storage;
1201
1202         nbd->sizey= (int)(nbd->percenty*nbd->image_in_height);
1203 }
1204 static int node_composit_buts_blur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1205 {
1206         if(block) {
1207                 NodeBlurData *nbd= node->storage;
1208                 uiBut *bt;
1209                 short dy= butr->ymin+58;
1210                 short dx= (butr->xmax-butr->xmin)/2;
1211                 char str[256];
1212                 
1213                 uiBlockBeginAlign(block);
1214                 sprintf(str, "Filter Type%%t|Flat %%x%d|Tent %%x%d|Quad %%x%d|Cubic %%x%d|Gauss %%x%d|Fast Gauss%%x%d|CatRom %%x%d|Mitch %%x%d", R_FILTER_BOX, R_FILTER_TENT, R_FILTER_QUAD, R_FILTER_CUBIC, R_FILTER_GAUSS, R_FILTER_FAST_GAUSS, R_FILTER_CATROM, R_FILTER_MITCH);
1215                 uiDefButS(block, MENU, B_NODE_EXEC+node->nr,str,
1216                                   butr->xmin, dy, dx*2, 19, 
1217                                   &nbd->filtertype, 0, 0, 0, 0, "Set sampling filter for blur");
1218                 dy-=19;
1219                 if (nbd->filtertype != R_FILTER_FAST_GAUSS) { 
1220                         uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Bokeh",
1221                                         butr->xmin, dy, dx, 19, 
1222                                         &nbd->bokeh, 0, 0, 0, 0, "Uses circular filter, warning it's slow!");
1223                         uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Gamma",
1224                                         butr->xmin+dx, dy, dx, 19, 
1225                                         &nbd->gamma, 0, 0, 0, 0, "Applies filter on gamma corrected values");
1226                 } else {
1227                         uiBlockEndAlign(block);
1228                         uiBlockBeginAlign(block);
1229                 }
1230                 dy-=19;
1231                 bt= uiDefButS(block, TOG, B_NOP, "Relative",
1232                                   butr->xmin, dy, dx*2, 19,
1233                                   &nbd->relative, 0, 0, 0, 0, "Use relative (percent) values to define blur radius");
1234                 uiButSetFunc(bt, node_blur_relative_cb, node, NULL);
1235
1236                 dy-=19;
1237                 if(nbd->relative) {
1238                         bt= uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "X:",
1239                                                  butr->xmin, dy, dx, 19, 
1240                                                  &nbd->percentx, 0.0f, 1.0f, 0, 0, "");
1241                         uiButSetFunc(bt, node_blur_update_sizex_cb, node, NULL);
1242                         bt= uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Y:",
1243                                                  butr->xmin+dx, dy, dx, 19, 
1244                                                  &nbd->percenty, 0.0f, 1.0f, 0, 0, "");
1245                         uiButSetFunc(bt, node_blur_update_sizey_cb, node, NULL);
1246                 }
1247                 else {
1248                         uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "X:",
1249                                                  butr->xmin, dy, dx, 19, 
1250                                                  &nbd->sizex, 0, 256, 0, 0, "");
1251                         uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Y:",
1252                                                  butr->xmin+dx, dy, dx, 19, 
1253                                                  &nbd->sizey, 0, 256, 0, 0, "");
1254                 }
1255                 uiBlockEndAlign(block);
1256         }
1257         return 77;
1258 }
1259
1260 static int node_composit_buts_dblur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1261 {
1262         if(block) {
1263                 NodeDBlurData *ndbd = node->storage;
1264                 short dy = butr->ymin + 171;
1265                 short dx = butr->xmax - butr->xmin;
1266                 short halfdx= (short)dx/2;
1267
1268                 uiBlockBeginAlign(block);
1269                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Iterations:",
1270                                 butr->xmin, dy, dx, 19,
1271                                 &ndbd->iter, 1, 32, 10, 0, "Amount of iterations");
1272                 uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Wrap",
1273                                 butr->xmin, dy-= 19, dx, 19, 
1274                                 &ndbd->wrap, 0, 0, 0, 0, "Wrap blur");
1275                 uiBlockEndAlign(block);
1276
1277                 dy-= 9;
1278
1279                 uiDefBut(block, LABEL, B_NOP, "Center", butr->xmin, dy-= 19, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
1280
1281                 uiBlockBeginAlign(block);
1282                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "X:",
1283                                 butr->xmin, dy-= 19, halfdx, 19,
1284                                 &ndbd->center_x, 0.0f, 1.0f, 10, 0, "X center in percents");
1285                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Y:",
1286                                 butr->xmin+halfdx, dy, halfdx, 19,
1287                                 &ndbd->center_y, 0.0f, 1.0f, 10, 0, "Y center in percents");
1288                 uiBlockEndAlign(block);
1289
1290                 dy-= 9;
1291
1292                 uiBlockBeginAlign(block);
1293                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Distance:",
1294                                 butr->xmin, dy-= 19, dx, 19,
1295                                 &ndbd->distance, -1.0f, 1.0f, 10, 0, "Amount of which the image moves");
1296                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Angle:",
1297                                 butr->xmin, dy-= 19, dx, 19,
1298                                 &ndbd->angle, 0.0f, 360.0f, 1000, 0, "Angle in which the image will be moved");
1299                 uiBlockEndAlign(block);
1300
1301                 dy-= 9;
1302
1303                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Spin:",
1304                                 butr->xmin, dy-= 19, dx, 19,
1305                                 &ndbd->spin, -360.0f, 360.0f, 1000, 0, "Angle that is used to spin the image");
1306
1307                 dy-= 9;
1308
1309                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Zoom:",
1310                                 butr->xmin, dy-= 19, dx, 19,
1311                                 &ndbd->zoom, 0.0f, 100.0f, 100, 0, "Amount of which the image is zoomed");
1312
1313         }
1314         return 190;
1315 }
1316
1317 static int node_composit_buts_bilateralblur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1318 {
1319         if(block) {
1320                 NodeBilateralBlurData *nbbd= node->storage;
1321                 short dy= butr->ymin+38;
1322                 short dx= (butr->xmax-butr->xmin);
1323                 
1324                 uiBlockBeginAlign(block);
1325                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Iterations:",
1326                                  butr->xmin, dy, dx, 19, 
1327                                  &nbbd->iter, 1, 128, 0, 0, "Amount of iterations");
1328                 dy-=19;
1329                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Color Sigma:",
1330                                   butr->xmin, dy, dx, 19, 
1331                                   &nbbd->sigma_color,0.01, 3, 10, 0, "Sigma value used to modify color");
1332                 dy-=19;
1333                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Space Sigma:",
1334                                   butr->xmin, dy, dx, 19, 
1335                                   &nbbd->sigma_space ,0.01, 30, 10, 0, "Sigma value used to modify space");
1336                 
1337         }
1338         return 57;
1339 }
1340
1341 /* qdn: defocus node */
1342 static int node_composit_buts_defocus(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1343 {
1344         if(block) {
1345                 NodeDefocus *nqd = node->storage;
1346                 short dy = butr->ymin + 209;
1347                 short dx = butr->xmax - butr->xmin; 
1348                 char* mstr1 = "Bokeh Type%t|Octagon %x8|Heptagon %x7|Hexagon %x6|Pentagon %x5|Square %x4|Triangle %x3|Disk %x0";
1349
1350                 uiDefBut(block, LABEL, B_NOP, "Bokeh Type", butr->xmin, dy, dx, 19, NULL, 0, 0, 0, 0, "");
1351                 uiDefButC(block, MENU, B_NODE_EXEC+node->nr, mstr1,
1352                           butr->xmin, dy-19, dx, 19,
1353                           &nqd->bktype, 0, 0, 0, 0, "Bokeh type");
1354                 if (nqd->bktype) { /* for some reason rotating a disk doesn't seem to work... ;) */
1355                         uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "Rotate:",
1356                                   butr->xmin, dy-38, dx, 19,
1357                                   &nqd->rotation, 0, 90, 0, 0, "Bokeh shape rotation offset in degrees");
1358                 }
1359                 uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Gamma Correct",
1360                           butr->xmin, dy-57, dx, 19,
1361                           &nqd->gamco, 0, 0, 0, 0, "Enable gamma correction before and after main process");
1362                 if (nqd->no_zbuf==0) {
1363                         // only needed for zbuffer input
1364                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "fStop:",
1365                                   butr->xmin, dy-76, dx, 19,
1366                                   &nqd->fstop, 0.5, 128, 10, 0, "Amount of focal blur, 128=infinity=perfect focus, half the value doubles the blur radius");
1367                 }
1368                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Maxblur:",
1369                           butr->xmin, dy-95, dx, 19,
1370                           &nqd->maxblur, 0, 10000, 1000, 0, "blur limit, maximum CoC radius, 0=no limit");
1371                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "BThreshold:",
1372                           butr->xmin, dy-114, dx, 19,
1373                           &nqd->bthresh, 0, 100, 100, 0, "CoC radius threshold, prevents background bleed on in-focus midground, 0=off");
1374                 uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Preview",
1375                           butr->xmin, dy-142, dx, 19,
1376                           &nqd->preview, 0, 0, 0, 0, "Enable sampling mode, useful for preview when using low samplecounts");
1377                 if (nqd->preview) {
1378                         /* only visible when sampling mode enabled */
1379                         uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Samples:",
1380                                   butr->xmin, dy-161, dx, 19,
1381                                   &nqd->samples, 16, 256, 0, 0, "Number of samples (16=grainy, higher=less noise)");
1382                 }
1383                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "No zbuffer",
1384                           butr->xmin, dy-190, dx, 19,
1385                           &nqd->no_zbuf, 0, 0, 0, 0, "Enable when using an image as input instead of actual zbuffer (auto enabled if node not image based, eg. time node)");
1386                 if (nqd->no_zbuf) {
1387                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Zscale:",
1388                             butr->xmin, dy-209, dx, 19,
1389                             &nqd->scale, 0, 1000, 100, 0, "Scales the Z input when not using a zbuffer, controls maximum blur designated by the color white or input value 1");
1390                 }
1391         }
1392         return 228;
1393 }
1394
1395
1396 /* qdn: glare node */
1397 static int node_composit_buts_glare(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1398 {
1399         if(block) {
1400                 NodeGlare *ndg = node->storage;
1401                 short dy = butr->ymin + 152, dx = butr->xmax - butr->xmin; 
1402                 char* mn1 = "Type%t|Ghosts%x3|Streaks%x2|Fog Glow%x1|Simple Star%x0";
1403                 char* mn2 = "Quality/Speed%t|High/Slow%x0|Medium/Medium%x1|Low/Fast%x2";
1404                 uiDefButC(block, MENU, B_NODE_EXEC+node->nr, mn1,
1405                           butr->xmin, dy, dx, 19,
1406                           &ndg->type, 0, 0, 0, 0, "Glow/Flare/Bloom type");
1407                 uiDefButC(block, MENU, B_NODE_EXEC+node->nr, mn2,
1408                           butr->xmin, dy-19, dx, 19,
1409                           &ndg->quality, 0, 0, 0, 0,
1410                           "Quality speed trade off, if not set to high quality, effect will be applied to low-res copy of source image");
1411                 if (ndg->type != 1) {
1412                         uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "Iterations:",
1413                                   butr->xmin, dy-38, dx, 19,
1414                                   &ndg->iter, 2, 5, 1, 0,
1415                                   "higher values will generate longer/more streaks/ghosts");
1416                         if (ndg->type != 0)
1417                                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "ColMod:",
1418                                           butr->xmin, dy-57, dx, 19,
1419                                           &ndg->colmod, 0, 1, 10, 0,
1420                                           "Amount of Color Modulation, modulates colors of streaks and ghosts for a spectral dispersion effect");
1421                 }
1422                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Mix:",
1423                           butr->xmin, dy-76, dx, 19,
1424                           &ndg->mix, -1, 1, 10, 0,
1425                           "Mix balance, -1 is original image only, 0 is exact 50/50 mix, 1 is processed image only");
1426                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Threshold:",
1427                           butr->xmin, dy-95, dx, 19,
1428                           &ndg->threshold, 0, 1000, 10, 0,
1429                           "Brightness threshold, the glarefilter will be applied only to pixels brighter than this value");
1430                 if ((ndg->type == 2) || (ndg->type == 0))
1431                 {
1432                         if (ndg->type == 2) {
1433                                 uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "streaks:",
1434                                           butr->xmin, dy-114, dx, 19,
1435                                           &ndg->angle, 2, 16, 1000, 0,
1436                                           "Total number of streaks");
1437                                 uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "AngOfs:",
1438                                           butr->xmin, dy-133, dx, 19,
1439                                           &ndg->angle_ofs, 0, 180, 1000, 0,
1440                                           "Streak angle rotation offset in degrees");
1441                         }
1442                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Fade:",
1443                                   butr->xmin, dy-152, dx, 19,
1444                                   &ndg->fade, 0.75, 1, 5, 0,
1445                                   "Streak fade out factor");
1446                 }
1447                 if (ndg->type == 0)
1448                         uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Rot45",
1449                                   butr->xmin, dy-114, dx, 19,
1450                                   &ndg->angle, 0, 0, 0, 0,
1451                                   "simple star filter, add 45 degree rotation offset");
1452                 if ((ndg->type == 1) || (ndg->type > 3))        // PBGH and fog glow
1453                         uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "Size:",
1454                                   butr->xmin, dy-114, dx, 19,
1455                                   &ndg->size, 6, 9, 1000, 0,
1456                                   "glow/glare size (not actual size, relative to initial size of bright area of pixels)");
1457         }
1458         return 171;
1459 }
1460
1461 /* qdn: tonemap node */
1462 static int node_composit_buts_tonemap(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1463 {
1464         if(block) {
1465                 NodeTonemap *ntm = node->storage;
1466                 short dy = butr->ymin + 76, dx = butr->xmax - butr->xmin; 
1467                 char* mn = "Type%t|R/D Photoreceptor%x1|Rh Simple%x0";
1468                 
1469                 uiBlockBeginAlign(block);
1470                 uiDefButI(block, MENU, B_NODE_EXEC+node->nr, mn,
1471                           butr->xmin, dy, dx, 19,
1472                           &ntm->type, 0, 0, 0, 0,
1473                           "Tone mapping type");
1474                 if (ntm->type == 0) {
1475                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Key:",
1476                                   butr->xmin, dy-19, dx, 19,
1477                                   &ntm->key, 0, 1, 5, 0,
1478                                   "The value the average luminance is mapped to");
1479                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Offset:",
1480                                   butr->xmin, dy-38, dx, 19,
1481                                   &ntm->offset, 0.001, 10, 5, 0,
1482                                   "Tonemap offset, normally always 1, but can be used as an extra control to alter the brightness curve");
1483                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Gamma:",
1484                                   butr->xmin, dy-57, dx, 19,
1485                                   &ntm->gamma, 0.001, 3, 5, 0,
1486                                   "Gamma factor, if not used, set to 1");
1487                 }
1488                 else {
1489                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Intensity:",
1490                                   butr->xmin, dy-19, dx, 19,
1491                                   &ntm->f, -8, 8, 10, 0, "if less than zero, darkens image, otherwise makes it brighter");
1492                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Contrast:",
1493                                   butr->xmin, dy-38, dx, 19,
1494                                   &ntm->m, 0, 1, 5, 0, "Set to 0 to use estimate from input image");
1495                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Adaptation:",
1496                                   butr->xmin, dy-57, dx, 19,
1497                                   &ntm->a, 0, 1, 5, 0, "if 0, global, if 1, based on pixel intensity");
1498                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "ColCorrect:",
1499                                   butr->xmin, dy-76, dx, 19,
1500                                   &ntm->c, 0, 1, 5, 0, "color correction, if 0, same for all channels, if 1, each independent");
1501                 }
1502                 uiBlockEndAlign(block);
1503         }
1504         return 95;
1505 }
1506
1507 /* qdn: lens distortion node */
1508 static int node_composit_buts_lensdist(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1509 {
1510         if(block) {
1511                 NodeLensDist *nld = node->storage;
1512                 short dy = butr->ymin + 19, dx = butr->xmax - butr->xmin; 
1513                 uiBlockBeginAlign(block);
1514                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Projector",
1515                           butr->xmin, dy, dx, 19,
1516                           &nld->proj, 0, 0, 0, 0,
1517                           "Enable/disable projector mode, effect is applied in horizontal direction only");
1518                 if (!nld->proj) {
1519                         uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Jitter",
1520                                   butr->xmin, dy-19, dx/2, 19,
1521                                   &nld->jit, 0, 0, 0, 0,
1522                                   "Enable/disable jittering, faster, but also noisier");
1523                         uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Fit",
1524                                   butr->xmin+dx/2, dy-19, dx/2, 19,
1525                                   &nld->fit, 0, 0, 0, 0,
1526                                   "For positive distortion factor only, scale image such that black areas are not visible");
1527                 }
1528                 uiBlockEndAlign(block);
1529         }
1530         return 38;
1531 }
1532
1533
1534 static int node_composit_buts_vecblur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1535 {
1536         if(block) {
1537                 NodeBlurData *nbd= node->storage;
1538                 short dy= butr->ymin;
1539                 short dx= (butr->xmax-butr->xmin);
1540                 
1541                 uiBlockBeginAlign(block);
1542                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Samples:",
1543                                  butr->xmin, dy+76, dx, 19, 
1544                                  &nbd->samples, 1, 256, 0, 0, "Amount of samples");
1545                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "MinSpeed:",
1546                                   butr->xmin, dy+57, dx, 19, 
1547                                   &nbd->minspeed, 0, 1024, 0, 0, "Minimum speed for a pixel to be blurred, used to separate background from foreground");
1548                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "MaxSpeed:",
1549                                   butr->xmin, dy+38, dx, 19, 
1550                                   &nbd->maxspeed, 0, 1024, 0, 0, "If not zero, maximum speed in pixels");
1551                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "BlurFac:",
1552                                   butr->xmin, dy+19, dx, 19, 
1553                                   &nbd->fac, 0.0f, 2.0f, 10, 2, "Scaling factor for motion vectors, actually 'shutter speed' in frames");
1554                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Curved",
1555                                   butr->xmin, dy, dx, 19, 
1556                                   &nbd->curved, 0.0f, 2.0f, 10, 2, "Interpolate between frames in a bezier curve, rather than linearly");
1557                 uiBlockEndAlign(block);
1558         }
1559         return 95;
1560 }
1561
1562 static int node_composit_buts_filter(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1563 {
1564         if(block) {
1565                 uiBut *bt;
1566                 
1567                 /* blend type */
1568                 bt=uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Soften %x0|Sharpen %x1|Laplace %x2|Sobel %x3|Prewitt %x4|Kirsch %x5|Shadow %x6",
1569                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1570                                          &node->custom1, 0, 0, 0, 0, "");
1571                 uiButSetFunc(bt, node_but_title_cb, node, bt);
1572         }
1573         return 20;
1574 }
1575
1576 static int node_composit_buts_flip(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
1577 {
1578         if(block) {
1579                 uiBut *bt;
1580                 
1581                 /* flip x\y */
1582                 bt=uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Flip X %x0|Flip Y %x1|Flip X & Y %x2",
1583                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1584                                          &node->custom1, 0, 0, 0, 0, "");
1585                 uiButSetFunc(bt, node_but_title_cb, node, bt);
1586         }
1587         return 20;      
1588 }
1589
1590 static int node_composit_buts_crop(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1591 {
1592         if(block) {
1593                 NodeTwoXYs *ntxy= node->storage;
1594                 char elementheight = 19;
1595                 short dx= (butr->xmax-butr->xmin)/2;
1596                 short dy= butr->ymax - elementheight;
1597                 short xymin= 0, xymax= 10000;
1598
1599                 uiBlockBeginAlign(block);
1600
1601                 /* crop image size toggle */
1602                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Crop Image Size",
1603                                   butr->xmin, dy, dx*2, elementheight, 
1604                                   &node->custom1, 0, 0, 0, 0, "Crop the size of the input image.");
1605
1606                 dy-=elementheight;
1607
1608                 /* x1 */
1609                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "X1:",
1610                                          butr->xmin, dy, dx, elementheight,
1611                                          &ntxy->x1, xymin, xymax, 0, 0, "");
1612                 /* y1 */
1613                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Y1:",
1614                                          butr->xmin+dx, dy, dx, elementheight,
1615                                          &ntxy->y1, xymin, xymax, 0, 0, "");
1616
1617                 dy-=elementheight;
1618
1619                 /* x2 */
1620                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "X2:",
1621                                          butr->xmin, dy, dx, elementheight,
1622                                          &ntxy->x2, xymin, xymax, 0, 0, "");
1623                 /* y2 */
1624                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Y2:",
1625                                          butr->xmin+dx, dy, dx, elementheight,
1626                                          &ntxy->y2, xymin, xymax, 0, 0, "");
1627
1628                 uiBlockEndAlign(block);
1629         }
1630         return 60;
1631 }
1632
1633 static int node_composit_buts_splitviewer(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1634 {
1635         if(block) {     
1636                 uiBlockBeginAlign(block);
1637                 
1638                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "X",
1639                                   butr->xmin, butr->ymin+19, (butr->xmax-butr->xmin)/2, 20, 
1640                                   &node->custom2, 0.0, 0.0, 0, 0, "");
1641                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Y",
1642                                   butr->xmin+(butr->xmax-butr->xmin)/2, butr->ymin+19, (butr->xmax-butr->xmin)/2, 20, 
1643                                   &node->custom2, 0.0, 1.0, 0, 0, "");
1644                                   
1645                 uiDefButS(block, NUMSLI, B_NODE_EXEC+node->nr, "Split %: ",
1646                                 butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, &node->custom1, 0, 100, 10, 0, "");
1647         }
1648         return 40;
1649 }
1650
1651 static int node_composit_buts_map_value(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1652 {
1653         if(block) {
1654                 TexMapping *texmap= node->storage;
1655                 short xstart= (short)butr->xmin;
1656                 short dy= (short)(butr->ymax-19.0f);
1657                 short dx= (short)(butr->xmax-butr->xmin)/2;
1658                 
1659                 uiBlockBeginAlign(block);
1660                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Offs:", xstart, dy, 2*dx, 19, texmap->loc, -1000.0f, 1000.0f, 10, 2, "");
1661                 dy-= 19;
1662                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Size:", xstart, dy, 2*dx, 19, texmap->size, -1000.0f, 1000.0f, 10, 3, "");
1663                 dy-= 23;
1664                 uiBlockBeginAlign(block);
1665                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MIN, B_NODE_EXEC+node->nr, "Min", xstart, dy, dx, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
1666                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", xstart+dx, dy, dx, 19, texmap->min, -1000.0f, 1000.0f, 10, 2, "");
1667                 dy-= 19;
1668                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MAX, B_NODE_EXEC+node->nr, "Max", xstart, dy, dx, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
1669                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", xstart+dx, dy, dx, 19, texmap->max, -1000.0f, 1000.0f, 10, 2, "");
1670         }
1671         return 80;
1672 }
1673
1674 static int node_composit_buts_alphaover(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1675 {
1676         if(block) {
1677                 NodeTwoFloats *ntf= node->storage;
1678                 
1679                 /* alpha type */
1680                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "ConvertPremul",
1681                                   butr->xmin, butr->ymin+19, butr->xmax-butr->xmin, 19, 
1682                                   &node->custom1, 0, 0, 0, 0, "");
1683                 /* mix factor */
1684                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Premul: ",
1685                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 19, 
1686                                   &ntf->x, 0.0f, 1.0f, 100, 0, "");
1687         }
1688         return 38;
1689 }
1690
1691 static int node_composit_buts_hue_sat(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1692 {
1693         if(block) {
1694                 NodeHueSat *nhs= node->storage;
1695                 
1696                 uiBlockBeginAlign(block);
1697                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Hue: ",
1698                                   butr->xmin, butr->ymin+40.0f, butr->xmax-butr->xmin, 20, 
1699                                   &nhs->hue, 0.0f, 1.0f, 100, 0, "");
1700                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Sat: ",
1701                                   butr->xmin, butr->ymin+20.0f, butr->xmax-butr->xmin, 20, 
1702                                   &nhs->sat, 0.0f, 2.0f, 100, 0, "");
1703                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Val: ",
1704                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1705                                   &nhs->val, 0.0f, 2.0f, 100, 0, "");
1706         }
1707         return 60;
1708 }
1709
1710 static int node_composit_buts_dilateerode(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1711 {
1712         if(block) {
1713                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Distance:",
1714                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1715                                   &node->custom2, -100, 100, 0, 0, "Distance to grow/shrink (number of iterations)");
1716         }
1717         return 20;
1718 }
1719
1720 static int node_composit_buts_diff_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1721 {
1722         if(block) {
1723                 short sx= (butr->xmax-butr->xmin)/4;
1724                 short dx= (butr->xmax-butr->xmin)/3;
1725                 NodeChroma *c= node->storage;
1726                 
1727                 uiBlockBeginAlign(block);
1728                 /*color space selectors*/
1729                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"RGB",
1730                                                         butr->xmin,butr->ymin+60,sx,20,
1731                                                         &node->custom1,1,1, 0, 0, "RGB Color Space");
1732                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"HSV",
1733                                                         butr->xmin+sx,butr->ymin+60,sx,20,
1734                                                         &node->custom1,1,2, 0, 0, "HSV Color Space");
1735                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"YUV",
1736                                                         butr->xmin+2*sx,butr->ymin+60,sx,20,
1737                                                         &node->custom1,1,3, 0, 0, "YUV Color Space");
1738                                         uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"YCC",
1739                                                         butr->xmin+3*sx,butr->ymin+60,sx,20,
1740                                                         &node->custom1,1,4, 0, 0, "YCbCr Color Space");
1741                 /*channel tolorences*/
1742                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, " ",
1743                                                         butr->xmin, butr->ymin+40, dx, 20,
1744                                                         &c->t1, 0.0f, 1.0f, 100, 0, "Channel 1 Tolerance");
1745                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, " ",
1746                                                         butr->xmin+dx, butr->ymin+40, dx, 20,
1747                                                         &c->t2, 0.0f, 1.0f, 100, 0, "Channel 2 Tolorence");
1748                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, " ",
1749                                                         butr->xmin+2*dx, butr->ymin+40, dx, 20,
1750                                                         &c->t3, 0.0f, 1.0f, 100, 0, "Channel 3 Tolorence");
1751                 /*falloff parameters*/
1752                 /*
1753                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Falloff Size ",
1754                         butr->xmin, butr->ymin+20, butr->xmax-butr->xmin, 20,
1755                         &c->fsize, 0.0f, 1.0f, 100, 0, "");
1756                 */
1757                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Falloff: ",
1758                         butr->xmin, butr->ymin+20, butr->xmax-butr->xmin, 20,
1759                         &c->fstrength, 0.0f, 1.0f, 100, 0, "");
1760         }
1761         return 80;
1762 }
1763
1764 static int node_composit_buts_color_spill(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1765 {
1766         if(block) {
1767                 short dx= (butr->xmax-butr->xmin)/3;
1768
1769                 NodeChroma *c=node->storage;
1770                 uiBlockBeginAlign(block);
1771                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Enhance: ", 
1772                                 butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1773                                 &c->t1, 0.0f, 0.5f, 100, 2, "Adjusts how much selected channel is affected by color spill algorithm");
1774                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "R",
1775                                 butr->xmin,butr->ymin,dx,20,
1776                                 &node->custom1,1,1, 0, 0, "Red Spill Suppression");
1777                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "G",
1778                                 butr->xmin+dx,butr->ymin,dx,20,
1779                                 &node->custom1,1,2, 0, 0, "Green Spill Suppression");
1780                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "B",
1781                                 butr->xmin+2*dx,butr->ymin,dx,20,
1782                                 &node->custom1, 1, 3, 0, 0, "Blue Spill Suppression");
1783                 uiBlockEndAlign(block);
1784         }
1785         return 60;
1786 }
1787
1788 static int node_composit_buts_chroma_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1789 {
1790         if(block) {
1791                 short dx=(butr->xmax-butr->xmin)/2;
1792                 NodeChroma *c= node->storage;
1793                 uiBlockBeginAlign(block);
1794
1795                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Acceptance ",
1796                         butr->xmin, butr->ymin+60, butr->xmax-butr->xmin, 20,
1797                         &c->t1, 1.0f, 80.0f, 100, 0, "Tolerance for colors to be considered a keying color");
1798                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Cutoff ",
1799                         butr->xmin, butr->ymin+40, butr->xmax-butr->xmin, 20,
1800                         &c->t2, 0.0f, 30.0f, 100, 0, "Colors below this will be considered as exact matches for keying color");
1801
1802                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Lift ",
1803                         butr->xmin, butr->ymin+20, dx, 20,
1804                         &c->fsize, 0.0f, 1.0f, 100, 0, "Alpha Lift");
1805                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Gain ",
1806                         butr->xmin+dx, butr->ymin+20, dx, 20,
1807                         &c->fstrength, 0.0f, 1.0f, 100, 0, "Alpha Gain");
1808
1809                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Shadow Adjust ",
1810                         butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1811                         &c->t3, 0.0f, 1.0f, 100, 0, "Adjusts the brightness of any shadows captured");
1812
1813                 if(c->t2 > c->t1)
1814                         c->t2=c->t1;
1815         }
1816         return 80;
1817 }
1818
1819 static int node_composit_buts_channel_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1820 {
1821         if(block) {
1822                 short sx= (butr->xmax-butr->xmin)/4;
1823                 short cx= (butr->xmax-butr->xmin)/3;
1824                 NodeChroma *c=node->storage;
1825                 char *c1, *c2, *c3;
1826         
1827                 /*color space selectors*/
1828                 uiBlockBeginAlign(block);
1829                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"RGB",
1830                         butr->xmin,butr->ymin+60,sx,20,&node->custom1,1,1, 0, 0, "RGB Color Space");
1831                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"HSV",
1832                         butr->xmin+sx,butr->ymin+60,sx,20,&node->custom1,1,2, 0, 0, "HSV Color Space");
1833                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"YUV",
1834                         butr->xmin+2*sx,butr->ymin+60,sx,20,&node->custom1,1,3, 0, 0, "YUV Color Space");
1835                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"YCC",
1836                         butr->xmin+3*sx,butr->ymin+60,sx,20,&node->custom1,1,4, 0, 0, "YCbCr Color Space");
1837         
1838                 if (node->custom1==1) {
1839                         c1="R"; c2="G"; c3="B";
1840                 }
1841                 else if(node->custom1==2){
1842                         c1="H"; c2="S"; c3="V";
1843                 }
1844                 else if(node->custom1==3){
1845                         c1="Y"; c2="U"; c3="V";
1846                 }
1847                 else { // if(node->custom1==4){
1848                         c1="Y"; c2="Cb"; c3="Cr";
1849                 }
1850         
1851                 /*channel selector */
1852                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, c1,
1853                         butr->xmin,butr->ymin+40,cx,20,&node->custom2,1, 1, 0, 0, "Channel 1");
1854                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, c2,
1855                         butr->xmin+cx,butr->ymin+40,cx,20,&node->custom2,1, 2, 0, 0, "Channel 2");
1856                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, c3,
1857                         butr->xmin+cx+cx,butr->ymin+40,cx,20,&node->custom2, 1, 3, 0, 0, "Channel 3");
1858         
1859                 /*tolerance sliders */
1860                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "High ", 
1861                         butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1862                         &c->t1, 0.0f, 1.0f, 100, 0, "Values higher than this setting are 100% opaque");
1863                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Low ", 
1864                         butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1865                         &c->t2, 0.0f, 1.0f, 100, 0, "Values lower than this setting are 100% keyed");
1866                 uiBlockEndAlign(block);
1867         
1868                 /*keep t2 (low) less than t1 (high) */
1869                 if(c->t2 > c->t1) {
1870                         c->t2=c->t1;
1871                 }
1872         }
1873         return 80;
1874 }
1875
1876 static int node_composit_buts_luma_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1877 {
1878         if(block) {
1879                 NodeChroma *c=node->storage;
1880         
1881                 /*tolerance sliders */
1882                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "High ", 
1883                         butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1884                         &c->t1, 0.0f, 1.0f, 100, 0, "Values higher than this setting are 100% opaque");
1885                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Low ", 
1886                         butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1887                         &c->t2, 0.0f, 1.0f, 100, 0, "Values lower than this setting are 100% keyed");
1888                 uiBlockEndAlign(block);
1889         
1890                 /*keep t2 (low) less than t1 (high) */
1891                 if(c->t2 > c->t1) {
1892                         c->t2=c->t1;
1893                 }
1894         }
1895         return 40;
1896 }
1897
1898 static int node_composit_buts_map_uv(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1899 {
1900         if(block) {
1901                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Alpha:",
1902                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1903                                   &node->custom1, 0, 100, 0, 0, "Conversion percentage of UV differences to Alpha");
1904         }
1905         return 20;
1906 }
1907
1908 static int node_composit_buts_id_mask(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1909 {
1910         if(block) {
1911                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "ID:",
1912                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1913                                   &node->custom1, 0, 10000, 0, 0, "Pass Index number to convert to Alpha");
1914         }
1915         return 20;
1916 }
1917
1918 /* allocate sufficient! */
1919 static void node_imagetype_string(char *str)
1920 {
1921         str += sprintf(str, "Save Image as: %%t|");
1922         str += sprintf(str, "Targa %%x%d|", R_TARGA);
1923         str += sprintf(str, "Targa Raw %%x%d|", R_RAWTGA);
1924         str += sprintf(str, "PNG %%x%d|", R_PNG);
1925         str += sprintf(str, "BMP %%x%d|", R_BMP);
1926         str += sprintf(str, "Jpeg %%x%d|", R_JPEG90);
1927         str += sprintf(str, "Iris %%x%d|", R_IRIS);
1928         str += sprintf(str, "Radiance HDR %%x%d|", R_RADHDR);
1929         str += sprintf(str, "Cineon %%x%d|", R_CINEON);
1930         str += sprintf(str, "DPX %%x%d|", R_DPX);
1931         str += sprintf(str, "OpenEXR %%x%d", R_OPENEXR);
1932 }
1933
1934 static void node_set_image_cb(void *ntree_v, void *node_v)
1935 {
1936         bNodeTree *ntree= ntree_v;
1937         bNode *node= node_v;
1938         
1939         nodeSetActive(ntree, node);
1940 }
1941
1942 static int node_composit_buts_file_output(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1943 {
1944         if(block) {
1945                 NodeImageFile *nif= node->storage;
1946                 uiBut *bt;
1947                 short x= (short)butr->xmin;
1948                 short y= (short)butr->ymin;
1949                 short w= (short)butr->xmax-butr->xmin;
1950                 char str[320];
1951                 
1952                 node_imagetype_string(str);
1953                 
1954                 uiBlockBeginAlign(block);
1955                 
1956                 bt = uiDefIconBut(block, BUT, B_NODE_SETIMAGE, ICON_FILESEL,
1957                                   x, y+60, 20, 20,
1958                                   0, 0, 0, 0, 0, "Open Fileselect to get Backbuf image");
1959                 uiButSetFunc(bt, node_set_image_cb, ntree, node);
1960                 
1961                 uiDefBut(block, TEX, B_NOP, "",
1962                                   20+x, y+60, w-20, 20, 
1963                                   nif->name, 0.0f, 240.0f, 0, 0, "");
1964                 
1965                 uiDefButS(block, MENU, B_NOP, str,
1966                                   x, y+40, w, 20, 
1967                                   &nif->imtype, 0.0f, 1.0f, 0, 0, "");
1968                 
1969                 if(nif->imtype==R_OPENEXR) {
1970                         uiDefButBitS(block, TOG, R_OPENEXR_HALF, B_REDR, "Half",        
1971                                                 x, y+20, w/2, 20, 
1972                                                 &nif->subimtype, 0, 0, 0, 0, "");
1973
1974                         uiDefButS(block, MENU,B_NOP, "Codec %t|None %x0|Pxr24 (lossy) %x1|ZIP (lossless) %x2|PIZ (lossless) %x3|RLE (lossless) %x4",  
1975                                                 x+w/2, y+20, w/2, 20, 
1976                                                 &nif->codec, 0, 0, 0, 0, "");
1977                 }
1978                 else {
1979                         uiDefButS(block, NUM, B_NOP, "Quality: ",
1980                                   x, y+20, w, 20, 
1981                                   &nif->quality, 10.0f, 100.0f, 10, 0, "");
1982                 }
1983                 
1984                 /* start frame, end frame */
1985                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "SFra: ", 
1986                                   x, y, w/2, 20, 
1987                                   &nif->sfra, 1, MAXFRAMEF, 10, 0, "");
1988                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "EFra: ", 
1989                                   x+w/2, y, w/2, 20, 
1990                                   &nif->efra, 1, MAXFRAMEF, 10, 0, "");
1991                 
1992         }
1993         return 80;
1994 }
1995
1996 static void node_scale_cb(void *node_v, void *unused_v)
1997 {
1998         bNode *node= node_v;
1999         bNodeSocket *nsock;
2000
2001         /* check the 2 inputs, and set them to reasonable values */
2002         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
2003                 if(ELEM(node->custom1, CMP_SCALE_RELATIVE, CMP_SCALE_SCENEPERCENT))
2004                         nsock->ns.vec[0]= 1.0;
2005                 else {
2006                         if(nsock->next==NULL)
2007                                 nsock->ns.vec[0]= (float)G.scene->r.ysch;
2008                         else
2009                                 nsock->ns.vec[0]= (float)G.scene->r.xsch;
2010                 }
2011         }       
2012 }
2013
2014 static int node_composit_buts_scale(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
2015 {
2016         if(block) {
2017                 uiBut *bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Relative %x0|Absolute %x1|Scene Size % %x2|",
2018                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
2019                                   &node->custom1, 0, 0, 0, 0, "Scale new image to absolute pixel size, size relative to the incoming image, or using the 'percent' size of the scene");
2020                 uiButSetFunc(bt, node_scale_cb, node, NULL);
2021         }
2022         return 20;
2023 }
2024
2025 static int node_composit_buts_invert(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
2026 {
2027         if(block) {
2028                 uiBlockBeginAlign(block);
2029                 uiDefButBitS(block, TOG, CMP_CHAN_RGB, B_NODE_EXEC+node->nr, "RGB",
2030                                          butr->xmin, butr->ymin, (butr->xmax-butr->xmin)/2, 20, 
2031                                          &node->custom1, 0, 0, 0, 0, "");
2032                 uiDefButBitS(block, TOG, CMP_CHAN_A, B_NODE_EXEC+node->nr, "A",
2033                                          butr->xmin+(butr->xmax-butr->xmin)/2, butr->ymin, (butr->xmax-butr->xmin)/2, 20, 
2034                                          &node->custom1, 0, 0, 0, 0, "");
2035                 uiBlockEndAlign(block);
2036         }
2037         return 20;      
2038 }
2039
2040 static int node_composit_buts_premulkey(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
2041 {
2042         if(block) {
2043                 uiBut *bt;
2044                 
2045                 /* blend type */
2046                 bt=uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Key to Premul %x0|Premul to Key %x1",
2047                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
2048                                          &node->custom1, 0, 0, 0, 0, "Conversion between premultiplied alpha and key alpha");
2049         }
2050         return 20;
2051 }
2052
2053 /* only once called */
2054 static void node_composit_set_butfunc(bNodeType *ntype)
2055 {
2056         switch(ntype->type) {
2057                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
2058
2059                 case CMP_NODE_IMAGE:
2060                         ntype->butfunc= node_composit_buts_image;
2061                         break;
2062                 case CMP_NODE_R_LAYERS:
2063                         ntype->butfunc= node_composit_buts_renderlayers;
2064                         break;
2065                 case CMP_NODE_NORMAL:
2066                         ntype->butfunc= node_buts_normal;
2067                         break;
2068                 case CMP_NODE_CURVE_VEC:
2069                         ntype->butfunc= node_buts_curvevec;
2070                         break;
2071                 case CMP_NODE_CURVE_RGB:
2072                         ntype->butfunc= node_buts_curvecol;
2073                         break;
2074                 case CMP_NODE_VALUE:
2075                         ntype->butfunc= node_buts_value;
2076                         break;
2077                 case CMP_NODE_RGB:
2078                         ntype->butfunc= node_buts_rgb;
2079                         break;
2080                 case CMP_NODE_FLIP:
2081                         ntype->butfunc= node_composit_buts_flip;
2082                         break;
2083                 case CMP_NODE_SPLITVIEWER:
2084                         ntype->butfunc= node_composit_buts_splitviewer;
2085                         break;
2086                 case CMP_NODE_MIX_RGB:
2087                         ntype->butfunc= node_buts_mix_rgb;
2088                         break;
2089                 case CMP_NODE_VALTORGB:
2090                         ntype->butfunc= node_buts_valtorgb;
2091                         break;
2092                 case CMP_NODE_CROP:
2093                         ntype->butfunc= node_composit_buts_crop;
2094                         break;
2095                 case CMP_NODE_BLUR:
2096                         ntype->butfunc= node_composit_buts_blur;
2097                         break;
2098                 case CMP_NODE_DBLUR:
2099                         ntype->butfunc= node_composit_buts_dblur;
2100                         break;
2101                 case CMP_NODE_BILATERALBLUR:
2102                         ntype->butfunc= node_composit_buts_bilateralblur;
2103                         break;
2104                 /* qdn: defocus node */
2105                 case CMP_NODE_DEFOCUS:
2106                         ntype->butfunc = node_composit_buts_defocus;
2107                         break;
2108                 /* qdn: glare node */
2109                 case CMP_NODE_GLARE:
2110                         ntype->butfunc = node_composit_buts_glare;
2111                         break;
2112                 /* qdn: tonemap node */
2113                 case CMP_NODE_TONEMAP:
2114                         ntype->butfunc = node_composit_buts_tonemap;
2115                         break;
2116                 /* qdn: lens distortion node */
2117                 case CMP_NODE_LENSDIST:
2118                         ntype->butfunc = node_composit_buts_lensdist;
2119                         break;
2120                 case CMP_NODE_VECBLUR:
2121                         ntype->butfunc= node_composit_buts_vecblur;
2122                         break;
2123                 case CMP_NODE_FILTER:
2124                         ntype->butfunc= node_composit_buts_filter;
2125                         break;
2126                 case CMP_NODE_MAP_VALUE:
2127                         ntype->butfunc= node_composit_buts_map_value;
2128                         break;
2129                 case CMP_NODE_TIME:
2130                         ntype->butfunc= node_buts_time;
2131                         break;
2132                 case CMP_NODE_ALPHAOVER:
2133                         ntype->butfunc= node_composit_buts_alphaover;
2134                         break;
2135                 case CMP_NODE_HUE_SAT:
2136                         ntype->butfunc= node_composit_buts_hue_sat;
2137                         break;
2138                 case CMP_NODE_TEXTURE:
2139                         ntype->butfunc= node_buts_texture;
2140                         break;
2141                 case CMP_NODE_DILATEERODE:
2142                         ntype->butfunc= node_composit_buts_dilateerode;
2143                         break;
2144                 case CMP_NODE_OUTPUT_FILE:
2145                         ntype->butfunc= node_composit_buts_file_output;
2146                         break;
2147         
2148                 case CMP_NODE_DIFF_MATTE:
2149                         ntype->butfunc=node_composit_buts_diff_matte;
2150                         break;
2151                 case CMP_NODE_COLOR_SPILL:
2152                         ntype->butfunc=node_composit_buts_color_spill;
2153                         break;
2154                 case CMP_NODE_CHROMA:
2155                         ntype->butfunc=node_composit_buts_chroma_matte;
2156                         break;
2157                 case CMP_NODE_SCALE:
2158                         ntype->butfunc= node_composit_buts_scale;
2159                         break;
2160                 case CMP_NODE_CHANNEL_MATTE:
2161                         ntype->butfunc= node_composit_buts_channel_matte;
2162                         break;
2163                 case CMP_NODE_LUMA_MATTE:
2164                         ntype->butfunc= node_composit_buts_luma_matte;
2165                         break;
2166                 case CMP_NODE_MAP_UV:
2167                         ntype->butfunc= node_composit_buts_map_uv;
2168                         break;
2169                 case CMP_NODE_ID_MASK:
2170                         ntype->butfunc= node_composit_buts_id_mask;
2171                         break;
2172                 case CMP_NODE_MATH:
2173                         ntype->butfunc= node_buts_math;
2174                         break;
2175                 case CMP_NODE_INVERT:
2176                         ntype->butfunc= node_composit_buts_invert;
2177                         break;
2178                 case CMP_NODE_PREMULKEY:
2179                         ntype->butfunc= node_composit_buts_premulkey;
2180                         break;
2181                 default:
2182                         ntype->butfunc= NULL;
2183         }
2184 }
2185
2186
2187 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2188
2189 void init_node_butfuncs(void)
2190 {
2191         bNodeType *ntype;
2192         
2193         /* shader nodes */
2194         ntype= node_all_shaders.first;
2195         while(ntype) {
2196                 node_shader_set_butfunc(ntype);
2197                 ntype= ntype->next;
2198         }
2199         /* composit nodes */
2200         ntype= node_all_composit.first;
2201         while(ntype) {
2202                 node_composit_set_butfunc(ntype);
2203                 ntype= ntype->next;
2204         }
2205 }
2206
2207 /* ************** Generic drawing ************** */
2208
2209 void node_rename_but(char *s)
2210 {
2211         uiBlock *block;
2212         ListBase listb={0, 0};
2213         int dy, x1, y1, sizex=80, sizey=30;
2214         short pivot[2], mval[2], ret=0;
2215         
2216         getmouseco_sc(mval);
2217
2218         pivot[0]= CLAMPIS(mval[0], (sizex+10), G.curscreen->sizex-30);
2219         pivot[1]= CLAMPIS(mval[1], (sizey/2)+10, G.curscreen->sizey-(sizey/2)-10);
2220         
2221         if (pivot[0]!=mval[0] || pivot[1]!=mval[1])
2222                 warp_pointer(pivot[0], pivot[1]);
2223
2224         mywinset(G.curscreen->mainwin);
2225         
2226         x1= pivot[0]-sizex+10;
2227         y1= pivot[1]-sizey/2;
2228         dy= sizey/2;
2229         
2230         block= uiNewBlock(&listb, "button", UI_EMBOSS, UI_HELV, G.curscreen->mainwin);
2231         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_ENTER_OK);
2232         
2233         /* buttons have 0 as return event, to prevent menu to close on hotkeys */
2234         uiBlockBeginAlign(block);
2235         
2236         uiDefBut(block, TEX, B_NOP, "Name: ", (short)(x1),(short)(y1+dy), 150, 19, s, 0.0, 19.0, 0, 0, "Node user name");
2237         
2238         uiBlockEndAlign(block);
2239
2240         uiDefBut(block, BUT, 32767, "OK", (short)(x1+150), (short)(y1+dy), 29, 19, NULL, 0, 0, 0, 0, "");
2241
2242         uiBoundsBlock(block, 2);
2243
2244         ret= uiDoBlocks(&listb, 0, 0);
2245 }
2246
2247
2248 static void draw_nodespace_grid(SpaceNode *snode)
2249 {
2250         float start, step= 25.0f;
2251
2252         BIF_ThemeColorShade(TH_BACK, -10);
2253         
2254         start= snode->v2d.cur.xmin -fmod(snode->v2d.cur.xmin, step);
2255         
2256         glBegin(GL_LINES);
2257         for(; start<snode->v2d.cur.xmax; start+=step) {
2258                 glVertex2f(start, snode->v2d.cur.ymin);
2259                 glVertex2f(start, snode->v2d.cur.ymax);
2260         }
2261
2262         start= snode->v2d.cur.ymin -fmod(snode->v2d.cur.ymin, step);
2263         for(; start<snode->v2d.cur.ymax; start+=step) {
2264                 glVertex2f(snode->v2d.cur.xmin, start);
2265                 glVertex2f(snode->v2d.cur.xmax, start);
2266         }
2267         
2268         /* X and Y axis */
2269         BIF_ThemeColorShade(TH_BACK, -18);
2270         glVertex2f(0.0f, snode->v2d.cur.ymin);
2271         glVertex2f(0.0f, snode->v2d.cur.ymax);
2272         glVertex2f(snode->v2d.cur.xmin, 0.0f);
2273         glVertex2f(snode->v2d.cur.xmax, 0.0f);
2274         
2275         glEnd();
2276 }
2277
2278 static void draw_nodespace_back_pix(ScrArea *sa, SpaceNode *snode)
2279 {
2280         
2281         draw_nodespace_grid(snode);
2282         
2283         if((snode->flag & SNODE_BACKDRAW) && snode->treetype==NTREE_COMPOSIT) {
2284                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2285                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2286                 if(ibuf) {
2287                         int x, y; 
2288                         /* somehow the offset has to be calculated inverse */
2289                         
2290                         glaDefine2DArea(&sa->winrct);
2291                         /* ortho at pixel level curarea */
2292                         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
2293                         
2294                         x = (sa->winx-ibuf->x)/2 + snode->xof;
2295                         y = (sa->winy-ibuf->y)/2 + snode->yof;
2296                         
2297                         if(ibuf->rect)
2298                                 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2299                         else if(ibuf->channels==4)
2300                                 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);
2301                         
2302                         /* sort this out, this should not be needed */
2303                         myortho2(snode->v2d.cur.xmin, snode->v2d.cur.xmax, snode->v2d.cur.ymin, snode->v2d.cur.ymax);
2304                         bwin_clear_viewmat(sa->win);    /* clear buttons view */
2305                         glLoadIdentity();
2306                 }
2307         }
2308 }
2309
2310 #if 0
2311 /* note: needs to be userpref or opengl profile option */
2312 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
2313 {
2314
2315         draw_nodespace_grid(snode);
2316         
2317         if(snode->flag & SNODE_BACKDRAW) {
2318                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2319                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2320                 if(ibuf) {
2321                         int x, y;
2322                         float zoom = 1.0;
2323
2324                         glMatrixMode(GL_PROJECTION);
2325                         glPushMatrix();
2326                         glMatrixMode(GL_MODELVIEW);
2327                         glPushMatrix();
2328                         
2329                         glaDefine2DArea(&sa->winrct);
2330
2331                         if(ibuf->x > sa->winx || ibuf->y > sa->winy) {
2332                                 float zoomx, zoomy;
2333                                 zoomx= (float)sa->winx/ibuf->x;
2334                                 zoomy= (float)sa->winy/ibuf->y;
2335                                 zoom = MIN2(zoomx, zoomy);
2336                         }
2337                         
2338                         x = (sa->winx-zoom*ibuf->x)/2 + snode->xof;
2339                         y = (sa->winy-zoom*ibuf->y)/2 + snode->yof;
2340
2341                         glPixelZoom(zoom, zoom);
2342
2343                         glColor4f(1.0, 1.0, 1.0, 1.0);
2344                         if(ibuf->rect)
2345                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
2346                         else if(ibuf->channels==4)
2347                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float);
2348
2349                         glPixelZoom(1.0, 1.0);
2350
2351                         glMatrixMode(GL_PROJECTION);
2352                         glPopMatrix();
2353                         glMatrixMode(GL_MODELVIEW);
2354                         glPopMatrix();
2355                 }
2356         }
2357 }
2358 #endif
2359
2360 /* nice AA filled circle */
2361 /* this might have some more generic use */
2362 static void circle_draw(float x, float y, float size, int type, int col[3])
2363 {
2364         /* 16 values of sin function */
2365         static float si[16] = {
2366                 0.00000000, 0.39435585,0.72479278,0.93775213,
2367                 0.99871650,0.89780453,0.65137248,0.29936312,
2368                 -0.10116832,-0.48530196,-0.79077573,-0.96807711,
2369                 -0.98846832,-0.84864425,-0.57126821,-0.20129852
2370         };
2371         /* 16 values of cos function */
2372         static float co[16] ={
2373                 1.00000000,0.91895781,0.68896691,0.34730525,
2374                 -0.05064916,-0.44039415,-0.75875812,-0.95413925,
2375                 -0.99486932,-0.87434661,-0.61210598,-0.25065253,
2376                 0.15142777,0.52896401,0.82076344,0.97952994,
2377         };
2378         int a;
2379         
2380         glColor3ub(col[0], col[1], col[2]);
2381         
2382         glBegin(GL_POLYGON);
2383         for(a=0; a<16; a++)
2384                 glVertex2f(x+size*si[a], y+size*co[a]);
2385         glEnd();
2386         
2387         glColor4ub(0, 0, 0, 150);
2388         glEnable(GL_BLEND);
2389         glEnable( GL_LINE_SMOOTH );
2390         glBegin(GL_LINE_LOOP);
2391         for(a=0; a<16; a++)
2392                 glVertex2f(x+size*si[a], y+size*co[a]);
2393         glEnd();
2394         glDisable( GL_LINE_SMOOTH );
2395         glDisable(GL_BLEND);
2396 }
2397
2398 static void socket_circle_draw(bNodeSocket *sock, float size)
2399 {
2400         int col[3];
2401         
2402         /* choose color based on sock flags */
2403         if(sock->flag & SELECT) {
2404                 if(sock->flag & SOCK_SEL) {
2405                         col[0]= 240; col[1]= 200; col[2]= 40;}
2406                 else if(sock->type==SOCK_VALUE) {
2407                         col[0]= 200; col[1]= 200; col[2]= 200;}
2408                 else if(sock->type==SOCK_VECTOR) {
2409                         col[0]= 140; col[1]= 140; col[2]= 240;}
2410                 else if(sock->type==SOCK_RGBA) {
2411                         col[0]= 240; col[1]= 240; col[2]= 100;}
2412                 else {
2413                         col[0]= 140; col[1]= 240; col[2]= 140;}
2414         }
2415         else if(sock->flag & SOCK_SEL) {
2416                 col[0]= 200; col[1]= 160; col[2]= 0;}
2417         else {
2418                 if(sock->type==-1) {
2419                         col[0]= 0; col[1]= 0; col[2]= 0;}
2420                 else if(sock->type==SOCK_VALUE) {
2421                         col[0]= 160; col[1]= 160; col[2]= 160;}
2422                 else if(sock->type==SOCK_VECTOR) {
2423                         col[0]= 100; col[1]= 100; col[2]= 200;}
2424                 else if(sock->type==SOCK_RGBA) {
2425                         col[0]= 200; col[1]= 200; col[2]= 40;}
2426                 else { 
2427                         col[0]= 100; col[1]= 200; col[2]= 100;}
2428         }
2429         
2430         circle_draw(sock->locx, sock->locy, size, sock->type, col);
2431 }
2432
2433 /* not a callback */
2434 static void node_draw_preview(bNodePreview *preview, rctf *prv)
2435 {
2436         float xscale= (prv->xmax-prv->xmin)/((float)preview->xsize);
2437         float yscale= (prv->ymax-prv->ymin)/((float)preview->ysize);
2438         float tile= (prv->xmax - prv->xmin) / 10.0;
2439         float x, y;
2440         
2441         /* draw checkerboard backdrop to show alpha */
2442         glColor3ub(120, 120, 120);
2443         glRectf(prv->xmin, prv->ymin, prv->xmax, prv->ymax);
2444         glColor3ub(160, 160, 160);
2445         
2446         for(y=prv->ymin; y<prv->ymax; y+=tile*2) {
2447                 for(x=prv->xmin; x<prv->xmax; x+=tile*2) {
2448                         float tilex= tile, tiley= tile;
2449
2450                         if(x+tile > prv->xmax)
2451                                 tilex= prv->xmax-x;
2452                         if(y+tile > prv->ymax)
2453                                 tiley= prv->ymax-y;
2454
2455                         glRectf(x, y, x + tilex, y + tiley);
2456                 }
2457         }
2458         for(y=prv->ymin+tile; y<prv->ymax; y+=tile*2) {
2459                 for(x=prv->xmin+tile; x<prv->xmax; x+=tile*2) {
2460                         float tilex= tile, tiley= tile;
2461
2462                         if(x+tile > prv->xmax)
2463                                 tilex= prv->xmax-x;
2464                         if(y+tile > prv->ymax)
2465                                 tiley= prv->ymax-y;
2466
2467                         glRectf(x, y, x + tilex, y + tiley);
2468                 }
2469         }
2470         
2471         glPixelZoom(xscale, yscale);
2472         glEnable(GL_BLEND);
2473         glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );  /* premul graphics */
2474         
2475         glColor4f(1.0, 1.0, 1.0, 1.0);
2476         glaDrawPixelsTex(prv->xmin, prv->ymin, preview->xsize, preview->ysize, GL_FLOAT, preview->rect);
2477         
2478         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
2479         glDisable(GL_BLEND);
2480         glPixelZoom(1.0f, 1.0f);
2481
2482         BIF_ThemeColorShadeAlpha(TH_BACK, -15, +100);
2483         fdrawbox(prv->xmin, prv->ymin, prv->xmax, prv->ymax);
2484         
2485 }
2486
2487 /* based on settings in node, sets drawing rect info. each redraw! */
2488 static void node_update_hidden(bNode *node)
2489 {
2490         bNodeSocket *nsock;
2491         float rad, drad, hiddenrad= HIDDEN_RAD;
2492         int totin=0, totout=0, tot;
2493         
2494         /* calculate minimal radius */
2495         for(nsock= node->inputs.first; nsock; nsock= nsock->next)
2496                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
2497                         totin++;
2498         for(nsock= node->outputs.first; nsock; nsock= nsock->next)
2499                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
2500                         totout++;
2501         
2502         tot= MAX2(totin, totout);
2503         if(tot>4) {
2504                 hiddenrad += 5.0*(float)(tot-4);
2505         }
2506         
2507         node->totr.xmin= node->locx;
2508         node->totr.xmax= node->locx + 3*hiddenrad + node->miniwidth;
2509         node->totr.ymax= node->locy + (hiddenrad - 0.5f*NODE_DY);
2510         node->totr.ymin= node->totr.ymax - 2*hiddenrad;
2511         
2512         /* output sockets */
2513         rad=drad= M_PI/(1.0f + (float)totout);
2514         
2515         for(nsock= node->outputs.first; nsock; nsock= nsock->next) {
2516                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2517                         nsock->locx= node->totr.xmax - hiddenrad + sin(rad)*hiddenrad;
2518                         nsock->locy= node->totr.ymin + hiddenrad + cos(rad)*hiddenrad;
2519                         rad+= drad;
2520                 }
2521         }
2522         
2523         /* input sockets */
2524         rad=drad= - M_PI/(1.0f + (float)totin);
2525         
2526         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
2527                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2528                         nsock->locx= node->totr.xmin + hiddenrad + sin(rad)*hiddenrad;
2529                         nsock->locy= node->totr.ymin + hiddenrad + cos(rad)*hiddenrad;
2530                         rad+= drad;
2531                 }
2532         }
2533 }
2534
2535 /* based on settings in node, sets drawing rect info. each redraw! */
2536 static void node_update(bNode *node)
2537 {
2538         bNodeSocket *nsock;
2539         float dy= node->locy;
2540         
2541         /* header */
2542         dy-= NODE_DY;
2543         
2544         /* little bit space in top */
2545         if(node->outputs.first)
2546                 dy-= NODE_DYS/2;
2547         
2548         /* output sockets */
2549         for(nsock= node->outputs.first; nsock; nsock= nsock->next) {
2550                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2551                         nsock->locx= node->locx + node->width;
2552                         nsock->locy= dy - NODE_DYS;
2553                         dy-= NODE_DY;
2554                 }
2555         }
2556         
2557         node->prvr.xmin= node->butr.xmin= node->locx + NODE_DYS;
2558         node->prvr.xmax= node->butr.xmax= node->locx + node->width- NODE_DYS;
2559         
2560         /* preview rect? */
2561         if(node->flag & NODE_PREVIEW) {
2562                 /* only recalculate size when there's a preview actually, otherwise we use stored result */
2563                 if(node->preview && node->preview->rect) {
2564                         float aspect= 1.0f;
2565                         
2566                         if(node->preview && node->preview->xsize && node->preview->ysize) 
2567                                 aspect= (float)node->preview->ysize/(float)node->preview->xsize;
2568                         
2569                         dy-= NODE_DYS/2;
2570                         node->prvr.ymax= dy;
2571                         
2572                         if(aspect <= 1.0f)
2573                                 node->prvr.ymin= dy - aspect*(node->width-NODE_DY);
2574                         else {
2575                                 float dx= (node->width - NODE_DYS) - (node->width- NODE_DYS)/aspect;    /* width correction of image */
2576                                 
2577                                 node->prvr.ymin= dy - (node->width-NODE_DY);
2578                                 
2579                                 node->prvr.xmin+= 0.5*dx;
2580                                 node->prvr.xmax-= 0.5*dx;
2581                         }
2582
2583                         dy= node->prvr.ymin - NODE_DYS/2;
2584
2585                         /* make sure that maximums are bigger or equal to minimums */
2586                         if(node->prvr.xmax < node->prvr.xmin) SWAP(float, node->prvr.xmax, node->prvr.xmin);
2587                         if(node->prvr.ymax < node->prvr.ymin) SWAP(float, node->prvr.ymax, node->prvr.ymin);
2588                 }
2589                 else {
2590                         float oldh= node->prvr.ymax - node->prvr.ymin;
2591                         if(oldh==0.0f)
2592                                 oldh= 0.6f*node->width-NODE_DY;
2593                         dy-= NODE_DYS/2;
2594                         node->prvr.ymax= dy;
2595                         node->prvr.ymin= dy - oldh;
2596                         dy= node->prvr.ymin - NODE_DYS/2;
2597                 }
2598         }
2599
2600         /* XXX ugly hack, typeinfo for group is generated */
2601         if(node->type == NODE_GROUP)
2602                 node->typeinfo->butfunc= node_buts_group;
2603         
2604         /* buttons rect? */
2605         if((node->flag & NODE_OPTIONS) && node->typeinfo->butfunc) {
2606                 dy-= NODE_DYS/2;
2607                 node->butr.ymax= dy;
2608                 node->butr.ymin= dy - (float)node->typeinfo->butfunc(NULL, NULL, node, NULL);
2609                 dy= node->butr.ymin - NODE_DYS/2;
2610         }
2611         
2612         /* input sockets */
2613         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
2614                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2615                         nsock->locx= node->locx;
2616                         nsock->locy= dy - NODE_DYS;
2617                         dy-= NODE_DY;
2618                 }
2619         }
2620         
2621         /* little bit space in end */
2622         if(node->inputs.first || (node->flag & (NODE_OPTIONS|NODE_PREVIEW))==0 )
2623                 dy-= NODE_DYS/2;
2624         
2625         node->totr.xmin= node->locx;
2626         node->totr.xmax= node->locx + node->width;
2627         node->totr.ymax= node->locy;
2628         node->totr.ymin= dy;
2629 }
2630
2631 /* based on settings in node, sets drawing rect info. each redraw! */
2632 /* note: this assumes only 1 group at a time is drawn (linked data) */
2633 /* in node->totr the entire boundbox for the group is stored */
2634 static void node_update_group(bNode *gnode)
2635 {
2636         bNodeTree *ngroup= (bNodeTree *)gnode->id;
2637         bNode *node;
2638         bNodeSocket *nsock;
2639         rctf *rect= &gnode->totr;
2640         int counter;
2641         
2642         /* center them, is a bit of abuse of locx and locy though */
2643         for(node= ngroup->nodes.first; node; node= node->next) {
2644                 node->locx+= gnode->locx;
2645                 node->locy+= gnode->locy;
2646                 if(node->flag & NODE_HIDDEN)
2647                         node_update_hidden(node);
2648                 else
2649                         node_update(node);
2650                 node->locx-= gnode->locx;
2651                 node->locy-= gnode->locy;
2652         }
2653         counter= 1;
2654         for(node= ngroup->nodes.first; node; node= node->next) {
2655                 if(counter) {
2656                         *rect= node->totr;
2657                         counter= 0;
2658                 }
2659                 else
2660                         BLI_union_rctf(rect, &node->totr);
2661         }
2662         if(counter==1) return;  /* should be prevented? */
2663         
2664         rect->xmin-= NODE_DY;
2665         rect->ymin-= NODE_DY;
2666         rect->xmax+= NODE_DY;
2667         rect->ymax+= NODE_DY;
2668         
2669         /* output sockets */
2670         for(nsock= gnode->outputs.first; nsock; nsock= nsock->next) {
2671                 nsock->locx= rect->xmax;
2672                 nsock->locy= nsock->tosock->locy;
2673         }
2674         
2675         /* input sockets */
2676         for(nsock= gnode->inputs.first; nsock; nsock= nsock->next) {
2677                 nsock->locx= rect->xmin;
2678                 nsock->locy= nsock->tosock->locy;
2679         }
2680 }
2681
2682 static void node_scaling_widget(int color_id, float aspect, float xmin, float ymin, float xmax, float ymax)
2683 {
2684         float dx;
2685         float dy;
2686         
2687         dx= 0.5f*(xmax-xmin);
2688         dy= 0.5f*(ymax-ymin);
2689         
2690         BIF_ThemeColorShade(color_id, +30);     
2691         fdrawline(xmin, ymin, xmax, ymax);
2692         fdrawline(xmin+dx, ymin, xmax, ymax-dy);
2693         
2694         BIF_ThemeColorShade(color_id, -10);
2695         fdrawline(xmin, ymin+aspect, xmax, ymax+aspect);
2696         fdrawline(xmin+dx, ymin+aspect, xmax, ymax-dy+aspect);
2697 }
2698
2699 static int node_get_colorid(bNode *node)
2700 {
2701         if(node->typeinfo->nclass==NODE_CLASS_INPUT)
2702                 return TH_NODE_IN_OUT;
2703         if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
2704                 if(node->flag & NODE_DO_OUTPUT)
2705                         return TH_NODE_IN_OUT;
2706                 else
2707                         return TH_NODE;
2708         }
2709         if(node->typeinfo->nclass==NODE_CLASS_CONVERTOR)
2710                 return TH_NODE_CONVERTOR;
2711         if(ELEM3(node->typeinfo->nclass, NODE_CLASS_OP_COLOR, NODE_CLASS_OP_VECTOR, NODE_CLASS_OP_FILTER))
2712                 return TH_NODE_OPERATOR;
2713         if(node->typeinfo->nclass==NODE_CLASS_GROUP)
2714                 return TH_NODE_GROUP;
2715         return TH_NODE;
2716 }
2717
2718 static void node_draw_link_bezier(float vec[][3], int th_col1, int th_col2, int do_shaded)
2719 {
2720         float dist;
2721         
2722         dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
2723         
2724         /* check direction later, for top sockets */
2725         vec[1][0]= vec[0][0]+dist;
2726         vec[1][1]= vec[0][1];
2727         
2728         vec[2][0]= vec[3][0]-dist;
2729         vec[2][1]= vec[3][1];
2730         
2731         if( MIN4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) > G.v2d->cur.xmax); /* clipped */  
2732         else if ( MAX4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) < G.v2d->cur.xmin); /* clipped */
2733         else {
2734                 float curve_res = 24, spline_step = 0.0f;
2735                 
2736                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
2737                 dist = 1.0f/curve_res;
2738                 
2739                 glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, vec[0]);
2740                 glBegin(GL_LINE_STRIP);
2741                 while (spline_step < 1.000001f) {
2742                         if(do_shaded)
2743                                 BIF_ThemeColorBlend(th_col1, th_col2, spline_step);
2744                         glEvalCoord1f(spline_step);
2745                         spline_step += dist;
2746                 }
2747                 glEnd();
2748         }
2749         
2750 }
2751
2752 /* note; this is used for fake links in groups too */
2753 void node_draw_link(SpaceNode *snode, bNodeLink *link)
2754 {
2755         float vec[4][3];
2756         float mx=0.0f, my=0.0f;
2757         int do_shaded= 1, th_col1= TH_WIRE, th_col2= TH_WIRE;
2758         
2759         if(link->fromnode==NULL && link->tonode==NULL)
2760                 return;
2761         
2762         /* this is dragging link */
2763         if(link->fromnode==NULL || link->tonode==NULL) {
2764                 short mval[2];
2765                 getmouseco_areawin(mval);
2766                 areamouseco_to_ipoco(G.v2d, mval, &mx, &my);
2767                 BIF_ThemeColor(TH_WIRE);
2768                 do_shaded= 0;
2769         }
2770         else {
2771                 /* going to give issues once... */
2772                 if(link->tosock->flag & SOCK_UNAVAIL)
2773                         return;
2774                 if(link->fromsock->flag & SOCK_UNAVAIL)
2775                         return;
2776                 
2777                 /* a bit ugly... but thats how we detect the internal group links */
2778                 if(link->fromnode==link->tonode) {
2779                         BIF_ThemeColorBlend(TH_BACK, TH_WIRE, 0.25f);
2780                         do_shaded= 0;
2781                 }
2782                 else {
2783                         /* check cyclic */
2784                         if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
2785                                 if(link->fromnode->flag & SELECT)
2786                                         th_col1= TH_EDGE_SELECT;
2787                                 if(link->tonode->flag & SELECT)
2788                                         th_col2= TH_EDGE_SELECT;
2789                         }                               
2790                         else {
2791                                 BIF_ThemeColor(TH_REDALERT);
2792                                 do_shaded= 0;
2793                         }
2794                 }
2795         }
2796         
2797         vec[0][2]= vec[1][2]= vec[2][2]= vec[3][2]= 0.0; /* only 2d spline, set the Z to 0*/
2798         
2799         /* in v0 and v3 we put begin/end points */
2800         if(link->fromnode) {
2801                 vec[0][0]= link->fromsock->locx;
2802                 vec[0][1]= link->fromsock->locy;
2803         }
2804         else {
2805                 vec[0][0]= mx;
2806                 vec[0][1]= my;
2807         }
2808         if(link->tonode) {
2809                 vec[3][0]= link->tosock->locx;
2810                 vec[3][1]= link->tosock->locy;
2811         }
2812         else {
2813                 vec[3][0]= mx;
2814                 vec[3][1]= my;
2815         }
2816         
2817         node_draw_link_bezier(vec, th_col1, th_col2, do_shaded);
2818 }
2819
2820
2821 /* note: in cmp_util.c is similar code, for node_compo_pass_on() */
2822 static void node_draw_mute_line(SpaceNode *snode, bNode *node)
2823 {
2824         bNodeSocket *valsock= NULL, *colsock= NULL, *vecsock= NULL;
2825         bNodeSocket *sock;
2826         float vec[4][3];
2827         int a;
2828         
2829         vec[0][2]= vec[1][2]= vec[2][2]= vec[3][2]= 0.0; /* only 2d spline, set the Z to 0*/
2830         
2831         /* connect the first value buffer in with first value out */
2832         /* connect the first RGBA buffer in with first RGBA out */
2833         
2834         /* test the inputs */
2835         for(a=0, sock= node->inputs.first; sock; sock= sock->next, a++) {
2836                 if(nodeCountSocketLinks(snode->edittree, sock)) {
2837                         if(sock->type==SOCK_VALUE && valsock==NULL) valsock= sock;
2838                         if(sock->type==SOCK_VECTOR && vecsock==NULL) vecsock= sock;
2839                         if(sock->type==SOCK_RGBA && colsock==NULL) colsock= sock;
2840                 }
2841         }
2842         
2843         /* outputs, draw lines */
2844         BIF_ThemeColor(TH_REDALERT);
2845         glEnable(GL_BLEND);
2846         glEnable( GL_LINE_SMOOTH );
2847         
2848         if(valsock || colsock || vecsock) {
2849                 for(a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
2850                         if(nodeCountSocketLinks(snode->edittree, sock)) {
2851                                 vec[3][0]= sock->locx;
2852                                 vec[3][1]= sock->locy;
2853                                 
2854                                 if(sock->type==SOCK_VALUE && valsock) {
2855                                         vec[0][0]= valsock->locx;
2856                                         vec[0][1]= valsock->locy;
2857                                         node_draw_link_bezier(vec, TH_WIRE, TH_WIRE, 0);
2858                                         valsock= NULL;
2859                                 }
2860                                 if(sock->type==SOCK_VECTOR && vecsock) {
2861                                         vec[0][0]= vecsock->locx;
2862                                         vec[0][1]= vecsock->locy;
2863                                         node_draw_link_bezier(vec, TH_WIRE, TH_WIRE, 0);
2864                                         vecsock= NULL;
2865                                 }
2866                                 if(sock->type==SOCK_RGBA && colsock) {
2867                                         vec[0][0]= colsock->locx;
2868                                         vec[0][1]= colsock->locy;
2869                                         node_draw_link_bezier(vec, TH_WIRE, TH_WIRE, 0);
2870                                         colsock= NULL;
2871                                 }
2872                         }
2873                 }
2874         }
2875         glDisable(GL_BLEND);
2876         glDisable( GL_LINE_SMOOTH );
2877 }
2878
2879
2880 static void node_draw_basis(ScrArea *sa, SpaceNode *snode, bNode *node)
2881 {
2882         bNodeSocket *sock;
2883         uiBlock *block= NULL;
2884         uiBut *bt;
2885         rctf *rct= &node->totr;
2886         float slen, iconofs;
2887         int ofs, color_id= node_get_colorid(node);
2888         char showname[128]; /* 128 used below */
2889         
2890         uiSetRoundBox(15-4);
2891         ui_dropshadow(rct, BASIS_RAD, snode->aspect, node->flag & SELECT);
2892         
2893         /* header */
2894         if(color_id==TH_NODE)
2895                 BIF_ThemeColorShade(color_id, -20);
2896         else
2897                 BIF_ThemeColor(color_id);
2898                 
2899         uiSetRoundBox(3);
2900         uiRoundBox(rct->xmin, rct->ymax-NODE_DY, rct->xmax, rct->ymax, BASIS_RAD);
2901         
2902         /* show/hide icons, note this sequence is copied in editnode.c */
2903         iconofs= rct->xmax;
2904         
2905         if(node->typeinfo->flag & NODE_PREVIEW) {
2906                 int icon_id;
2907                 
2908                 if(node->flag & (NODE_ACTIVE_ID|NODE_DO_OUTPUT))
2909                         icon_id= ICON_MATERIAL;
2910                 else
2911                         icon_id= ICON_MATERIAL_DEHLT;
2912                 iconofs-= 18.0f;
2913                 glEnable(GL_BLEND);
2914                 BIF_icon_draw_aspect_blended(iconofs, rct->ymax-NODE_DY+2, icon_id, snode->aspect, -60);
2915                 glDisable(GL_BLEND);
2916         }
2917         if(node->type == NODE_GROUP) {
2918                 
2919                 iconofs-= 18.0f;
2920                 glEnable(GL_BLEND);
2921                 if(node->id->lib) {
2922                         glPixelTransferf(GL_GREEN_SCALE, 0.7f);
2923                         glPixelTransferf(GL_BLUE_SCALE, 0.3f);
2924                         BIF_icon_draw_aspect(iconofs, rct->ymax-NODE_DY+2, ICON_NODE, snode->aspect);
2925                         glPixelTransferf(GL_GREEN_SCALE, 1.0f);
2926                         glPixelTransferf(GL_BLUE_SCALE, 1.0f);
2927                 }
2928                 else {
2929                         BIF_icon_draw_aspect_blended(iconofs, rct->ymax-NODE_DY+2, ICON_NODE, snode->aspect, -60);
2930                 }
2931                 glDisable(GL_BLEND);
2932         }
2933         if(node->typeinfo->flag & NODE_OPTIONS) {
2934                 iconofs-= 18.0f;
2935                 glEnable(GL_BLEND);
2936                 BIF_icon_draw_aspect_blended(iconofs, rct->ymax-NODE_DY+2, ICON_BUTS, snode->aspect, -60);
2937                 glDisable(GL_BLEND);
2938         }
2939         {       /* always hide/reveil unused sockets */ 
2940                 int shade;
2941
2942                 iconofs-= 18.0f;
2943                 if(node_has_hidden_sockets(node))
2944                         shade= -40;
2945                 else
2946                         shade= -90;
2947                 glEnable(GL_BLEND);
2948                 BIF_icon_draw_aspect_blended(iconofs, rct->ymax-NODE_DY+2, ICON_PLUS, snode->aspect, shade);
2949                 glDisable(GL_BLEND);
2950         }
2951         
2952         /* title */
2953         if(node->flag & SELECT) 
2954                 BIF_ThemeColor(TH_TEXT_HI);
2955         else
2956                 BIF_ThemeColorBlendShade(TH_TEXT, color_id, 0.4, 10);
2957         
2958         /* open/close entirely? */
2959         ui_draw_tria_icon(rct->xmin+8.0f, rct->ymax-NODE_DY+4.0f, snode->aspect, 'v');
2960
2961         if(node->flag & SELECT) 
2962                 BIF_ThemeColor(TH_TEXT_HI);
2963         else
2964                 BIF_ThemeColor(TH_TEXT);
2965         
2966         ui_rasterpos_safe(rct->xmin+19.0f, rct->ymax-NODE_DY+5.0f, snode->aspect);
2967         
2968         if(node->flag & NODE_MUTED)
2969                 sprintf(showname, "[%s]", node->name);
2970         else if(node->username[0])
2971                 sprintf(showname, "(%s) %s", node->username, node->name);
2972         else
2973                 BLI_strncpy(showname, node->name, 128);
2974
2975         snode_drawstring(snode, showname, (int)(iconofs - rct->xmin-18.0f));
2976
2977         /* body */
2978         BIF_ThemeColor4(TH_NODE);
2979         glEnable(GL_BLEND);
2980         uiSetRoundBox(8);
2981         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax-NODE_DY, BASIS_RAD);
2982         glDisable(GL_BLEND);
2983
2984         /* scaling indicator */
2985         node_scaling_widget(TH_NODE, snode->aspect, rct->xmax-BASIS_RAD*snode->aspect, rct->ymin, rct->xmax, rct->ymin+BASIS_RAD*snode->aspect);
2986
2987         /* outline active emphasis */
2988         if(node->flag & NODE_ACTIVE) {
2989                 glEnable(GL_BLEND);
2990                 glColor4ub(200, 200, 200, 140);
2991                 uiSetRoundBox(15-4);
2992                 gl_round_box(GL_LINE_LOOP, rct->xmin, rct->ymin, rct->xmax, rct->ymax, BASIS_RAD);
2993                 glDisable(GL_BLEND);
2994         }
2995         
2996         /* disable lines */
2997         if(node->flag & NODE_MUTED)
2998                 node_draw_mute_line(snode, node);
2999
3000         /* we make buttons for input sockets, if... */
3001         if(node->flag & NODE_OPTIONS) {
3002                 if(node->inputs.first || node->typeinfo->butfunc) {
3003                         char str[32];
3004                         
3005                         /* make unique block name, also used for handling blocks in editnode.c */
3006                         sprintf(str, "node buttons %p", node);
3007                         
3008                         block= uiNewBlock(&sa->uiblocks, str, UI_EMBOSS, UI_HELV, sa->win);
3009                         uiBlockSetFlag(block, UI_BLOCK_NO_HILITE);
3010                         if(snode->id)
3011                                 uiSetButLock(snode->id->lib!=NULL, ERROR_LIBDATA_MESSAGE);
3012                 }
3013         }
3014         
3015         /* hurmf... another candidate for callback, have to see how this works first */
3016         if(node->id && block && snode->treetype==NTREE_SHADER)
3017                 nodeShaderSynchronizeID(node, 0);
3018         
3019         /* socket inputs, buttons */
3020         for(sock= node->inputs.first; sock; sock= sock->next) {
3021                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
3022                         socket_circle_draw(sock, NODE_SOCKSIZE);
3023                         
3024                         if(block && sock->link==NULL) {
3025                                 float *butpoin= sock->ns.vec;
3026                                 
3027                                 if(sock->type==SOCK_VALUE) {
3028                                         bt= uiDefButF(block, NUM, B_NODE_EXEC+node->nr, sock->name, 
3029                                                   (short)sock->locx+NODE_DYS, (short)(sock->locy)-9, (short)node->width-NODE_DY, 17, 
3030                                                   butpoin, sock->ns.min, sock->ns.max, 10, 2, "");
3031                                         uiButSetFunc(bt, node_sync_cb, snode, node);
3032                                 }
3033                                 else if(sock->type==SOCK_VECTOR) {
3034                                         uiDefBlockBut(block, socket_vector_menu, sock, sock->name, 
3035                                                   (short)sock->locx+NODE_DYS, (short)sock->locy-9, (short)node->width-NODE_DY, 17, 
3036                                                   "");
3037                                 }
3038                                 else if(block && sock->type==SOCK_RGBA) {
3039                                         short labelw= node->width-NODE_DY-40, width;
3040                                         
3041                                         if(labelw>0) width= 40; else width= node->width-NODE_DY;
3042                                         
3043                                         bt= uiDefButF(block, COL, B_NODE_EXEC+node->nr, "", 
3044                                                 (short)(sock->locx+NODE_DYS), (short)sock->locy-8, width, 15, 
3045                                                    butpoin, 0, 0, 0, 0, "");
3046                                         uiButSetFunc(bt, node_sync_cb, snode, node);
3047                                         
3048                                         if(labelw>0) uiDefBut(block, LABEL, 0, sock->name, 
3049                                                                                    (short)(sock->locx+NODE_DYS) + 40, (short)sock->locy-8, labelw, 15, 
3050                                                                                    NULL, 0, 0, 0, 0, "");
3051                                 }
3052                         }
3053                         else {
3054                                 BIF_ThemeColor(TH_TEXT);
3055                                 ui_rasterpos_safe(sock->locx+8.0f, sock->locy-5.0f, snode->aspect);
3056                                 BIF_DrawString(snode->curfont, sock->name, 0);
3057                         }
3058                 }
3059         }
3060         
3061         /* socket outputs */
3062         for(sock= node->outputs.first; sock; sock= sock->next) {
3063                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
3064                         socket_circle_draw(sock, NODE_SOCKSIZE);
3065                         
3066                         BIF_ThemeColor(TH_TEXT);
3067                         ofs= 0;
3068                         slen= snode->aspect*BIF_GetStringWidth(snode->curfont, sock->name, 0);
3069                         while(slen > node->width) {
3070                                 ofs++;
3071                                 slen= snode->aspect*BIF_GetStringWidth(snode->curfont, sock->name+ofs, 0);
3072                         }
3073                         ui_rasterpos_safe(sock->locx-8.0f-slen, sock->locy-5.0f, snode->aspect);
3074                         BIF_DrawString(snode->curfont, sock->name+ofs, 0);
3075                 }
3076         }
3077         
3078         /* preview */
3079         if(node->flag & NODE_PREVIEW)
3080                 if(node->preview && node->preview->rect)
3081                         node_draw_preview(node->preview, &node->prvr);
3082                 
3083         /* buttons */
3084         if(node->flag & NODE_OPTIONS) {
3085                 if(block) {
3086                         if(node->typeinfo->butfunc) {
3087                                 node->typeinfo->butfunc(block, snode->nodetree, node, &node->butr);
3088                         }
3089                         uiDrawBlock(block);
3090                 }
3091         }
3092         
3