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