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