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