Patch #7897 Texture Nodes!
[blender-staging.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         if( ntree->type == NTREE_TEXTURE )
455                 ntreeTexCheckCyclics( ntree );
456         
457         allqueue(REDRAWBUTSSHADING, 0);
458         allqueue(REDRAWNODE, 0);
459         NodeTagChanged(ntree, node); 
460         
461         node->menunr= 0;
462 }
463
464 static void node_dynamic_update_cb(void *ntree_v, void *node_v)
465 {
466         Material *ma;
467         bNode *node= (bNode *)node_v;
468         ID *id= node->id;
469         int error= 0;
470
471         if (BTST(node->custom1, NODE_DYNAMIC_ERROR)) error= 1;
472
473         /* Users only have to press the "update" button in one pynode
474          * and we also update all others sharing the same script */
475         for (ma= G.main->mat.first; ma; ma= ma->id.next) {
476                 if (ma->nodetree) {
477                         bNode *nd;
478                         for (nd= ma->nodetree->nodes.first; nd; nd= nd->next) {
479                                 if ((nd->type == NODE_DYNAMIC) && (nd->id == id)) {
480                                         nd->custom1= 0;
481                                         nd->custom1= BSET(nd->custom1, NODE_DYNAMIC_REPARSE);
482                                         nd->menunr= 0;
483                                         if (error)
484                                                 nd->custom1= BSET(nd->custom1, NODE_DYNAMIC_ERROR);
485                                 }
486                         }
487                 }
488         }
489
490         allqueue(REDRAWBUTSSHADING, 0);
491         allqueue(REDRAWNODE, 0);
492         BIF_preview_changed(ID_MA);
493 }
494
495 static int node_buts_texture(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
496 {
497         short multi = (node->id && ((Tex*)node->id)->use_nodes && (node->type != CMP_NODE_TEXTURE));
498         
499         if(block) {
500                 uiBut *bt;
501                 char *strp;
502                 short width = (short)(butr->xmax - butr->xmin);
503                 
504                 /* browse button texture */
505                 uiBlockBeginAlign(block);
506                 IDnames_to_pupstring(&strp, NULL, "", &(G.main->tex), NULL, NULL);
507                 node->menunr= 0;
508                 bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, strp, 
509                                 butr->xmin, butr->ymin+(multi?30:0), 20, 19, 
510                                           &node->menunr, 0, 0, 0, 0, "Browse texture");
511                 uiButSetFunc(bt, node_browse_tex_cb, ntree, node);
512                 if(strp) MEM_freeN(strp);
513                 
514                 if(node->id) {
515                         bt= uiDefBut(block, TEX, B_NOP, "TE:",
516                                         butr->xmin+19, butr->ymin+(multi?30:0), butr->xmax-butr->xmin-19, 19, 
517                                                  node->id->name+2, 0.0, 19.0, 0, 0, "Texture name");
518                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
519                 }
520                 uiBlockEndAlign(block);
521                 
522                 if(multi) {
523                         char *menustr = ntreeTexOutputMenu(((Tex*)node->id)->nodetree);
524                         uiDefButS(block, MENU, B_MATPRV, menustr, butr->xmin, butr->ymin, width, 19, &node->custom1, 0, 0, 0, 0, "Which output to use, for multi-output textures");
525                         free(menustr);
526                         return 50;
527                 }
528                 return 20;
529         }       
530         else return multi? 50: 20;
531 }
532
533 static int node_buts_math(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
534
535         if(block) { 
536                 uiBut *bt; 
537
538                 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, ""); 
539                 uiButSetFunc(bt, node_but_title_cb, node, bt); 
540         } 
541         return 20; 
542 }
543
544
545 /* ****************** BUTTON CALLBACKS FOR SHADER NODES ***************** */
546
547 static void node_browse_text_cb(void *ntree_v, void *node_v)
548 {
549         bNodeTree *ntree= ntree_v;
550         bNode *node= node_v;
551         ID *oldid;
552         
553         if(node->menunr<1) return;
554         
555         if(node->id) {
556                 node->id->us--;
557         }
558         oldid= node->id;
559         node->id= BLI_findlink(&G.main->text, node->menunr-1);
560         id_us_plus(node->id);
561         BLI_strncpy(node->name, node->id->name+2, 21); /* huh? why 21? */
562
563         node->custom1= BSET(node->custom1, NODE_DYNAMIC_NEW);
564         
565         nodeSetActive(ntree, node);
566
567         allqueue(REDRAWBUTSSHADING, 0);
568         allqueue(REDRAWNODE, 0);
569
570         node->menunr= 0;
571 }
572
573 static void node_mat_alone_cb(void *node_v, void *unused)
574 {
575         bNode *node= node_v;
576         
577         node->id= (ID *)copy_material((Material *)node->id);
578         
579         BIF_undo_push("Single user material");
580         allqueue(REDRAWBUTSSHADING, 0);
581         allqueue(REDRAWNODE, 0);
582         allqueue(REDRAWOOPS, 0);
583 }
584
585 static void node_browse_mat_cb(void *ntree_v, void *node_v)
586 {
587         bNodeTree *ntree= ntree_v;
588         bNode *node= node_v;
589         
590         if(node->menunr<1) return;
591         
592         if(node->menunr==32767) {       /* code for Add New */
593                 if(node->id) {
594                         /* make copy, but make sure it doesnt have the node tag nor nodes */
595                         Material *ma= (Material *)node->id;
596                         ma->id.us--;
597                         ma= copy_material(ma);
598                         ma->use_nodes= 0;
599                         if(ma->nodetree) {
600                                 ntreeFreeTree(ma->nodetree);
601                                 MEM_freeN(ma->nodetree);
602                         }
603                         ma->nodetree= NULL;
604                         node->id= (ID *)ma;
605                 }
606                 else node->id= (ID *)add_material("MatNode");
607         }
608         else {
609                 if(node->id) node->id->us--;
610                 node->id= BLI_findlink(&G.main->mat, node->menunr-1);
611                 id_us_plus(node->id);
612         }
613         BLI_strncpy(node->name, node->id->name+2, 21);
614         
615         nodeSetActive(ntree, node);
616
617         allqueue(REDRAWBUTSSHADING, 0);
618         allqueue(REDRAWNODE, 0);
619         BIF_preview_changed(ID_MA);
620
621         node->menunr= 0;
622 }
623
624 static void node_new_mat_cb(void *ntree_v, void *node_v)
625 {
626         bNodeTree *ntree= ntree_v;
627         bNode *node= node_v;
628         
629         node->id= (ID *)add_material("MatNode");
630         BLI_strncpy(node->name, node->id->name+2, 21);
631
632         nodeSetActive(ntree, node);
633
634         allqueue(REDRAWBUTSSHADING, 0);
635         allqueue(REDRAWNODE, 0);
636         BIF_preview_changed(ID_MA);
637
638 }
639
640 static void node_texmap_cb(void *texmap_v, void *unused_v)
641 {
642         init_mapping(texmap_v);
643 }
644
645 static int node_shader_buts_material(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
646 {
647         if(block) {
648                 uiBut *bt;
649                 short dx= (short)((butr->xmax-butr->xmin)/3.0f), has_us= (node->id && node->id->us>1);
650                 short dy= (short)butr->ymin;
651                 char *strp;
652                 
653                 /* WATCH IT: we use this callback in material buttons, but then only want first row */
654                 if(butr->ymax-butr->ymin > 21.0f) dy+= 19;
655                 
656                 uiBlockBeginAlign(block);
657                 if(node->id==NULL) uiBlockSetCol(block, TH_REDALERT);
658                 else if(has_us) uiBlockSetCol(block, TH_BUT_SETTING1);
659                 else uiBlockSetCol(block, TH_BUT_SETTING2);
660                 
661                 /* browse button */
662                 IDnames_to_pupstring(&strp, NULL, "ADD NEW %x32767", &(G.main->mat), NULL, NULL);
663                 node->menunr= 0;
664                 bt= uiDefButS(block, MENU, B_NOP, strp, 
665                                   butr->xmin, dy, 19, 19, 
666                                   &node->menunr, 0, 0, 0, 0, "Browses existing choices or adds NEW");
667                 uiButSetFunc(bt, node_browse_mat_cb, ntree, node);
668                 if(strp) MEM_freeN(strp);
669                 
670                 /* Add New button */
671                 if(node->id==NULL) {
672                         bt= uiDefBut(block, BUT, B_NOP, "Add New",
673                                                  butr->xmin+19, dy, (short)(butr->xmax-butr->xmin-19.0f), 19, 
674                                                  NULL, 0.0, 0.0, 0, 0, "Add new Material");
675                         uiButSetFunc(bt, node_new_mat_cb, ntree, node);
676                         uiBlockSetCol(block, TH_AUTO);
677                 }
678                 else {
679                         /* name button */
680                         short width= (short)(butr->xmax-butr->xmin-19.0f - (has_us?19.0f:0.0f));
681                         bt= uiDefBut(block, TEX, B_NOP, "MA:",
682                                                   butr->xmin+19, dy, width, 19, 
683                                                   node->id->name+2, 0.0, 19.0, 0, 0, "Material name");
684                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
685                         
686                         /* user amount */
687                         if(has_us) {
688                                 char str1[32];
689                                 sprintf(str1, "%d", node->id->us);
690                                 bt= uiDefBut(block, BUT, B_NOP, str1, 
691                                                           butr->xmax-19, dy, 19, 19, 
692                                                           NULL, 0, 0, 0, 0, "Displays number of users. Click to make a single-user copy.");
693                                 uiButSetFunc(bt, node_mat_alone_cb, node, NULL);
694                         }
695                         
696                         /* WATCH IT: we use this callback in material buttons, but then only want first row */
697                         if(butr->ymax-butr->ymin > 21.0f) {
698                                 /* node options */
699                                 uiBlockSetCol(block, TH_AUTO);
700                                 uiDefButBitS(block, TOG, SH_NODE_MAT_DIFF, B_NODE_EXEC+node->nr, "Diff",
701                                                          butr->xmin, butr->ymin, dx, 19, 
702                                                          &node->custom1, 0, 0, 0, 0, "Material Node outputs Diffuse");
703                                 uiDefButBitS(block, TOG, SH_NODE_MAT_SPEC, B_NODE_EXEC+node->nr, "Spec",
704                                                          butr->xmin+dx, butr->ymin, dx, 19, 
705                                                          &node->custom1, 0, 0, 0, 0, "Material Node outputs Specular");
706                                 uiDefButBitS(block, TOG, SH_NODE_MAT_NEG, B_NODE_EXEC+node->nr, "Neg Normal",
707                                                          butr->xmax-dx, butr->ymin, dx, 19,
708                                                          &node->custom1, 0, 0, 0, 0, "Material Node uses inverted Normal");
709                         }
710                 }
711                 uiBlockEndAlign(block);
712         }       
713         return 38;
714 }
715
716 static int node_shader_buts_mapping(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
717 {
718         if(block) {
719                 TexMapping *texmap= node->storage;
720                 short dx= (short)((butr->xmax-butr->xmin)/7.0f);
721                 short dy= (short)(butr->ymax-19);
722                 
723                 uiBlockSetFunc(block, node_texmap_cb, texmap, NULL);    /* all buttons get this */
724                 
725                 uiBlockBeginAlign(block);
726                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->loc, -1000.0f, 1000.0f, 10, 2, "");
727                 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, "");
728                 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, "");
729                 dy-= 19;
730                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->rot, -1000.0f, 1000.0f, 1000, 1, "");
731                 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, "");
732                 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, "");
733                 dy-= 19;
734                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->size, -1000.0f, 1000.0f, 10, 2, "");
735                 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, "");
736                 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, "");
737                 dy-= 25;
738                 uiBlockBeginAlign(block);
739                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->min, -10.0f, 10.0f, 100, 2, "");
740                 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, "");
741                 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, "");
742                 dy-= 19;
743                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->max, -10.0f, 10.0f, 10, 2, "");
744                 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, "");
745                 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, "");
746                 uiBlockEndAlign(block);
747                 
748                 /* labels/options */
749                 
750                 dy= (short)(butr->ymax-19);
751                 uiDefBut(block, LABEL, B_NOP, "Loc", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
752                 dy-= 19;
753                 uiDefBut(block, LABEL, B_NOP, "Rot", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
754                 dy-= 19;
755                 uiDefBut(block, LABEL, B_NOP, "Size", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
756                 dy-= 25;
757                 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, "");
758                 dy-= 19;
759                 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, "");
760                 
761         }       
762         return 5*19 + 6;
763 }
764
765 static int node_shader_buts_vect_math(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
766
767         if(block) { 
768                 uiBut *bt; 
769         
770                 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, ""); 
771                 uiButSetFunc(bt, node_but_title_cb, node, bt); 
772         } 
773         return 20; 
774 }
775
776 static int node_shader_buts_geometry(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
777 {
778         if(block) {
779                 uiBut *but;
780                 NodeGeometry *ngeo= (NodeGeometry*)node->storage;
781
782                 if(!verify_valid_uv_name(ngeo->uvname))
783                         uiBlockSetCol(block, TH_REDALERT);
784                 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");
785                 uiButSetCompleteFunc(but, autocomplete_uv, NULL);
786                 uiBlockSetCol(block, TH_AUTO);
787
788                 if(!verify_valid_vcol_name(ngeo->colname))
789                         uiBlockSetCol(block, TH_REDALERT);
790                 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");
791                 uiButSetCompleteFunc(but, autocomplete_vcol, NULL);
792                 uiBlockSetCol(block, TH_AUTO);
793         }
794
795         return 40;
796 }
797
798 static int node_shader_buts_dynamic(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
799
800         if (block) { 
801                 uiBut *bt;
802                 SpaceNode *snode= curarea->spacedata.first;
803                 short dy= (short)butr->ymin;
804                 int xoff=0;
805
806                 /* B_NODE_EXEC is handled in butspace.c do_node_buts */
807                 if(!node->id) {
808                                 char *strp;
809                                 IDnames_to_pupstring(&strp, NULL, "", &(G.main->text), NULL, NULL);
810                                 node->menunr= 0;
811                                 bt= uiDefButS(block, MENU, B_NODE_EXEC/*+node->nr*/, strp, 
812                                                                 butr->xmin, dy, 19, 19, 
813                                                                 &node->menunr, 0, 0, 0, 0, "Browses existing choices");
814                                 uiButSetFunc(bt, node_browse_text_cb, ntree, node);
815                                 xoff=19;
816                                 if(strp) MEM_freeN(strp);       
817                 }
818                 else {
819                         bt = uiDefBut(block, BUT, B_NOP, "Update",
820                                         butr->xmin+xoff, butr->ymin+20, 50, 19,
821                                         &node->menunr, 0.0, 19.0, 0, 0, "Refresh this node (and all others that use the same script)");
822                         uiButSetFunc(bt, node_dynamic_update_cb, ntree, node);
823
824                         if (BTST(node->custom1, NODE_DYNAMIC_ERROR)) {
825                                 BIF_ThemeColor(TH_REDALERT);
826                                 ui_rasterpos_safe(butr->xmin + xoff, butr->ymin + 5, snode->aspect);
827                                 snode_drawstring(snode, "Error! Check console...", butr->xmax - butr->xmin);
828                         }
829                 }
830         }
831         return 20+19; 
832 }
833
834 /* only once called */
835 static void node_shader_set_butfunc(bNodeType *ntype)
836 {
837         switch(ntype->type) {
838                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
839
840                 case SH_NODE_MATERIAL:
841                 case SH_NODE_MATERIAL_EXT:
842                         ntype->butfunc= node_shader_buts_material;
843                         break;
844                 case SH_NODE_TEXTURE:
845                         ntype->butfunc= node_buts_texture;
846                         break;
847                 case SH_NODE_NORMAL:
848                         ntype->butfunc= node_buts_normal;
849                         break;
850                 case SH_NODE_CURVE_VEC:
851                         ntype->butfunc= node_buts_curvevec;
852                         break;
853                 case SH_NODE_CURVE_RGB:
854                         ntype->butfunc= node_buts_curvecol;
855                         break;
856                 case SH_NODE_MAPPING:
857                         ntype->butfunc= node_shader_buts_mapping;
858                         break;
859                 case SH_NODE_VALUE:
860                         ntype->butfunc= node_buts_value;
861                         break;
862                 case SH_NODE_RGB:
863                         ntype->butfunc= node_buts_rgb;
864                         break;
865                 case SH_NODE_MIX_RGB:
866                         ntype->butfunc= node_buts_mix_rgb;
867                         break;
868                 case SH_NODE_VALTORGB:
869                         ntype->butfunc= node_buts_valtorgb;
870                         break;
871                 case SH_NODE_MATH: 
872                         ntype->butfunc= node_buts_math;
873                         break; 
874                 case SH_NODE_VECT_MATH: 
875                         ntype->butfunc= node_shader_buts_vect_math;
876                         break; 
877                 case SH_NODE_GEOMETRY:
878                         ntype->butfunc= node_shader_buts_geometry;
879                         break;
880                 case NODE_DYNAMIC:
881                         ntype->butfunc= node_shader_buts_dynamic;
882                         break;
883                 default:
884                         ntype->butfunc= NULL;
885         }
886 }
887
888 /* ****************** BUTTON CALLBACKS FOR COMPOSITE NODES ***************** */
889
890
891
892 static void node_browse_image_cb(void *ntree_v, void *node_v)
893 {
894         bNodeTree *ntree= ntree_v;
895         bNode *node= node_v;
896         
897         nodeSetActive(ntree, node);
898         
899         if(node->menunr<1) return;
900         if(node->menunr==32767) {       /* code for Load New */
901                 addqueue(curarea->win, UI_BUT_EVENT, B_NODE_LOADIMAGE);
902         }
903         else {
904                 if(node->id) node->id->us--;
905                 node->id= BLI_findlink(&G.main->image, node->menunr-1);
906                 id_us_plus(node->id);
907
908                 BLI_strncpy(node->name, node->id->name+2, 21);
909
910                 NodeTagChanged(ntree, node); 
911                 BKE_image_signal((Image *)node->id, node->storage, IMA_SIGNAL_USER_NEW_IMAGE);
912                 addqueue(curarea->win, UI_BUT_EVENT, B_NODE_EXEC+node->nr);
913         }
914         node->menunr= 0;
915 }
916
917 static void node_active_cb(void *ntree_v, void *node_v)
918 {
919         nodeSetActive(ntree_v, node_v);
920 }
921 static void node_image_type_cb(void *node_v, void *unused)
922 {
923         
924         allqueue(REDRAWNODE, 1);
925 }
926
927 static char *node_image_type_pup(void)
928 {
929         char *str= MEM_mallocN(256, "image type pup");
930         int a;
931         
932         str[0]= 0;
933         
934         a= sprintf(str, "Image Type %%t|");
935         a+= sprintf(str+a, "  Image %%x%d %%i%d|", IMA_SRC_FILE, ICON_IMAGE_DEHLT);
936         a+= sprintf(str+a, "  Movie %%x%d %%i%d|", IMA_SRC_MOVIE, ICON_SEQUENCE);
937         a+= sprintf(str+a, "  Sequence %%x%d %%i%d|", IMA_SRC_SEQUENCE, ICON_IMAGE_COL);
938         a+= sprintf(str+a, "  Generated %%x%d %%i%d", IMA_SRC_GENERATED, ICON_BLANK1);
939         
940         return str;
941 }
942
943 /* copy from buttons_shading.c */
944 static char *layer_menu(RenderResult *rr)
945 {
946         RenderLayer *rl;
947         int len= 40 + 40*BLI_countlist(&rr->layers);
948         short a, nr;
949         char *str= MEM_callocN(len, "menu layers");
950         
951         strcpy(str, "Layer %t");
952         a= strlen(str);
953         for(nr=0, rl= rr->layers.first; rl; rl= rl->next, nr++) {
954                 a+= sprintf(str+a, "|%s %%x%d", rl->name, nr);
955         }
956         
957         return str;
958 }
959
960 static void image_layer_cb(void *ima_v, void *iuser_v)
961 {
962         
963         ntreeCompositForceHidden(G.scene->nodetree);
964         BKE_image_multilayer_index(ima_v, iuser_v);
965         allqueue(REDRAWNODE, 0);
966 }
967
968 static int node_composit_buts_image(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
969 {
970         ImageUser *iuser= node->storage;
971         
972         if(block) {
973                 uiBut *bt;
974                 short dy= (short)butr->ymax-19;
975                 char *strp;
976                 
977                 uiBlockBeginAlign(block);
978                 uiBlockSetCol(block, TH_BUT_SETTING2);
979                 
980                 /* browse button */
981                 IMAnames_to_pupstring(&strp, NULL, "LOAD NEW %x32767", &(G.main->image), NULL, NULL);
982                 node->menunr= 0;
983                 bt= uiDefButS(block, MENU, B_NOP, strp, 
984                                           butr->xmin, dy, 19, 19, 
985                                           &node->menunr, 0, 0, 0, 0, "Browses existing choices");
986                 uiButSetFunc(bt, node_browse_image_cb, ntree, node);
987                 if(strp) MEM_freeN(strp);
988                 
989                 /* Add New button */
990                 if(node->id==NULL) {
991                         bt= uiDefBut(block, BUT, B_NODE_LOADIMAGE, "Load New",
992                                                  butr->xmin+19, dy, (short)(butr->xmax-butr->xmin-19.0f), 19, 
993                                                  NULL, 0.0, 0.0, 0, 0, "Add new Image");
994                         uiButSetFunc(bt, node_active_cb, ntree, node);
995                         uiBlockSetCol(block, TH_AUTO);
996                 }
997                 else {
998                         /* name button + type */
999                         Image *ima= (Image *)node->id;
1000                         short xmin= (short)butr->xmin, xmax= (short)butr->xmax;
1001                         short width= xmax - xmin - 45;
1002                         short icon= ICON_IMAGE_DEHLT;
1003                         
1004                         if(ima->source==IMA_SRC_MOVIE) icon= ICON_SEQUENCE;
1005                         else if(ima->source==IMA_SRC_SEQUENCE) icon= ICON_IMAGE_COL;
1006                         else if(ima->source==IMA_SRC_GENERATED) icon= ICON_BLANK1;
1007                         
1008                         bt= uiDefBut(block, TEX, B_NOP, "IM:",
1009                                                  xmin+19, dy, width, 19, 
1010                                                  node->id->name+2, 0.0, 19.0, 0, 0, "Image name");
1011                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
1012                         
1013                         /* buffer type option */
1014                         strp= node_image_type_pup();
1015                         bt= uiDefIconTextButS(block, MENU, B_NOP, icon, strp,
1016                                                  xmax-26, dy, 26, 19, 
1017                                                  &ima->source, 0.0, 19.0, 0, 0, "Image type");
1018                         uiButSetFunc(bt, node_image_type_cb, node, ima);
1019                         MEM_freeN(strp);
1020                         
1021                         if( ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE) ) {
1022                                 width= (xmax-xmin)/2;
1023                                 
1024                                 dy-= 19;
1025                                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "Frs:",
1026                                                   xmin, dy, width, 19, 
1027                                                   &iuser->frames, 1.0, MAXFRAMEF, 0, 0, "Amount of images used in animation");
1028                                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "SFra:",
1029                                                   xmin+width, dy, width, 19, 
1030                                                   &iuser->sfra, 1.0, MAXFRAMEF, 0, 0, "Start frame of animation");
1031                                 dy-= 19;
1032                                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "Offs:",
1033                                                   xmin, dy, width, 19, 
1034                                                   &iuser->offset, -MAXFRAMEF, MAXFRAMEF, 0, 0, "Offsets the number of the frame to use in the animation");
1035                                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Cycl",
1036                                                   xmin+width, dy, width-20, 19, 
1037                                                   &iuser->cycl, 0.0, 0.0, 0, 0, "Make animation go cyclic");
1038                                 uiDefIconButBitS(block, TOG, IMA_ANIM_ALWAYS, B_NODE_EXEC+node->nr, ICON_AUTO,
1039                                                   xmax-20, dy, 20, 19, 
1040                                                   &iuser->flag, 0.0, 0.0, 0, 0, "Always refresh Image on frame changes");
1041                         }
1042                         if( ima->type==IMA_TYPE_MULTILAYER && ima->rr) {
1043                                 RenderLayer *rl= BLI_findlink(&ima->rr->layers, iuser->layer);
1044                                 if(rl) {
1045                                         width= (xmax-xmin);
1046                                         dy-= 19;
1047                                         strp= layer_menu(ima->rr);
1048                                         bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, strp,
1049                                                           xmin, dy, width, 19, 
1050                                                           &iuser->layer, 0.0, 10000.0, 0, 0, "Layer");
1051                                         uiButSetFunc(bt, image_layer_cb, ima->rr, node->storage);
1052                                         MEM_freeN(strp);
1053                                 }
1054                         }
1055                 }
1056                 
1057         }       
1058         if(node->id) {
1059                 Image *ima= (Image *)node->id;
1060                 int retval= 19;
1061                 
1062                 /* for each draw we test for anim refresh event */
1063                 if(iuser->flag & IMA_ANIM_REFRESHED) {
1064                         iuser->flag &= ~IMA_ANIM_REFRESHED;
1065                         addqueue(curarea->win, UI_BUT_EVENT, B_NODE_EXEC+node->nr);
1066                 }
1067                 
1068                 if( ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE) )
1069                         retval+= 38;
1070                 if( ima->type==IMA_TYPE_MULTILAYER)
1071                         retval+= 19;
1072                 return retval;
1073         }
1074         else
1075                 return 19;
1076 }
1077
1078 /* if we use render layers from other scene, we make a nice title */
1079 static void set_render_layers_title(void *node_v, void *unused)
1080 {
1081         bNode *node= node_v;
1082         Scene *sce;
1083         SceneRenderLayer *srl;
1084         char str[64];
1085         
1086         if(node->id) {
1087                 BLI_strncpy(str, node->id->name+2, 21);
1088                 strcat(str, "|");
1089                 sce= (Scene *)node->id;
1090         }
1091         else {
1092                 str[0]= 0;
1093                 sce= G.scene;
1094         }
1095         srl= BLI_findlink(&sce->r.layers, node->custom1);
1096         if(srl==NULL) {
1097                 node->custom1= 0;
1098                 srl= sce->r.layers.first;
1099         }
1100         
1101         strcat(str, srl->name);
1102         BLI_strncpy(node->name, str, 32);
1103 }
1104
1105 static char *scene_layer_menu(Scene *sce)
1106 {
1107         SceneRenderLayer *srl;
1108         int len= 40 + 40*BLI_countlist(&sce->r.layers);
1109         short a, nr;
1110         char *str= MEM_callocN(len, "menu layers");
1111         
1112         strcpy(str, "Active Layer %t");
1113         a= strlen(str);
1114         for(nr=0, srl= sce->r.layers.first; srl; srl= srl->next, nr++) {
1115                 a+= sprintf(str+a, "|%s %%x%d", srl->name, nr);
1116         }
1117         
1118         return str;
1119 }
1120
1121 static void node_browse_scene_cb(void *ntree_v, void *node_v)
1122 {
1123         bNodeTree *ntree= ntree_v;
1124         bNode *node= node_v;
1125         Scene *sce;
1126         
1127         if(node->menunr<1) return;
1128         
1129         if(node->id) {
1130                 node->id->us--;
1131                 node->id= NULL;
1132         }
1133         sce= BLI_findlink(&G.main->scene, node->menunr-1);
1134         if(sce!=G.scene) {
1135                 node->id= &sce->id;
1136                 id_us_plus(node->id);
1137         }
1138         
1139         set_render_layers_title(node, NULL);
1140         nodeSetActive(ntree, node);
1141
1142         allqueue(REDRAWBUTSSHADING, 0);
1143         allqueue(REDRAWNODE, 0);
1144         NodeTagChanged(ntree, node); 
1145
1146         node->menunr= 0;
1147 }
1148
1149
1150 static int node_composit_buts_renderlayers(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1151 {
1152         if(block) {
1153                 uiBut *bt;
1154                 char *strp;
1155                 
1156                 /* browse button scene */
1157                 uiBlockBeginAlign(block);
1158                 IDnames_to_pupstring(&strp, NULL, "", &(G.main->scene), NULL, NULL);
1159                 node->menunr= 0;
1160                 bt= uiDefButS(block, MENU, B_NOP, strp, 
1161                                           butr->xmin, butr->ymin, 20, 19, 
1162                                           &node->menunr, 0, 0, 0, 0, "Browse Scene to use RenderLayer from");
1163                 uiButSetFunc(bt, node_browse_scene_cb, ntree, node);
1164                 if(strp) MEM_freeN(strp);
1165                 
1166                 /* browse button layer */
1167                 strp= scene_layer_menu(node->id?(Scene *)node->id:G.scene);
1168                 if(node->id)
1169                         bt= uiDefIconTextButS(block, MENU, B_NODE_EXEC+node->nr, ICON_SCENE_DEHLT, strp, 
1170                                   butr->xmin+20, butr->ymin, (butr->xmax-butr->xmin)-40, 19, 
1171                                   &node->custom1, 0, 0, 0, 0, "Choose Render Layer");
1172                 else
1173                         bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, strp, 
1174                                   butr->xmin+20, butr->ymin, (butr->xmax-butr->xmin)-40, 19, 
1175                                   &node->custom1, 0, 0, 0, 0, "Choose Render Layer");
1176                 uiButSetFunc(bt, set_render_layers_title, node, NULL);
1177                 MEM_freeN(strp);
1178                 
1179                 /* re-render */
1180                 /* uses custom2, not the best implementation of the world... but we need it to work now :) */
1181                 bt= uiDefIconButS(block, TOG, B_NODE_EXEC+node->nr, ICON_SCENE, 
1182                                   butr->xmax-20, butr->ymin, 20, 19, 
1183                                   &node->custom2, 0, 0, 0, 0, "Re-render this Layer");
1184                 
1185         }
1186         return 19;
1187 }
1188
1189 static void node_blur_relative_cb(void *node, void *poin2)
1190 {
1191         bNode *nodev= node;
1192         NodeBlurData *nbd= nodev->storage;
1193         if(nbd->image_in_width != 0){
1194                 if(nbd->relative){ /* convert absolute values to relative */
1195                         nbd->percentx= (float)(nbd->sizex)/nbd->image_in_width;
1196                         nbd->percenty= (float)(nbd->sizey)/nbd->image_in_height;
1197                 }else{ /* convert relative values to absolute */
1198                         nbd->sizex= (int)(nbd->percentx*nbd->image_in_width);
1199                         nbd->sizey= (int)(nbd->percenty*nbd->image_in_height);
1200                 }
1201         }
1202         allqueue(REDRAWNODE, 0);
1203 }
1204 static void node_blur_update_sizex_cb(void *node, void *poin2)
1205 {
1206         bNode *nodev= node;
1207         NodeBlurData *nbd= nodev->storage;
1208
1209         nbd->sizex= (int)(nbd->percentx*nbd->image_in_width);
1210 }
1211 static void node_blur_update_sizey_cb(void *node, void *poin2)
1212 {
1213         bNode *nodev= node;
1214         NodeBlurData *nbd= nodev->storage;
1215
1216         nbd->sizey= (int)(nbd->percenty*nbd->image_in_height);
1217 }
1218 static int node_composit_buts_blur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1219 {
1220         if(block) {
1221                 NodeBlurData *nbd= node->storage;
1222                 uiBut *bt;
1223                 short dy= butr->ymin+58;
1224                 short dx= (butr->xmax-butr->xmin)/2;
1225                 char str[256];
1226                 
1227                 uiBlockBeginAlign(block);
1228                 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);
1229                 uiDefButS(block, MENU, B_NODE_EXEC+node->nr,str,
1230                                   butr->xmin, dy, dx*2, 19, 
1231                                   &nbd->filtertype, 0, 0, 0, 0, "Set sampling filter for blur");
1232                 dy-=19;
1233                 if (nbd->filtertype != R_FILTER_FAST_GAUSS) { 
1234                         uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Bokeh",
1235                                         butr->xmin, dy, dx, 19, 
1236                                         &nbd->bokeh, 0, 0, 0, 0, "Uses circular filter, warning it's slow!");
1237                         uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Gamma",
1238                                         butr->xmin+dx, dy, dx, 19, 
1239                                         &nbd->gamma, 0, 0, 0, 0, "Applies filter on gamma corrected values");
1240                 } else {
1241                         uiBlockEndAlign(block);
1242                         uiBlockBeginAlign(block);
1243                 }
1244                 dy-=19;
1245                 bt= uiDefButS(block, TOG, B_NOP, "Relative",
1246                                   butr->xmin, dy, dx*2, 19,
1247                                   &nbd->relative, 0, 0, 0, 0, "Use relative (percent) values to define blur radius");
1248                 uiButSetFunc(bt, node_blur_relative_cb, node, NULL);
1249
1250                 dy-=19;
1251                 if(nbd->relative) {
1252                         bt= uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "X:",
1253                                                  butr->xmin, dy, dx, 19, 
1254                                                  &nbd->percentx, 0.0f, 1.0f, 0, 0, "");
1255                         uiButSetFunc(bt, node_blur_update_sizex_cb, node, NULL);
1256                         bt= uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Y:",
1257                                                  butr->xmin+dx, dy, dx, 19, 
1258                                                  &nbd->percenty, 0.0f, 1.0f, 0, 0, "");
1259                         uiButSetFunc(bt, node_blur_update_sizey_cb, node, NULL);
1260                 }
1261                 else {
1262                         uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "X:",
1263                                                  butr->xmin, dy, dx, 19, 
1264                                                  &nbd->sizex, 0, 256, 0, 0, "");
1265                         uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Y:",
1266                                                  butr->xmin+dx, dy, dx, 19, 
1267                                                  &nbd->sizey, 0, 256, 0, 0, "");
1268                 }
1269                 uiBlockEndAlign(block);
1270         }
1271         return 77;
1272 }
1273
1274 static int node_composit_buts_dblur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1275 {
1276         if(block) {
1277                 NodeDBlurData *ndbd = node->storage;
1278                 short dy = butr->ymin + 171;
1279                 short dx = butr->xmax - butr->xmin;
1280                 short halfdx= (short)dx/2;
1281
1282                 uiBlockBeginAlign(block);
1283                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Iterations:",
1284                                 butr->xmin, dy, dx, 19,
1285                                 &ndbd->iter, 1, 32, 10, 0, "Amount of iterations");
1286                 uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Wrap",
1287                                 butr->xmin, dy-= 19, dx, 19, 
1288                                 &ndbd->wrap, 0, 0, 0, 0, "Wrap blur");
1289                 uiBlockEndAlign(block);
1290
1291                 dy-= 9;
1292
1293                 uiDefBut(block, LABEL, B_NOP, "Center", butr->xmin, dy-= 19, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
1294
1295                 uiBlockBeginAlign(block);
1296                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "X:",
1297                                 butr->xmin, dy-= 19, halfdx, 19,
1298                                 &ndbd->center_x, 0.0f, 1.0f, 10, 0, "X center in percents");
1299                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Y:",
1300                                 butr->xmin+halfdx, dy, halfdx, 19,
1301                                 &ndbd->center_y, 0.0f, 1.0f, 10, 0, "Y center in percents");
1302                 uiBlockEndAlign(block);
1303
1304                 dy-= 9;
1305
1306                 uiBlockBeginAlign(block);
1307                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Distance:",
1308                                 butr->xmin, dy-= 19, dx, 19,
1309                                 &ndbd->distance, -1.0f, 1.0f, 10, 0, "Amount of which the image moves");
1310                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Angle:",
1311                                 butr->xmin, dy-= 19, dx, 19,
1312                                 &ndbd->angle, 0.0f, 360.0f, 1000, 0, "Angle in which the image will be moved");
1313                 uiBlockEndAlign(block);
1314
1315                 dy-= 9;
1316
1317                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Spin:",
1318                                 butr->xmin, dy-= 19, dx, 19,
1319                                 &ndbd->spin, -360.0f, 360.0f, 1000, 0, "Angle that is used to spin the image");
1320
1321                 dy-= 9;
1322
1323                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Zoom:",
1324                                 butr->xmin, dy-= 19, dx, 19,
1325                                 &ndbd->zoom, 0.0f, 100.0f, 100, 0, "Amount of which the image is zoomed");
1326
1327         }
1328         return 190;
1329 }
1330
1331 static int node_composit_buts_bilateralblur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1332 {
1333         if(block) {
1334                 NodeBilateralBlurData *nbbd= node->storage;
1335                 short dy= butr->ymin+38;
1336                 short dx= (butr->xmax-butr->xmin);
1337                 
1338                 uiBlockBeginAlign(block);
1339                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Iterations:",
1340                                  butr->xmin, dy, dx, 19, 
1341                                  &nbbd->iter, 1, 128, 0, 0, "Amount of iterations");
1342                 dy-=19;
1343                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Color Sigma:",
1344                                   butr->xmin, dy, dx, 19, 
1345                                   &nbbd->sigma_color,0.01, 3, 10, 0, "Sigma value used to modify color");
1346                 dy-=19;
1347                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Space Sigma:",
1348                                   butr->xmin, dy, dx, 19, 
1349                                   &nbbd->sigma_space ,0.01, 30, 10, 0, "Sigma value used to modify space");
1350                 
1351         }
1352         return 57;
1353 }
1354
1355 /* qdn: defocus node */
1356 static int node_composit_buts_defocus(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1357 {
1358         if(block) {
1359                 NodeDefocus *nqd = node->storage;
1360                 short dy = butr->ymin + 209;
1361                 short dx = butr->xmax - butr->xmin; 
1362                 char* mstr1 = "Bokeh Type%t|Octagon %x8|Heptagon %x7|Hexagon %x6|Pentagon %x5|Square %x4|Triangle %x3|Disk %x0";
1363
1364                 uiDefBut(block, LABEL, B_NOP, "Bokeh Type", butr->xmin, dy, dx, 19, NULL, 0, 0, 0, 0, "");
1365                 uiDefButC(block, MENU, B_NODE_EXEC+node->nr, mstr1,
1366                           butr->xmin, dy-19, dx, 19,
1367                           &nqd->bktype, 0, 0, 0, 0, "Bokeh type");
1368                 if (nqd->bktype) { /* for some reason rotating a disk doesn't seem to work... ;) */
1369                         uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "Rotate:",
1370                                   butr->xmin, dy-38, dx, 19,
1371                                   &nqd->rotation, 0, 90, 0, 0, "Bokeh shape rotation offset in degrees");
1372                 }
1373                 uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Gamma Correct",
1374                           butr->xmin, dy-57, dx, 19,
1375                           &nqd->gamco, 0, 0, 0, 0, "Enable gamma correction before and after main process");
1376                 if (nqd->no_zbuf==0) {
1377                         // only needed for zbuffer input
1378                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "fStop:",
1379                                   butr->xmin, dy-76, dx, 19,
1380                                   &nqd->fstop, 0.5, 128, 10, 0, "Amount of focal blur, 128=infinity=perfect focus, half the value doubles the blur radius");
1381                 }
1382                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Maxblur:",
1383                           butr->xmin, dy-95, dx, 19,
1384                           &nqd->maxblur, 0, 10000, 1000, 0, "blur limit, maximum CoC radius, 0=no limit");
1385                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "BThreshold:",
1386                           butr->xmin, dy-114, dx, 19,
1387                           &nqd->bthresh, 0, 100, 100, 0, "CoC radius threshold, prevents background bleed on in-focus midground, 0=off");
1388                 uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Preview",
1389                           butr->xmin, dy-142, dx, 19,
1390                           &nqd->preview, 0, 0, 0, 0, "Enable sampling mode, useful for preview when using low samplecounts");
1391                 if (nqd->preview) {
1392                         /* only visible when sampling mode enabled */
1393                         uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Samples:",
1394                                   butr->xmin, dy-161, dx, 19,
1395                                   &nqd->samples, 16, 256, 0, 0, "Number of samples (16=grainy, higher=less noise)");
1396                 }
1397                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "No zbuffer",
1398                           butr->xmin, dy-190, dx, 19,
1399                           &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)");
1400                 if (nqd->no_zbuf) {
1401                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Zscale:",
1402                             butr->xmin, dy-209, dx, 19,
1403                             &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");
1404                 }
1405         }
1406         return 228;
1407 }
1408
1409
1410 /* qdn: glare node */
1411 static int node_composit_buts_glare(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1412 {
1413         if(block) {
1414                 NodeGlare *ndg = node->storage;
1415                 short dy = butr->ymin + 152, dx = butr->xmax - butr->xmin; 
1416                 char* mn1 = "Type%t|Ghosts%x3|Streaks%x2|Fog Glow%x1|Simple Star%x0";
1417                 char* mn2 = "Quality/Speed%t|High/Slow%x0|Medium/Medium%x1|Low/Fast%x2";
1418                 uiDefButC(block, MENU, B_NODE_EXEC+node->nr, mn1,
1419                           butr->xmin, dy, dx, 19,
1420                           &ndg->type, 0, 0, 0, 0, "Glow/Flare/Bloom type");
1421                 uiDefButC(block, MENU, B_NODE_EXEC+node->nr, mn2,
1422                           butr->xmin, dy-19, dx, 19,
1423                           &ndg->quality, 0, 0, 0, 0,
1424                           "Quality speed trade off, if not set to high quality, effect will be applied to low-res copy of source image");
1425                 if (ndg->type != 1) {
1426                         uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "Iterations:",
1427                                   butr->xmin, dy-38, dx, 19,
1428                                   &ndg->iter, 2, 5, 1, 0,
1429                                   "higher values will generate longer/more streaks/ghosts");
1430                         if (ndg->type != 0)
1431                                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "ColMod:",
1432                                           butr->xmin, dy-57, dx, 19,
1433                                           &ndg->colmod, 0, 1, 10, 0,
1434                                           "Amount of Color Modulation, modulates colors of streaks and ghosts for a spectral dispersion effect");
1435                 }
1436                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Mix:",
1437                           butr->xmin, dy-76, dx, 19,
1438                           &ndg->mix, -1, 1, 10, 0,
1439                           "Mix balance, -1 is original image only, 0 is exact 50/50 mix, 1 is processed image only");
1440                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Threshold:",
1441                           butr->xmin, dy-95, dx, 19,
1442                           &ndg->threshold, 0, 1000, 10, 0,
1443                           "Brightness threshold, the glarefilter will be applied only to pixels brighter than this value");
1444                 if ((ndg->type == 2) || (ndg->type == 0))
1445                 {
1446                         if (ndg->type == 2) {
1447                                 uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "streaks:",
1448                                           butr->xmin, dy-114, dx, 19,
1449                                           &ndg->angle, 2, 16, 1000, 0,
1450                                           "Total number of streaks");
1451                                 uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "AngOfs:",
1452                                           butr->xmin, dy-133, dx, 19,
1453                                           &ndg->angle_ofs, 0, 180, 1000, 0,
1454                                           "Streak angle rotation offset in degrees");
1455                         }
1456                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Fade:",
1457                                   butr->xmin, dy-152, dx, 19,
1458                                   &ndg->fade, 0.75, 1, 5, 0,
1459                                   "Streak fade out factor");
1460                 }
1461                 if (ndg->type == 0)
1462                         uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Rot45",
1463                                   butr->xmin, dy-114, dx, 19,
1464                                   &ndg->angle, 0, 0, 0, 0,
1465                                   "simple star filter, add 45 degree rotation offset");
1466                 if ((ndg->type == 1) || (ndg->type > 3))        // PBGH and fog glow
1467                         uiDefButC(block, NUM, B_NODE_EXEC+node->nr, "Size:",
1468                                   butr->xmin, dy-114, dx, 19,
1469                                   &ndg->size, 6, 9, 1000, 0,
1470                                   "glow/glare size (not actual size, relative to initial size of bright area of pixels)");
1471         }
1472         return 171;
1473 }
1474
1475 /* qdn: tonemap node */
1476 static int node_composit_buts_tonemap(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1477 {
1478         if(block) {
1479                 NodeTonemap *ntm = node->storage;
1480                 short dy = butr->ymin + 76, dx = butr->xmax - butr->xmin; 
1481                 char* mn = "Type%t|R/D Photoreceptor%x1|Rh Simple%x0";
1482                 
1483                 uiBlockBeginAlign(block);
1484                 uiDefButI(block, MENU, B_NODE_EXEC+node->nr, mn,
1485                           butr->xmin, dy, dx, 19,
1486                           &ntm->type, 0, 0, 0, 0,
1487                           "Tone mapping type");
1488                 if (ntm->type == 0) {
1489                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Key:",
1490                                   butr->xmin, dy-19, dx, 19,
1491                                   &ntm->key, 0, 1, 5, 0,
1492                                   "The value the average luminance is mapped to");
1493                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Offset:",
1494                                   butr->xmin, dy-38, dx, 19,
1495                                   &ntm->offset, 0.001, 10, 5, 0,
1496                                   "Tonemap offset, normally always 1, but can be used as an extra control to alter the brightness curve");
1497                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Gamma:",
1498                                   butr->xmin, dy-57, dx, 19,
1499                                   &ntm->gamma, 0.001, 3, 5, 0,
1500                                   "Gamma factor, if not used, set to 1");
1501                 }
1502                 else {
1503                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Intensity:",
1504                                   butr->xmin, dy-19, dx, 19,
1505                                   &ntm->f, -8, 8, 10, 0, "if less than zero, darkens image, otherwise makes it brighter");
1506                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Contrast:",
1507                                   butr->xmin, dy-38, dx, 19,
1508                                   &ntm->m, 0, 1, 5, 0, "Set to 0 to use estimate from input image");
1509                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Adaptation:",
1510                                   butr->xmin, dy-57, dx, 19,
1511                                   &ntm->a, 0, 1, 5, 0, "if 0, global, if 1, based on pixel intensity");
1512                         uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "ColCorrect:",
1513                                   butr->xmin, dy-76, dx, 19,
1514                                   &ntm->c, 0, 1, 5, 0, "color correction, if 0, same for all channels, if 1, each independent");
1515                 }
1516                 uiBlockEndAlign(block);
1517         }
1518         return 95;
1519 }
1520
1521 /* qdn: lens distortion node */
1522 static int node_composit_buts_lensdist(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1523 {
1524         if(block) {
1525                 NodeLensDist *nld = node->storage;
1526                 short dy = butr->ymin + 19, dx = butr->xmax - butr->xmin; 
1527                 uiBlockBeginAlign(block);
1528                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Projector",
1529                           butr->xmin, dy, dx, 19,
1530                           &nld->proj, 0, 0, 0, 0,
1531                           "Enable/disable projector mode, effect is applied in horizontal direction only");
1532                 if (!nld->proj) {
1533                         uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Jitter",
1534                                   butr->xmin, dy-19, dx/2, 19,
1535                                   &nld->jit, 0, 0, 0, 0,
1536                                   "Enable/disable jittering, faster, but also noisier");
1537                         uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Fit",
1538                                   butr->xmin+dx/2, dy-19, dx/2, 19,
1539                                   &nld->fit, 0, 0, 0, 0,
1540                                   "For positive distortion factor only, scale image such that black areas are not visible");
1541                 }
1542                 uiBlockEndAlign(block);
1543         }
1544         return 38;
1545 }
1546
1547
1548 static int node_composit_buts_vecblur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1549 {
1550         if(block) {
1551                 NodeBlurData *nbd= node->storage;
1552                 short dy= butr->ymin;
1553                 short dx= (butr->xmax-butr->xmin);
1554                 
1555                 uiBlockBeginAlign(block);
1556                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Samples:",
1557                                  butr->xmin, dy+76, dx, 19, 
1558                                  &nbd->samples, 1, 256, 0, 0, "Amount of samples");
1559                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "MinSpeed:",
1560                                   butr->xmin, dy+57, dx, 19, 
1561                                   &nbd->minspeed, 0, 1024, 0, 0, "Minimum speed for a pixel to be blurred, used to separate background from foreground");
1562                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "MaxSpeed:",
1563                                   butr->xmin, dy+38, dx, 19, 
1564                                   &nbd->maxspeed, 0, 1024, 0, 0, "If not zero, maximum speed in pixels");
1565                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "BlurFac:",
1566                                   butr->xmin, dy+19, dx, 19, 
1567                                   &nbd->fac, 0.0f, 2.0f, 10, 2, "Scaling factor for motion vectors, actually 'shutter speed' in frames");
1568                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Curved",
1569                                   butr->xmin, dy, dx, 19, 
1570                                   &nbd->curved, 0.0f, 2.0f, 10, 2, "Interpolate between frames in a bezier curve, rather than linearly");
1571                 uiBlockEndAlign(block);
1572         }
1573         return 95;
1574 }
1575
1576 static int node_composit_buts_filter(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1577 {
1578         if(block) {
1579                 uiBut *bt;
1580                 
1581                 /* blend type */
1582                 bt=uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Soften %x0|Sharpen %x1|Laplace %x2|Sobel %x3|Prewitt %x4|Kirsch %x5|Shadow %x6",
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_flip(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
1591 {
1592         if(block) {
1593                 uiBut *bt;
1594                 
1595                 /* flip x\y */
1596                 bt=uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Flip X %x0|Flip Y %x1|Flip X & Y %x2",
1597                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1598                                          &node->custom1, 0, 0, 0, 0, "");
1599                 uiButSetFunc(bt, node_but_title_cb, node, bt);
1600         }
1601         return 20;      
1602 }
1603
1604 static int node_composit_buts_crop(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1605 {
1606         if(block) {
1607                 NodeTwoXYs *ntxy= node->storage;
1608                 char elementheight = 19;
1609                 short dx= (butr->xmax-butr->xmin)/2;
1610                 short dy= butr->ymax - elementheight;
1611                 short xymin= 0, xymax= 10000;
1612
1613                 uiBlockBeginAlign(block);
1614
1615                 /* crop image size toggle */
1616                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "Crop Image Size",
1617                                   butr->xmin, dy, dx*2, elementheight, 
1618                                   &node->custom1, 0, 0, 0, 0, "Crop the size of the input image.");
1619
1620                 dy-=elementheight;
1621
1622                 /* x1 */
1623                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "X1:",
1624                                          butr->xmin, dy, dx, elementheight,
1625                                          &ntxy->x1, xymin, xymax, 0, 0, "");
1626                 /* y1 */
1627                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Y1:",
1628                                          butr->xmin+dx, dy, dx, elementheight,
1629                                          &ntxy->y1, xymin, xymax, 0, 0, "");
1630
1631                 dy-=elementheight;
1632
1633                 /* x2 */
1634                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "X2:",
1635                                          butr->xmin, dy, dx, elementheight,
1636                                          &ntxy->x2, xymin, xymax, 0, 0, "");
1637                 /* y2 */
1638                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Y2:",
1639                                          butr->xmin+dx, dy, dx, elementheight,
1640                                          &ntxy->y2, xymin, xymax, 0, 0, "");
1641
1642                 uiBlockEndAlign(block);
1643         }
1644         return 60;
1645 }
1646
1647 static int node_composit_buts_splitviewer(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1648 {
1649         if(block) {     
1650                 uiBlockBeginAlign(block);
1651                 
1652                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "X",
1653                                   butr->xmin, butr->ymin+19, (butr->xmax-butr->xmin)/2, 20, 
1654                                   &node->custom2, 0.0, 0.0, 0, 0, "");
1655                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Y",
1656                                   butr->xmin+(butr->xmax-butr->xmin)/2, butr->ymin+19, (butr->xmax-butr->xmin)/2, 20, 
1657                                   &node->custom2, 0.0, 1.0, 0, 0, "");
1658                                   
1659                 uiDefButS(block, NUMSLI, B_NODE_EXEC+node->nr, "Split %: ",
1660                                 butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, &node->custom1, 0, 100, 10, 0, "");
1661         }
1662         return 40;
1663 }
1664
1665 static int node_composit_buts_map_value(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1666 {
1667         if(block) {
1668                 TexMapping *texmap= node->storage;
1669                 short xstart= (short)butr->xmin;
1670                 short dy= (short)(butr->ymax-19.0f);
1671                 short dx= (short)(butr->xmax-butr->xmin)/2;
1672                 
1673                 uiBlockBeginAlign(block);
1674                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Offs:", xstart, dy, 2*dx, 19, texmap->loc, -1000.0f, 1000.0f, 10, 2, "");
1675                 dy-= 19;
1676                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Size:", xstart, dy, 2*dx, 19, texmap->size, -1000.0f, 1000.0f, 10, 3, "");
1677                 dy-= 23;
1678                 uiBlockBeginAlign(block);
1679                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MIN, B_NODE_EXEC+node->nr, "Min", xstart, dy, dx, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
1680                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", xstart+dx, dy, dx, 19, texmap->min, -1000.0f, 1000.0f, 10, 2, "");
1681                 dy-= 19;
1682                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MAX, B_NODE_EXEC+node->nr, "Max", xstart, dy, dx, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
1683                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", xstart+dx, dy, dx, 19, texmap->max, -1000.0f, 1000.0f, 10, 2, "");
1684         }
1685         return 80;
1686 }
1687
1688 static int node_composit_buts_alphaover(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1689 {
1690         if(block) {
1691                 NodeTwoFloats *ntf= node->storage;
1692                 
1693                 /* alpha type */
1694                 uiDefButS(block, TOG, B_NODE_EXEC+node->nr, "ConvertPremul",
1695                                   butr->xmin, butr->ymin+19, butr->xmax-butr->xmin, 19, 
1696                                   &node->custom1, 0, 0, 0, 0, "");
1697                 /* mix factor */
1698                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Premul: ",
1699                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 19, 
1700                                   &ntf->x, 0.0f, 1.0f, 100, 0, "");
1701         }
1702         return 38;
1703 }
1704
1705 static int node_composit_buts_hue_sat(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1706 {
1707         if(block) {
1708                 NodeHueSat *nhs= node->storage;
1709                 
1710                 uiBlockBeginAlign(block);
1711                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Hue: ",
1712                                   butr->xmin, butr->ymin+40.0f, butr->xmax-butr->xmin, 20, 
1713                                   &nhs->hue, 0.0f, 1.0f, 100, 0, "");
1714                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Sat: ",
1715                                   butr->xmin, butr->ymin+20.0f, butr->xmax-butr->xmin, 20, 
1716                                   &nhs->sat, 0.0f, 2.0f, 100, 0, "");
1717                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Val: ",
1718                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1719                                   &nhs->val, 0.0f, 2.0f, 100, 0, "");
1720         }
1721         return 60;
1722 }
1723
1724 static int node_composit_buts_dilateerode(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1725 {
1726         if(block) {
1727                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Distance:",
1728                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1729                                   &node->custom2, -100, 100, 0, 0, "Distance to grow/shrink (number of iterations)");
1730         }
1731         return 20;
1732 }
1733
1734 static int node_composit_buts_diff_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1735 {
1736         if(block) {
1737                 short sx= (butr->xmax-butr->xmin)/4;
1738                 short dx= (butr->xmax-butr->xmin)/3;
1739                 NodeChroma *c= node->storage;
1740                 
1741                 uiBlockBeginAlign(block);
1742                 /*color space selectors*/
1743                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"RGB",
1744                                                         butr->xmin,butr->ymin+60,sx,20,
1745                                                         &node->custom1,1,1, 0, 0, "RGB Color Space");
1746                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"HSV",
1747                                                         butr->xmin+sx,butr->ymin+60,sx,20,
1748                                                         &node->custom1,1,2, 0, 0, "HSV Color Space");
1749                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"YUV",
1750                                                         butr->xmin+2*sx,butr->ymin+60,sx,20,
1751                                                         &node->custom1,1,3, 0, 0, "YUV Color Space");
1752                                         uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"YCC",
1753                                                         butr->xmin+3*sx,butr->ymin+60,sx,20,
1754                                                         &node->custom1,1,4, 0, 0, "YCbCr Color Space");
1755                 /*channel tolorences*/
1756                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, " ",
1757                                                         butr->xmin, butr->ymin+40, dx, 20,
1758                                                         &c->t1, 0.0f, 1.0f, 100, 0, "Channel 1 Tolerance");
1759                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, " ",
1760                                                         butr->xmin+dx, butr->ymin+40, dx, 20,
1761                                                         &c->t2, 0.0f, 1.0f, 100, 0, "Channel 2 Tolorence");
1762                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, " ",
1763                                                         butr->xmin+2*dx, butr->ymin+40, dx, 20,
1764                                                         &c->t3, 0.0f, 1.0f, 100, 0, "Channel 3 Tolorence");
1765                 /*falloff parameters*/
1766                 /*
1767                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Falloff Size ",
1768                         butr->xmin, butr->ymin+20, butr->xmax-butr->xmin, 20,
1769                         &c->fsize, 0.0f, 1.0f, 100, 0, "");
1770                 */
1771                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Falloff: ",
1772                         butr->xmin, butr->ymin+20, butr->xmax-butr->xmin, 20,
1773                         &c->fstrength, 0.0f, 1.0f, 100, 0, "");
1774         }
1775         return 80;
1776 }
1777
1778 static int node_composit_buts_color_spill(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1779 {
1780         if(block) {
1781                 short dx= (butr->xmax-butr->xmin)/3;
1782
1783                 NodeChroma *c=node->storage;
1784                 uiBlockBeginAlign(block);
1785                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Enhance: ", 
1786                                 butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1787                                 &c->t1, 0.0f, 0.5f, 100, 2, "Adjusts how much selected channel is affected by color spill algorithm");
1788                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "R",
1789                                 butr->xmin,butr->ymin,dx,20,
1790                                 &node->custom1,1,1, 0, 0, "Red Spill Suppression");
1791                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "G",
1792                                 butr->xmin+dx,butr->ymin,dx,20,
1793                                 &node->custom1,1,2, 0, 0, "Green Spill Suppression");
1794                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "B",
1795                                 butr->xmin+2*dx,butr->ymin,dx,20,
1796                                 &node->custom1, 1, 3, 0, 0, "Blue Spill Suppression");
1797                 uiBlockEndAlign(block);
1798         }
1799         return 60;
1800 }
1801
1802 static int node_composit_buts_chroma_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1803 {
1804         if(block) {
1805                 short dx=(butr->xmax-butr->xmin)/2;
1806                 NodeChroma *c= node->storage;
1807                 uiBlockBeginAlign(block);
1808
1809                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Acceptance ",
1810                         butr->xmin, butr->ymin+60, butr->xmax-butr->xmin, 20,
1811                         &c->t1, 1.0f, 80.0f, 100, 0, "Tolerance for colors to be considered a keying color");
1812                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Cutoff ",
1813                         butr->xmin, butr->ymin+40, butr->xmax-butr->xmin, 20,
1814                         &c->t2, 0.0f, 30.0f, 100, 0, "Colors below this will be considered as exact matches for keying color");
1815
1816                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Lift ",
1817                         butr->xmin, butr->ymin+20, dx, 20,
1818                         &c->fsize, 0.0f, 1.0f, 100, 0, "Alpha Lift");
1819                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Gain ",
1820                         butr->xmin+dx, butr->ymin+20, dx, 20,
1821                         &c->fstrength, 0.0f, 1.0f, 100, 0, "Alpha Gain");
1822
1823                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Shadow Adjust ",
1824                         butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1825                         &c->t3, 0.0f, 1.0f, 100, 0, "Adjusts the brightness of any shadows captured");
1826
1827                 if(c->t2 > c->t1)
1828                         c->t2=c->t1;
1829         }
1830         return 80;
1831 }
1832
1833 static int node_composit_buts_channel_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1834 {
1835         if(block) {
1836                 short sx= (butr->xmax-butr->xmin)/4;
1837                 short cx= (butr->xmax-butr->xmin)/3;
1838                 NodeChroma *c=node->storage;
1839                 char *c1, *c2, *c3;
1840         
1841                 /*color space selectors*/
1842                 uiBlockBeginAlign(block);
1843                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"RGB",
1844                         butr->xmin,butr->ymin+60,sx,20,&node->custom1,1,1, 0, 0, "RGB Color Space");
1845                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"HSV",
1846                         butr->xmin+sx,butr->ymin+60,sx,20,&node->custom1,1,2, 0, 0, "HSV Color Space");
1847                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"YUV",
1848                         butr->xmin+2*sx,butr->ymin+60,sx,20,&node->custom1,1,3, 0, 0, "YUV Color Space");
1849                 uiDefButS(block, ROW,B_NODE_EXEC+node->nr,"YCC",
1850                         butr->xmin+3*sx,butr->ymin+60,sx,20,&node->custom1,1,4, 0, 0, "YCbCr Color Space");
1851         
1852                 if (node->custom1==1) {
1853                         c1="R"; c2="G"; c3="B";
1854                 }
1855                 else if(node->custom1==2){
1856                         c1="H"; c2="S"; c3="V";
1857                 }
1858                 else if(node->custom1==3){
1859                         c1="Y"; c2="U"; c3="V";
1860                 }
1861                 else { // if(node->custom1==4){
1862                         c1="Y"; c2="Cb"; c3="Cr";
1863                 }
1864         
1865                 /*channel selector */
1866                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, c1,
1867                         butr->xmin,butr->ymin+40,cx,20,&node->custom2,1, 1, 0, 0, "Channel 1");
1868                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, c2,
1869                         butr->xmin+cx,butr->ymin+40,cx,20,&node->custom2,1, 2, 0, 0, "Channel 2");
1870                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, c3,
1871                         butr->xmin+cx+cx,butr->ymin+40,cx,20,&node->custom2, 1, 3, 0, 0, "Channel 3");
1872         
1873                 /*tolerance sliders */
1874                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "High ", 
1875                         butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1876                         &c->t1, 0.0f, 1.0f, 100, 0, "Values higher than this setting are 100% opaque");
1877                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Low ", 
1878                         butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1879                         &c->t2, 0.0f, 1.0f, 100, 0, "Values lower than this setting are 100% keyed");
1880                 uiBlockEndAlign(block);
1881         
1882                 /*keep t2 (low) less than t1 (high) */
1883                 if(c->t2 > c->t1) {
1884                         c->t2=c->t1;
1885                 }
1886         }
1887         return 80;
1888 }
1889
1890 static int node_composit_buts_luma_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1891 {
1892         if(block) {
1893                 NodeChroma *c=node->storage;
1894         
1895                 /*tolerance sliders */
1896                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "High ", 
1897                         butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1898                         &c->t1, 0.0f, 1.0f, 100, 0, "Values higher than this setting are 100% opaque");
1899                 uiDefButF(block, NUMSLI, B_NODE_EXEC+node->nr, "Low ", 
1900                         butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1901                         &c->t2, 0.0f, 1.0f, 100, 0, "Values lower than this setting are 100% keyed");
1902                 uiBlockEndAlign(block);
1903         
1904                 /*keep t2 (low) less than t1 (high) */
1905                 if(c->t2 > c->t1) {
1906                         c->t2=c->t1;
1907                 }
1908         }
1909         return 40;
1910 }
1911
1912 static int node_composit_buts_map_uv(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1913 {
1914         if(block) {
1915                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Alpha:",
1916                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1917                                   &node->custom1, 0, 100, 0, 0, "Conversion percentage of UV differences to Alpha");
1918         }
1919         return 20;
1920 }
1921
1922 static int node_composit_buts_id_mask(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1923 {
1924         if(block) {
1925                 uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "ID:",
1926                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1927                                   &node->custom1, 0, 10000, 0, 0, "Pass Index number to convert to Alpha");
1928         }
1929         return 20;
1930 }
1931
1932 /* allocate sufficient! */
1933 static void node_imagetype_string(char *str)
1934 {
1935         str += sprintf(str, "Save Image as: %%t|");
1936         str += sprintf(str, "Targa %%x%d|", R_TARGA);
1937         str += sprintf(str, "Targa Raw %%x%d|", R_RAWTGA);
1938         str += sprintf(str, "PNG %%x%d|", R_PNG);
1939         str += sprintf(str, "BMP %%x%d|", R_BMP);
1940         str += sprintf(str, "Jpeg %%x%d|", R_JPEG90);
1941         str += sprintf(str, "Iris %%x%d|", R_IRIS);
1942         str += sprintf(str, "Radiance HDR %%x%d|", R_RADHDR);
1943         str += sprintf(str, "Cineon %%x%d|", R_CINEON);
1944         str += sprintf(str, "DPX %%x%d|", R_DPX);
1945         str += sprintf(str, "OpenEXR %%x%d", R_OPENEXR);
1946 }
1947
1948 static void node_set_image_cb(void *ntree_v, void *node_v)
1949 {
1950         bNodeTree *ntree= ntree_v;
1951         bNode *node= node_v;
1952         
1953         nodeSetActive(ntree, node);
1954 }
1955
1956 static int node_composit_buts_file_output(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1957 {
1958         if(block) {
1959                 NodeImageFile *nif= node->storage;
1960                 uiBut *bt;
1961                 short x= (short)butr->xmin;
1962                 short y= (short)butr->ymin;
1963                 short w= (short)butr->xmax-butr->xmin;
1964                 char str[320];
1965                 
1966                 node_imagetype_string(str);
1967                 
1968                 uiBlockBeginAlign(block);
1969                 
1970                 bt = uiDefIconBut(block, BUT, B_NODE_SETIMAGE, ICON_FILESEL,
1971                                   x, y+60, 20, 20,
1972                                   0, 0, 0, 0, 0, "Open Fileselect to get Backbuf image");
1973                 uiButSetFunc(bt, node_set_image_cb, ntree, node);
1974                 
1975                 uiDefBut(block, TEX, B_NOP, "",
1976                                   20+x, y+60, w-20, 20, 
1977                                   nif->name, 0.0f, 240.0f, 0, 0, "");
1978                 
1979                 uiDefButS(block, MENU, B_NOP, str,
1980                                   x, y+40, w, 20, 
1981                                   &nif->imtype, 0.0f, 1.0f, 0, 0, "");
1982                 
1983                 if(nif->imtype==R_OPENEXR) {
1984                         uiDefButBitS(block, TOG, R_OPENEXR_HALF, B_REDR, "Half",        
1985                                                 x, y+20, w/2, 20, 
1986                                                 &nif->subimtype, 0, 0, 0, 0, "");
1987
1988                         uiDefButS(block, MENU,B_NOP, "Codec %t|None %x0|Pxr24 (lossy) %x1|ZIP (lossless) %x2|PIZ (lossless) %x3|RLE (lossless) %x4",  
1989                                                 x+w/2, y+20, w/2, 20, 
1990                                                 &nif->codec, 0, 0, 0, 0, "");
1991                 }
1992                 else {
1993                         uiDefButS(block, NUM, B_NOP, "Quality: ",
1994                                   x, y+20, w, 20, 
1995                                   &nif->quality, 10.0f, 100.0f, 10, 0, "");
1996                 }
1997                 
1998                 /* start frame, end frame */
1999                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "SFra: ", 
2000                                   x, y, w/2, 20, 
2001                                   &nif->sfra, 1, MAXFRAMEF, 10, 0, "");
2002                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "EFra: ", 
2003                                   x+w/2, y, w/2, 20, 
2004                                   &nif->efra, 1, MAXFRAMEF, 10, 0, "");
2005                 
2006         }
2007         return 80;
2008 }
2009
2010 static void node_scale_cb(void *node_v, void *unused_v)
2011 {
2012         bNode *node= node_v;
2013         bNodeSocket *nsock;
2014
2015         /* check the 2 inputs, and set them to reasonable values */
2016         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
2017                 if(ELEM(node->custom1, CMP_SCALE_RELATIVE, CMP_SCALE_SCENEPERCENT))
2018                         nsock->ns.vec[0]= 1.0;
2019                 else {
2020                         if(nsock->next==NULL)
2021                                 nsock->ns.vec[0]= (float)G.scene->r.ysch;
2022                         else
2023                                 nsock->ns.vec[0]= (float)G.scene->r.xsch;
2024                 }
2025         }       
2026 }
2027
2028 static int node_composit_buts_scale(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
2029 {
2030         if(block) {
2031                 uiBut *bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Relative %x0|Absolute %x1|Scene Size % %x2|",
2032                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
2033                                   &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");
2034                 uiButSetFunc(bt, node_scale_cb, node, NULL);
2035         }
2036         return 20;
2037 }
2038
2039 static int node_composit_buts_invert(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
2040 {
2041         if(block) {
2042                 uiBlockBeginAlign(block);
2043                 uiDefButBitS(block, TOG, CMP_CHAN_RGB, B_NODE_EXEC+node->nr, "RGB",
2044                                          butr->xmin, butr->ymin, (butr->xmax-butr->xmin)/2, 20, 
2045                                          &node->custom1, 0, 0, 0, 0, "");
2046                 uiDefButBitS(block, TOG, CMP_CHAN_A, B_NODE_EXEC+node->nr, "A",
2047                                          butr->xmin+(butr->xmax-butr->xmin)/2, butr->ymin, (butr->xmax-butr->xmin)/2, 20, 
2048                                          &node->custom1, 0, 0, 0, 0, "");
2049                 uiBlockEndAlign(block);
2050         }
2051         return 20;      
2052 }
2053
2054 static int node_composit_buts_premulkey(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
2055 {
2056         if(block) {
2057                 uiBut *bt;
2058                 
2059                 /* blend type */
2060                 bt=uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Key to Premul %x0|Premul to Key %x1",
2061                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
2062                                          &node->custom1, 0, 0, 0, 0, "Conversion between premultiplied alpha and key alpha");
2063         }
2064         return 20;
2065 }
2066
2067 /* only once called */
2068 static void node_composit_set_butfunc(bNodeType *ntype)
2069 {
2070         switch(ntype->type) {
2071                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
2072
2073                 case CMP_NODE_IMAGE:
2074                         ntype->butfunc= node_composit_buts_image;
2075                         break;
2076                 case CMP_NODE_R_LAYERS:
2077                         ntype->butfunc= node_composit_buts_renderlayers;
2078                         break;
2079                 case CMP_NODE_NORMAL:
2080                         ntype->butfunc= node_buts_normal;
2081                         break;
2082                 case CMP_NODE_CURVE_VEC:
2083                         ntype->butfunc= node_buts_curvevec;
2084                         break;
2085                 case CMP_NODE_CURVE_RGB:
2086                         ntype->butfunc= node_buts_curvecol;
2087                         break;
2088                 case CMP_NODE_VALUE:
2089                         ntype->butfunc= node_buts_value;
2090                         break;
2091                 case CMP_NODE_RGB:
2092                         ntype->butfunc= node_buts_rgb;
2093                         break;
2094                 case CMP_NODE_FLIP:
2095                         ntype->butfunc= node_composit_buts_flip;
2096                         break;
2097                 case CMP_NODE_SPLITVIEWER:
2098                         ntype->butfunc= node_composit_buts_splitviewer;
2099                         break;
2100                 case CMP_NODE_MIX_RGB:
2101                         ntype->butfunc= node_buts_mix_rgb;
2102                         break;
2103                 case CMP_NODE_VALTORGB:
2104                         ntype->butfunc= node_buts_valtorgb;
2105                         break;
2106                 case CMP_NODE_CROP:
2107                         ntype->butfunc= node_composit_buts_crop;
2108                         break;
2109                 case CMP_NODE_BLUR:
2110                         ntype->butfunc= node_composit_buts_blur;
2111                         break;
2112                 case CMP_NODE_DBLUR:
2113                         ntype->butfunc= node_composit_buts_dblur;
2114                         break;
2115                 case CMP_NODE_BILATERALBLUR:
2116                         ntype->butfunc= node_composit_buts_bilateralblur;
2117                         break;
2118                 /* qdn: defocus node */
2119                 case CMP_NODE_DEFOCUS:
2120                         ntype->butfunc = node_composit_buts_defocus;
2121                         break;
2122                 /* qdn: glare node */
2123                 case CMP_NODE_GLARE:
2124                         ntype->butfunc = node_composit_buts_glare;
2125                         break;
2126                 /* qdn: tonemap node */
2127                 case CMP_NODE_TONEMAP:
2128                         ntype->butfunc = node_composit_buts_tonemap;
2129                         break;
2130                 /* qdn: lens distortion node */
2131                 case CMP_NODE_LENSDIST:
2132                         ntype->butfunc = node_composit_buts_lensdist;
2133                         break;
2134                 case CMP_NODE_VECBLUR:
2135                         ntype->butfunc= node_composit_buts_vecblur;
2136                         break;
2137                 case CMP_NODE_FILTER:
2138                         ntype->butfunc= node_composit_buts_filter;
2139                         break;
2140                 case CMP_NODE_MAP_VALUE:
2141                         ntype->butfunc= node_composit_buts_map_value;
2142                         break;
2143                 case CMP_NODE_TIME:
2144                         ntype->butfunc= node_buts_time;
2145                         break;
2146                 case CMP_NODE_ALPHAOVER:
2147                         ntype->butfunc= node_composit_buts_alphaover;
2148                         break;
2149                 case CMP_NODE_HUE_SAT:
2150                         ntype->butfunc= node_composit_buts_hue_sat;
2151                         break;
2152                 case CMP_NODE_TEXTURE:
2153                         ntype->butfunc= node_buts_texture;
2154                         break;
2155                 case CMP_NODE_DILATEERODE:
2156                         ntype->butfunc= node_composit_buts_dilateerode;
2157                         break;
2158                 case CMP_NODE_OUTPUT_FILE:
2159                         ntype->butfunc= node_composit_buts_file_output;
2160                         break;
2161         
2162                 case CMP_NODE_DIFF_MATTE:
2163                         ntype->butfunc=node_composit_buts_diff_matte;
2164                         break;
2165                 case CMP_NODE_COLOR_SPILL:
2166                         ntype->butfunc=node_composit_buts_color_spill;
2167                         break;
2168                 case CMP_NODE_CHROMA:
2169                         ntype->butfunc=node_composit_buts_chroma_matte;
2170                         break;
2171                 case CMP_NODE_SCALE:
2172                         ntype->butfunc= node_composit_buts_scale;
2173                         break;
2174                 case CMP_NODE_CHANNEL_MATTE:
2175                         ntype->butfunc= node_composit_buts_channel_matte;
2176                         break;
2177                 case CMP_NODE_LUMA_MATTE:
2178                         ntype->butfunc= node_composit_buts_luma_matte;
2179                         break;
2180                 case CMP_NODE_MAP_UV:
2181                         ntype->butfunc= node_composit_buts_map_uv;
2182                         break;
2183                 case CMP_NODE_ID_MASK:
2184                         ntype->butfunc= node_composit_buts_id_mask;
2185                         break;
2186                 case CMP_NODE_MATH:
2187                         ntype->butfunc= node_buts_math;
2188                         break;
2189                 case CMP_NODE_INVERT:
2190                         ntype->butfunc= node_composit_buts_invert;
2191                         break;
2192                 case CMP_NODE_PREMULKEY:
2193                         ntype->butfunc= node_composit_buts_premulkey;
2194                         break;
2195                 default:
2196                         ntype->butfunc= NULL;
2197         }
2198 }
2199
2200 /* ****************** BUTTON CALLBACKS FOR TEXTURE NODES ***************** */
2201
2202 static int node_texture_buts_bricks(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
2203 {
2204         if(block) {
2205                 short w = butr->xmax-butr->xmin;
2206                 short ofw = 32;
2207                 
2208                 uiBlockBeginAlign(block);
2209                 
2210                 /* Offset */
2211                 uiDefButF(
2212                         block, NUM, B_NODE_EXEC+node->nr, "Offset",
2213                         butr->xmin, butr->ymin+20, w-ofw, 20,
2214                         &node->custom3,
2215                         0, 1, 0.25, 2,
2216                         "Offset amount" );
2217                 uiDefButS(
2218                         block, NUM, B_NODE_EXEC+node->nr, "",
2219                         butr->xmin+w-ofw, butr->ymin+20, ofw, 20,
2220                         &node->custom1,
2221                         2, 99, 0, 0,
2222                         "Offset every N rows" );
2223                 
2224                 /* Squash */
2225                 uiDefButF(
2226                         block, NUM, B_NODE_EXEC+node->nr, "Squash",
2227                         butr->xmin, butr->ymin+0, w-ofw, 20,
2228                         &node->custom4,
2229                         0, 99, 0.25, 2,
2230                         "Stretch amount" );
2231                 uiDefButS(
2232                         block, NUM, B_NODE_EXEC+node->nr, "",
2233                         butr->xmin+w-ofw, butr->ymin+0, ofw, 20,
2234                         &node->custom2,
2235                         2, 99, 0, 0,
2236                         "Stretch every N rows" );
2237                 
2238                 uiBlockEndAlign(block);
2239         }
2240         return 40;
2241 }
2242
2243 /* Copied from buttons_shading.c -- needs unifying */
2244 static char* noisebasis_menu()
2245 {
2246         static char nbmenu[256];
2247         sprintf(nbmenu, "Noise Basis %%t|Blender Original %%x%d|Original Perlin %%x%d|Improved Perlin %%x%d|Voronoi F1 %%x%d|Voronoi F2 %%x%d|Voronoi F3 %%x%d|Voronoi F4 %%x%d|Voronoi F2-F1 %%x%d|Voronoi Crackle %%x%d|CellNoise %%x%d", TEX_BLENDER, TEX_STDPERLIN, TEX_NEWPERLIN, TEX_VORONOI_F1, TEX_VORONOI_F2, TEX_VORONOI_F3, TEX_VORONOI_F4, TEX_VORONOI_F2F1, TEX_VORONOI_CRACKLE, TEX_CELLNOISE);
2248         return nbmenu;
2249 }
2250
2251 static int node_texture_buts_proc(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
2252 {
2253         Tex *tex = (Tex *)node->storage;
2254         short x,y,w,h;
2255         
2256         if( block ) {
2257                 x = butr->xmin;
2258                 y = butr->ymin;
2259                 w = butr->xmax - x;
2260                 h = butr->ymax - y;
2261         }
2262         
2263         switch( tex->type ) {
2264                 case TEX_BLEND:
2265                         if( block ) {
2266                                 uiBlockBeginAlign( block );
2267                                 uiDefButS( block, MENU, B_NODE_EXEC+node->nr,
2268                                         "Linear %x0|Quad %x1|Ease %x2|Diag %x3|Sphere %x4|Halo %x5|Radial %x6",
2269                                         x, y+20, w, 20, &tex->stype, 0, 1, 0, 0, "Blend Type" );
2270                                 uiDefButBitS(block, TOG, TEX_FLIPBLEND, B_NODE_EXEC+node->nr, "Flip XY", x, y, w, 20,
2271                                         &tex->flag, 0, 0, 0, 0, "Flips the direction of the progression 90 degrees");
2272                                 uiBlockEndAlign( block );
2273                         }
2274                         return 40;
2275                         
2276                         
2277                 case TEX_MARBLE:
2278                         if( block ) {
2279                                 uiBlockBeginAlign(block);
2280                         
2281                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Soft",       0*w/3+x, 40+y, w/3, 18, &tex->stype, 2.0, (float)TEX_SOFT, 0, 0, "Uses soft marble"); 
2282                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Sharp",      1*w/3+x, 40+y, w/3, 18, &tex->stype, 2.0, (float)TEX_SHARP, 0, 0, "Uses more clearly defined marble"); 
2283                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Sharper",    2*w/3+x, 40+y, w/3, 18, &tex->stype, 2.0, (float)TEX_SHARPER, 0, 0, "Uses very clearly defined marble"); 
2284                                 
2285                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Soft noise", 0*w/2+x, 20+y, w/2, 19, &tex->noisetype, 12.0, (float)TEX_NOISESOFT, 0, 0, "Generates soft noise");
2286                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Hard noise", 1*w/2+x, 20+y, w/2, 19, &tex->noisetype, 12.0, (float)TEX_NOISEPERL, 0, 0, "Generates hard noise");
2287                                 
2288                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Sin",        0*w/3+x,  0+y, w/3, 18, &tex->noisebasis2, 8.0, 0.0, 0, 0, "Uses a sine wave to produce bands."); 
2289                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Saw",        1*w/3+x,  0+y, w/3, 18, &tex->noisebasis2, 8.0, 1.0, 0, 0, "Uses a saw wave to produce bands"); 
2290                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Tri",        2*w/3+x,  0+y, w/3, 18, &tex->noisebasis2, 8.0, 2.0, 0, 0, "Uses a triangle wave to produce bands"); 
2291             
2292                                 uiBlockEndAlign(block);
2293                         }
2294                         return 60;
2295                         
2296                 case TEX_WOOD:
2297                         if( block ) {
2298                                 uiDefButS(block, MENU, B_TEXPRV, noisebasis_menu(), x, y+64, w, 18, &tex->noisebasis, 0,0,0,0, "Sets the noise basis used for turbulence");
2299                                 
2300                                 uiBlockBeginAlign(block);
2301                                 uiDefButS(block, ROW, B_TEXPRV,             "Bands",     x,  40+y, w/2, 18, &tex->stype, 2.0, (float)TEX_BANDNOISE, 0, 0, "Uses standard noise"); 
2302                                 uiDefButS(block, ROW, B_TEXPRV,             "Rings", w/2+x,  40+y, w/2, 18, &tex->stype, 2.0, (float)TEX_RINGNOISE, 0, 0, "Lets Noise return RGB value"); 
2303                                 
2304                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Sin", 0*w/3+x,  20+y, w/3, 18, &tex->noisebasis2, 8.0, (float)TEX_SIN, 0, 0, "Uses a sine wave to produce bands."); 
2305                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Saw", 1*w/3+x,  20+y, w/3, 18, &tex->noisebasis2, 8.0, (float)TEX_SAW, 0, 0, "Uses a saw wave to produce bands"); 
2306                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Tri", 2*w/3+x,  20+y, w/3, 18, &tex->noisebasis2, 8.0, (float)TEX_TRI, 0, 0, "Uses a triangle wave to produce bands");
2307                                 
2308                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Soft noise", 0*w/2+x, 0+y, w/2, 19, &tex->noisetype, 12.0, (float)TEX_NOISESOFT, 0, 0, "Generates soft noise");
2309                                 uiDefButS(block, ROW, B_NODE_EXEC+node->nr, "Hard noise", 1*w/2+x, 0+y, w/2, 19, &tex->noisetype, 12.0, (float)TEX_NOISEPERL, 0, 0, "Generates hard noise");
2310                                 uiBlockEndAlign(block);
2311                         }
2312                         return 80; 
2313                         
2314                 case TEX_CLOUDS:
2315                         if( block ) {
2316                                 uiDefButS(block, MENU, B_TEXPRV, noisebasis_menu(), x, y+60, w, 18, &tex->noisebasis, 0,0,0,0, "Sets the noise basis used for turbulence");
2317                                 
2318                                 uiBlockBeginAlign(block);
2319                                 uiDefButS(block, ROW, B_TEXPRV, "B/W",       x, y+38, w/2, 18, &tex->stype, 2.0, (float)TEX_DEFAULT, 0, 0, "Uses standard noise"); 
2320                                 uiDefButS(block, ROW, B_TEXPRV, "Color", w/2+x, y+38, w/2, 18, &tex->stype, 2.0, (float)TEX_COLOR, 0, 0, "Lets Noise return RGB value"); 
2321                                 uiDefButS(block, ROW, B_TEXPRV, "Soft",      x, y+20, w/2, 18, &tex->noisetype, 12.0, (float)TEX_NOISESOFT, 0, 0, "Generates soft noise");
2322                                 uiDefButS(block, ROW, B_TEXPRV, "Hard",  w/2+x, y+20, w/2, 18, &tex->noisetype, 12.0, (float)TEX_NOISEPERL, 0, 0, "Generates hard noise");
2323                                 uiBlockEndAlign(block);
2324                                 
2325                                 uiDefButS(block, NUM, B_TEXPRV, "Depth:", x, y, w, 18, &tex->noisedepth, 0.0, 6.0, 0, 0, "Sets the depth of the cloud calculation");
2326                         }
2327                         return 80;
2328                         
2329                 case TEX_DISTNOISE:
2330                         if( block ) {
2331                                 uiBlockBeginAlign(block);
2332                                 uiDefButS(block, MENU, B_TEXPRV, noisebasis_menu(), x, y+18, w, 18, &tex->noisebasis2, 0,0,0,0, "Sets the noise basis to distort");
2333                                 uiDefButS(block, MENU, B_TEXPRV, noisebasis_menu(), x, y,    w, 18, &tex->noisebasis,  0,0,0,0, "Sets the noise basis which does the distortion");
2334                                 uiBlockEndAlign(block);
2335                         }
2336                         return 36;
2337         }
2338         return 0;
2339 }
2340
2341 static int node_texture_buts_image(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
2342 {
2343         char *strp;
2344         uiBut *bt;
2345         
2346         if( block ) {
2347                 uiBlockBeginAlign(block);
2348                 uiBlockSetCol(block, TH_BUT_SETTING2);
2349                 
2350                 /* browse button */
2351                 IMAnames_to_pupstring(&strp, NULL, "LOAD NEW %x32767", &(G.main->image), NULL, NULL);
2352                 node->menunr= 0;
2353                 bt= uiDefButS(block, MENU, B_NOP, strp, 
2354                                           butr->xmin, butr->ymin, 19, 19, 
2355                                           &node->menunr, 0, 0, 0, 0, "Browses existing choices");
2356                 uiButSetFunc(bt, node_browse_image_cb, ntree, node);
2357                 if(strp) MEM_freeN(strp);
2358                 
2359                 /* Add New button */
2360                 if(node->id==NULL) {
2361                         bt= uiDefBut(block, BUT, B_NODE_LOADIMAGE, "Load New",
2362                                                  butr->xmin+19, butr->ymin, (short)(butr->xmax-butr->xmin-19.0f), 19, 
2363                                                  NULL, 0.0, 0.0, 0, 0, "Add new Image");
2364                         uiButSetFunc(bt, node_active_cb, ntree, node);
2365                         uiBlockSetCol(block, TH_AUTO);
2366                 }
2367                 else {
2368                         /* name button */
2369                         short xmin= (short)butr->xmin, xmax= (short)butr->xmax;
2370                         short width= xmax - xmin - 19;
2371                         
2372                         bt= uiDefBut(block, TEX, B_NOP, "IM:",
2373                                                  xmin+19, butr->ymin, width, 19, 
2374                                                  node->id->name+2, 0.0, 19.0, 0, 0, "Image name");
2375                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
2376                 }
2377         }
2378         return 20;
2379 }
2380
2381 static int node_texture_buts_output(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
2382 {
2383         if( block ) {
2384                 uiBut *bt;
2385                 short width;
2386                 char *name = ((TexNodeOutput*)node->storage)->name;
2387                 
2388                 uiBlockBeginAlign(block);
2389                 
2390                 width = (short)(butr->xmax - butr->xmin);
2391                 
2392                 bt = uiDefBut(
2393                         block, TEX, B_NOP,
2394                         "Name:",
2395                         butr->xmin, butr->ymin,
2396                         width, 19, 
2397                         name, 0, 31,
2398                         0, 0, 
2399                         "Name this output"
2400                 );
2401                 
2402                 uiBlockEndAlign(block);
2403         }
2404         return 19;
2405 }
2406
2407 /* only once called */
2408 static void node_texture_set_butfunc(bNodeType *ntype)
2409 {
2410         if( ntype->type >= TEX_NODE_PROC && ntype->type < TEX_NODE_PROC_MAX ) {
2411                 ntype->butfunc = node_texture_buts_proc;
2412         }
2413         else switch(ntype->type) {
2414                 
2415                 case TEX_NODE_MATH:
2416                         ntype->butfunc = node_buts_math;
2417                         break;
2418                 
2419                 case TEX_NODE_MIX_RGB:
2420                         ntype->butfunc = node_buts_mix_rgb;
2421                         break;
2422                         
2423                 case TEX_NODE_VALTORGB:
2424                         ntype->butfunc = node_buts_valtorgb;
2425                         break;
2426                         
2427                 case TEX_NODE_CURVE_RGB:
2428                         ntype->butfunc= node_buts_curvecol;
2429                         break;
2430                         
2431                 case TEX_NODE_CURVE_TIME:
2432                         ntype->butfunc = node_buts_time;
2433                         break;
2434                         
2435                 case TEX_NODE_TEXTURE:
2436                         ntype->butfunc = node_buts_texture;
2437                         break;
2438                         
2439                 case TEX_NODE_BRICKS:
2440                         ntype->butfunc = node_texture_buts_bricks;
2441                         break;
2442                         
2443                 case TEX_NODE_IMAGE:
2444                         ntype->butfunc = node_texture_buts_image;
2445                         break;
2446                         
2447                 case TEX_NODE_OUTPUT:
2448                         ntype->butfunc = node_texture_buts_output;
2449                         break;
2450                         
2451                 default:
2452                         ntype->butfunc= NULL;
2453         }
2454 }
2455
2456 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2457
2458 void init_node_butfuncs(void)
2459 {
2460         bNodeType *ntype;
2461         
2462         /* shader nodes */
2463         ntype= node_all_shaders.first;
2464         while(ntype) {
2465                 node_shader_set_butfunc(ntype);
2466                 ntype= ntype->next;
2467         }
2468         /* composit nodes */
2469         ntype= node_all_composit.first;
2470         while(ntype) {
2471                 node_composit_set_butfunc(ntype);
2472                 ntype= ntype->next;
2473         }
2474         ntype = node_all_textures.first;
2475         while(ntype) {
2476                 node_texture_set_butfunc(ntype);
2477                 ntype= ntype->next;
2478         }
2479 }
2480
2481 /* ************** Generic drawing ************** */
2482
2483 void node_rename_but(char *s)
2484 {
2485         uiBlock *block;
2486         ListBase listb={0, 0};
2487         int dy, x1, y1, sizex=80, sizey=30;
2488         short pivot[2], mval[2], ret=0;
2489         
2490         getmouseco_sc(mval);
2491
2492         pivot[0]= CLAMPIS(mval[0], (sizex+10), G.curscreen->sizex-30);
2493         pivot[1]= CLAMPIS(mval[1], (sizey/2)+10, G.curscreen->sizey-(sizey/2)-10);
2494         
2495         if (pivot[0]!=mval[0] || pivot[1]!=mval[1])
2496                 warp_pointer(pivot[0], pivot[1]);
2497
2498         mywinset(G.curscreen->mainwin);
2499         
2500         x1= pivot[0]-sizex+10;
2501         y1= pivot[1]-sizey/2;
2502         dy= sizey/2;
2503         
2504         block= uiNewBlock(&listb, "button", UI_EMBOSS, UI_HELV, G.curscreen->mainwin);
2505         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_ENTER_OK);
2506         
2507         /* buttons have 0 as return event, to prevent menu to close on hotkeys */
2508         uiBlockBeginAlign(block);
2509         
2510         uiDefBut(block, TEX, B_NOP, "Name: ", (short)(x1),(short)(y1+dy), 150, 19, s, 0.0, 19.0, 0, 0, "Node user name");
2511         
2512         uiBlockEndAlign(block);
2513
2514         uiDefBut(block, BUT, 32767, "OK", (short)(x1+150), (short)(y1+dy), 29, 19, NULL, 0, 0, 0, 0, "");
2515
2516         uiBoundsBlock(block, 2);
2517
2518         ret= uiDoBlocks(&listb, 0, 0);
2519 }
2520
2521
2522 static void draw_nodespace_grid(SpaceNode *snode)
2523 {
2524         float start, step= 25.0f;
2525
2526         BIF_ThemeColorShade(TH_BACK, -10);
2527         
2528         start= snode->v2d.cur.xmin -fmod(snode->v2d.cur.xmin, step);
2529         
2530         glBegin(GL_LINES);
2531         for(; start<snode->v2d.cur.xmax; start+=step) {
2532                 glVertex2f(start, snode->v2d.cur.ymin);
2533                 glVertex2f(start, snode->v2d.cur.ymax);
2534         }
2535
2536         start= snode->v2d.cur.ymin -fmod(snode->v2d.cur.ymin, step);
2537         for(; start<snode->v2d.cur.ymax; start+=step) {
2538                 glVertex2f(snode->v2d.cur.xmin, start);
2539                 glVertex2f(snode->v2d.cur.xmax, start);
2540         }
2541         
2542         /* X and Y axis */
2543         BIF_ThemeColorShade(TH_BACK, -18);
2544         glVertex2f(0.0f, snode->v2d.cur.ymin);
2545         glVertex2f(0.0f, snode->v2d.cur.ymax);
2546         glVertex2f(snode->v2d.cur.xmin, 0.0f);
2547         glVertex2f(snode->v2d.cur.xmax, 0.0f);
2548         
2549         glEnd();
2550 }
2551
2552 static void draw_nodespace_back_pix(ScrArea *sa, SpaceNode *snode)
2553 {
2554         
2555         draw_nodespace_grid(snode);
2556         
2557         if((snode->flag & SNODE_BACKDRAW) && snode->treetype==NTREE_COMPOSIT) {
2558                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2559                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2560                 if(ibuf) {
2561                         int x, y; 
2562                         /* somehow the offset has to be calculated inverse */
2563                         
2564                         glaDefine2DArea(&sa->winrct);
2565                         /* ortho at pixel level curarea */
2566                         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
2567                         
2568                         x = (sa->winx-ibuf->x)/2 + snode->xof;
2569                         y = (sa->winy-ibuf->y)/2 + snode->yof;
2570                         
2571                         if(ibuf->rect)
2572                                 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2573                         else if(ibuf->channels==4)
2574                                 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);
2575                         
2576                         /* sort this out, this should not be needed */
2577                         myortho2(snode->v2d.cur.xmin, snode->v2d.cur.xmax, snode->v2d.cur.ymin, snode->v2d.cur.ymax);
2578                         bwin_clear_viewmat(sa->win);    /* clear buttons view */
2579                         glLoadIdentity();
2580                 }
2581         }
2582 }
2583
2584 #if 0
2585 /* note: needs to be userpref or opengl profile option */
2586 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
2587 {
2588
2589         draw_nodespace_grid(snode);
2590         
2591         if(snode->flag & SNODE_BACKDRAW) {
2592                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2593                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2594                 if(ibuf) {
2595                         int x, y;
2596                         float zoom = 1.0;
2597
2598                         glMatrixMode(GL_PROJECTION);
2599                         glPushMatrix();
2600                         glMatrixMode(GL_MODELVIEW);
2601                         glPushMatrix();
2602                         
2603                         glaDefine2DArea(&sa->winrct);
2604
2605                         if(ibuf->x > sa->winx || ibuf->y > sa->winy) {
2606                                 float zoomx, zoomy;
2607                                 zoomx= (float)sa->winx/ibuf->x;
2608                                 zoomy= (float)sa->winy/ibuf->y;
2609                                 zoom = MIN2(zoomx, zoomy);
2610                         }
2611                         
2612                         x = (sa->winx-zoom*ibuf->x)/2 + snode->xof;
2613                         y = (sa->winy-zoom*ibuf->y)/2 + snode->yof;
2614
2615                         glPixelZoom(zoom, zoom);
2616
2617                         glColor4f(1.0, 1.0, 1.0, 1.0);
2618                         if(ibuf->rect)
2619                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
2620                         else if(ibuf->channels==4)
2621                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float);
2622
2623                         glPixelZoom(1.0, 1.0);
2624
2625                         glMatrixMode(GL_PROJECTION);
2626                         glPopMatrix();
2627                         glMatrixMode(GL_MODELVIEW);
2628                         glPopMatrix();
2629                 }
2630         }
2631 }
2632 #endif
2633
2634 /* nice AA filled circle */
2635 /* this might have some more generic use */
2636 static void circle_draw(float x, float y, float size, int type, int col[3])
2637 {
2638         /* 16 values of sin function */
2639         static float si[16] = {
2640                 0.00000000, 0.39435585,0.72479278,0.93775213,
2641                 0.99871650,0.89780453,0.65137248,0.29936312,
2642                 -0.10116832,-0.48530196,-0.79077573,-0.96807711,
2643                 -0.98846832,-0.84864425,-0.57126821,-0.20129852
2644         };
2645         /* 16 values of cos function */
2646         static float co[16] ={
2647                 1.00000000,0.91895781,0.68896691,0.34730525,
2648                 -0.05064916,-0.44039415,-0.75875812,-0.95413925,
2649                 -0.99486932,-0.87434661,-0.61210598,-0.25065253,
2650                 0.15142777,0.52896401,0.82076344,0.97952994,
2651         };
2652         int a;
2653         
2654         glColor3ub(col[0], col[1], col[2]);
2655         
2656         glBegin(GL_POLYGON);
2657         for(a=0; a<16; a++)
2658                 glVertex2f(x+size*si[a], y+size*co[a]);
2659         glEnd();
2660         
2661         glColor4ub(0, 0, 0, 150);
2662         glEnable(GL_BLEND);
2663         glEnable( GL_LINE_SMOOTH );
2664         glBegin(GL_LINE_LOOP);
2665         for(a=0; a<16; a++)
2666                 glVertex2f(x+size*si[a], y+size*co[a]);
2667         glEnd();
2668         glDisable( GL_LINE_SMOOTH );
2669         glDisable(GL_BLEND);
2670 }
2671
2672 static void socket_circle_draw(bNodeSocket *sock, float size)
2673 {
2674         int col[3];
2675         
2676         /* choose color based on sock flags */
2677         if(sock->flag & SELECT) {
2678                 if(sock->flag & SOCK_SEL) {
2679                         col[0]= 240; col[1]= 200; col[2]= 40;}
2680                 else if(sock->type==SOCK_VALUE) {
2681                         col[0]= 200; col[1]= 200; col[2]= 200;}
2682                 else if(sock->type==SOCK_VECTOR) {
2683                         col[0]= 140; col[1]= 140; col[2]= 240;}
2684                 else if(sock->type==SOCK_RGBA) {
2685                         col[0]= 240; col[1]= 240; col[2]= 100;}
2686                 else {
2687                         col[0]= 140; col[1]= 240; col[2]= 140;}
2688         }
2689         else if(sock->flag & SOCK_SEL) {
2690                 col[0]= 200; col[1]= 160; col[2]= 0;}
2691         else {
2692                 if(sock->type==-1) {
2693                         col[0]= 0; col[1]= 0; col[2]= 0;}
2694                 else if(sock->type==SOCK_VALUE) {
2695                         col[0]= 160; col[1]= 160; col[2]= 160;}
2696                 else if(sock->type==SOCK_VECTOR) {
2697                         col[0]= 100; col[1]= 100; col[2]= 200;}
2698                 else if(sock->type==SOCK_RGBA) {
2699                         col[0]= 200; col[1]= 200; col[2]= 40;}
2700                 else { 
2701                         col[0]= 100; col[1]= 200; col[2]= 100;}
2702         }
2703         
2704         circle_draw(sock->locx, sock->locy, size, sock->type, col);
2705 }
2706
2707 /* not a callback */
2708 static void node_draw_preview(bNodePreview *preview, rctf *prv)
2709 {
2710         float xscale= (prv->xmax-prv->xmin)/((float)preview->xsize);
2711         float yscale= (prv->ymax-prv->ymin)/((float)preview->ysize);
2712         float tile= (prv->xmax - prv->xmin) / 10.0;
2713         float x, y;
2714         
2715         /* draw checkerboard backdrop to show alpha */
2716         glColor3ub(120, 120, 120);
2717         glRectf(prv->xmin, prv->ymin, prv->xmax, prv->ymax);
2718         glColor3ub(160, 160, 160);
2719         
2720         for(y=prv->ymin; y<prv->ymax; y+=tile*2) {
2721                 for(x=prv->xmin; x<prv->xmax; x+=tile*2) {
2722                         float tilex= tile, tiley= tile;
2723
2724                         if(x+tile > prv->xmax)
2725                                 tilex= prv->xmax-x;
2726                         if(y+tile > prv->ymax)
2727                                 tiley= prv->ymax-y;
2728
2729                         glRectf(x, y, x + tilex, y + tiley);
2730                 }
2731         }
2732         for(y=prv->ymin+tile; y<prv->ymax; y+=tile*2) {
2733                 for(x=prv->xmin+tile; x<prv->xmax; x+=tile*2) {
2734                         float tilex= tile, tiley= tile;
2735
2736                         if(x+tile > prv->xmax)
2737                                 tilex= prv->xmax-x;
2738                         if(y+tile > prv->ymax)
2739                                 tiley= prv->ymax-y;
2740
2741                         glRectf(x, y, x + tilex, y + tiley);
2742                 }
2743         }
2744         
2745 #ifdef __APPLE__
2746         if(is_a_really_crappy_nvidia_card()) {
2747                 float zoomx= curarea->winx/(float)(G.v2d->cur.xmax-G.v2d->cur.xmin);
2748                 float zoomy= curarea->winy/(float)(G.v2d->cur.ymax-G.v2d->cur.ymin);
2749                 glPixelZoom(zoomx*xscale, zoomy*yscale);
2750         }
2751         else
2752 #endif
2753                 glPixelZoom(xscale, yscale);
2754
2755         glEnable(GL_BLEND);
2756         glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );  /* premul graphics */
2757         
2758         glColor4f(1.0, 1.0, 1.0, 1.0);
2759         glaDrawPixelsTex(prv->xmin, prv->ymin, preview->xsize, preview->ysize, GL_FLOAT, preview->rect);
2760         
2761         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
2762         glDisable(GL_BLEND);
2763         glPixelZoom(1.0f, 1.0f);
2764
2765         BIF_ThemeColorShadeAlpha(TH_BACK, -15, +100);
2766         fdrawbox(prv->xmin, prv->ymin, prv->xmax, prv->ymax);
2767         
2768 }
2769
2770 /* based on settings in node, sets drawing rect info. each redraw! */
2771 static void node_update_hidden(bNode *node)
2772 {
2773         bNodeSocket *nsock;
2774         float rad, drad, hiddenrad= HIDDEN_RAD;
2775         int totin=0, totout=0, tot;
2776         
2777         /* calculate minimal radius */
2778         for(nsock= node->inputs.first; nsock; nsock= nsock->next)
2779                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
2780                         totin++;
2781         for(nsock= node->outputs.first; nsock; nsock= nsock->next)
2782                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
2783                         totout++;
2784         
2785         tot= MAX2(totin, totout);
2786         if(tot>4) {
2787                 hiddenrad += 5.0*(float)(tot-4);
2788         }
2789         
2790         node->totr.xmin= node->locx;
2791         node->totr.xmax= node->locx + 3*hiddenrad + node->miniwidth;
2792         node->totr.ymax= node->locy + (hiddenrad - 0.5f*NODE_DY);
2793         node->totr.ymin= node->totr.ymax - 2*hiddenrad;
2794         
2795         /* output sockets */
2796         rad=drad= M_PI/(1.0f + (float)totout);
2797         
2798         for(nsock= node->outputs.first; nsock; nsock= nsock->next) {
2799                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2800                         nsock->locx= node->totr.xmax - hiddenrad + sin(rad)*hiddenrad;
2801                         nsock->locy= node->totr.ymin + hiddenrad + cos(rad)*hiddenrad;
2802                         rad+= drad;
2803                 }
2804         }
2805         
2806         /* input sockets */
2807         rad=drad= - M_PI/(1.0f + (float)totin);
2808         
2809         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
2810                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2811                         nsock->locx= node->totr.xmin + hiddenrad + sin(rad)*hiddenrad;
2812                         nsock->locy= node->totr.ymin + hiddenrad + cos(rad)*hiddenrad;
2813                         rad+= drad;
2814                 }
2815         }
2816 }
2817
2818 /* based on settings in node, sets drawing rect info. each redraw! */
2819 static void node_update(bNode *node)
2820 {
2821         bNodeSocket *nsock;
2822         float dy= node->locy;
2823         
2824         /* header */
2825         dy-= NODE_DY;
2826         
2827         /* little bit space in top */
2828         if(node->outputs.first)
2829                 dy-= NODE_DYS/2;
2830         
2831         /* output sockets */
2832         for(nsock= node->outputs.first; nsock; nsock= nsock->next) {
2833                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2834                         nsock->locx= node->locx + node->width;
2835                         nsock->locy= dy - NODE_DYS;
2836                         dy-= NODE_DY;
2837                 }
2838         }
2839         
2840         node->prvr.xmin= node->butr.xmin= node->locx + NODE_DYS;
2841         node->prvr.xmax= node->butr.xmax= node->locx + node->width- NODE_DYS;
2842         
2843         /* preview rect? */
2844         if(node->flag & NODE_PREVIEW) {
2845                 /* only recalculate size when there's a preview actually, otherwise we use stored result */
2846                 if(node->preview && node->preview->rect) {
2847                         float aspect= 1.0f;
2848                         
2849                         if(node->preview && node->preview->xsize && node->preview->ysize) 
2850                                 aspect= (float)node->preview->ysize/(float)node->preview->xsize;
2851                         
2852                         dy-= NODE_DYS/2;
2853                         node->prvr.ymax= dy;
2854                         
2855                         if(aspect <= 1.0f)
2856                                 node->prvr.ymin= dy - aspect*(node->width-NODE_DY);
2857                         else {
2858                                 float dx= (node->width - NODE_DYS) - (node->width- NODE_DYS)/aspect;    /* width correction of image */
2859                                 
2860                                 node->prvr.ymin= dy - (node->width-NODE_DY);
2861                                 
2862                                 node->prvr.xmin+= 0.5*dx;
2863                                 node->prvr.xmax-= 0.5*dx;
2864                         }
2865
2866                         dy= node->prvr.ymin - NODE_DYS/2;
2867
2868                         /* make sure that maximums are bigger or equal to minimums */
2869                         if(node->prvr.xmax < node->prvr.xmin) SWAP(float, node->prvr.xmax, node->prvr.xmin);
2870                         if(node->prvr.ymax < node->prvr.ymin) SWAP(float, node->prvr.ymax, node->prvr.ymin);
2871                 }
2872                 else {
2873                         float oldh= node->prvr.ymax - node->prvr.ymin;
2874                         if(oldh==0.0f)
2875                                 oldh= 0.6f*node->width-NODE_DY;
2876                         dy-= NODE_DYS/2;
2877                         node->prvr.ymax= dy;
2878                         node->prvr.ymin= dy - oldh;
2879                         dy= node->prvr.ymin - NODE_DYS/2;
2880                 }
2881         }
2882
2883         /* XXX ugly hack, typeinfo for group is generated */
2884         if(node->type == NODE_GROUP)
2885                 node->typeinfo->butfunc= node_buts_group;
2886         
2887         /* buttons rect? */
2888         if((node->flag & NODE_OPTIONS) && node->typeinfo->butfunc) {
2889                 dy-= NODE_DYS/2;
2890                 node->butr.ymax= dy;
2891                 node->butr.ymin= dy - (float)node->typeinfo->butfunc(NULL, NULL, node, NULL);
2892                 dy= node->butr.ymin - NODE_DYS/2;
2893         }
2894         
2895         /* input sockets */
2896         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
2897                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2898                         nsock->locx= node->locx;
2899                         nsock->locy= dy - NODE_DYS;
2900                         dy-= NODE_DY;
2901                 }
2902         }
2903         
2904         /* little bit space in end */
2905         if(node->inputs.first || (node->flag & (NODE_OPTIONS|NODE_PREVIEW))==0 )
2906                 dy-= NODE_DYS/2;
2907         
2908         node->totr.xmin= node->locx;
2909         node->totr.xmax= node->locx + node->width;
2910         node->totr.ymax= node->locy;
2911         node->totr.ymin= dy;
2912 }
2913
2914 /* based on settings in node, sets drawing rect info. each redraw! */
2915 /* note: this assumes only 1 group at a time is drawn (linked data) */
2916 /* in node->totr the entire boundbox for the group is stored */
2917 static void node_update_group(bNode *gnode)
2918 {
2919         bNodeTree *ngroup= (bNodeTree *)gnode->id;
2920         bNode *node;
2921         bNodeSocket *nsock;
2922         rctf *rect= &gnode->totr;
2923         int counter;
2924         
2925         /* center them, is a bit of abuse of locx and locy though */
2926         for(node= ngroup->nodes.first; node; node= node->next) {
2927                 node->locx+= gnode->locx;
2928                 node->locy+= gnode->locy;
2929                 if(node->flag & NODE_HIDDEN)
2930                         node_update_hidden(node);
2931                 else
2932                         node_update(node);
2933                 node->locx-= gnode->locx;
2934                 node->locy-= gnode->locy;
2935         }
2936         counter= 1;
2937         for(node= ngroup->nodes.first; node; node= node->next) {
2938                 if(counter) {
2939                         *rect= node->totr;
2940                         counter= 0;
2941                 }
2942                 else
2943                         BLI_union_rctf(rect, &node->totr);
2944         }
2945         if(counter==1) return;  /* should be prevented? */
2946         
2947         rect->xmin-= NODE_DY;
2948         rect->ymin-= NODE_DY;
2949         rect->xmax+= NODE_DY;
2950         rect->ymax+= NODE_DY;
2951         
2952         /* output sockets */
2953         for(nsock= gnode->outputs.first; nsock; nsock= nsock->next) {
2954                 nsock->locx= rect->xmax;
2955                 nsock->locy= nsock->tosock->locy;
2956         }
2957         
2958         /* input sockets */
2959         for(nsock= gnode->inputs.first; nsock; nsock= nsock->next) {
2960                 nsock->locx= rect->xmin;
2961                 nsock->locy= nsock->tosock->locy;
2962         }
2963 }
2964
2965 static void node_scaling_widget(int color_id, float aspect, float xmin, float ymin, float xmax, float ymax)
2966 {
2967         float dx;
2968         float dy;
2969         
2970         dx= 0.5f*(xmax-xmin);
2971         dy= 0.5f*(ymax-ymin);
2972         
2973         BIF_ThemeColorShade(color_id, +30);     
2974         fdrawline(xmin, ymin, xmax, ymax);
2975         fdrawline(xmin+dx, ymin, xmax, ymax-dy);
2976         
2977         BIF_ThemeColorShade(color_id, -10);
2978         fdrawline(xmin, ymin+aspect, xmax, ymax+aspect);
2979         fdrawline(xmin+dx, ymin+aspect, xmax, ymax-dy+aspect);
2980 }
2981
2982 static int node_get_colorid(bNode *node)
2983 {
2984         if(node->typeinfo->nclass==NODE_CLASS_INPUT)
2985                 return TH_NODE_IN_OUT;
2986         if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
2987                 if(node->flag & NODE_DO_OUTPUT)
2988                         return TH_NODE_IN_OUT;
2989                 else
2990                         return TH_NODE;
2991         }
2992         if(node->typeinfo->nclass==NODE_CLASS_CONVERTOR)
2993                 return TH_NODE_CONVERTOR;
2994         if(ELEM3(node->typeinfo->nclass, NODE_CLASS_OP_COLOR, NODE_CLASS_OP_VECTOR, NODE_CLASS_OP_FILTER))
2995                 return TH_NODE_OPERATOR;
2996         if(node->typeinfo->nclass==NODE_CLASS_GROUP)
2997                 return TH_NODE_GROUP;