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