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