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