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