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