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