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