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