Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[blender.git] / source / blender / editors / space_node / drawnode.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2005 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): David Millan Escriva, Juho Vepsäläinen
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "BLI_blenlib.h"
35 #include "BLI_arithb.h"
36
37 #include "DNA_ID.h"
38 #include "DNA_node_types.h"
39 #include "DNA_image_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_mesh_types.h"
42 #include "DNA_action_types.h"
43 #include "DNA_color_types.h"
44 #include "DNA_customdata_types.h"
45 #include "DNA_gpencil_types.h"
46 #include "DNA_ipo_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_space_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_texture_types.h"
52 #include "DNA_text_types.h"
53 #include "DNA_userdef_types.h"
54
55 #include "BKE_context.h"
56 #include "BKE_curve.h"
57 #include "BKE_global.h"
58 #include "BKE_image.h"
59 #include "BKE_library.h"
60 #include "BKE_main.h"
61 #include "BKE_material.h"
62 #include "BKE_node.h"
63 #include "BKE_object.h"
64 #include "BKE_texture.h"
65 #include "BKE_text.h"
66 #include "BKE_utildefines.h"
67
68 #include "CMP_node.h"
69 #include "SHD_node.h"
70
71 #include "BIF_gl.h"
72 #include "BIF_glutil.h"
73
74 #include "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",
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                         uiDefButS(block, TOG, B_NODE_EXEC, "A",
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_SCENE_DATA, 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                 NodeBlurData *nbd= node->storage;
1463                 short dy= butr->ymin;
1464                 short dx= (butr->xmax-butr->xmin);
1465                 
1466                 uiBlockBeginAlign(block);
1467                 uiDefButS(block, NUM, B_NODE_EXEC, "Samples:",
1468                                  butr->xmin, dy+76, dx, 19, 
1469                                  &nbd->samples, 1, 256, 0, 0, "Amount of samples");
1470                 uiDefButS(block, NUM, B_NODE_EXEC, "MinSpeed:",
1471                                   butr->xmin, dy+57, dx, 19, 
1472                                   &nbd->minspeed, 0, 1024, 0, 0, "Minimum speed for a pixel to be blurred, used to separate background from foreground");
1473                 uiDefButS(block, NUM, B_NODE_EXEC, "MaxSpeed:",
1474                                   butr->xmin, dy+38, dx, 19, 
1475                                   &nbd->maxspeed, 0, 1024, 0, 0, "If not zero, maximum speed in pixels");
1476                 uiDefButF(block, NUM, B_NODE_EXEC, "BlurFac:",
1477                                   butr->xmin, dy+19, dx, 19, 
1478                                   &nbd->fac, 0.0f, 2.0f, 10, 2, "Scaling factor for motion vectors, actually 'shutter speed' in frames");
1479                 uiDefButS(block, TOG, B_NODE_EXEC, "Curved",
1480                                   butr->xmin, dy, dx, 19, 
1481                                   &nbd->curved, 0.0f, 2.0f, 10, 2, "Interpolate between frames in a bezier curve, rather than linearly");
1482                 uiBlockEndAlign(block);
1483         }
1484         return 95;
1485 }
1486
1487 static int node_composit_buts_filter(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1488 {
1489         if(block) {
1490                 uiBut *bt;
1491                 
1492                 /* blend type */
1493                 bt=uiDefButS(block, MENU, B_NODE_EXEC, "Soften %x0|Sharpen %x1|Laplace %x2|Sobel %x3|Prewitt %x4|Kirsch %x5|Shadow %x6",
1494                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1495                                          &node->custom1, 0, 0, 0, 0, "");
1496                 uiButSetFunc(bt, node_but_title_cb, node, bt);
1497         }
1498         return 20;
1499 }
1500
1501 static int node_composit_buts_flip(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
1502 {
1503         if(block) {
1504                 uiBut *bt;
1505                 
1506                 /* flip x\y */
1507                 bt=uiDefButS(block, MENU, B_NODE_EXEC, "Flip X %x0|Flip Y %x1|Flip X & Y %x2",
1508                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1509                                          &node->custom1, 0, 0, 0, 0, "");
1510                 uiButSetFunc(bt, node_but_title_cb, node, bt);
1511         }
1512         return 20;      
1513 }
1514
1515 static int node_composit_buts_crop(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1516 {
1517         if(block) {
1518                 NodeTwoXYs *ntxy= node->storage;
1519                 char elementheight = 19;
1520                 short dx= (butr->xmax-butr->xmin)/2;
1521                 short dy= butr->ymax - elementheight;
1522                 short xymin= 0, xymax= 10000;
1523
1524                 uiBlockBeginAlign(block);
1525
1526                 /* crop image size toggle */
1527                 uiDefButS(block, TOG, B_NODE_EXEC, "Crop Image Size",
1528                                   butr->xmin, dy, dx*2, elementheight, 
1529                                   &node->custom1, 0, 0, 0, 0, "Crop the size of the input image.");
1530
1531                 dy-=elementheight;
1532
1533                 /* x1 */
1534                 uiDefButS(block, NUM, B_NODE_EXEC, "X1:",
1535                                          butr->xmin, dy, dx, elementheight,
1536                                          &ntxy->x1, xymin, xymax, 0, 0, "");
1537                 /* y1 */
1538                 uiDefButS(block, NUM, B_NODE_EXEC, "Y1:",
1539                                          butr->xmin+dx, dy, dx, elementheight,
1540                                          &ntxy->y1, xymin, xymax, 0, 0, "");
1541
1542                 dy-=elementheight;
1543
1544                 /* x2 */
1545                 uiDefButS(block, NUM, B_NODE_EXEC, "X2:",
1546                                          butr->xmin, dy, dx, elementheight,
1547                                          &ntxy->x2, xymin, xymax, 0, 0, "");
1548                 /* y2 */
1549                 uiDefButS(block, NUM, B_NODE_EXEC, "Y2:",
1550                                          butr->xmin+dx, dy, dx, elementheight,
1551                                          &ntxy->y2, xymin, xymax, 0, 0, "");
1552
1553                 uiBlockEndAlign(block);
1554         }
1555         return 60;
1556 }
1557
1558 static int node_composit_buts_splitviewer(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1559 {
1560         if(block) {     
1561                 uiBlockBeginAlign(block);
1562                 
1563                 uiDefButS(block, ROW, B_NODE_EXEC, "X",
1564                                   butr->xmin, butr->ymin+19, (butr->xmax-butr->xmin)/2, 20, 
1565                                   &node->custom2, 0.0, 0.0, 0, 0, "");
1566                 uiDefButS(block, ROW, B_NODE_EXEC, "Y",
1567                                   butr->xmin+(butr->xmax-butr->xmin)/2, butr->ymin+19, (butr->xmax-butr->xmin)/2, 20, 
1568                                   &node->custom2, 0.0, 1.0, 0, 0, "");
1569                                   
1570                 uiDefButS(block, NUMSLI, B_NODE_EXEC, "Split %: ",
1571                                 butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, &node->custom1, 0, 100, 10, 0, "");
1572         }
1573         return 40;
1574 }
1575
1576 static int node_composit_buts_map_value(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1577 {
1578         if(block) {
1579                 TexMapping *texmap= node->storage;
1580                 short xstart= (short)butr->xmin;
1581                 short dy= (short)(butr->ymax-19.0f);
1582                 short dx= (short)(butr->xmax-butr->xmin)/2;
1583                 
1584                 uiBlockBeginAlign(block);
1585                 uiDefButF(block, NUM, B_NODE_EXEC, "Offs:", xstart, dy, 2*dx, 19, texmap->loc, -1000.0f, 1000.0f, 10, 2, "");
1586                 dy-= 19;
1587                 uiDefButF(block, NUM, B_NODE_EXEC, "Size:", xstart, dy, 2*dx, 19, texmap->size, -1000.0f, 1000.0f, 10, 3, "");
1588                 dy-= 23;
1589                 uiBlockBeginAlign(block);
1590                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MIN, B_NODE_EXEC, "Min", xstart, dy, dx, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
1591                 uiDefButF(block, NUM, B_NODE_EXEC, "", xstart+dx, dy, dx, 19, texmap->min, -1000.0f, 1000.0f, 10, 2, "");
1592                 dy-= 19;
1593                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MAX, B_NODE_EXEC, "Max", xstart, dy, dx, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
1594                 uiDefButF(block, NUM, B_NODE_EXEC, "", xstart+dx, dy, dx, 19, texmap->max, -1000.0f, 1000.0f, 10, 2, "");
1595         }
1596         return 80;
1597 }
1598
1599 static int node_composit_buts_alphaover(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1600 {
1601         if(block) {
1602                 NodeTwoFloats *ntf= node->storage;
1603                 
1604                 /* alpha type */
1605                 uiDefButS(block, TOG, B_NODE_EXEC, "ConvertPremul",
1606                                   butr->xmin, butr->ymin+19, butr->xmax-butr->xmin, 19, 
1607                                   &node->custom1, 0, 0, 0, 0, "");
1608                 /* mix factor */
1609                 uiDefButF(block, NUM, B_NODE_EXEC, "Premul: ",
1610                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 19, 
1611                                   &ntf->x, 0.0f, 1.0f, 100, 0, "");
1612         }
1613         return 38;
1614 }
1615
1616 static int node_composit_buts_hue_sat(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1617 {
1618         if(block) {
1619                 NodeHueSat *nhs= node->storage;
1620                 
1621                 uiBlockBeginAlign(block);
1622                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "Hue: ",
1623                                   butr->xmin, butr->ymin+40.0f, butr->xmax-butr->xmin, 20, 
1624                                   &nhs->hue, 0.0f, 1.0f, 100, 0, "");
1625                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "Sat: ",
1626                                   butr->xmin, butr->ymin+20.0f, butr->xmax-butr->xmin, 20, 
1627                                   &nhs->sat, 0.0f, 2.0f, 100, 0, "");
1628                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "Val: ",
1629                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1630                                   &nhs->val, 0.0f, 2.0f, 100, 0, "");
1631         }
1632         return 60;
1633 }
1634
1635 static int node_composit_buts_dilateerode(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1636 {
1637         if(block) {
1638                 uiDefButS(block, NUM, B_NODE_EXEC, "Distance:",
1639                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1640                                   &node->custom2, -100, 100, 0, 0, "Distance to grow/shrink (number of iterations)");
1641         }
1642         return 20;
1643 }
1644
1645 static int node_composit_buts_diff_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1646 {
1647         if(block) {
1648                 short sx= (butr->xmax-butr->xmin)/4;
1649                 short dx= (butr->xmax-butr->xmin)/3;
1650                 NodeChroma *c= node->storage;
1651                 
1652                 uiBlockBeginAlign(block);
1653                 /*color space selectors*/
1654                 uiDefButS(block, ROW,B_NODE_EXEC,"RGB",
1655                                                         butr->xmin,butr->ymin+60,sx,20,
1656                                                         &node->custom1,1,1, 0, 0, "RGB Color Space");
1657                 uiDefButS(block, ROW,B_NODE_EXEC,"HSV",
1658                                                         butr->xmin+sx,butr->ymin+60,sx,20,
1659                                                         &node->custom1,1,2, 0, 0, "HSV Color Space");
1660                 uiDefButS(block, ROW,B_NODE_EXEC,"YUV",
1661                                                         butr->xmin+2*sx,butr->ymin+60,sx,20,
1662                                                         &node->custom1,1,3, 0, 0, "YUV Color Space");
1663                                         uiDefButS(block, ROW,B_NODE_EXEC,"YCC",
1664                                                         butr->xmin+3*sx,butr->ymin+60,sx,20,
1665                                                         &node->custom1,1,4, 0, 0, "YCbCr Color Space");
1666                 /*channel tolorences*/
1667                 uiDefButF(block, NUM, B_NODE_EXEC, " ",
1668                                                         butr->xmin, butr->ymin+40, dx, 20,
1669                                                         &c->t1, 0.0f, 1.0f, 100, 0, "Channel 1 Tolerance");
1670                 uiDefButF(block, NUM, B_NODE_EXEC, " ",
1671                                                         butr->xmin+dx, butr->ymin+40, dx, 20,
1672                                                         &c->t2, 0.0f, 1.0f, 100, 0, "Channel 2 Tolorence");
1673                 uiDefButF(block, NUM, B_NODE_EXEC, " ",
1674                                                         butr->xmin+2*dx, butr->ymin+40, dx, 20,
1675                                                         &c->t3, 0.0f, 1.0f, 100, 0, "Channel 3 Tolorence");
1676                 /*falloff parameters*/
1677                 /*
1678                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "Falloff Size ",
1679                         butr->xmin, butr->ymin+20, butr->xmax-butr->xmin, 20,
1680                         &c->fsize, 0.0f, 1.0f, 100, 0, "");
1681                 */
1682                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "Falloff: ",
1683                         butr->xmin, butr->ymin+20, butr->xmax-butr->xmin, 20,
1684                         &c->fstrength, 0.0f, 1.0f, 100, 0, "");
1685         }
1686         return 80;
1687 }
1688
1689 static int node_composit_buts_color_spill(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1690 {
1691         if(block) {
1692                 short dx= (butr->xmax-butr->xmin)/3;
1693
1694                 NodeChroma *c=node->storage;
1695                 uiBlockBeginAlign(block);
1696                 uiDefButF(block, NUM, B_NODE_EXEC, "Enhance: ", 
1697                                 butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1698                                 &c->t1, 0.0f, 0.5f, 100, 2, "Adjusts how much selected channel is affected by color spill algorithm");
1699                 uiDefButS(block, ROW, B_NODE_EXEC, "R",
1700                                 butr->xmin,butr->ymin,dx,20,
1701                                 &node->custom1,1,1, 0, 0, "Red Spill Suppression");
1702                 uiDefButS(block, ROW, B_NODE_EXEC, "G",
1703                                 butr->xmin+dx,butr->ymin,dx,20,
1704                                 &node->custom1,1,2, 0, 0, "Green Spill Suppression");
1705                 uiDefButS(block, ROW, B_NODE_EXEC, "B",
1706                                 butr->xmin+2*dx,butr->ymin,dx,20,
1707                                 &node->custom1, 1, 3, 0, 0, "Blue Spill Suppression");
1708                 uiBlockEndAlign(block);
1709         }
1710         return 60;
1711 }
1712
1713 static int node_composit_buts_chroma_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1714 {
1715         if(block) {
1716                 short dx=(butr->xmax-butr->xmin)/2;
1717                 NodeChroma *c= node->storage;
1718                 uiBlockBeginAlign(block);
1719
1720                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "Acceptance ",
1721                         butr->xmin, butr->ymin+60, butr->xmax-butr->xmin, 20,
1722                         &c->t1, 1.0f, 80.0f, 100, 0, "Tolerance for colors to be considered a keying color");
1723                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "Cutoff ",
1724                         butr->xmin, butr->ymin+40, butr->xmax-butr->xmin, 20,
1725                         &c->t2, 0.0f, 30.0f, 100, 0, "Colors below this will be considered as exact matches for keying color");
1726
1727                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "Lift ",
1728                         butr->xmin, butr->ymin+20, dx, 20,
1729                         &c->fsize, 0.0f, 1.0f, 100, 0, "Alpha Lift");
1730                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "Gain ",
1731                         butr->xmin+dx, butr->ymin+20, dx, 20,
1732                         &c->fstrength, 0.0f, 1.0f, 100, 0, "Alpha Gain");
1733
1734                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "Shadow Adjust ",
1735                         butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1736                         &c->t3, 0.0f, 1.0f, 100, 0, "Adjusts the brightness of any shadows captured");
1737
1738                 if(c->t2 > c->t1)
1739                         c->t2=c->t1;
1740         }
1741         return 80;
1742 }
1743
1744 static int node_composit_buts_channel_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1745 {
1746         if(block) {
1747                 short sx= (butr->xmax-butr->xmin)/4;
1748                 short cx= (butr->xmax-butr->xmin)/3;
1749                 NodeChroma *c=node->storage;
1750                 char *c1, *c2, *c3;
1751         
1752                 /*color space selectors*/
1753                 uiBlockBeginAlign(block);
1754                 uiDefButS(block, ROW,B_NODE_EXEC,"RGB",
1755                         butr->xmin,butr->ymin+60,sx,20,&node->custom1,1,1, 0, 0, "RGB Color Space");
1756                 uiDefButS(block, ROW,B_NODE_EXEC,"HSV",
1757                         butr->xmin+sx,butr->ymin+60,sx,20,&node->custom1,1,2, 0, 0, "HSV Color Space");
1758                 uiDefButS(block, ROW,B_NODE_EXEC,"YUV",
1759                         butr->xmin+2*sx,butr->ymin+60,sx,20,&node->custom1,1,3, 0, 0, "YUV Color Space");
1760                 uiDefButS(block, ROW,B_NODE_EXEC,"YCC",
1761                         butr->xmin+3*sx,butr->ymin+60,sx,20,&node->custom1,1,4, 0, 0, "YCbCr Color Space");
1762         
1763                 if (node->custom1==1) {
1764                         c1="R"; c2="G"; c3="B";
1765                 }
1766                 else if(node->custom1==2){
1767                         c1="H"; c2="S"; c3="V";
1768                 }
1769                 else if(node->custom1==3){
1770                         c1="Y"; c2="U"; c3="V";
1771                 }
1772                 else { // if(node->custom1==4){
1773                         c1="Y"; c2="Cb"; c3="Cr";
1774                 }
1775         
1776                 /*channel selector */
1777                 uiDefButS(block, ROW, B_NODE_EXEC, c1,
1778                         butr->xmin,butr->ymin+40,cx,20,&node->custom2,1, 1, 0, 0, "Channel 1");
1779                 uiDefButS(block, ROW, B_NODE_EXEC, c2,
1780                         butr->xmin+cx,butr->ymin+40,cx,20,&node->custom2,1, 2, 0, 0, "Channel 2");
1781                 uiDefButS(block, ROW, B_NODE_EXEC, c3,
1782                         butr->xmin+cx+cx,butr->ymin+40,cx,20,&node->custom2, 1, 3, 0, 0, "Channel 3");
1783         
1784                 /*tolerance sliders */
1785                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "High ", 
1786                         butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1787                         &c->t1, 0.0f, 1.0f, 100, 0, "Values higher than this setting are 100% opaque");
1788                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "Low ", 
1789                         butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1790                         &c->t2, 0.0f, 1.0f, 100, 0, "Values lower than this setting are 100% keyed");
1791                 uiBlockEndAlign(block);
1792         
1793                 /*keep t2 (low) less than t1 (high) */
1794                 if(c->t2 > c->t1) {
1795                         c->t2=c->t1;
1796                 }
1797         }
1798         return 80;
1799 }
1800
1801 static int node_composit_buts_luma_matte(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1802 {
1803         if(block) {
1804                 NodeChroma *c=node->storage;
1805         
1806                 /*tolerance sliders */
1807                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "High ", 
1808                         butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1809                         &c->t1, 0.0f, 1.0f, 100, 0, "Values higher than this setting are 100% opaque");
1810                 uiDefButF(block, NUMSLI, B_NODE_EXEC, "Low ", 
1811                         butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1812                         &c->t2, 0.0f, 1.0f, 100, 0, "Values lower than this setting are 100% keyed");
1813                 uiBlockEndAlign(block);
1814         
1815                 /*keep t2 (low) less than t1 (high) */
1816                 if(c->t2 > c->t1) {
1817                         c->t2=c->t1;
1818                 }
1819         }
1820         return 40;
1821 }
1822
1823 static int node_composit_buts_map_uv(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1824 {
1825         if(block) {
1826                 uiDefButS(block, NUM, B_NODE_EXEC, "Alpha:",
1827                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1828                                   &node->custom1, 0, 100, 0, 0, "Conversion percentage of UV differences to Alpha");
1829         }
1830         return 20;
1831 }
1832
1833 static int node_composit_buts_id_mask(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1834 {
1835         if(block) {
1836                 uiDefButS(block, NUM, B_NODE_EXEC, "ID:",
1837                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1838                                   &node->custom1, 0, 10000, 0, 0, "Pass Index number to convert to Alpha");
1839         }
1840         return 20;
1841 }
1842
1843 /* allocate sufficient! */
1844 static void node_imagetype_string(char *str)
1845 {
1846         str += sprintf(str, "Save Image as: %%t|");
1847         str += sprintf(str, "Targa %%x%d|", R_TARGA);
1848         str += sprintf(str, "Targa Raw %%x%d|", R_RAWTGA);
1849         str += sprintf(str, "PNG %%x%d|", R_PNG);
1850         str += sprintf(str, "BMP %%x%d|", R_BMP);
1851         str += sprintf(str, "Jpeg %%x%d|", R_JPEG90);
1852         str += sprintf(str, "Iris %%x%d|", R_IRIS);
1853         str += sprintf(str, "Radiance HDR %%x%d|", R_RADHDR);
1854         str += sprintf(str, "Cineon %%x%d|", R_CINEON);
1855         str += sprintf(str, "DPX %%x%d|", R_DPX);
1856         str += sprintf(str, "OpenEXR %%x%d", R_OPENEXR);
1857 }
1858
1859 static void node_set_image_cb(bContext *C, void *ntree_v, void *node_v)
1860 {
1861         bNodeTree *ntree= ntree_v;
1862         bNode *node= node_v;
1863         
1864         nodeSetActive(ntree, node);
1865 }
1866
1867 static int node_composit_buts_file_output(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1868 {
1869         if(block) {
1870                 NodeImageFile *nif= node->storage;
1871                 uiBut *bt;
1872                 short x= (short)butr->xmin;
1873                 short y= (short)butr->ymin;
1874                 short w= (short)butr->xmax-butr->xmin;
1875                 char str[320];
1876                 
1877                 node_imagetype_string(str);
1878                 
1879                 uiBlockBeginAlign(block);
1880                 
1881                 bt = uiDefIconBut(block, BUT, B_NODE_SETIMAGE, ICON_FILESEL,
1882                                   x, y+60, 20, 20,
1883                                   0, 0, 0, 0, 0, "Open Fileselect to get Backbuf image");
1884                 uiButSetFunc(bt, node_set_image_cb, ntree, node);
1885                 
1886                 uiDefBut(block, TEX, B_NOP, "",
1887                                   20+x, y+60, w-20, 20, 
1888                                   nif->name, 0.0f, 240.0f, 0, 0, "");
1889                 
1890                 uiDefButS(block, MENU, B_NOP, str,
1891                                   x, y+40, w, 20, 
1892                                   &nif->imtype, 0.0f, 1.0f, 0, 0, "");
1893                 
1894                 if(nif->imtype==R_OPENEXR) {
1895                         uiDefButBitS(block, TOG, R_OPENEXR_HALF, B_REDR, "Half",        
1896                                                 x, y+20, w/2, 20, 
1897                                                 &nif->subimtype, 0, 0, 0, 0, "");
1898
1899                         uiDefButS(block, MENU,B_NOP, "Codec %t|None %x0|Pxr24 (lossy) %x1|ZIP (lossless) %x2|PIZ (lossless) %x3|RLE (lossless) %x4",  
1900                                                 x+w/2, y+20, w/2, 20, 
1901                                                 &nif->codec, 0, 0, 0, 0, "");
1902                 }
1903                 else {
1904                         uiDefButS(block, NUM, B_NOP, "Quality: ",
1905                                   x, y+20, w, 20, 
1906                                   &nif->quality, 10.0f, 100.0f, 10, 0, "");
1907                 }
1908                 
1909                 /* start frame, end frame */
1910                 uiDefButI(block, NUM, B_NODE_EXEC, "SFra: ", 
1911                                   x, y, w/2, 20, 
1912                                   &nif->sfra, 1, MAXFRAMEF, 10, 0, "");
1913                 uiDefButI(block, NUM, B_NODE_EXEC, "EFra: ", 
1914                                   x+w/2, y, w/2, 20, 
1915                                   &nif->efra, 1, MAXFRAMEF, 10, 0, "");
1916                 
1917         }
1918         return 80;
1919 }
1920
1921 static void node_scale_cb(bContext *C, void *node_v, void *unused_v)
1922 {
1923         bNode *node= node_v;
1924         bNodeSocket *nsock;
1925
1926         /* check the 2 inputs, and set them to reasonable values */
1927         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
1928                 if(ELEM(node->custom1, CMP_SCALE_RELATIVE, CMP_SCALE_SCENEPERCENT))
1929                         nsock->ns.vec[0]= 1.0;
1930                 else {
1931                         if(nsock->next==NULL)
1932                                 nsock->ns.vec[0]= (float)CTX_data_scene(C)->r.ysch;
1933                         else
1934                                 nsock->ns.vec[0]= (float)CTX_data_scene(C)->r.xsch;
1935                 }
1936         }       
1937 }
1938
1939 static int node_composit_buts_scale(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1940 {
1941         if(block) {
1942                 uiBut *bt= uiDefButS(block, MENU, B_NODE_EXEC, "Relative %x0|Absolute %x1|Scene Size % %x2|",
1943                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1944                                   &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");
1945                 uiButSetFunc(bt, node_scale_cb, node, NULL);
1946         }
1947         return 20;
1948 }
1949
1950 static int node_composit_buts_invert(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr) 
1951 {
1952         if(block) {
1953                 uiBlockBeginAlign(block);
1954                 uiDefButBitS(block, TOG, CMP_CHAN_RGB, B_NODE_EXEC, "RGB",
1955                                          butr->xmin, butr->ymin, (butr->xmax-butr->xmin)/2, 20, 
1956                                          &node->custom1, 0, 0, 0, 0, "");
1957                 uiDefButBitS(block, TOG, CMP_CHAN_A, B_NODE_EXEC, "A",
1958                                          butr->xmin+(butr->xmax-butr->xmin)/2, butr->ymin, (butr->xmax-butr->xmin)/2, 20, 
1959                                          &node->custom1, 0, 0, 0, 0, "");
1960                 uiBlockEndAlign(block);
1961         }
1962         return 20;      
1963 }
1964
1965 static int node_composit_buts_premulkey(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
1966 {
1967         if(block) {
1968                 uiBut *bt;
1969                 
1970                 /* blend type */
1971                 bt=uiDefButS(block, MENU, B_NODE_EXEC, "Key to Premul %x0|Premul to Key %x1",
1972                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1973                                          &node->custom1, 0, 0, 0, 0, "Conversion between premultiplied alpha and key alpha");
1974         }
1975         return 20;
1976 }
1977
1978 /* only once called */
1979 static void node_composit_set_butfunc(bNodeType *ntype)
1980 {
1981         switch(ntype->type) {
1982                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
1983
1984                 case CMP_NODE_IMAGE:
1985                         ntype->butfunc= node_composit_buts_image;
1986                         break;
1987                 case CMP_NODE_R_LAYERS:
1988                         ntype->butfunc= node_composit_buts_renderlayers;
1989                         break;
1990                 case CMP_NODE_NORMAL:
1991                         ntype->butfunc= node_buts_normal;
1992                         break;
1993                 case CMP_NODE_CURVE_VEC:
1994                         ntype->butfunc= node_buts_curvevec;
1995                         break;
1996                 case CMP_NODE_CURVE_RGB:
1997                         ntype->butfunc= node_buts_curvecol;
1998                         break;
1999                 case CMP_NODE_VALUE:
2000                         ntype->butfunc= node_buts_value;
2001                         break;
2002                 case CMP_NODE_RGB:
2003                         ntype->butfunc= node_buts_rgb;
2004                         break;
2005                 case CMP_NODE_FLIP:
2006                         ntype->butfunc= node_composit_buts_flip;
2007                         break;
2008                 case CMP_NODE_SPLITVIEWER:
2009                         ntype->butfunc= node_composit_buts_splitviewer;
2010                         break;
2011                 case CMP_NODE_MIX_RGB:
2012                         ntype->butfunc= node_buts_mix_rgb;
2013                         break;
2014                 case CMP_NODE_VALTORGB:
2015                         ntype->butfunc= node_buts_valtorgb;
2016                         break;
2017                 case CMP_NODE_CROP:
2018                         ntype->butfunc= node_composit_buts_crop;
2019                         break;
2020                 case CMP_NODE_BLUR:
2021                         ntype->butfunc= node_composit_buts_blur;
2022                         break;
2023                 case CMP_NODE_DBLUR:
2024                         ntype->butfunc= node_composit_buts_dblur;
2025                         break;
2026                 case CMP_NODE_BILATERALBLUR:
2027                         ntype->butfunc= node_composit_buts_bilateralblur;
2028                         break;
2029                 /* qdn: defocus node */
2030                 case CMP_NODE_DEFOCUS:
2031                         ntype->butfunc = node_composit_buts_defocus;
2032                         break;
2033                 /* qdn: glare node */
2034                 case CMP_NODE_GLARE:
2035                         ntype->butfunc = node_composit_buts_glare;
2036                         break;
2037                 /* qdn: tonemap node */
2038                 case CMP_NODE_TONEMAP:
2039                         ntype->butfunc = node_composit_buts_tonemap;
2040                         break;
2041                 /* qdn: lens distortion node */
2042                 case CMP_NODE_LENSDIST:
2043                         ntype->butfunc = node_composit_buts_lensdist;
2044                         break;
2045                 case CMP_NODE_VECBLUR:
2046                         ntype->butfunc= node_composit_buts_vecblur;
2047                         break;
2048                 case CMP_NODE_FILTER:
2049                         ntype->butfunc= node_composit_buts_filter;
2050                         break;
2051                 case CMP_NODE_MAP_VALUE:
2052                         ntype->butfunc= node_composit_buts_map_value;
2053                         break;
2054                 case CMP_NODE_TIME:
2055                         ntype->butfunc= node_buts_time;
2056                         break;
2057                 case CMP_NODE_ALPHAOVER:
2058                         ntype->butfunc= node_composit_buts_alphaover;
2059                         break;
2060                 case CMP_NODE_HUE_SAT:
2061                         ntype->butfunc= node_composit_buts_hue_sat;
2062                         break;
2063                 case CMP_NODE_TEXTURE:
2064                         ntype->butfunc= node_buts_texture;
2065                         break;
2066                 case CMP_NODE_DILATEERODE:
2067                         ntype->butfunc= node_composit_buts_dilateerode;
2068                         break;
2069                 case CMP_NODE_OUTPUT_FILE:
2070                         ntype->butfunc= node_composit_buts_file_output;
2071                         break;
2072         
2073                 case CMP_NODE_DIFF_MATTE:
2074                         ntype->butfunc=node_composit_buts_diff_matte;
2075                         break;
2076                 case CMP_NODE_COLOR_SPILL:
2077                         ntype->butfunc=node_composit_buts_color_spill;
2078                         break;
2079                 case CMP_NODE_CHROMA:
2080                         ntype->butfunc=node_composit_buts_chroma_matte;
2081                         break;
2082                 case CMP_NODE_SCALE:
2083                         ntype->butfunc= node_composit_buts_scale;
2084                         break;
2085                 case CMP_NODE_CHANNEL_MATTE:
2086                         ntype->butfunc= node_composit_buts_channel_matte;
2087                         break;
2088                 case CMP_NODE_LUMA_MATTE:
2089                         ntype->butfunc= node_composit_buts_luma_matte;
2090                         break;
2091                 case CMP_NODE_MAP_UV:
2092                         ntype->butfunc= node_composit_buts_map_uv;
2093                         break;
2094                 case CMP_NODE_ID_MASK:
2095                         ntype->butfunc= node_composit_buts_id_mask;
2096                         break;
2097                 case CMP_NODE_MATH:
2098                         ntype->butfunc= node_buts_math;
2099                         break;
2100                 case CMP_NODE_INVERT:
2101                         ntype->butfunc= node_composit_buts_invert;
2102                         break;
2103                 case CMP_NODE_PREMULKEY:
2104                         ntype->butfunc= node_composit_buts_premulkey;
2105                         break;
2106                 default:
2107                         ntype->butfunc= NULL;
2108         }
2109 }
2110
2111 /* ****************** BUTTON CALLBACKS FOR TEXTURE NODES ***************** */
2112
2113 static int node_texture_buts_bricks(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
2114 {
2115         if(block) {
2116                 short w = butr->xmax-butr->xmin;
2117                 short ofw = 32;
2118                 
2119                 uiBlockBeginAlign(block);
2120                 
2121                 /* Offset */
2122                 uiDefButF(
2123                         block, NUM, B_NODE_EXEC, "Offset",
2124                         butr->xmin, butr->ymin+20, w-ofw, 20,
2125                         &node->custom3,
2126                         0, 1, 0.25, 2,
2127                         "Offset amount" );
2128                 uiDefButS(
2129                         block, NUM, B_NODE_EXEC, "",
2130                         butr->xmin+w-ofw, butr->ymin+20, ofw, 20,
2131                         &node->custom1,
2132                         2, 99, 0, 0,
2133                         "Offset every N rows" );
2134                 
2135                 /* Squash */
2136                 uiDefButF(
2137                         block, NUM, B_NODE_EXEC, "Squash",
2138                         butr->xmin, butr->ymin+0, w-ofw, 20,
2139                         &node->custom4,
2140                         0, 99, 0.25, 2,
2141                         "Stretch amount" );
2142                 uiDefButS(
2143                         block, NUM, B_NODE_EXEC, "",
2144                         butr->xmin+w-ofw, butr->ymin+0, ofw, 20,
2145                         &node->custom2,
2146                         2, 99, 0, 0,
2147                         "Stretch every N rows" );
2148                 
2149                 uiBlockEndAlign(block);
2150         }
2151         return 40;
2152 }
2153
2154 /* Copied from buttons_shading.c -- needs unifying */
2155 static char* noisebasis_menu()
2156 {
2157         static char nbmenu[256];
2158         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);
2159         return nbmenu;
2160 }
2161
2162 static int node_texture_buts_proc(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
2163 {
2164         Tex *tex = (Tex *)node->storage;
2165         short x,y,w,h;
2166         
2167         if( block ) {
2168                 x = butr->xmin;
2169                 y = butr->ymin;
2170                 w = butr->xmax - x;
2171                 h = butr->ymax - y;
2172         }
2173         else
2174                 return 0;
2175         
2176         switch( tex->type ) {
2177                 case TEX_BLEND:
2178                         if( block ) {
2179                                 uiBlockBeginAlign( block );
2180                                 uiDefButS( block, MENU, B_NODE_EXEC,
2181                                         "Linear %x0|Quad %x1|Ease %x2|Diag %x3|Sphere %x4|Halo %x5|Radial %x6",
2182                                         x, y+20, w, 20, &tex->stype, 0, 1, 0, 0, "Blend Type" );
2183                                 uiDefButBitS(block, TOG, TEX_FLIPBLEND, B_NODE_EXEC, "Flip XY", x, y, w, 20,
2184                                         &tex->flag, 0, 0, 0, 0, "Flips the direction of the progression 90 degrees");
2185                                 uiBlockEndAlign( block );
2186                         }
2187                         return 40;
2188                         
2189                         
2190                 case TEX_MARBLE:
2191                         if( block ) {
2192                                 uiBlockBeginAlign(block);
2193                         
2194                                 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"); 
2195                                 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"); 
2196                                 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"); 
2197                                 
2198                                 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");
2199                                 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");
2200                                 
2201                                 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."); 
2202                                 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"); 
2203                                 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"); 
2204             
2205                                 uiBlockEndAlign(block);
2206                         }
2207                         return 60;
2208                         
2209                 case TEX_WOOD:
2210                         if( block ) {
2211                                 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");
2212                                 
2213                                 uiBlockBeginAlign(block);
2214                                 uiDefButS(block, ROW, B_TEXPRV,             "Bands",     x,  40+y, w/2, 18, &tex->stype, 2.0, (float)TEX_BANDNOISE, 0, 0, "Uses standard noise"); 
2215                                 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"); 
2216                                 
2217                                 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."); 
2218                                 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"); 
2219                                 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");
2220                                 
2221                                 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");
2222                                 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");
2223                                 uiBlockEndAlign(block);
2224                         }
2225                         return 80; 
2226                         
2227                 case TEX_CLOUDS:
2228                         if( block ) {
2229                                 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");
2230                                 
2231                                 uiBlockBeginAlign(block);
2232                                 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"); 
2233                                 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"); 
2234                                 uiDefButS(block, ROW, B_TEXPRV, "Soft",      x, y+20, w/2, 18, &tex->noisetype, 12.0, (float)TEX_NOISESOFT, 0, 0, "Generates soft noise");
2235                                 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");
2236                                 uiBlockEndAlign(block);
2237                                 
2238                                 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");
2239                         }
2240                         return 80;
2241                         
2242                 case TEX_DISTNOISE:
2243                         if( block ) {
2244                                 uiBlockBeginAlign(block);
2245                                 uiDefButS(block, MENU, B_TEXPRV, noisebasis_menu(), x, y+18, w, 18, &tex->noisebasis2, 0,0,0,0, "Sets the noise basis to distort");
2246                                 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");
2247                                 uiBlockEndAlign(block);
2248                         }
2249                         return 36;
2250         }
2251         return 0;
2252 }
2253
2254 static int node_texture_buts_image(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
2255 {
2256         char *strp;
2257         uiBut *bt;
2258         
2259         if( block ) {
2260                 uiBlockBeginAlign(block);
2261                 
2262                 /* browse button */
2263                 IMAnames_to_pupstring(&strp, NULL, "LOAD NEW %x32767", &(G.main->image), NULL, NULL);
2264                 node->menunr= 0;
2265                 bt= uiDefButS(block, MENU, B_NOP, strp, 
2266                                           butr->xmin, butr->ymin, 19, 19, 
2267                                           &node->menunr, 0, 0, 0, 0, "Browses existing choices");
2268                 uiButSetFunc(bt, node_browse_image_cb, ntree, node);
2269                 if(strp) MEM_freeN(strp);
2270                 
2271                 /* Add New button */
2272                 if(node->id==NULL) {
2273                         bt= uiDefBut(block, BUT, B_NODE_LOADIMAGE, "Load New",
2274                                                  butr->xmin+19, butr->ymin, (short)(butr->xmax-butr->xmin-19.0f), 19, 
2275                                                  NULL, 0.0, 0.0, 0, 0, "Add new Image");
2276                         uiButSetFunc(bt, node_active_cb, ntree, node);
2277                 }
2278                 else {
2279                         /* name button */
2280                         short xmin= (short)butr->xmin, xmax= (short)butr->xmax;
2281                         short width= xmax - xmin - 19;
2282                         
2283                         bt= uiDefBut(block, TEX, B_NOP, "IM:",
2284                                                  xmin+19, butr->ymin, width, 19, 
2285                                                  node->id->name+2, 0.0, 19.0, 0, 0, "Image name");
2286                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
2287                 }
2288         }
2289         return 20;
2290 }
2291
2292 static int node_texture_buts_output(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
2293 {
2294         if( block ) {
2295                 uiBut *bt;
2296                 short width;
2297                 char *name = ((TexNodeOutput*)node->storage)->name;
2298                 
2299                 uiBlockBeginAlign(block);
2300                 
2301                 width = (short)(butr->xmax - butr->xmin);
2302                 
2303                 bt = uiDefBut(
2304                         block, TEX, B_NOP,
2305                         "Name:",
2306                         butr->xmin, butr->ymin,
2307                         width, 19, 
2308                         name, 0, 31,
2309                         0, 0, 
2310                         "Name this output"
2311                 );
2312                 
2313                 uiBlockEndAlign(block);
2314         }
2315         return 19;
2316 }
2317
2318 /* only once called */
2319 static void node_texture_set_butfunc(bNodeType *ntype)
2320 {
2321         if( ntype->type >= TEX_NODE_PROC && ntype->type < TEX_NODE_PROC_MAX ) {
2322                 ntype->butfunc = node_texture_buts_proc;
2323         }
2324         else switch(ntype->type) {
2325                 
2326                 case TEX_NODE_MATH:
2327                         ntype->butfunc = node_buts_math;
2328                         break;
2329                 
2330                 case TEX_NODE_MIX_RGB:
2331                         ntype->butfunc = node_buts_mix_rgb;
2332                         break;
2333                         
2334                 case TEX_NODE_VALTORGB:
2335                         ntype->butfunc = node_buts_valtorgb;
2336                         break;
2337                         
2338                 case TEX_NODE_CURVE_RGB:
2339                         ntype->butfunc= node_buts_curvecol;
2340                         break;
2341                         
2342                 case TEX_NODE_CURVE_TIME:
2343                         ntype->butfunc = node_buts_time;
2344                         break;
2345                         
2346                 case TEX_NODE_TEXTURE:
2347                         ntype->butfunc = node_buts_texture;
2348                         break;
2349                         
2350                 case TEX_NODE_BRICKS:
2351                         ntype->butfunc = node_texture_buts_bricks;
2352                         break;
2353                         
2354                 case TEX_NODE_IMAGE:
2355                         ntype->butfunc = node_texture_buts_image;
2356                         break;
2357                         
2358                 case TEX_NODE_OUTPUT:
2359                         ntype->butfunc = node_texture_buts_output;
2360                         break;
2361                         
2362                 default:
2363                         ntype->butfunc= NULL;
2364         }
2365 }
2366
2367 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2368
2369 void ED_init_node_butfuncs(void)
2370 {
2371         bNodeType *ntype;
2372         
2373         /* shader nodes */
2374         ntype= node_all_shaders.first;
2375         while(ntype) {
2376                 node_shader_set_butfunc(ntype);
2377                 ntype= ntype->next;
2378         }
2379         /* composit nodes */
2380         ntype= node_all_composit.first;
2381         while(ntype) {
2382                 node_composit_set_butfunc(ntype);
2383                 ntype= ntype->next;
2384         }
2385         ntype = node_all_textures.first;
2386         while(ntype) {
2387                 node_texture_set_butfunc(ntype);
2388                 ntype= ntype->next;
2389         }
2390 }
2391
2392 /* ************** Generic drawing ************** */
2393
2394 #if 0
2395 void node_rename_but(char *s)
2396 {
2397         uiBlock *block;
2398         ListBase listb={0, 0};
2399         int dy, x1, y1, sizex=80, sizey=30;
2400         short pivot[2], mval[2], ret=0;
2401         
2402         getmouseco_sc(mval);
2403
2404         pivot[0]= CLAMPIS(mval[0], (sizex+10), G.curscreen->sizex-30);
2405         pivot[1]= CLAMPIS(mval[1], (sizey/2)+10, G.curscreen->sizey-(sizey/2)-10);
2406         
2407         if (pivot[0]!=mval[0] || pivot[1]!=mval[1])
2408                 warp_pointer(pivot[0], pivot[1]);
2409
2410         mywinset(G.curscreen->mainwin);
2411         
2412         x1= pivot[0]-sizex+10;
2413         y1= pivot[1]-sizey/2;
2414         dy= sizey/2;
2415         
2416         block= uiNewBlock(&listb, "button", UI_EMBOSS, UI_HELV, G.curscreen->mainwin);
2417         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_ENTER_OK);
2418         
2419         /* buttons have 0 as return event, to prevent menu to close on hotkeys */
2420         uiBlockBeginAlign(block);
2421         
2422         uiDefBut(block, TEX, B_NOP, "Name: ", (short)(x1),(short)(y1+dy), 150, 19, s, 0.0, 19.0, 0, 0, "Node user name");
2423         
2424         uiBlockEndAlign(block);
2425
2426         uiDefBut(block, BUT, 32767, "OK", (short)(x1+150), (short)(y1+dy), 29, 19, NULL, 0, 0, 0, 0, "");
2427
2428         uiBoundsBlock(block, 2);
2429
2430         ret= uiDoBlocks(&listb, 0, 0);
2431 }
2432
2433 #endif
2434
2435 void draw_nodespace_back_pix(ARegion *ar, SpaceNode *snode)
2436 {
2437         
2438         if((snode->flag & SNODE_BACKDRAW) && snode->treetype==NTREE_COMPOSIT) {
2439                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2440                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2441                 if(ibuf) {
2442                         float x, y; 
2443                         
2444                         wmPushMatrix();
2445                         
2446                         /* somehow the offset has to be calculated inverse */
2447                         
2448                         glaDefine2DArea(&ar->winrct);
2449                         /* ortho at pixel level curarea */
2450                         wmOrtho2(-0.375, ar->winx-0.375, -0.375, ar->winy-0.375);
2451                         
2452                         x = (ar->winx-ibuf->x)/2 + snode->xof;
2453                         y = (ar->winy-ibuf->y)/2 + snode->yof;
2454                         
2455                         if(ibuf->rect)
2456                                 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2457                         else if(ibuf->channels==4)
2458                                 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);
2459                         
2460                         wmPopMatrix();
2461                 }
2462         }
2463 }
2464
2465 #if 0
2466 /* note: needs to be userpref or opengl profile option */
2467 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
2468 {
2469
2470         draw_nodespace_grid(snode);
2471         
2472         if(snode->flag & SNODE_BACKDRAW) {
2473                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2474                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2475                 if(ibuf) {
2476                         int x, y;
2477                         float zoom = 1.0;
2478
2479                         glMatrixMode(GL_PROJECTION);
2480                         glPushMatrix();
2481                         glMatrixMode(GL_MODELVIEW);
2482                         glPushMatrix();
2483                         
2484                         glaDefine2DArea(&sa->winrct);
2485
2486                         if(ibuf->x > sa->winx || ibuf->y > sa->winy) {
2487                                 float zoomx, zoomy;
2488                                 zoomx= (float)sa->winx/ibuf->x;
2489                                 zoomy= (float)sa->winy/ibuf->y;
2490                                 zoom = MIN2(zoomx, zoomy);
2491                         }
2492                         
2493                         x = (sa->winx-zoom*ibuf->x)/2 + snode->xof;
2494                         y = (sa->winy-zoom*ibuf->y)/2 + snode->yof;
2495
2496                         glPixelZoom(zoom, zoom);
2497
2498                         glColor4f(1.0, 1.0, 1.0, 1.0);
2499                         if(ibuf->rect)
2500                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
2501                         else if(ibuf->channels==4)
2502                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float);
2503
2504                         glPixelZoom(1.0, 1.0);
2505
2506                         glMatrixMode(GL_PROJECTION);
2507                         glPopMatrix();
2508                         glMatrixMode(GL_MODELVIEW);
2509                         glPopMatrix();
2510                 }
2511         }
2512 }
2513 #endif
2514
2515 /* if v2d not NULL, it clips and returns 0 if not visible */
2516 int node_link_bezier_points(View2D *v2d, SpaceNode *snode, bNodeLink *link, float coord_array[][2], int resol)
2517 {
2518         float dist, vec[4][2];
2519         
2520         /* in v0 and v3 we put begin/end points */
2521         if(link->fromsock) {
2522                 vec[0][0]= link->fromsock->locx;
2523                 vec[0][1]= link->fromsock->locy;
2524         }
2525         else {
2526                 if(snode==NULL) return 0;
2527                 vec[0][0]= snode->mx;
2528                 vec[0][1]= snode->my;
2529         }
2530         if(link->tosock) {
2531                 vec[3][0]= link->tosock->locx;
2532                 vec[3][1]= link->tosock->locy;
2533         }
2534         else {
2535                 if(snode==NULL) return 0;
2536                 vec[3][0]= snode->mx;
2537                 vec[3][1]= snode->my;
2538         }
2539         
2540         dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
2541         
2542         /* check direction later, for top sockets */
2543         vec[1][0]= vec[0][0]+dist;
2544         vec[1][1]= vec[0][1];
2545         
2546         vec[2][0]= vec[3][0]-dist;
2547         vec[2][1]= vec[3][1];
2548         
2549         if(v2d && MIN4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) > v2d->cur.xmax); /* clipped */      
2550         else if (v2d && MAX4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) < v2d->cur.xmin); /* clipped */
2551         else {
2552                 
2553                 /* always do all three, to prevent data hanging around */
2554                 forward_diff_bezier(vec[0][0], vec[1][0], vec[2][0], vec[3][0], coord_array[0], resol, 2);
2555                 forward_diff_bezier(vec[0][1], vec[1][1], vec[2][1], vec[3][1], coord_array[0]+1, resol, 2);
2556                 
2557                 return 1;
2558         }
2559         return 0;
2560 }
2561
2562 #define LINK_RESOL      24
2563 void node_draw_link_bezier(View2D *v2d, SpaceNode *snode, bNodeLink *link, int th_col1, int th_col2, int do_shaded)
2564 {
2565         float coord_array[LINK_RESOL+1][2];
2566         
2567         if(node_link_bezier_points(v2d, snode, link, coord_array, LINK_RESOL)) {
2568                 float dist, spline_step = 0.0f;
2569                 int i;
2570                 
2571                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
2572                 dist = 1.0f/(float)LINK_RESOL;
2573                 
2574                 glBegin(GL_LINE_STRIP);
2575                 for(i=0; i<=LINK_RESOL; i++) {
2576                         if(do_shaded) {
2577                                 UI_ThemeColorBlend(th_col1, th_col2, spline_step);
2578                                 spline_step += dist;
2579                         }                               
2580                         glVertex2fv(coord_array[i]);
2581                 }
2582                 glEnd();
2583         }
2584 }
2585
2586 /* note; this is used for fake links in groups too */
2587 void node_draw_link(View2D *v2d, SpaceNode *snode, bNodeLink *link)
2588 {
2589         int do_shaded= 1, th_col1= TH_WIRE, th_col2= TH_WIRE;
2590         
2591         if(link->fromnode==NULL && link->tonode==NULL)
2592                 return;
2593         
2594         if(link->fromnode==NULL || link->tonode==NULL) {
2595                 UI_ThemeColor(TH_WIRE);
2596                 do_shaded= 0;
2597         }
2598         else {
2599                 /* going to give issues once... */
2600                 if(link->tosock->flag & SOCK_UNAVAIL)
2601                         return;
2602                 if(link->fromsock->flag & SOCK_UNAVAIL)
2603                         return;
2604                 
2605                 /* a bit ugly... but thats how we detect the internal group links */
2606                 if(link->fromnode==link->tonode) {
2607                         UI_ThemeColorBlend(TH_BACK, TH_WIRE, 0.25f);
2608                         do_shaded= 0;
2609                 }
2610                 else {
2611                         /* check cyclic */
2612                         if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
2613                                 if(link->fromnode->flag & SELECT)
2614                                         th_col1= TH_EDGE_SELECT;
2615                                 if(link->tonode->flag & SELECT)
2616                                         th_col2= TH_EDGE_SELECT;
2617                         }                               
2618                         else {
2619                                 UI_ThemeColor(TH_REDALERT);
2620                                 do_shaded= 0;
2621                         }
2622                 }
2623         }
2624         
2625         node_draw_link_bezier(v2d, snode, link, th_col1, th_col2, do_shaded);
2626 }
2627
2628 #if 0
2629
2630 static void nodes_panel_gpencil(short cntrl)    // NODES_HANDLER_GREASEPENCIL
2631 {
2632         uiBlock *block;
2633         SpaceNode *snode;
2634         
2635         snode= curarea->spacedata.first;
2636
2637         block= uiNewBlock(&curarea->uiblocks, "nodes_panel_gpencil", UI_EMBOSS, UI_HELV, curarea->win);
2638         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
2639         uiSetPanelHandler(NODES_HANDLER_GREASEPENCIL);  // for close and esc
2640         if (uiNewPanel(curarea, block, "Grease Pencil", "SpaceNode", 100, 30, 318, 204)==0) return;
2641         
2642         /* we can only really draw stuff if there are nodes (otherwise no events are handled */
2643         if (snode->nodetree == NULL)
2644                 return;
2645         
2646         /* allocate memory for gpd if drawing enabled (this must be done first or else we crash) */
2647         if (snode->flag & SNODE_DISPGP) {
2648                 if (snode->gpd == NULL)
2649                         gpencil_data_setactive(curarea, gpencil_data_addnew());
2650         }
2651         
2652         if (snode->flag & SNODE_DISPGP) {
2653                 bGPdata *gpd= snode->gpd;
2654                 short newheight;
2655                 
2656                 /* this is a variable height panel, newpanel doesnt force new size on existing panels */
2657                 /* so first we make it default height */
2658                 uiNewPanelHeight(block, 204);
2659                 
2660                 /* draw button for showing gpencil settings and drawings */
2661                 uiDefButBitS(block, TOG, SNODE_DISPGP, B_REDR, "Use Grease Pencil", 10, 225, 150, 20, &snode->flag, 0, 0, 0, 0, "Display freehand annotations overlay over this Node Editor (draw using Shift-LMB)");
2662                 
2663                 /* extend the panel if the contents won't fit */
2664                 newheight= draw_gpencil_panel(block, gpd, curarea); 
2665                 uiNewPanelHeight(block, newheight);
2666         }
2667         else {
2668                 uiDefButBitS(block, TOG, SNODE_DISPGP, B_REDR, "Use Grease Pencil", 10, 225, 150, 20, &snode->flag, 0, 0, 0, 0, "Display freehand annotations overlay over this Node Editor");
2669                 uiDefBut(block, LABEL, 1, " ",  160, 180, 150, 20, NULL, 0.0, 0.0, 0, 0, "");
2670         }
2671 }
2672
2673 static void nodes_blockhandlers(ScrArea *sa)
2674 {
2675         SpaceNode *snode= sa->spacedata.first;
2676         short a;
2677         
2678         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
2679                 switch(snode->blockhandler[a]) {
2680                         case NODES_HANDLER_GREASEPENCIL:
2681                                 nodes_panel_gpencil(snode->blockhandler[a+1]);
2682                                 break;
2683                 }
2684                 
2685                 /* clear action value for event */
2686                 snode->blockhandler[a+1]= 0;
2687         }
2688         uiDrawBlocksPanels(sa, 0);
2689 }
2690 #endif