Fix segfault with bad setting in the Image node.
[blender-staging.git] / source / blender / src / drawnode.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2005 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): David Millan Escriva, Juho Vepsäläinen
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "BLI_blenlib.h"
35 #include "BLI_arithb.h"
36
37 #include "DNA_action_types.h"
38 #include "DNA_color_types.h"
39 #include "DNA_customdata_types.h"
40 #include "DNA_ipo_types.h"
41 #include "DNA_ID.h"
42 #include "DNA_image_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_node_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_space_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_texture_types.h"
51 #include "DNA_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, 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 int node_composit_buts_file_output(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1930 {
1931         if(block) {
1932                 NodeImageFile *nif= node->storage;
1933                 short x= (short)butr->xmin;
1934                 short y= (short)butr->ymin;
1935                 short w= (short)butr->xmax-butr->xmin;
1936                 char str[320];
1937                 
1938                 node_imagetype_string(str);
1939                 
1940                 uiBlockBeginAlign(block);
1941                 
1942                 uiDefBut(block, TEX, B_NOP, "",
1943                                   x, y+60, w, 20, 
1944                                   nif->name, 0.0f, 240.0f, 0, 0, "");
1945                 
1946                 uiDefButS(block, MENU, B_NOP, str,
1947                                   x, y+40, w, 20, 
1948                                   &nif->imtype, 0.0f, 1.0f, 0, 0, "");
1949                 
1950                 if(nif->imtype==R_OPENEXR) {
1951                         uiDefButBitS(block, TOG, R_OPENEXR_HALF, B_REDR, "Half",        
1952                                                 x, y+20, w/2, 20, 
1953                                                 &nif->subimtype, 0, 0, 0, 0, "");
1954
1955                         uiDefButS(block, MENU,B_NOP, "Codec %t|None %x0|Pxr24 (lossy) %x1|ZIP (lossless) %x2|PIZ (lossless) %x3|RLE (lossless) %x4",  
1956                                                 x+w/2, y+20, w/2, 20, 
1957                                                 &nif->codec, 0, 0, 0, 0, "");
1958                 }
1959                 else {
1960                         uiDefButS(block, NUM, B_NOP, "Quality: ",
1961                                   x, y+20, w, 20, 
1962                                   &nif->quality, 10.0f, 100.0f, 10, 0, "");
1963                 }
1964                 
1965                 /* start frame, end frame */
1966                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "SFra: ", 
1967                                   x, y, w/2, 20, 
1968                                   &nif->sfra, 1, MAXFRAMEF, 10, 0, "");
1969                 uiDefButI(block, NUM, B_NODE_EXEC+node->nr, "EFra: ", 
1970                                   x+w/2, y, w/2, 20, 
1971                                   &nif->efra, 1, MAXFRAMEF, 10, 0, "");
1972                 
1973         }
1974         return 80;
1975 }
1976
1977 static void node_scale_cb(void *node_v, void *unused_v)
1978 {
1979         bNode *node= node_v;
1980         bNodeSocket *nsock;
1981
1982         /* check the 2 inputs, and set them to reasonable values */
1983         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
1984                 if(ELEM(node->custom1, CMP_SCALE_RELATIVE, CMP_SCALE_SCENEPERCENT))
1985                         nsock->ns.vec[0]= 1.0;
1986                 else {
1987                         if(nsock->next==NULL)
1988                                 nsock->ns.vec[0]= (float)G.scene->r.ysch;
1989                         else
1990                                 nsock->ns.vec[0]= (float)G.scene->r.xsch;
1991                 }
1992         }       
1993 }
1994
1995 static int node_composit_buts_scale(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1996 {
1997         if(block) {
1998                 uiBut *bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Relative %x0|Absolute %x1|Scene Size % %x2|",
1999                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
2000                                   &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");
2001                 uiButSetFunc(bt, node_scale_cb, node, NULL);
2002         }
2003         return 20;
2004 }
2005
2006 static int node_composit_buts_invert(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
2007 {
2008         if(block) {
2009                 uiBlockBeginAlign(block);
2010                 uiDefButBitS(block, TOG, CMP_CHAN_RGB, B_NODE_EXEC+node->nr, "RGB",
2011                                          butr->xmin, butr->ymin, (butr->xmax-butr->xmin)/2, 20, 
2012                                          &node->custom1, 0, 0, 0, 0, "");
2013                 uiDefButBitS(block, TOG, CMP_CHAN_A, B_NODE_EXEC+node->nr, "A",
2014                                          butr->xmin+(butr->xmax-butr->xmin)/2, butr->ymin, (butr->xmax-butr->xmin)/2, 20, 
2015                                          &node->custom1, 0, 0, 0, 0, "");
2016                 uiBlockEndAlign(block);
2017         }
2018         return 20;      
2019 }
2020
2021 /* only once called */
2022 static void node_composit_set_butfunc(bNodeType *ntype)
2023 {
2024         switch(ntype->type) {
2025                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
2026
2027                 case CMP_NODE_IMAGE:
2028                         ntype->butfunc= node_composit_buts_image;
2029                         break;
2030                 case CMP_NODE_R_LAYERS:
2031                         ntype->butfunc= node_composit_buts_renderlayers;
2032                         break;
2033                 case CMP_NODE_NORMAL:
2034                         ntype->butfunc= node_buts_normal;
2035                         break;
2036                 case CMP_NODE_CURVE_VEC:
2037                         ntype->butfunc= node_buts_curvevec;
2038                         break;
2039                 case CMP_NODE_CURVE_RGB:
2040                         ntype->butfunc= node_buts_curvecol;
2041                         break;
2042                 case CMP_NODE_VALUE:
2043                         ntype->butfunc= node_buts_value;
2044                         break;
2045                 case CMP_NODE_RGB:
2046                         ntype->butfunc= node_buts_rgb;
2047                         break;
2048                 case CMP_NODE_FLIP:
2049                         ntype->butfunc= node_composit_buts_flip;
2050                         break;
2051                 case CMP_NODE_SPLITVIEWER:
2052                         ntype->butfunc= node_composit_buts_splitviewer;
2053                         break;
2054                 case CMP_NODE_MIX_RGB:
2055                         ntype->butfunc= node_buts_mix_rgb;
2056                         break;
2057                 case CMP_NODE_VALTORGB:
2058                         ntype->butfunc= node_buts_valtorgb;
2059                         break;
2060                 case CMP_NODE_CROP:
2061                         ntype->butfunc= node_composit_buts_crop;
2062                         break;
2063                 case CMP_NODE_BLUR:
2064                         ntype->butfunc= node_composit_buts_blur;
2065                         break;
2066                 case CMP_NODE_DBLUR:
2067                         ntype->butfunc= node_composit_buts_dblur;
2068                         break;
2069                 case CMP_NODE_BILATERALBLUR:
2070                         ntype->butfunc= node_composit_buts_bilateralblur;
2071                         break;
2072                 /* qdn: defocus node */
2073                 case CMP_NODE_DEFOCUS:
2074                         ntype->butfunc = node_composit_buts_defocus;
2075                         break;
2076                 /* qdn: glare node */
2077                 case CMP_NODE_GLARE:
2078                         ntype->butfunc = node_composit_buts_glare;
2079                         break;
2080                 /* qdn: tonemap node */
2081                 case CMP_NODE_TONEMAP:
2082                         ntype->butfunc = node_composit_buts_tonemap;
2083                         break;
2084                 /* qdn: lens distortion node */
2085                 case CMP_NODE_LENSDIST:
2086                         ntype->butfunc = node_composit_buts_lensdist;
2087                         break;
2088                 case CMP_NODE_VECBLUR:
2089                         ntype->butfunc= node_composit_buts_vecblur;
2090                         break;
2091                 case CMP_NODE_FILTER:
2092                         ntype->butfunc= node_composit_buts_filter;
2093                         break;
2094                 case CMP_NODE_MAP_VALUE:
2095                         ntype->butfunc= node_composit_buts_map_value;
2096                         break;
2097                 case CMP_NODE_TIME:
2098                         ntype->butfunc= node_buts_time;
2099                         break;
2100                 case CMP_NODE_ALPHAOVER:
2101                         ntype->butfunc= node_composit_buts_alphaover;
2102                         break;
2103                 case CMP_NODE_HUE_SAT:
2104                         ntype->butfunc= node_composit_buts_hue_sat;
2105                         break;
2106                 case CMP_NODE_TEXTURE:
2107                         ntype->butfunc= node_buts_texture;
2108                         break;
2109                 case CMP_NODE_DILATEERODE:
2110                         ntype->butfunc= node_composit_buts_dilateerode;
2111                         break;
2112                 case CMP_NODE_OUTPUT_FILE:
2113                         ntype->butfunc= node_composit_buts_file_output;
2114                         break;
2115         
2116                 case CMP_NODE_DIFF_MATTE:
2117                         ntype->butfunc=node_composit_buts_diff_matte;
2118                         break;
2119                 case CMP_NODE_COLOR_SPILL:
2120                         ntype->butfunc=node_composit_buts_color_spill;
2121                         break;
2122                 case CMP_NODE_CHROMA:
2123                         ntype->butfunc=node_composit_buts_chroma_matte;
2124                         break;
2125                 case CMP_NODE_SCALE:
2126                         ntype->butfunc= node_composit_buts_scale;
2127                         break;
2128                 case CMP_NODE_CHANNEL_MATTE:
2129                         ntype->butfunc= node_composit_buts_channel_matte;
2130                         break;
2131                 case CMP_NODE_LUMA_MATTE:
2132                         ntype->butfunc= node_composit_buts_luma_matte;
2133                         break;
2134                 case CMP_NODE_MAP_UV:
2135                         ntype->butfunc= node_composit_buts_map_uv;
2136                         break;
2137                 case CMP_NODE_ID_MASK:
2138                         ntype->butfunc= node_composit_buts_id_mask;
2139                         break;
2140                 case CMP_NODE_MATH:
2141                         ntype->butfunc= node_buts_math;
2142                         break;
2143                 case CMP_NODE_INVERT:
2144                         ntype->butfunc= node_composit_buts_invert;
2145                         break;
2146                 default:
2147                         ntype->butfunc= NULL;
2148         }
2149 }
2150
2151
2152 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2153
2154 void init_node_butfuncs(void)
2155 {
2156         bNodeType *ntype;
2157         
2158         /* shader nodes */
2159         ntype= node_all_shaders.first;
2160         while(ntype) {
2161                 node_shader_set_butfunc(ntype);
2162                 ntype= ntype->next;
2163         }
2164         /* composit nodes */
2165         ntype= node_all_composit.first;
2166         while(ntype) {
2167                 node_composit_set_butfunc(ntype);
2168                 ntype= ntype->next;
2169         }
2170 }
2171
2172 /* ************** Generic drawing ************** */
2173
2174 void node_rename_but(char *s)
2175 {
2176         uiBlock *block;
2177         ListBase listb={0, 0};
2178         int dy, x1, y1, sizex=80, sizey=30;
2179         short pivot[2], mval[2], ret=0;
2180         
2181         getmouseco_sc(mval);
2182
2183         pivot[0]= CLAMPIS(mval[0], (sizex+10), G.curscreen->sizex-30);
2184         pivot[1]= CLAMPIS(mval[1], (sizey/2)+10, G.curscreen->sizey-(sizey/2)-10);
2185         
2186         if (pivot[0]!=mval[0] || pivot[1]!=mval[1])
2187                 warp_pointer(pivot[0], pivot[1]);
2188
2189         mywinset(G.curscreen->mainwin);
2190         
2191         x1= pivot[0]-sizex+10;
2192         y1= pivot[1]-sizey/2;
2193         dy= sizey/2;
2194         
2195         block= uiNewBlock(&listb, "button", UI_EMBOSS, UI_HELV, G.curscreen->mainwin);
2196         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_ENTER_OK);
2197         
2198         /* buttons have 0 as return event, to prevent menu to close on hotkeys */
2199         uiBlockBeginAlign(block);
2200         
2201         uiDefBut(block, TEX, B_NOP, "Name: ", (short)(x1),(short)(y1+dy), 150, 19, s, 0.0, 19.0, 0, 0, "Node user name");
2202         
2203         uiBlockEndAlign(block);
2204
2205         uiDefBut(block, BUT, 32767, "OK", (short)(x1+150), (short)(y1+dy), 29, 19, NULL, 0, 0, 0, 0, "");
2206
2207         uiBoundsBlock(block, 2);
2208
2209         ret= uiDoBlocks(&listb, 0, 0);
2210 }
2211
2212
2213 static void draw_nodespace_grid(SpaceNode *snode)
2214 {
2215         float start, step= 25.0f;
2216
2217         BIF_ThemeColorShade(TH_BACK, -10);
2218         
2219         start= snode->v2d.cur.xmin -fmod(snode->v2d.cur.xmin, step);
2220         
2221         glBegin(GL_LINES);
2222         for(; start<snode->v2d.cur.xmax; start+=step) {
2223                 glVertex2f(start, snode->v2d.cur.ymin);
2224                 glVertex2f(start, snode->v2d.cur.ymax);
2225         }
2226
2227         start= snode->v2d.cur.ymin -fmod(snode->v2d.cur.ymin, step);
2228         for(; start<snode->v2d.cur.ymax; start+=step) {
2229                 glVertex2f(snode->v2d.cur.xmin, start);
2230                 glVertex2f(snode->v2d.cur.xmax, start);
2231         }
2232         
2233         /* X and Y axis */
2234         BIF_ThemeColorShade(TH_BACK, -18);
2235         glVertex2f(0.0f, snode->v2d.cur.ymin);
2236         glVertex2f(0.0f, snode->v2d.cur.ymax);
2237         glVertex2f(snode->v2d.cur.xmin, 0.0f);
2238         glVertex2f(snode->v2d.cur.xmax, 0.0f);
2239         
2240         glEnd();
2241 }
2242
2243 static void draw_nodespace_back_pix(ScrArea *sa, SpaceNode *snode)
2244 {
2245         
2246         draw_nodespace_grid(snode);
2247         
2248         if((snode->flag & SNODE_BACKDRAW) && snode->treetype==NTREE_COMPOSIT) {
2249                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2250                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2251                 if(ibuf) {
2252                         int x, y; 
2253                         /* somehow the offset has to be calculated inverse */
2254                         
2255                         glaDefine2DArea(&sa->winrct);
2256                         /* ortho at pixel level curarea */
2257                         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
2258                         
2259                         x = (sa->winx-ibuf->x)/2 + snode->xof;
2260                         y = (sa->winy-ibuf->y)/2 + snode->yof;
2261                         
2262                         if(ibuf->rect)
2263                                 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2264                         else if(ibuf->channels==4)
2265                                 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);
2266                         
2267                         /* sort this out, this should not be needed */
2268                         myortho2(snode->v2d.cur.xmin, snode->v2d.cur.xmax, snode->v2d.cur.ymin, snode->v2d.cur.ymax);
2269                         bwin_clear_viewmat(sa->win);    /* clear buttons view */
2270                         glLoadIdentity();
2271                 }
2272         }
2273 }
2274
2275 #if 0
2276 /* note: needs to be userpref or opengl profile option */
2277 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
2278 {
2279
2280         draw_nodespace_grid(snode);
2281         
2282         if(snode->flag & SNODE_BACKDRAW) {
2283                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2284                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2285                 if(ibuf) {
2286                         int x, y;
2287                         float zoom = 1.0;
2288
2289                         glMatrixMode(GL_PROJECTION);
2290                         glPushMatrix();
2291                         glMatrixMode(GL_MODELVIEW);
2292                         glPushMatrix();
2293                         
2294                         glaDefine2DArea(&sa->winrct);
2295
2296                         if(ibuf->x > sa->winx || ibuf->y > sa->winy) {
2297                                 float zoomx, zoomy;
2298                                 zoomx= (float)sa->winx/ibuf->x;
2299                                 zoomy= (float)sa->winy/ibuf->y;
2300                                 zoom = MIN2(zoomx, zoomy);
2301                         }
2302                         
2303                         x = (sa->winx-zoom*ibuf->x)/2 + snode->xof;
2304                         y = (sa->winy-zoom*ibuf->y)/2 + snode->yof;
2305
2306                         glPixelZoom(zoom, zoom);
2307
2308                         glColor4f(1.0, 1.0, 1.0, 1.0);
2309                         if(ibuf->rect)
2310                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
2311                         else if(ibuf->channels==4)
2312                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float);
2313
2314                         glPixelZoom(1.0, 1.0);
2315
2316                         glMatrixMode(GL_PROJECTION);
2317                         glPopMatrix();
2318                         glMatrixMode(GL_MODELVIEW);
2319                         glPopMatrix();
2320                 }
2321         }
2322 }
2323 #endif
2324
2325 /* nice AA filled circle */
2326 /* this might have some more generic use */
2327 static void circle_draw(float x, float y, float size, int type, int col[3])
2328 {
2329         /* 16 values of sin function */
2330         static float si[16] = {
2331                 0.00000000, 0.39435585,0.72479278,0.93775213,
2332                 0.99871650,0.89780453,0.65137248,0.29936312,
2333                 -0.10116832,-0.48530196,-0.79077573,-0.96807711,
2334                 -0.98846832,-0.84864425,-0.57126821,-0.20129852
2335         };
2336         /* 16 values of cos function */
2337         static float co[16] ={
2338                 1.00000000,0.91895781,0.68896691,0.34730525,
2339                 -0.05064916,-0.44039415,-0.75875812,-0.95413925,
2340                 -0.99486932,-0.87434661,-0.61210598,-0.25065253,
2341                 0.15142777,0.52896401,0.82076344,0.97952994,
2342         };
2343         int a;
2344         
2345         glColor3ub(col[0], col[1], col[2]);
2346         
2347         glBegin(GL_POLYGON);
2348         for(a=0; a<16; a++)
2349                 glVertex2f(x+size*si[a], y+size*co[a]);
2350         glEnd();
2351         
2352         glColor4ub(0, 0, 0, 150);
2353         glEnable(GL_BLEND);
2354         glEnable( GL_LINE_SMOOTH );
2355         glBegin(GL_LINE_LOOP);
2356         for(a=0; a<16; a++)
2357                 glVertex2f(x+size*si[a], y+size*co[a]);
2358         glEnd();
2359         glDisable( GL_LINE_SMOOTH );
2360         glDisable(GL_BLEND);
2361 }
2362
2363 static void socket_circle_draw(bNodeSocket *sock, float size)
2364 {
2365         int col[3];
2366         
2367         /* choose color based on sock flags */
2368         if(sock->flag & SELECT) {
2369                 if(sock->flag & SOCK_SEL) {
2370                         col[0]= 240; col[1]= 200; col[2]= 40;}
2371                 else if(sock->type==SOCK_VALUE) {
2372                         col[0]= 200; col[1]= 200; col[2]= 200;}
2373                 else if(sock->type==SOCK_VECTOR) {
2374                         col[0]= 140; col[1]= 140; col[2]= 240;}
2375                 else if(sock->type==SOCK_RGBA) {
2376                         col[0]= 240; col[1]= 240; col[2]= 100;}
2377                 else {
2378                         col[0]= 140; col[1]= 240; col[2]= 140;}
2379         }
2380         else if(sock->flag & SOCK_SEL) {
2381                 col[0]= 200; col[1]= 160; col[2]= 0;}
2382         else {
2383                 if(sock->type==-1) {
2384                         col[0]= 0; col[1]= 0; col[2]= 0;}
2385                 else if(sock->type==SOCK_VALUE) {
2386                         col[0]= 160; col[1]= 160; col[2]= 160;}
2387                 else if(sock->type==SOCK_VECTOR) {
2388                         col[0]= 100; col[1]= 100; col[2]= 200;}
2389                 else if(sock->type==SOCK_RGBA) {
2390                         col[0]= 200; col[1]= 200; col[2]= 40;}
2391                 else { 
2392                         col[0]= 100; col[1]= 200; col[2]= 100;}
2393         }
2394         
2395         circle_draw(sock->locx, sock->locy, size, sock->type, col);
2396 }
2397
2398 /* not a callback */
2399 static void node_draw_preview(bNodePreview *preview, rctf *prv)
2400 {
2401         float xscale= (prv->xmax-prv->xmin)/((float)preview->xsize);
2402         float yscale= (prv->ymax-prv->ymin)/((float)preview->ysize);
2403         float tile= (prv->xmax - prv->xmin) / 10.0;
2404         float x, y;
2405         
2406         /* draw checkerboard backdrop to show alpha */
2407         glColor3ub(120, 120, 120);
2408         glRectf(prv->xmin, prv->ymin, prv->xmax, prv->ymax);
2409         glColor3ub(160, 160, 160);
2410         
2411         for(y=prv->ymin; y<prv->ymax; y+=tile*2) {
2412                 for(x=prv->xmin; x<prv->xmax; x+=tile*2) {
2413                         float tilex= tile, tiley= tile;
2414
2415                         if(x+tile > prv->xmax)
2416                                 tilex= prv->xmax-x;
2417                         if(y+tile > prv->ymax)
2418                                 tiley= prv->ymax-y;
2419
2420                         glRectf(x, y, x + tilex, y + tiley);
2421                 }
2422         }
2423         for(y=prv->ymin+tile; y<prv->ymax; y+=tile*2) {
2424                 for(x=prv->xmin+tile; x<prv->xmax; x+=tile*2) {
2425                         float tilex= tile, tiley= tile;
2426
2427                         if(x+tile > prv->xmax)
2428                                 tilex= prv->xmax-x;
2429                         if(y+tile > prv->ymax)
2430                                 tiley= prv->ymax-y;
2431
2432                         glRectf(x, y, x + tilex, y + tiley);
2433                 }
2434         }
2435         
2436         glPixelZoom(xscale, yscale);
2437         glEnable(GL_BLEND);
2438         glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );  /* premul graphics */
2439         
2440         glColor4f(1.0, 1.0, 1.0, 1.0);
2441         glaDrawPixelsTex(prv->xmin, prv->ymin, preview->xsize, preview->ysize, GL_FLOAT, preview->rect);
2442         
2443         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
2444         glDisable(GL_BLEND);
2445         glPixelZoom(1.0f, 1.0f);
2446
2447         BIF_ThemeColorShadeAlpha(TH_BACK, -15, +100);
2448         fdrawbox(prv->xmin, prv->ymin, prv->xmax, prv->ymax);
2449         
2450 }
2451
2452 /* based on settings in node, sets drawing rect info. each redraw! */
2453 static void node_update_hidden(bNode *node)
2454 {
2455         bNodeSocket *nsock;
2456         float rad, drad, hiddenrad= HIDDEN_RAD;
2457         int totin=0, totout=0, tot;
2458         
2459         /* calculate minimal radius */
2460         for(nsock= node->inputs.first; nsock; nsock= nsock->next)
2461                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
2462                         totin++;
2463         for(nsock= node->outputs.first; nsock; nsock= nsock->next)
2464                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
2465                         totout++;
2466         
2467         tot= MAX2(totin, totout);
2468         if(tot>4) {
2469                 hiddenrad += 5.0*(float)(tot-4);
2470         }
2471         
2472         node->totr.xmin= node->locx;
2473         node->totr.xmax= node->locx + 3*hiddenrad + node->miniwidth;
2474         node->totr.ymax= node->locy + (hiddenrad - 0.5f*NODE_DY);
2475         node->totr.ymin= node->totr.ymax - 2*hiddenrad;
2476         
2477         /* output sockets */
2478         rad=drad= M_PI/(1.0f + (float)totout);
2479         
2480         for(nsock= node->outputs.first; nsock; nsock= nsock->next) {
2481                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2482                         nsock->locx= node->totr.xmax - hiddenrad + sin(rad)*hiddenrad;
2483                         nsock->locy= node->totr.ymin + hiddenrad + cos(rad)*hiddenrad;
2484                         rad+= drad;
2485                 }
2486         }
2487         
2488         /* input sockets */
2489         rad=drad= - M_PI/(1.0f + (float)totin);
2490         
2491         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
2492                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2493                         nsock->locx= node->totr.xmin + hiddenrad + sin(rad)*hiddenrad;
2494                         nsock->locy= node->totr.ymin + hiddenrad + cos(rad)*hiddenrad;
2495                         rad+= drad;
2496                 }
2497         }
2498 }
2499
2500 /* based on settings in node, sets drawing rect info. each redraw! */
2501 static void node_update(bNode *node)
2502 {
2503         bNodeSocket *nsock;
2504         float dy= node->locy;
2505         
2506         /* header */
2507         dy-= NODE_DY;
2508         
2509         /* little bit space in top */
2510         if(node->outputs.first)
2511                 dy-= NODE_DYS/2;
2512         
2513         /* output sockets */
2514         for(nsock= node->outputs.first; nsock; nsock= nsock->next) {
2515                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2516                         nsock->locx= node->locx + node->width;
2517                         nsock->locy= dy - NODE_DYS;
2518                         dy-= NODE_DY;
2519                 }
2520         }
2521         
2522         node->prvr.xmin= node->butr.xmin= node->locx + NODE_DYS;
2523         node->prvr.xmax= node->butr.xmax= node->locx + node->width- NODE_DYS;
2524         
2525         /* preview rect? */
2526         if(node->flag & NODE_PREVIEW) {
2527                 /* only recalculate size when there's a preview actually, otherwise we use stored result */
2528                 if(node->preview && node->preview->rect) {
2529                         float aspect= 1.0f;
2530                         
2531                         if(node->preview && node->preview->xsize && node->preview->ysize) 
2532                                 aspect= (float)node->preview->ysize/(float)node->preview->xsize;
2533                         
2534                         dy-= NODE_DYS/2;
2535                         node->prvr.ymax= dy;
2536                         
2537                         if(aspect <= 1.0f)
2538                                 node->prvr.ymin= dy - aspect*(node->width-NODE_DY);
2539                         else {
2540                                 float dx= (node->width - NODE_DYS) - (node->width- NODE_DYS)/aspect;    /* width correction of image */
2541                                 
2542                                 node->prvr.ymin= dy - (node->width-NODE_DY);
2543                                 
2544                                 node->prvr.xmin+= 0.5*dx;
2545                                 node->prvr.xmax-= 0.5*dx;
2546                         }
2547
2548                         dy= node->prvr.ymin - NODE_DYS/2;
2549
2550                         /* make sure that maximums are bigger or equal to minimums */
2551                         if(node->prvr.xmax < node->prvr.xmin) SWAP(float, node->prvr.xmax, node->prvr.xmin);
2552                         if(node->prvr.ymax < node->prvr.ymin) SWAP(float, node->prvr.ymax, node->prvr.ymin);
2553                 }
2554                 else {
2555                         float oldh= node->prvr.ymax - node->prvr.ymin;
2556                         if(oldh==0.0f)
2557                                 oldh= 0.6f*node->width-NODE_DY;
2558                         dy-= NODE_DYS/2;
2559                         node->prvr.ymax= dy;
2560                         node->prvr.ymin= dy - oldh;
2561                         dy= node->prvr.ymin - NODE_DYS/2;
2562                 }
2563         }
2564
2565         /* XXX ugly hack, typeinfo for group is generated */
2566         if(node->type == NODE_GROUP)
2567                 node->typeinfo->butfunc= node_buts_group;
2568         
2569         /* buttons rect? */
2570         if((node->flag & NODE_OPTIONS) && node->typeinfo->butfunc) {
2571                 dy-= NODE_DYS/2;
2572                 node->butr.ymax= dy;
2573                 node->butr.ymin= dy - (float)node->typeinfo->butfunc(NULL, NULL, node, NULL);
2574                 dy= node->butr.ymin - NODE_DYS/2;
2575         }
2576         
2577         /* input sockets */
2578         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
2579                 if(!(nsock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2580                         nsock->locx= node->locx;
2581                         nsock->locy= dy - NODE_DYS;
2582                         dy-= NODE_DY;
2583                 }
2584         }
2585         
2586         /* little bit space in end */
2587         if(node->inputs.first || (node->flag & (NODE_OPTIONS|NODE_PREVIEW))==0 )
2588                 dy-= NODE_DYS/2;
2589         
2590         node->totr.xmin= node->locx;
2591         node->totr.xmax= node->locx + node->width;
2592         node->totr.ymax= node->locy;
2593         node->totr.ymin= dy;
2594 }
2595
2596 /* based on settings in node, sets drawing rect info. each redraw! */
2597 /* note: this assumes only 1 group at a time is drawn (linked data) */
2598 /* in node->totr the entire boundbox for the group is stored */
2599 static void node_update_group(bNode *gnode)
2600 {
2601         bNodeTree *ngroup= (bNodeTree *)gnode->id;
2602         bNode *node;
2603         bNodeSocket *nsock;
2604         rctf *rect= &gnode->totr;
2605         int counter;
2606         
2607         /* center them, is a bit of abuse of locx and locy though */
2608         for(node= ngroup->nodes.first; node; node= node->next) {
2609                 node->locx+= gnode->locx;
2610                 node->locy+= gnode->locy;
2611                 if(node->flag & NODE_HIDDEN)
2612                         node_update_hidden(node);
2613                 else
2614                         node_update(node);
2615                 node->locx-= gnode->locx;
2616                 node->locy-= gnode->locy;
2617         }
2618         counter= 1;
2619         for(node= ngroup->nodes.first; node; node= node->next) {
2620                 if(counter) {
2621                         *rect= node->totr;
2622                         counter= 0;
2623                 }
2624                 else
2625                         BLI_union_rctf(rect, &node->totr);
2626         }
2627         if(counter==1) return;  /* should be prevented? */
2628         
2629         rect->xmin-= NODE_DY;
2630         rect->ymin-= NODE_DY;
2631         rect->xmax+= NODE_DY;
2632         rect->ymax+= NODE_DY;
2633         
2634         /* output sockets */
2635         for(nsock= gnode->outputs.first; nsock; nsock= nsock->next) {
2636                 nsock->locx= rect->xmax;
2637                 nsock->locy= nsock->tosock->locy;
2638         }
2639         
2640         /* input sockets */
2641         for(nsock= gnode->inputs.first; nsock; nsock= nsock->next) {
2642                 nsock->locx= rect->xmin;
2643                 nsock->locy= nsock->tosock->locy;
2644         }
2645 }
2646
2647 static void node_scaling_widget(int color_id, float aspect, float xmin, float ymin, float xmax, float ymax)
2648 {
2649         float dx;
2650         float dy;
2651         
2652         dx= 0.5f*(xmax-xmin);
2653         dy= 0.5f*(ymax-ymin);
2654         
2655         BIF_ThemeColorShade(color_id, +30);     
2656         fdrawline(xmin, ymin, xmax, ymax);
2657         fdrawline(xmin+dx, ymin, xmax, ymax-dy);
2658         
2659         BIF_ThemeColorShade(color_id, -10);
2660         fdrawline(xmin, ymin+aspect, xmax, ymax+aspect);
2661         fdrawline(xmin+dx, ymin+aspect, xmax, ymax-dy+aspect);
2662 }
2663
2664 static int node_get_colorid(bNode *node)
2665 {
2666         if(node->typeinfo->nclass==NODE_CLASS_INPUT)
2667                 return TH_NODE_IN_OUT;
2668         if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
2669                 if(node->flag & NODE_DO_OUTPUT)
2670                         return TH_NODE_IN_OUT;
2671                 else
2672                         return TH_NODE;
2673         }
2674         if(node->typeinfo->nclass==NODE_CLASS_CONVERTOR)
2675                 return TH_NODE_CONVERTOR;
2676         if(ELEM3(node->typeinfo->nclass, NODE_CLASS_OP_COLOR, NODE_CLASS_OP_VECTOR, NODE_CLASS_OP_FILTER))
2677                 return TH_NODE_OPERATOR;
2678         if(node->typeinfo->nclass==NODE_CLASS_GROUP)
2679                 return TH_NODE_GROUP;
2680         return TH_NODE;
2681 }
2682
2683 static void node_draw_basis(ScrArea *sa, SpaceNode *snode, bNode *node)
2684 {
2685         bNodeSocket *sock;
2686         uiBlock *block= NULL;
2687         uiBut *bt;
2688         rctf *rct= &node->totr;
2689         float slen, iconofs;
2690         int ofs, color_id= node_get_colorid(node);
2691         char showname[128];
2692         
2693         uiSetRoundBox(15-4);
2694         ui_dropshadow(rct, BASIS_RAD, snode->aspect, node->flag & SELECT);
2695         
2696         /* header */
2697         if(color_id==TH_NODE)
2698                 BIF_ThemeColorShade(color_id, -20);
2699         else
2700                 BIF_ThemeColor(color_id);
2701                 
2702         uiSetRoundBox(3);
2703         uiRoundBox(rct->xmin, rct->ymax-NODE_DY, rct->xmax, rct->ymax, BASIS_RAD);
2704         
2705         /* show/hide icons, note this sequence is copied in editnode.c */
2706         iconofs= rct->xmax;
2707         
2708         if(node->typeinfo->flag & NODE_PREVIEW) {
2709                 int icon_id;
2710                 
2711                 if(node->flag & (NODE_ACTIVE_ID|NODE_DO_OUTPUT))
2712                         icon_id= ICON_MATERIAL;
2713                 else
2714                         icon_id= ICON_MATERIAL_DEHLT;
2715                 iconofs-= 18.0f;
2716                 glEnable(GL_BLEND);
2717                 BIF_icon_draw_aspect_blended(iconofs, rct->ymax-NODE_DY+2, icon_id, snode->aspect, -60);
2718                 glDisable(GL_BLEND);
2719         }
2720         if(node->type == NODE_GROUP) {
2721                 
2722                 iconofs-= 18.0f;
2723                 glEnable(GL_BLEND);
2724                 if(node->id->lib) {
2725                         glPixelTransferf(GL_GREEN_SCALE, 0.7f);
2726                         glPixelTransferf(GL_BLUE_SCALE, 0.3f);
2727                         BIF_icon_draw_aspect(iconofs, rct->ymax-NODE_DY+2, ICON_NODE, snode->aspect);
2728                         glPixelTransferf(GL_GREEN_SCALE, 1.0f);
2729                         glPixelTransferf(GL_BLUE_SCALE, 1.0f);
2730                 }
2731                 else {
2732                         BIF_icon_draw_aspect_blended(iconofs, rct->ymax-NODE_DY+2, ICON_NODE, snode->aspect, -60);
2733                 }
2734                 glDisable(GL_BLEND);
2735         }
2736         if(node->typeinfo->flag & NODE_OPTIONS) {
2737                 iconofs-= 18.0f;
2738                 glEnable(GL_BLEND);
2739                 BIF_icon_draw_aspect_blended(iconofs, rct->ymax-NODE_DY+2, ICON_BUTS, snode->aspect, -60);
2740                 glDisable(GL_BLEND);
2741         }
2742         {       /* always hide/reveil unused sockets */ 
2743                 int shade;
2744
2745                 iconofs-= 18.0f;
2746                 if(node_has_hidden_sockets(node))
2747                         shade= -40;
2748                 else
2749                         shade= -90;
2750                 glEnable(GL_BLEND);
2751                 BIF_icon_draw_aspect_blended(iconofs, rct->ymax-NODE_DY+2, ICON_PLUS, snode->aspect, shade);
2752                 glDisable(GL_BLEND);
2753         }
2754         
2755         /* title */
2756         if(node->flag & SELECT) 
2757                 BIF_ThemeColor(TH_TEXT_HI);
2758         else
2759                 BIF_ThemeColorBlendShade(TH_TEXT, color_id, 0.4, 10);
2760         
2761         /* open/close entirely? */
2762         ui_draw_tria_icon(rct->xmin+8.0f, rct->ymax-NODE_DY+4.0f, snode->aspect, 'v');
2763
2764         if(node->flag & SELECT) 
2765                 BIF_ThemeColor(TH_TEXT_HI);
2766         else
2767                 BIF_ThemeColor(TH_TEXT);
2768         
2769         ui_rasterpos_safe(rct->xmin+19.0f, rct->ymax-NODE_DY+5.0f, snode->aspect);
2770         
2771         if(node->username[0]) {
2772                 strcpy(showname,"(");
2773                 strcat(showname, node->username);
2774                 strcat(showname,") ");
2775                 strcat(showname, node->name);
2776         }
2777         else
2778                 strcpy(showname, node->name);
2779
2780         snode_drawstring(snode, showname, (int)(iconofs - rct->xmin-18.0f));
2781
2782         /* body */
2783         BIF_ThemeColor4(TH_NODE);
2784         glEnable(GL_BLEND);
2785         uiSetRoundBox(8);
2786         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax-NODE_DY, BASIS_RAD);
2787         glDisable(GL_BLEND);
2788
2789         /* scaling indicator */
2790         node_scaling_widget(TH_NODE, snode->aspect, rct->xmax-BASIS_RAD*snode->aspect, rct->ymin, rct->xmax, rct->ymin+BASIS_RAD*snode->aspect);
2791
2792         /* outline active emphasis */
2793         if(node->flag & NODE_ACTIVE) {
2794                 glEnable(GL_BLEND);
2795                 glColor4ub(200, 200, 200, 140);
2796                 uiSetRoundBox(15-4);
2797                 gl_round_box(GL_LINE_LOOP, rct->xmin, rct->ymin, rct->xmax, rct->ymax, BASIS_RAD);
2798                 glDisable(GL_BLEND);
2799         }
2800         
2801         /* we make buttons for input sockets, if... */
2802         if(node->flag & NODE_OPTIONS) {
2803                 if(node->inputs.first || node->typeinfo->butfunc) {
2804                         char str[32];
2805                         
2806                         /* make unique block name, also used for handling blocks in editnode.c */
2807                         sprintf(str, "node buttons %p", node);
2808                         
2809                         block= uiNewBlock(&sa->uiblocks, str, UI_EMBOSS, UI_HELV, sa->win);
2810                         uiBlockSetFlag(block, UI_BLOCK_NO_HILITE);
2811                         if(snode->id)
2812                                 uiSetButLock(snode->id->lib!=NULL, ERROR_LIBDATA_MESSAGE);
2813                 }
2814         }
2815         
2816         /* hurmf... another candidate for callback, have to see how this works first */
2817         if(node->id && block && snode->treetype==NTREE_SHADER)
2818                 nodeShaderSynchronizeID(node, 0);
2819         
2820         /* socket inputs, buttons */
2821         for(sock= node->inputs.first; sock; sock= sock->next) {
2822                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2823                         socket_circle_draw(sock, NODE_SOCKSIZE);
2824                         
2825                         if(block && sock->link==NULL) {
2826                                 float *butpoin= sock->ns.vec;
2827                                 
2828                                 if(sock->type==SOCK_VALUE) {
2829                                         bt= uiDefButF(block, NUM, B_NODE_EXEC+node->nr, sock->name, 
2830                                                   (short)sock->locx+NODE_DYS, (short)(sock->locy)-9, (short)node->width-NODE_DY, 17, 
2831                                                   butpoin, sock->ns.min, sock->ns.max, 10, 2, "");
2832                                         uiButSetFunc(bt, node_sync_cb, snode, node);
2833                                 }
2834                                 else if(sock->type==SOCK_VECTOR) {
2835                                         uiDefBlockBut(block, socket_vector_menu, sock, sock->name, 
2836                                                   (short)sock->locx+NODE_DYS, (short)sock->locy-9, (short)node->width-NODE_DY, 17, 
2837                                                   "");
2838                                 }
2839                                 else if(block && sock->type==SOCK_RGBA) {
2840                                         short labelw= node->width-NODE_DY-40, width;
2841                                         
2842                                         if(labelw>0) width= 40; else width= node->width-NODE_DY;
2843                                         
2844                                         bt= uiDefButF(block, COL, B_NODE_EXEC+node->nr, "", 
2845                                                 (short)(sock->locx+NODE_DYS), (short)sock->locy-8, width, 15, 
2846                                                    butpoin, 0, 0, 0, 0, "");
2847                                         uiButSetFunc(bt, node_sync_cb, snode, node);
2848                                         
2849                                         if(labelw>0) uiDefBut(block, LABEL, 0, sock->name, 
2850                                                                                    (short)(sock->locx+NODE_DYS) + 40, (short)sock->locy-8, labelw, 15, 
2851                                                                                    NULL, 0, 0, 0, 0, "");
2852                                 }
2853                         }
2854                         else {
2855                                 BIF_ThemeColor(TH_TEXT);
2856                                 ui_rasterpos_safe(sock->locx+8.0f, sock->locy-5.0f, snode->aspect);
2857                                 BIF_DrawString(snode->curfont, sock->name, 0);
2858                         }
2859                 }
2860         }
2861         
2862         /* socket outputs */
2863         for(sock= node->outputs.first; sock; sock= sock->next) {
2864                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL))) {
2865                         socket_circle_draw(sock, NODE_SOCKSIZE);
2866                         
2867                         BIF_ThemeColor(TH_TEXT);
2868                         ofs= 0;
2869                         slen= snode->aspect*BIF_GetStringWidth(snode->curfont, sock->name, 0);
2870                         while(slen > node->width) {
2871                                 ofs++;
2872                                 slen= snode->aspect*BIF_GetStringWidth(snode->curfont, sock->name+ofs, 0);
2873                         }
2874                         ui_rasterpos_safe(sock->locx-8.0f-slen, sock->locy-5.0f, snode->aspect);
2875                         BIF_DrawString(snode->curfont, sock->name+ofs, 0);
2876                 }
2877         }
2878         
2879         /* preview */
2880         if(node->flag & NODE_PREVIEW)
2881                 if(node->preview && node->preview->rect)
2882                         node_draw_preview(node->preview, &node->prvr);
2883                 
2884         /* buttons */
2885         if(node->flag & NODE_OPTIONS) {
2886                 if(block) {
2887                         if(node->typeinfo->butfunc) {
2888                                 node->typeinfo->butfunc(block, snode->nodetree, node, &node->butr);
2889                         }
2890                         uiDrawBlock(block);
2891                 }
2892         }
2893
2894 }
2895
2896 static void node_draw_hidden(SpaceNode *snode, bNode *node)
2897 {
2898         bNodeSocket *sock;
2899         rctf *rct= &node->totr;
2900         float dx, centy= 0.5f*(rct->ymax+rct->ymin);
2901         float hiddenrad= 0.5f*(rct->ymax-rct->ymin);
2902         int color_id= node_get_colorid(node);
2903         char showname[128];
2904         
2905         /* shadow */
2906         uiSetRoundBox(15);
2907         ui_dropshadow(rct, hiddenrad, snode->aspect, node->flag & SELECT);
2908
2909         /* body */
2910         BIF_ThemeColor(color_id);       
2911         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax, hiddenrad);
2912         
2913         /* outline active emphasis */
2914         if(node->flag & NODE_ACTIVE) {
2915                 glEnable(GL_BLEND);
2916                 glColor4ub(200, 200, 200, 140);
2917                 gl_round_box(GL_LINE_LOOP, rct->xmin, rct->ymin, rct->xmax, rct->ymax, hiddenrad);
2918                 glDisable(GL_BLEND);
2919         }
2920         
2921         /* title */
2922         if(node->flag & SELECT) 
2923                 BIF_ThemeColor(TH_TEXT_HI);
2924         else
2925                 BIF_ThemeColorBlendShade(TH_TEXT, color_id, 0.4, 10);
2926         
2927         /* open entirely icon */
2928         ui_draw_tria_icon(rct->xmin+9.0f, centy-6.0f, snode->aspect, 'h');      
2929         
2930         if(node->flag & SELECT) 
2931                 BIF_ThemeColor(TH_TEXT_HI);
2932         else
2933                 BIF_ThemeColor(TH_TEXT);
2934         
2935         if(node->miniwidth>0.0f) {
2936                 ui_rasterpos_safe(rct->xmin+21.0f, centy-4.0f, snode->aspect);
2937
2938                 if(node->username[0]) {
2939                         strcpy(showname,"(");
2940                         strcat(showname, node->username);
2941                         strcat(showname,") ");
2942                         strcat(showname, node->name);
2943                 }
2944                 else
2945                         strcpy(showname, node->name);
2946
2947                 snode_drawstring(snode, showname, (int)(rct->xmax - rct->xmin-18.0f -12.0f));
2948         }       
2949
2950         /* scale widget thing */
2951         BIF_ThemeColorShade(color_id, -10);     
2952         dx= 10.0f;
2953         fdrawline(rct->xmax-dx, centy-4.0f, rct->xmax-dx, centy+4.0f);
2954         fdrawline(rct->xmax-dx-3.0f*snode->aspect, centy-4.0f, rct->xmax-dx-3.0f*snode->aspect, centy+4.0f);
2955         
2956         BIF_ThemeColorShade(color_id, +30);
2957         dx-= snode->aspect;
2958         fdrawline(rct->xmax-dx, centy-4.0f, rct->xmax-dx, centy+4.0f);
2959         fdrawline(rct->xmax-dx-3.0f*snode->aspect, centy-4.0f, rct->xmax-dx-3.0f*snode->aspect, centy+4.0f);
2960         
2961         /* sockets */
2962         for(sock= node->inputs.first; sock; sock= sock->next) {
2963                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
2964                         socket_circle_draw(sock, NODE_SOCKSIZE);
2965         }
2966         
2967         for(sock= node->outputs.first; sock; sock= sock->next) {
2968                 if(!(sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL)))
2969                         socket_circle_draw(sock, NODE_SOCKSIZE);
2970         }
2971 }
2972
2973 /* note; this is used for fake links in groups too */
2974 void node_draw_link(SpaceNode *snode, bNodeLink *link)
2975 {
2976         float vec[4][3];
2977         float dist, spline_step, mx=0.0f, my=0.0f;
2978         int curve_res, do_shaded= 1, th_col1= TH_WIRE, th_col2= TH_WIRE;
2979         
2980         if(link->fromnode==NULL && link->tonode==NULL)
2981                 return;
2982         
2983         /* this is dragging link */
2984         if(link->fromnode==NULL || link->tonode==NULL) {
2985                 short mval[2];
2986                 getmouseco_areawin(mval);
2987                 areamouseco_to_ipoco(G.v2d, mval, &mx, &my);
2988                 BIF_ThemeColor(TH_WIRE);
2989                 do_shaded= 0;
2990         }
2991         else {
2992                 /* going to give issues once... */
2993                 if(link->tosock->flag & SOCK_UNAVAIL)
2994                         return;
2995                 if(link->fromsock->flag & SOCK_UNAVAIL)
2996                         return;
2997                 
2998                 /* a bit ugly... but thats how we detect the internal group links */
2999                 if(link->fromnode==link->tonode) {
3000                         BIF_ThemeColorBlend(TH_BACK, TH_WIRE, 0.25f);
3001                         do_shaded= 0;
3002                 }
3003                 else {
3004                         /* check cyclic */
3005                         if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
3006                                 if(link->fromnode->flag & SELECT)
3007                                         th_col1= TH_EDGE_SELECT;
3008                                 if(link->tonode->flag & SELECT)
3009                                         th_col2= TH_EDGE_SELECT;
3010                         }                               
3011                         else {
3012                                 BIF_ThemeColor(TH_REDALERT);
3013                                 do_shaded= 0;
3014                         }
3015                 }
3016         }
3017         
3018         vec[0][2]= vec[1][2]= vec[2][2]= vec[3][2]= 0.0; /* only 2d spline, set the Z to 0*/
3019         
3020         /* in v0 and v3 we put begin/end points */
3021         if(link->fromnode) {
3022                 vec[0][0]= link->fromsock->locx;
3023                 vec[0][1]= link->fromsock->locy;
3024         }
3025         else {
3026 &nbs