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